473,856 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

visibility/passing of variables

Hello
i don't understand the visibility/passing of variables. I've in
texture.h:

class CTexture
{
public:
unsigned int texID;
GLuint texture[5];

int LoadTex(char* filename);
}
and in terrain.h:
class CTerrain
{
public:
CTexture ebene;
}
the texture loading function in texture.cpp:
int CTexture::LoadT ex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(fil ename);
if (!img) {
printf("Error: %s\n", SDL_GetError()) ;
exit(1);
}
glGenTextures(1 , &texture[i]);
glBindTexture(G L_TEXTURE_2D, texture[i]);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_ FILTER, FILTER);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_ FILTER, FILTER);

glTexImage2D(GL _TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
GL_UNSIGNED_BYT E, img->pixels);

printf("loaded file '%s' as texture %i\n", filename, i);
i++;
return 0;
}
and finally the function gets called in terrain.cpp

#include "terrain.h"
ebene.LoadTex(" pics/ground.bmp");

also another function here uses the variable:
void CTerrain::OnDra w()
{
glEnable(GL_TEX TURE_2D);
glBindTexture(G L_TEXTURE_2D, ebene.texID);
....
}

It compiles but the texture doesn't get drawed. I dont understand how to
pass texture[] and how to
draw it then. Also where texture[] is visible.
THANKS and regards
Michael
Sep 22 '05 #1
5 1585
Michael Sgier wrote:
Hello
i don't understand the visibility/passing of variables. I've in
texture.h:

class CTexture
{
public:
unsigned int texID;
GLuint texture[5];

int LoadTex(char* filename);
}
and in terrain.h:
class CTerrain
{
public:
CTexture ebene;
}
the texture loading function in texture.cpp:
int CTexture::LoadT ex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(fil ename);
if (!img) {
printf("Error: %s\n", SDL_GetError()) ;
exit(1);
}
glGenTextures(1 , &texture[i]);
glBindTexture(G L_TEXTURE_2D, texture[i]);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_ FILTER, FILTER);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_ FILTER, FILTER);

glTexImage2D(GL _TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
GL_UNSIGNED_BYT E, img->pixels);

printf("loaded file '%s' as texture %i\n", filename, i);
i++;
return 0;
}
and finally the function gets called in terrain.cpp

#include "terrain.h"
ebene.LoadTex(" pics/ground.bmp");

also another function here uses the variable:
void CTerrain::OnDra w()
{
glEnable(GL_TEX TURE_2D);
glBindTexture(G L_TEXTURE_2D, ebene.texID);
...
}

It compiles but the texture doesn't get drawed. I dont understand how to
pass texture[] and how to
draw it then. Also where texture[] is visible.
THANKS and regards
Michael


You're storing the texture ID in CTexture::textu re[0..4]. Then you bind
that ID to some texture data using glTexImage2D. Then the function ends,
and you lose your pointer to the image, possibly creating a memory leak.
(I'm not sure what the SDL policy is.)
Then later on you try to use the texture by calling glBindTexture with
CTexture::texID , but that variable doesn't have anything meaningful in
it. The texture IDs are in CTexture::textu re[...].

Here's a common "texture" object:

struct texture_data{
GLuint id;
vector<unsigned char> data;
GLuint width, height;
};

You could probably replace vector<unsigned char> with a pointer to an
SDL_Surface. The point is that you usually have a 1-to-1 relationship
between texture IDs and the actual pixels; not 1:5 as your class implies.

If your real question is "how do I write a class that loads 5 textures"
then here's some (completely untested) food for thought:

struct texture{
GLuint id;
SDL_Surface *image;
texture(): id(0), image(NULL){}
~texture(){ delete image; }
};

class five_textures{
vector<texture> textures;
public:
void load(const string& filename);

GLuint getTexture(int n) const{
if (n < textures.size() ){
return textures[n].id;
}
return 0;
}

vector<texture> ::size_type size() const{ return textures.size() ; }
};

void five_textures:: load(const string& filename){
texture tx;
tx.image = SDL_LoadBMP(fil ename.c_str());
glGenTextures(1 , &tx.id);
glBindTexture(G L_TEXTURE_2D, tx.id);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_ FILTER, FILTER);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_ FILTER, FILTER);
glTexImage2D(GL _TEXTURE_2D, 0, 3, tx.image->w,
tx.image->h, 0, GL_RGB, GL_UNSIGNED_BYT E, tx.image->pixels);
textures.push_b ack(tx);
}
Sep 22 '05 #2
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadT ex(char* filename)
{
...
return texture;
}
Sep 22 '05 #3
Michael Sgier wrote:
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadT ex(char* filename)
{
...
return texture;
}


Your first problem is that the code in your original post is buggy, as
Jacques pointed out. Pay more attention to his post. As for the
function you've written here, it should work with the previous code if
you change the return type of CTexture::LoadT ex() to match this one.

It sounds to me like you probably need to read a good C++ book,
however. Try Stroustrup's _The C++ Programming Language_ (3rd Ed.),
Lippman et al.'s _C++ Primer_ (4th ed.), or Koenig and Moo's
_Accelerated C++_.

Cheers! --M

Sep 22 '05 #4
Michael Sgier wrote:
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadT ex(char* filename)
{
...
return texture;
}


You don't need to return texture to anywhere... It's a member of
CTexture. When CTerrain::OnDra w() wants to use a texture it should refer
to ebene.texture[n] rather than ebene.texId.

Ideally texture[] would be private in CTexture, and you'd add a member
function to get the texture ID. For example:

class CTexture{
private:
GLuint texture[5];

public:
GLuint getTextureId(in t n){
if (n < 0 or n > 4){
return 0;
}
return texture[n];
}
// ...
};

void CTerrain::OnDra w(){
// ...
glBindTexture(G L_TEXTURE_2D, ebene.getTextur eId(0));
// ...
}
Sep 22 '05 #5
PS, Arrays are evil. Don't use them unless you must. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Sep 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3604
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
3
14968
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
3
1525
by: kele | last post by:
Do sub Namespaces have visibility to parent class. ie. I am trying to develop an application that is made up of modules and the main module contains a variables class that contains all the variables needed for the application. eg. MainModule Namespace Contains MainMod Class
4
25073
by: wasntme | last post by:
I want to toggle a <div in and out of view. My question, which of the below examples would best be supported. Or is there another approach, that would be better used..TIA.. ..A.. <a href="#" onclick="C1.style.visibility='visible';">open</a> <a href="#" onclick="C1.style.visibility='hidden'">close</a> ..B.. <a href="#" onclick="C1.style.display='block'">display</a>
8
3395
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
6
3265
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
6
2044
by: Nick Stansbury | last post by:
Hi, I have a loop running on Page_PreRender that sets a number of controls to invisible based on a set of criteria. Before I do this however, I set all of the drop down lists to be visible with a straightforward recursive loop. Now - I'm really muddled by this - why does this: If TypeOf Ctl Is DropDownList Then Ctl.Visible = true End If
12
2696
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
4
3204
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can get public members but not protected, private, static members"
0
9916
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10782
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10384
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5761
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3201
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.