Probably a newbie problem :-)
Hi,
I''ve got a small problem. I downloaded some of Nehe''s tutorials to see what OpenGL is like. After some code browsing, I very much liked it and wanted to try and build something of my own. I want to make a screen with a texture on it and three object in front of it which are see through. I can Make a screen with a texture on it, and three transparent object but when i try to give the object in front of the screen a color, it''s doesn''t work. The color of the objects stays black but the texture kinda gets the color of the objects, kinda glowy like. When I turn the texture loading of and give the screen a normal color, the objects DO get the right color. Does anyone have an answer for this problem?
Thanx a bunch..
Hugo
Post the source code or email it to me because I don''t exactly understand what you''re talking about.
Open mouth, insert foot
Open mouth, insert foot
Just a guess...
What your doing is probably something like this
// set the texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
//set the color
glColor4f(1.0f,0.5f,1.0f,1.0f);
glBegin(GL_QUADS);
//draw object
...
glEnd();
If this is correct then what happens is the quads that are drawn have the currently bound texture but the color applied acts more like a filter on the texture.
As for what your trying to do I don''t exactly understand. perhaps if you could explain it a little better I (or others here) might have an idea how to do it.
What your doing is probably something like this
// set the texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);
//set the color
glColor4f(1.0f,0.5f,1.0f,1.0f);
glBegin(GL_QUADS);
//draw object
...
glEnd();
If this is correct then what happens is the quads that are drawn have the currently bound texture but the color applied acts more like a filter on the texture.
As for what your trying to do I don''t exactly understand. perhaps if you could explain it a little better I (or others here) might have an idea how to do it.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
Hi again,
Maybe I wasn''t very clear, sorry. Maybe if I post some code it''s easier to understand my problem. The scene is build up with 4 object. The firts is a screen (rectangle) in the back which I want to attach a texture on. The other three object are in front of the screen and have to be a certain color. The problem is if a attach a texture to the screen, the other object don''t become the wanted color but it looks like the same texture is attached to one quad of the sphere. Here''s my code..
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit
if (TextureImage[0]=LoadBMP("Data/space.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(3, &texture[0]); // Create The Texture
// Create Nearest Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
//if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
//{
// return FALSE; // If Texture Didn''t Load Return FALSE
//}
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
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_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return TRUE; // Initialization Went OK
}
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-8.0f);
glColor3f(1.0, 0.0, 0.0);
DrawBack();
glLoadIdentity();
glColor4f(1.0, 1.0, 0.0, 0.5);
glTranslatef(-1.3f, 0.0f, -4.0f);
DrawSphere();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawDisk();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawCylinder();
return TRUE; // Keep Going
}
void DrawBack(void)
{
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -2.0f, 2.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 3.0f, -2.0f, 2.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 3.0f, 2.0f, 2.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 2.0f, 2.0f);
glEnd();
}
void DrawSphere(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Bol tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_LINE);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluSphere(qobj,0.5 ,20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
void DrawDisk(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Disk tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluDisk(qobj,0.25 ,0.5, 20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
void DrawCylinder(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Cylinder tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_LINE);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj,0.5 ,0.0, 0.6, 20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
Thanx a lot,
Hugo
Maybe I wasn''t very clear, sorry. Maybe if I post some code it''s easier to understand my problem. The scene is build up with 4 object. The firts is a screen (rectangle) in the back which I want to attach a texture on. The other three object are in front of the screen and have to be a certain color. The problem is if a attach a texture to the screen, the other object don''t become the wanted color but it looks like the same texture is attached to one quad of the sphere. Here''s my code..
AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit
if (TextureImage[0]=LoadBMP("Data/space.bmp"))
{
Status=TRUE; // Set The Status To TRUE
glGenTextures(3, &texture[0]); // Create The Texture
// Create Nearest Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}
free(TextureImage[0]); // Free The Image Structure
}
return Status; // Return The Status
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
//if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
//{
// return FALSE; // If Texture Didn''t Load Return FALSE
//}
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
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_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return TRUE; // Initialization Went OK
}
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-8.0f);
glColor3f(1.0, 0.0, 0.0);
DrawBack();
glLoadIdentity();
glColor4f(1.0, 1.0, 0.0, 0.5);
glTranslatef(-1.3f, 0.0f, -4.0f);
DrawSphere();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawDisk();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawCylinder();
return TRUE; // Keep Going
}
void DrawBack(void)
{
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.0f, -2.0f, 2.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 3.0f, -2.0f, 2.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 3.0f, 2.0f, 2.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.0f, 2.0f, 2.0f);
glEnd();
}
void DrawSphere(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Bol tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_LINE);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluSphere(qobj,0.5 ,20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
void DrawDisk(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Disk tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluDisk(qobj,0.25 ,0.5, 20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
void DrawCylinder(void)
{
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
//Cylinder tekenen
GLUquadricObj *qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GLU_LINE);
gluQuadricTexture(qobj, GL_FALSE);
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj,0.5 ,0.0, 0.6, 20, 20);
xrot+=xspeed;
yrot+=yspeed;
glPopMatrix();
}
Thanx a lot,
Hugo
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Ok! I got it!
You enable textures right away and then leave it enabled! That means everything is being drawn with the currently bound texture!!! What you need to do is turn off textures for the objects that don''t need them with glDisable(GL_TEXTURE_2D)!
Try this change and let me know if it works.
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-8.0f);
glColor3f(1.0, 0.0, 0.0);
glEnable(GL_TEXTURE_2D); // <------------this starts the texture
DrawBack();
glDisable(GL_TEXTURE_2D); // <-----------this stops using the texture
glLoadIdentity();
glColor4f(1.0, 1.0, 0.0, 0.5);
glTranslatef(-1.3f, 0.0f, -4.0f);
DrawSphere();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawDisk();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawCylinder();
return TRUE; // Keep Going
}
You enable textures right away and then leave it enabled! That means everything is being drawn with the currently bound texture!!! What you need to do is turn off textures for the objects that don''t need them with glDisable(GL_TEXTURE_2D)!
Try this change and let me know if it works.
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-8.0f);
glColor3f(1.0, 0.0, 0.0);
glEnable(GL_TEXTURE_2D); // <------------this starts the texture
DrawBack();
glDisable(GL_TEXTURE_2D); // <-----------this stops using the texture
glLoadIdentity();
glColor4f(1.0, 1.0, 0.0, 0.5);
glTranslatef(-1.3f, 0.0f, -4.0f);
DrawSphere();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawDisk();
glTranslatef (1.3f, 0.0f, 0.0f);
DrawCylinder();
return TRUE; // Keep Going
}
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
Hi,
Yes, It''s works.... Thanx a bunch. I thought I tried this option but I had the glEnable(GL_TEXTURE_2d) in the initgl. This was probably the problem :-)
Thanx
Hugo
Yes, It''s works.... Thanx a bunch. I thought I tried this option but I had the glEnable(GL_TEXTURE_2d) in the initgl. This was probably the problem :-)
Thanx
Hugo
correct me if I''m wrong (I''m a newbie too) but if you want a just colour object then don''t you have to disable texture mapping, do the stuff, then enable texture mapping.
I remember seeing that somewhere.
K
Baldur K
I remember seeing that somewhere.
K
Baldur K
That''s pretty much what I said but your looking at it backwards from the way I was....
you: disable textures, draw non-textured objects, enable textures.
me: Enable textures, draw textured object, disable textures.
Eather way it means that textures have to be disabled to have a solid color object!
l8r,
Rob - http:/tannara.2y.net/
you: disable textures, draw non-textured objects, enable textures.
me: Enable textures, draw textured object, disable textures.
Eather way it means that textures have to be disabled to have a solid color object!
l8r,
Rob - http:/tannara.2y.net/
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement