Jump to content
  • Advertisement
Sign in to follow this  
zoulou2356

OpenGL Textures and colors

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

Hi all, I'm currently trying to draw a 2D OpenGL scene. It contains a background (a texture from a BMP file) and some objects drawn over it. My problem is that all the objects drawn after the background appear in black and not in the color I defined for them. I found some information on the Web about blending. Is this the solution ? Can someone explain me what is wrong in my code ? Thanks a lot for your help. --------------------------- OpenGL init
// OpenGL Initialisation
   // Reset the Viewport
	glViewport(0,0,width,height);
	// Select the projection matrix
	glMatrixMode(GL_PROJECTION);
	// Reset the projection matrix
	glLoadIdentity();									
	// Calculate the aspect ratio of the window
	gluOrtho2D(-(width/2),(width/2),-(height/2),(height/2));
	// Select the modelview matrix
	glMatrixMode(GL_MODELVIEW);
   // Reset the modelview matrix
	glLoadIdentity();
	// smooth shading
	glShadeModel(GL_SMOOTH);
	// white background (RGBA)
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	// Depth buffer setup
	glClearDepth(1.0f);
	// Enable depth testing
	glEnable(GL_DEPTH_TEST);
   // Enable texturing
   glEnable(GL_TEXTURE_2D);
	// Type of depth testing
	glDepthFunc(GL_LEQUAL);
	// Setup really nice perspective calculations
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


Texture creation for the background
// loading the BMP file
   HBITMAP hBmp = (HBITMAP) ::LoadImage (NULL, file , IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
   // get a BITMAP structure from the file
   BITMAP BM;
   GetObject (hBmp, sizeof (BM), &BM);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
   // binding the new texture
   glBindTexture(GL_TEXTURE_2D, this->texID);
   // creating the texture
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, BM.bmWidth, BM.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE,BM.bmBits);

   // giving the right parameters for the texture
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


Drawing of the background
// select the texture
   glBindTexture(GL_TEXTURE_2D, texID);
   // reset the modelView matrix
   glLoadIdentity();
   // positioning background (center)
   glTranslatef(0.0f, 0.0f, 0.0f);
   // select white for background of texture
   glColor3f(1.0f,1.0f,1.0f);

   // draw the background
   glBegin(GL_QUADS);
      glTexCoord2f(1.0f, 1.0f); glVertex2f(width/2.0f, height/2.0f); // top left
      glTexCoord2f(0.0f, 1.0f); glVertex2f(-width/2.0f, height/2.0f); // top right
      glTexCoord2f(0.0f, 0.0f); glVertex2f(-width/2.0f,  -height/2.0f); // bottom right
      glTexCoord2f(1.0f, 0.0f); glVertex2f(width/2.0f,  -height/2.0f); // bottom left
   glEnd();


Drawing of other shapes over the background
glTranslatef(x, y, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
         glVertex2f(-hw, -hh); // top left
         glVertex2f(hw, -hh); // top right
         glVertex2f(hw, hh); // bottom right
         glVertex2f(-hw, hh); // bottom left
      glEnd();


Share this post


Link to post
Share on other sites
Advertisement
If you enable texturing only in the init function, it will be enabled all the time since you never disable it. That includes when you draw the other shapes, which is not what you want.

Never control states like that globally, becuase they will be set all the time, and you don't want texturing enabled all the time. Only enable texturing when you need it, and disable it immediately when you're done with it for the moment. That way, texturing will never be enabled when you don't expect it to.

Share this post


Link to post
Share on other sites
Thank you a lot for your answer, it works perfectly when I enable GL_TEXTURE_2D only when I draw textures.

I'm a beginner with OpenGL and I think I really have a lot to learn yet...

Thanks again

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!