Skip to main content
GameDev.net gamedev.net
🔒 Locked

glut texture mapping problem

Started by Xrid3r Sep 23, 2010 at 6:59 AM 4 replies 1k views
Original Post
Xrid3r
Xrid3r
Hello!
I've got a problem with my glut project.. You see.. I can compile without any warnings and errors.. It just showing a white window.. Instead of a mapped image..
Hope you can tell me whats wrong with it..

Here is the code:

#include
#ifdef __APPLE__
#include
#else
#include
#endif

#include
#include
#include
#include

GLuint LoadTexture(const char *filename,int width,int height,int wrap)
{
GLuint texture;
unsigned char *data;
FILE *file;

file = fopen(filename,"r+");
if(file == NULL) { return 0; }

data = (unsigned char*) malloc(width * height * 3);
fread(data,width * height * 3,1,file);
fclose(file);


glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glEnable(GL_TEXTURE_2D);

glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,wrap ? GL_REPEAT : GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,wrap ? GL_REPEAT : GL_CLAMP);

gluBuild2DMipmaps(GL_TEXTURE_2D,3,width,height,GL_RGB,GL_UNSIGNED_BYTE,data);

free(data);
return texture;

}

static void resize(int width, int height)
{
const float ar = (float) width / (float) height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}

static void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,0.0f);
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,0.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,0.0f);
glEnd();

glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;
}

glutPostRedisplay();
}

void init(void)
{
GLuint texture;
glClearColor(0.0f,0.0f,0.0f,0.0f);
glShadeModel(GL_SMOOTH);

glEnable(GL_TEXTURE_2D);
texture = LoadTexture("image.bmp",128,128,TRUE);
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("GLUT Shapes");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(display);
init();
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);



glutMainLoop();

return EXIT_SUCCESS;
}

~Xrid3r
Brother Bob
Brother Bob
Your frustum depth range is from Z=-2 to Z=-100, and you draw the quad at Z=0, so it's outside the view volume. Translate it, say, -5 units along the Z-axis to move it inside the view volume.
Xrid3r
Xrid3r
Quote:
Original post by Brother Bob
Your frustum depth range is from Z=-2 to Z=-100, and you draw the quad at Z=0, so it's outside the view volume. Translate it, say, -5 units along the Z-axis to move it inside the view volume.



Yes, but the image is still refusing be mapped.. Now it's just showing a white quadrat.. Can the problem be in the Loadtexture function or in the display function, were the scene is drawn?

~Xrid3r
Brother Bob
Brother Bob
The coordinates for your quad are broken also. They don't make a quad. Check them again. Perhaps it's the backface culling that's removing whatever is actually drawn.
Xrid3r
Xrid3r
I tried to remove the backface and cullface.. But it didn't made any differences.. Is there anything else that can be the reason to the images is not drawn? Or is it a bug in glut?

Also.. I belive the Coords for the Quadrat should be:

glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,0.0f);
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,0.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,0.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,0.0f);

~Xrid3r

[Edited by - Xrid3r on September 23, 2010 8:36:18 AM]
Brother Bob
Brother Bob
The code is working (without the texture, of course, since I have no file to load) with the changes I've said, so there's either something else you haven't mentioned or you haven't properly implemented the changes.

That, or the file just isn't loaded. Step through the code with a debugger to see if the texture actually loads.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.