PDA

View Full Version : Complete texture getting red?



nix
03-25-2001, 11:56 AM
I have a real big problem. This example is to put a picture on a GL_QUADS and then put a red triangle in front of it. When I donīt specify any color on the triangle, it got some braun color. Then I type the...

glColor3f(1,0,0);

...to get it red. The triangle gets red and also the picture gets red. I dont know what I'm doing wrong, please help me.

My Code:
// Loads A Bitmap Image
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL; // File Handle

if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
// Check To See If The File Exists
File=fopen(Filename,"r");

if (File) // Does The File Exist?
{
fclose(File);
return auxDIBImageLoad(Filename);
// Load The Bitmap And Return A Pointer
}
return NULL;
// If Load Failed Return NULL
}

/////////////////////////////////////////////
// Load Bitmaps And Convert To Textures
int LoadGLTextures()
{
int Status=FALSE;
// Status Indicator

AUX_RGBImageRec *TextureImage[1];
// Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*2); // Set The Pointer To NULL, Clear the image record

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ( TextureImage[0]=LoadBMP("Data/BG.bmp") )
{
Status=TRUE;
// Set The Status To TRUE

glGenTextures(1, &texture[0]);
// Build Linear Mipmapped Texture
glBindTexture( GL_TEXTURE_2D, texture[0] );

// 2D-MODE,
glTexImage2D(GL_TEXTURE_2D,0,3,
TextureImage[0]->sizeX, TextureImage[0]- >sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,
TextureImage[0]->data);

glTexParameteri
(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameteri
(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
GL_LINEAR);

if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
}
return Status;
}
/////////////////////////////////////////////int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE;
}
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(-1.5,0.0,-6.0);
glPushMatrix();
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glVertex3f(0,1,0);
glVertex3f(-1,-1,0);
glVertex3f(1,-1,0);
glEnd();
glPopMatrix();

glEnable(GL_TEXTURE_2D);
glPushMatrix();
glBindTexture( GL_TEXTURE_2D, texture[0] );
glTranslatef(0.0f, 0.0f, -2.0f);
glBegin( GL_QUADS );
glTexCoord2f(0.0f, 0.0f); glVertex3f(
-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(
1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(
1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(
-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

glFlush();

return TRUE;
}

I would be glad for some help....

Nutty
03-25-2001, 12:08 PM
After you've drawn the red, triangle, set the color back to white. glColor(1.0f, 1.0f, 1.0f);

The reason your texture goes red, is because the texture is multiplied by the current vertex colors. As the color is set to red still, your texture goes red.

Nutty

Bah, Fogerty just piped me to the answer by a few seconds... http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Nutty (edited 03-25-2001).]

Brent Fogarty
03-25-2001, 12:08 PM
The texture is red because you told opengl to draw in red when you were drawing the triangle and then didn't set it back to white before drawing the quad. If you want your texture to be the correct colour just call glColor3f(1, 1, 1); before drawing it and it will be correct. Setting the colour to anything but white will affect the way the texture looks.

This might seem like a problem to you now, but you can use this to create some interesting effects.

I hope that made sense

nix
03-25-2001, 09:14 PM
THANKS A LOT !!!!!!

IT NOW WORKS THE WAY IT SHOULD !!!

/nix