Jump to content
  • Advertisement
Sign in to follow this  
ManTis

OpenGL texturing troubles

This topic is 4886 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

I was trying to make texture for object. Since the tutorial on NeHe is using glaux, i had to write my own loader for TGA format. I failed miserably. So I used the wonderful technique of code re-using and used the TGA loader from gpwiki (http://gpwiki.org/index.php/LoadTGACpp). Everything loaded nicely (i've put some couts in code to check that), i got my 256 x 256 8 bit indexed image ready, and coded:
(at global scope)
    CTGAImg *tgaimage;
    GLuint texturename[1];

(at main() function)
    tgaimage = new CTGAImg;
    tgaimage->Load("water.tga"); // loads the file. CTGAImg class includes data, bpp, sizes, header.

(and here starts the opengl stuff)
    glGenTextures(1, &texturename[0]);
    glBindTexture(GL_TEXTURE_2D, texturename[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tgaimage->GetWidth(), tgaimage->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, tgaimage->GetImg());
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  // i use sdl to create window, nothing special.

  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, true);
  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5);
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6);
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5);

  // Register SDL_Quit to be called at exit; makes sure things are
  // cleaned up when we quit.
  atexit(SDL_Quit);
    
  // Attempt to create a 800x600 window with 32bit pixels.
  screen = SDL_SetVideoMode(800, 600, 16, SDL_OPENGL);
  SDL_WM_SetCaption("Battle", "Battle");
  
  int width = screen->w;
  int height = screen->h;
  
  glViewport(0,0,(GLint)width, (GLint)height);
  glMatrixMode(GL_PROJECTION);
  gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  glShadeModel(GL_SMOOTH);
  glEnable(GL_TEXTURE_2D);

  
  // If we fail, return error.
  if ( screen == NULL ) 
  {
    fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
    exit(1);
  }


  // Main loop: loop forever.
  while (1)
  {
    // Render stuff

	    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	    glMatrixMode(GL_MODELVIEW);
	    gluLookAt(-2,2,-2,0,0,0,0,1,0);

	   glBindTexture(GL_TEXTURE_2D, texturename[0]);

	    glBegin(GL_TRIANGLE_STRIP);
	    {
		glTexCoord2f(0,0);
		glVertex3f(0,0,0);
		glTexCoord2f(1,0);
		glVertex3f(1,0,0);
		glTexCoord2f(0,1);
		glVertex3f(0,0,1);
		glTexCoord2f(1,1);
		glVertex3d(1,0,1);
	    }
	    glLoadIdentity();
	    SDL_GL_SwapBuffers();

(... and so on)

The problem is, that the sqare described in GL_TRIANGLE_STRIP appears white, without any texture. glColor3f works nicely, and renders triangle in any color I want. I have absolutely no idea what I've done wrong, and I would be thankful for any help. tia, M.

Share this post


Link to post
Share on other sites
Advertisement
first, you need to make sure your texture is loaded correctly. Use glGetError and glIsTexture to check.

Share this post


Link to post
Share on other sites
if the usage of glIsTexture (first time seen it :) ) is:

if(glIsTexture(texturename[0]) == GL_FALSE)
{
cout << "No texture :S\n";
}

then it does return "No texture". Can you tell me how could I use glGetError here?

Share this post


Link to post
Share on other sites
this was the code that i've created:

	if (glGetError() == GL_NO_ERROR)
cout << "NO ERRORS\n";


i put it on the line under the one checking if texture is loaded. it did write "NO ERRORS" though. any ideas?

Share this post


Link to post
Share on other sites
Quote:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tgaimage->GetWidth(), tgaimage->GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, tgaimage->GetImg());


you said your image is 8 bit indexed image, why GL_RGB?

Share this post


Link to post
Share on other sites
changed to:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX, tgaimage->GetWidth(), tgaimage->GetHeight(), 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, tgaimage->GetImg());


still 'No texture' and 'no gl errors' :S or maybe i shouldn't have changed both of them?

Share this post


Link to post
Share on other sites
You need to have a valid OpenGL context before you make any OpenGL calls. In this case your OpenGL context is obtained by SDL within the SDL_SetVideoMode function, so all your texture code needs to be moved below that function call. Also change your glTexImage2D call back. GL_COLOR_INDEX is not a valid internal format and I suspect your TGA loader will convert to RGB or RGBA for you.

Enigma

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!