PDA

View Full Version : Texture not appearing



DoomAngel
06-01-2006, 04:25 PM
Hello everybody, i'm new in this forum.
I've started OpenGL (using c++) about 2 weeks ago. I pretty much like using windows api.

I'm making a program where it checks all the files in a directory and saves their path in a dynamic array : char *nameTextures
And the total number of textures found is:
int totalTextures;

Then I would like to bind for example the first Texture on a Quad.

Here's the part of my code where i'm trying to load all the textures using their path.
(I took a code from NeHe's tutorial and changed some parts of it)

int x,counter=0;
AUX_RGBImageRec *TextureImage[totalTextures];

texture = (GLuint*)malloc(sizeof(GLuint)*totalTextures);
if( texture==NULL ) return 0;

memset(TextureImage,0,sizeof(void *)*totalTextures);

for(x=0;x<totalTextures;x++)
{
char filename[MAX_PATH];
strcpy(filename,nameTextures+x*MAX_PATH);
TextureImage[x]=auxDIBImageLoad(filename);
if( TextureImage[x] ) counter++;

}

if( counter==totalTextures )
{
STATUS=1;
glGenTextures(totalTextures, texture);

for(x=0;x<totalTextures;x++)
{
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glBindTexture(GL_TEXTURE_2D, texture[x]);
showNum("sizeX",TextureImage[x]->sizeX);
showNum("sizeY",TextureImage[x]->sizeY);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[x]->sizeX, TextureImage[x]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[x]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
}
for(x=0;x<totalTextures;x++)
{
if(TextureImage[x])
{
if(TextureImage[x]->data)
{
free(TextureImage[x]->data);
}
free(TextureImage[x]);
}
}then here's the part where i try to draw a white triangle and a quad which is supposed to have a texture on it.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(-5.0f,-15.0f,-30.0f);
glVertex3f(5.0f,-15.0f,-30.0f);
glVertex3f(0.0f,-10.0f,-30.0f);
glEnd();
glTranslatef(0.0f,0.0f,-5.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
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();What i get is:

a white triangle at the bottom of the screen (OK)
a white square in the middle of the screen (NOT OK)

could someone tell me if i've made mistakes and help me correct them?
thanks

ZbuffeR
06-01-2006, 05:04 PM
try :

glDisable(GL_TEXTURE2D);
<triangle stuff>

glEnable(GL_TEXTURE2D);
<quad stuff>

honeyhuang
06-01-2006, 11:26 PM
Please be sure you have correctly loaded the bmpfile into texture object.

DoomAngel
06-02-2006, 01:57 AM
OK my problem is solved.
In fact, i tried using your solutions but unfortunately none of them made the texture appear.

The problem was:
I was loading the textures before the GL context was created (at the beginning of the MAIN).
I don't really know if that's the cause but loading all my textures after creating the GL context solved everything.

Thanks for your time and solutions!

RigidBody
06-02-2006, 02:21 AM
you're right, before you can use any gl* function, a valid context has to be created and made current.