Jump to content
  • Advertisement
Sign in to follow this  
jarod83

OpenGL OpenGL drawing from another class

This topic is 5027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys! Wonder if there is a expert that can answe my question... I am working with OpenGL. I have a main .cpp class where I include the main OpenGL header, that is gl.h, glu.h and glaux.h. Now, in that main class I have a function called "int drawGLScene(GLvoid)" where i can do all the drawing, and it works fine. The problem now is that i want to draw from another class. So I have a class called Box(.h), where I have a function called draw(). When I try to do glBegin(GL_QUADS); ... glEnd(); it works just fine, but then i try to do the glBindTexture(GL_TEXTURE_2D, someTexture[0]); in the same draw() function nothing happens, all I get is white color on the box. But it works fine in the main class drawGLScene()! So, why can't I bind the texture in the other class ?!? I'm sure that the textures are loaded correctly. I've also tried to get the texture from the Box class and bind it in the drawGLScene() function, but nothing happens. Strang I'll say... Got any clue anyone ?

Share this post


Link to post
Share on other sites
Advertisement
hmmmm..

Where is someTexture[0] declared, and what value is it when it is called in box.h(Are you sure you're calling it in here?)...

You should be fine making OpenGL calls from another class as long as OpenGL is initialized and set up, which it sounds like it is...

If the someTexure[0] is set correctly, then try calling glEnable(GL_TEXTURE_2D) in your external class before doing the bindtexture and drawing.

Share this post


Link to post
Share on other sites
Can you post some code? In the meantime a few things to check are:
  • Make sure you bind the texture outside of the begin-end pair.

  • Make sure you've enabled texturing.

  • Make sure that the someTexture array accessed in your Box class is the same array as you load the textures into, not a different array with the same name.

That's about all I can think of without seeing specific code.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by jarod83
someTexture[0] is declared in the box.h as private...



Where are you calling glGenTextures from? If you trace into your code, what value's in someTexture[0]?

If the value is set correctly, then you probably just need to enable GL_TEXTURE_2D.


Posting the code would help alot.

Share this post


Link to post
Share on other sites

Here is the source

[Source]
const int FACE_COUNT = 6; // 6 faces for a box

class Box {
public:
Box(float w, float h, float l, char *img[FACE_COUNT]) {
width = w;
height = h;
length = l;
LoadGLTextures();
}
~Box() {
// The destructor
}
void drawBox() {
// TODO: add rotations and other stuff here...

glBindTexture(GL_TEXTURE_2D, boxTextures[0]);
// *** Front of the box ***
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(0.5f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// *** Back of the box ***
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
// *** Top of the box ***
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
// *** Bottom of the box ***
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// *** Right of the box ***
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
// *** Left of the box ***
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
}
void setLocation(float x, float y, float z) {
location[0] = x;
location[1] = y;
location[2] = z;
}
void addToLocation(float x, float y, float z) {
location[0] += x;
location[1] += y;
location[2] += z;
}
void setWidth(float w) { width = w; }
void setHieght(float h) { height = h; }
void setLength(float l) { length = l; }
private:
AUX_RGBImageRec *LoadBMP(char *Filename) {
// FIle handler
FILE *File = NULL;
// Check if name of the file is valid, ei. the file path existed ?
if (!Filename) {
return NULL;
}
// Read the file
File = fopen(Filename, "r");
// Close file handler and return a pointer to bitmap
if (File) {
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}

// Loading textures in
int LoadGLTextures() {
int Status = FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage, 0, sizeof(void *)*1);

// Loading the texture image
if ( TextureImage[0] = LoadBMP("images/NeHe2.bmp") ) {
Status = TRUE;
}
// Create and generate the texture
glGenTextures(1, &boxTextures[0]);
glBindTexture(GL_TEXTURE_2D, boxTextures[0]);
// Generating the actual texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Set the filtering for the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// Free up the memory
if (TextureImage[0]) {
if (TextureImage[0]->data) {
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}

return Status;
}
float location[3];
float width;
float height;
float length;
GLuint boxTextures[1];
};
[/Source]

Share this post


Link to post
Share on other sites
Quote:
Original post by jarod83
btw, I did the glEnable(GL_TEXTURE_2D) int the main class!



What if you put glEnable(GL_TEXTURE_2D) right about this line?

glBindTexture(GL_TEXTURE_2D, boxTextures[0]);

Just to make sure that's not the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by jakem3s90
Quote:
Original post by jarod83
btw, I did the glEnable(GL_TEXTURE_2D) int the main class!



What if you put that glEnable right about this line?

glBindTexture(GL_TEXTURE_2D, boxTextures[0]);

Just to make sure that's not the problem?


Done! Nothing... It's still white... like snow ... :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jarod83
Quote:
Original post by jakem3s90
Quote:
Original post by jarod83
btw, I did the glEnable(GL_TEXTURE_2D) int the main class!



What if you put that glEnable right about this line?

glBindTexture(GL_TEXTURE_2D, boxTextures[0]);

Just to make sure that's not the problem?


Done! Nothing... It's still white... like snow ... :)


DAMN!

hmmm.. The only other thing I have to offer is to make sure that boxTextures[0] is a valid number, and make sure the texture loading code is ok!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!