TGA's not showing up

Started by
5 comments, last by DrewGreen 18 years, 7 months ago
I'm running the exact same code as NeHe does for loading TGA's except for my setup. Somewhere in WinMain I typed:
     
    GLfloat zt=1.0f;
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glClearColor(0.0f,0.0f,0.0f,0.0f);glClearDepth(1.0f);glDepthFunc(GL_LEQUAL)GL_ONE_MINUS_SRC_ALPHA);glEnable(GL_BLEND);
    LoadTGA(&textures2[0],"Data/Ground.tga");
    glEnable(GL_TEXTURE_2D);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f,-2.0f );
    glBindTexture(GL_TEXTURE_2D,textures2[0].texID);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);glVertex3f(-20.f,-20.f,0);
    glTexCoord2f(1.0f,0.0f);glVertex3f(20.f,-20.f,0);
    glTexCoord2f(1.0f,1.0f);glVertex3f(20.f,20.f,0);
    glTexCoord2f(0.0f,1.0f);glVertex3f(-20.f,20.f,0);glEnd();
    glLoadIdentity();
The texture is also NeHe's ground texture. It only appears when I translate 1 float into or out of the screen and looks blocky or else it doesn't appear at all with different values outside [-1.0f,1.0f]. Anyone know why?
Advertisement
Main question - you have not set up your view frustum. Check my post here, and put it in the initialisation routine or in whatever function you're using for resizing the display when the window size changes ( ResizeGLScene or something if you're using NeHe basecode )

#2 - You are loading the texture each time you run that loop. You only need to do it once - move it into your initialisation code (which is only called upon startup) or something.

#3 - Why are you switching between projection and modelview matrices? From what you've posted you can get rid of those calls. That may be why your tga is showing up blocky. Or it could be because the quad is too close. OR check your TGA loading routine - it may be because there is no filtering enabled. Add the following two calls after glBindTexture (in LoadTGA):
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

#4 - You don't need that last glLoadIdentity call. Unless your code goes on to draw something else.

InitStuff //Move this stuff to initialisation routines{glClearColor(0.0f,0.0f,0.0f,0.0f);glClearDepth(1.0f);glDepthFunc(GL_LEQUAL)GL_ONE_MINUS_SRC_ALPHA);LoadTGA(&textures2[0],"Data/Ground.tga");glEnable (GL_DEPTH_TEST);									// ADDED: Enable Depth Testing}TheRest // Required every loop{    GLfloat zt=1.0f; // What's this for?    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();    glTranslatef(0.0f, 0.0f,-2.0f );    glEnable(GL_TEXTURE_2D);    glEnable(GL_BLEND);    glBindTexture(GL_TEXTURE_2D,textures2[0].texID);    glBegin(GL_QUADS);    glTexCoord2f(0.0f,0.0f);glVertex3f(-20.f,-20.f,0);    glTexCoord2f(1.0f,0.0f);glVertex3f(20.f,-20.f,0);    glTexCoord2f(1.0f,1.0f);glVertex3f(20.f,20.f,0);    glTexCoord2f(0.0f,1.0f);glVertex3f(-20.f,20.f,0);    glEnd();}


I'm guessing "glDepthFunc(GL_LEQUAL)GL_ONE_MINUS_SRC_ALPHA);" is a copy-paste error but glDepthFunc(GL_LEQUAL); will work. if GL_ONE_MINUS_SRC_ALPHA is part of glBlendFunc that too can go in the initialisation function.

Finally it might be better if you move all that out of WinMain and into a new function, and call that from WinMain instead. Not necessary at all, but that's the standard NeHe way.

I'd recommend having a closer look at the first few nehe drawing tutorials again.

Regards,
Drew

[Edited by - DrewGreen on September 4, 2005 7:50:16 PM]
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
Thanks but the texture still doesn't show up for some reason.
For some reason it only appears when i use an ortho view but i want to use a perspective view. How can i do this?
hmm. Does the quad still show up in perspective view or do you just see a black screen? If there's no quad then it could be that you are drawing the vertices in the wrong order. By default they need to be drawn anti-clockwise - it looks to me like they're being drawn clockwise, and so the back face of the polygon is facing towards you. It may be that Opengl doesn't draw these faces normally iirc, disable back face culling with a call to "glDisable(GL_CULL_FACE);" before the glBegin.

If on the other hand you can see a quad being drawn but it's just not being textured, there's something wrong with the texturing itself, although if it's working in ortho view then I don't see why it shouldn't for perspective view. Could you post more of your code or the entire project?
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
Sorry, it doesn't show up when disabling back face culling and rearranging the order counterclockwise doesn't work either. Here's my code.

void Init(){  glViewport (0, 0, (GLsizei)(640), (GLsizei)(480));  gluPerspective (45.0f, (GLfloat)(640)/(GLfloat)(480),1.0f, 500.0f);  if (!LoadTGA(&textures2[0],"Data/Ground.tga")){     MessageBox(NULL,"Texture load failed","error",MB_OK|MB_ICONINFORMATION);  }  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_2D);}void Draw(){  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	  glBindTexture(GL_TEXTURE_2D, textures2[0].texID);  glDisable(GL_CULL_FACE);  glBegin(GL_QUADS);    glTexCoord2f(1.0f,1.0f);glVertex3f(320.f,320.f,0);//top right    glTexCoord2f(0.0f,1.0f);glVertex3f(-320.f,320.f,0);//top left    glTexCoord2f(0.0f,0.0f);glVertex3f(-320.f,-320.f,0);//bottom left    glTexCoord2f(1.0f,0.0f);glVertex3f(320.f,-320.f,0);//bottom right  glEnd();//in WinMain outside of the main loopInit();Draw();

Ok, here's a project taking my initially suggested code & putting it into NeHe Lesson 33 (TGA loading).

The TGA does not load fully as LoadTGA does not create an OpenGL texture. My apologies for that, I had assumed it did.

  if (!LoadTGA(&textures2[0],"Data/Ground.tga")){     MessageBox(NULL,"Texture load failed","error",MB_OK|MB_ICONINFORMATION);  }  glGenTextures(1, &textures2[0].texID);				// Create The Texture ( CHANGE )  glBindTexture(GL_TEXTURE_2D, textures2[0].texID);  glTexImage2D(GL_TEXTURE_2D, 0, textures2[0].bpp / 8, textures2[0].width, textures2[0].height, 0, textures2[0].type, GL_UNSIGNED_BYTE, textures2[0].imageData);  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


I have also reverted to using the -20 to 20.0f coordinates as 320 is very large. In perspective view 320 does not equate to 320 pixels. Screen space is typically around +1 to -1 in the y plane and +2 to -2 in the x plane when z depth is 0. Give or take, I haven't actually worked it out.

Download the whole project here

Apologies for the gratuitous use of mr. t, couldn't resist.
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web

This topic is closed to new replies.

Advertisement