Open GL texture Issues
I've been programming for a few years and I'm just starting out with OpenGL.
I decided to start by modifying the tutorials found at http://nehe.gamedev.net/
The problem I am having is that the glColorXf( ) functions seem to break when GL_TEXTURE_2D is enabled. And the effect is different in 10.3 and 10.4
10.3 -![[Image: 1033un.th.jpg]](http://img60.echo.cx/img60/4710/1033un.th.jpg)
10.4 -![[Image: 1043gp.th.jpg]](http://img20.echo.cx/img20/2735/1043gp.th.jpg)
and this is how it looks with textures disabled:
![[Image: notex1ow.th.jpg]](http://img20.echo.cx/img20/6918/notex1ow.th.jpg)
this is the draw code:
I decided to start by modifying the tutorials found at http://nehe.gamedev.net/
The problem I am having is that the glColorXf( ) functions seem to break when GL_TEXTURE_2D is enabled. And the effect is different in 10.3 and 10.4
10.3 -
![[Image: 1033un.th.jpg]](http://img60.echo.cx/img60/4710/1033un.th.jpg)
10.4 -
![[Image: 1043gp.th.jpg]](http://img20.echo.cx/img20/2735/1043gp.th.jpg)
and this is how it looks with textures disabled:
![[Image: notex1ow.th.jpg]](http://img20.echo.cx/img20/6918/notex1ow.th.jpg)
this is the draw code:
Code:
- (BOOL) loadGLTextures
{
BOOL status = FALSE;
if( [ self loadBitmap:[ NSString stringWithFormat:@"%@/%s",
[ [ NSBundle mainBundle ] resourcePath ],
"NeHe.bmp" ] intoIndex:0 ] ) {
status = TRUE;
glGenTextures( 1, &texture[ 0 ] ); // Create the texture
// Typical texture generation using data from the bitmap
glBindTexture( GL_TEXTURE_2D, texture[ 0 ] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, texSize[ 0 ].width,
texSize[ 0 ].height, 0, texFormat[ 0 ],
GL_UNSIGNED_BYTE, texBytes[ 0 ] );
// Linear filtering
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST );
free( texBytes[ 0 ] );
}
return status;
}
- (void) initGL {
[ self loadGLTextures ];
//Initilize OpenGL
//glEnable( GL_TEXTURE_2D ); // Enable texture mapping
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 ); // Enable depth testing
glDepthFunc( GL_LEQUAL ); // Type of depth test to do
// Really nice perspective calculations
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
- (void) drawRect: (NSRect) rect {
if (!initilized) {
[self initGL];
initilized = TRUE;
}
// Tutorial A's code
glViewport(0, 0, (GLsizei) rect.size.width, (GLsizei) rect.size.height);
glMatrixMode(GL_PROJECTION); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
// calculate the aspect ratio of the window.
gluPerspective(45.0f, (GLfloat) rect.size.width /(GLfloat) rect.size.height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // Sets the projection matrix.
glLoadIdentity(); // Reset the modelview matrix.
// Tutorial B's code
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();
// Move Left 1.5 Units And Into The Screen 6.0
glTranslatef( ((GLfloat) position[0]) + -1.5f, ((GLfloat) position[1]),-6.0f);
if (triDraw) {
glRotatef((GLfloat) tRotation,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor4f( 1.0f, 0.0f, 0.0f, 0.5); // Set color to red
glVertex3f( 0.0f, 1.0f, 0.0f ); // Top of front
glColor3f( 0.0f, 1.0f, 0.0f ); // Set color to green
glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom left of front
glColor3f( 0.0f, 0.0f, 1.0f ); // Set color to blue
glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom right of front
glColor3f( 1.0f, 0.0f, 0.0f ); // Red
glVertex3f( 0.0f, 1.0f, 0.0f ); // Top of right side
glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
glVertex3f( 1.0f, -1.0f, 1.0f ); // Left of right side
glColor3f( 0.0f, 1.0f, 0.0f ); // Green
glVertex3f( 1.0f, -1.0f, -1.0f ); // Right of right side
glColor3f( 1.0f, 0.0f, 0.0f ); // Red
glVertex3f( 0.0f, 1.0f, 0.0f ); // Top of back side
glColor3f( 0.0f, 1.0f, 0.0f ); // Green
glVertex3f( 1.0f, -1.0f, -1.0f ); // Left of back side
glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
glVertex3f( -1.0f, -1.0f, -1.0f ); // Right of back side
glColor3f( 1.0f, 0.0f, 0.0f ); // Red
glVertex3f( 0.0f, 1.0f, 0.0f ); // Top of left side
glColor3f( 0.0f, 0.0f, 1.0f ); // Blue
glVertex3f( -1.0f, -1.0f, -1.0f ); // Left of left side
glColor3f( 0.0f, 1.0f, 0.0f ); // Green
glVertex3f( -1.0f, -1.0f, 1.0f ); // Right of left side
glEnd();
glLoadIdentity();
glTranslatef(1.5f,0.0f,-7.0f);
} // Finished Drawing The Triangle
else {
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
}
glColor3f( 1.0f, 1.0f, 1.0f ); // set the color to white to cover the texture
glRotatef((GLfloat) sRotation,1.0f,1.0f,0.0f); // Rotate The Quad On The X axis
// Now we draw our texture mapped cube
glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture
glBegin( GL_QUADS );
// Front face
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, 1.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, 1.0f ); // Top left
// Back face
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, -1.0f ); // Top left
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom left
// Top face
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f ); // Top left
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, -1.0f ); // Top right
// Bottom face
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( -1.0f, -1.0f, -1.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 1.0f, -1.0f, -1.0f ); // Top left
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom right
// Right face
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, -1.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 1.0f, 1.0f, 1.0f ); // Top left
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom left
// Left face
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, 1.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( -1.0f, 1.0f, -1.0f ); // Top left
glEnd();
[[self openGLContext] flushBuffer];
}
LINEAR_MIPMAP_NEAREST is not a valid magnification mode. try LINEAR.
This may or may not be related, but you are not enabling blending, and the first color you set in the pyramid has 0.5f alpha. That should probably be glColor3f().
Aside from that, It seems that you are not disabling texturing before drawing the pyramid, and are not providing it any texture coordinates, so it's texturing it with a single pixel, which is likely to be black.
As glEnable(GL_TEXTURE_2D) is commented out at the start I don't see how it ever textures anything, so I'm guessing it was not commented out when you took the screenshots.
in short.. call glEnable(GL_TEXTURE_2D) before the drawing of the cube, and glDisable(GL_TEXTURE_2D) before the pyramid, and I think your problem will disappear.
David
Aside from that, It seems that you are not disabling texturing before drawing the pyramid, and are not providing it any texture coordinates, so it's texturing it with a single pixel, which is likely to be black.
As glEnable(GL_TEXTURE_2D) is commented out at the start I don't see how it ever textures anything, so I'm guessing it was not commented out when you took the screenshots.
in short.. call glEnable(GL_TEXTURE_2D) before the drawing of the cube, and glDisable(GL_TEXTURE_2D) before the pyramid, and I think your problem will disappear.
David
Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Can you post the 'original texture' ?
So, assuming you are using NSBitmapImageRep for loading your bitmap, starting from MacOS 10.4, the loadBitmap returns a GL_ARGB (Big endian) instead of GL_RGBA (
Little Endian). Apparently this change is undocumented (or if someone can point to the documentation stating it).
So it's texFormat[ 0 ] = GL_BGRA_EXT;
and replace GL_UNSIGNED_BYTE to GL_UNSIGNED_INT_8_8_8_8_REV
if running on MacOS 10.4 ...
[EDIT]
In, fact you need to use GL_UNSIGNED_BYTE_8_8_8_8_REV on PPC or GL_UNSIGNED_BYTE_8_8_8_8 on Intel.
#if __BIG_ENDIAN__
GL_UNSIGNED_BYTE_8_8_8_8_REV
#else
GL_UNSIGNED_BYTE_8_8_8_8
#endif
So, assuming you are using NSBitmapImageRep for loading your bitmap, starting from MacOS 10.4, the loadBitmap returns a GL_ARGB (Big endian) instead of GL_RGBA (
Little Endian). Apparently this change is undocumented (or if someone can point to the documentation stating it).
So it's texFormat[ 0 ] = GL_BGRA_EXT;
and replace GL_UNSIGNED_BYTE to GL_UNSIGNED_INT_8_8_8_8_REV
if running on MacOS 10.4 ...
[EDIT]
In, fact you need to use GL_UNSIGNED_BYTE_8_8_8_8_REV on PPC or GL_UNSIGNED_BYTE_8_8_8_8 on Intel.
#if __BIG_ENDIAN__
GL_UNSIGNED_BYTE_8_8_8_8_REV
#else
GL_UNSIGNED_BYTE_8_8_8_8
#endif
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Draw to texture using FBOs - aspect ratio issues | Madrayken | 2 | 3,294 |
Jul 15, 2010 11:47 AM Last Post: Madrayken |
|
| Texture crashing issues | Josh | 9 | 3,297 |
Apr 11, 2003 07:49 PM Last Post: Josh |
|

