PDA

View Full Version : Cube Env Mapping Texture problem



raoulduke
10-05-2004, 05:30 AM
Hi all.

I wanted to create a scene with a sphere that reflects all objects in its environment.
My first step is now to set up the cube env mapping textures to the sphere but
my program always crashes, when i use my makeCubeMap() method. But i don't know where
to put it either. I put it in my DrawGLScene() method but there must be something wrong.
It runs when it don't use this command, but then i only see a white sphere with no
textures.
I hope somebody can help me.
Thanx

</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if(!Filename)
return NULL;

File=fopen(Filename,"r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}



int LoadGLTextures(GLenum target, char *filename)
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if (mipmaps) {
gluBuild2DMipmaps(target, GL_RGB8, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
else
{
glTexImage2D(target, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}

return Status;
}


void makeCubeMap(void)
{
int i;
for (i=0; i<6; i++)
{
LoadGLTextures(faceTarget[i], faceFile[i]);
}
if (mipmaps)
{
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
} else
{
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glEnable(GL_TEXTURE_CUBE_MAP_EXT);

assert(mode == GL_NORMAL_MAP_EXT

Aeluned
10-05-2004, 05:48 AM
A few things:
first and formost *do not* create the texture in your render function. You're making a disk access each render frame! this is going to kill your performance.

Create your cube map in an initialization function once.

secondly, your loadGLTextures function never loads the AUX_RGBImageRec. You're passing an RGBImageRec with an uninitialized data array to the OpenGL texture creation functions, this is what's crashing your app.

where do you call LoadBMP? this should be called to initialize your AUX_RGBImageRec before you try passing it to OpenGL to upload a texture from it.

how are you storing and when do you bind the texture names? I don't see any of that here.

I suggest you read up on Texture Objects a little.

Relic
10-05-2004, 05:51 AM
In LoadGLTextures() you never assign something to the local AUX_RGBImageRec, you just zero the array of a pointer. (Why an array with one element?!)
Then you access the NULL pointer three times to get size and data which obviously will crash with a memory access violation.

Even if you called LoadBMP, you'd need to check if the return was not NULL to catch the loading errors.

The mipmap generation must be set before you download the textures, but then you wouldn't need the gluBuildMipmaps call at all.

The "3" (GL_RGB) as internalFormat in glTexImage2D should be GL_RGB8 like in the mipmap case.

That's enough errors for today.

raoulduke
10-05-2004, 11:13 PM
Thanks so far.
Unfortunately not everything i wanted to post of my code, was finally in this posting, so i'm putting the complete code once again.

I didn't want to create my textures in the rendering function. makeCubeMap should call LoadGLTextures to create them and should be called from my InitGL function.

I fixed most of the errors and it doesn't crash anymore, but still i don't see my textures on the sphere.

</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">

AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if(!Filename)
return NULL;

File=fopen(Filename,"r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}

int LoadGLTextures(GLenum target, char *filename)
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);

if (TextureImage[0]=LoadBMP(filename))
{
Status=TRUE;
glGenTextures(1, &cubeMapTexNum);

if (mipmaps)
{
gluBuild2DMipmaps(target, GL_RGB8, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB8, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
else
{
glTexImage2D(target, 0, GL_RGB8, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // TextureImage[0]->pixels
}
}

return Status;
}

void makeCubeMap(void)
{
int i;
for (i=0; i<6; i++)
{
LoadGLTextures(faceTarget[i], faceFile[i]);
}
if (mipmaps)
{
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
} else
{
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glEnable(GL_TEXTURE_CUBE_MAP_EXT);

assert(mode == GL_NORMAL_MAP_EXT

raoulduke
10-05-2004, 11:19 PM
my posting is just too long. But in the preview it shows the complete listing and when it's finally posted, the end is cut off. however - here's the rest:

</font><blockquote><font size="1" face="Verdana, Arial">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">

assert(mode == GL_NORMAL_MAP_EXT

raoulduke
10-05-2004, 11:24 PM
sorry for f*cking everything up, but it's not possible to write an logical OR (you know, like ll).
Once again - here's the rest continueing from asstert... in MakeCubeMap.
Every assert here means:
assert(mode == GL_NORMAL_MAP_EXT >OR< mode == GL_REFLECTION_MAP_EXT);


assert(mode == GL_NORMAL_MAP_EXT);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, mode);

glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);

}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective( 45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_TEXTURE_CUBE_MAP_EXT);
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

makeCubeMap(); // Should be here initialised

assert(mode == GL_NORMAL_MAP_EXT); // Ensure
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, mode);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, mode);

return TRUE; // Initialization Went OK
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
GLfloat m[4][4];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix

GLUquadricObj* quadric = gluNewQuadric();
glEnable(GL_BLEND);

glDisable(GL_TEXTURE_2D);
//glDisable(GL_TEXTURE_CUBE_MAP_EXT);
glEnable(GL_TEXTURE_CUBE_MAP_EXT);
//glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_CUBE_MAP_EXT, cubeMapTexNum);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);


glPushMatrix();
glTranslatef(0.0f,0.0f,-6.0f);
glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate on the X Axis by xrot
glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate on the Y Axis by yrot

gluSphere(quadric, 2.0f, 32,32);

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_CUBE_MAP_EXT);

glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);


glEnable(GL_TEXTURE_2D);
glPopMatrix();

xrot+=xspeed;
yrot+=yspeed;

return TRUE; // Everything Went OK
}

Relic
10-06-2004, 03:54 AM
Nothing obvious:
Try these:
- Check faceTarget[i] and faceFile[i] contents.
- Check loading succeded.
- Remove glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
- Cubemaps require GL_CLAMP_TO_EDGE to work nicely instead of
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP);
- Where's GL_TEXTURE_WRAP_R?
- Don't glEnable(GL_BLEND);
- Pair gluNewQuadric with gluDeleteQuadric or you leak memory.
- Better use glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- Add glGetError calls.
- Single step through the debugger.