visibility/passing of variables

M

Michael Sgier

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::LoadTex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(filename);
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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_BYTE, 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::OnDraw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_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
 
J

Jacques Labuschagne

Michael said:
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::LoadTex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(filename);
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

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_BYTE, 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::OnDraw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_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::texture[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::texture[...].

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(filename.c_str());
glGenTextures(1, &tx.id);
glBindTexture(GL_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_BYTE, tx.image->pixels);
textures.push_back(tx);
}
 
M

Michael Sgier

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::LoadTex(char* filename)
{
...
return texture;
}
 
M

mlimber

Michael said:
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::LoadTex(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::LoadTex() 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
 
J

Jacques Labuschagne

Michael said:
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::LoadTex(char* filename)
{
...
return texture;
}

You don't need to return texture to anywhere... It's a member of
CTexture. When CTerrain::OnDraw() 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(int n){
if (n < 0 or n > 4){
return 0;
}
return texture[n];
}
// ...
};

void CTerrain::OnDraw(){
// ...
glBindTexture(GL_TEXTURE_2D, ebene.getTextureId(0));
// ...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top