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::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[i]);
glBindTexture(GL_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_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);
}