kodex
2005.04.04, 03:14 PM
Ok guys this is where i stand, I am adapting texture binding code for my project. Everything compiles with no problems i Succeed on finding and loading the texture,but when i go to display it i Get an untextured quad. Please be gentle on my code its just the texture loading section and its slapped together till i can get it loaded and working correctly. If you have any follow up questions please feel free to ask. Please be gentle OSC I know its sloppy =).
GLenum texFormat[ 1 ]; // Format of texture (GL_RGB, GL_RGBA)
NSSize texSize[ 1 ]; // Width and height
char *texBytes[ 1 ]; // Texture data
GLuint texture[ 1 ]; // Storage for one texture
Boolean loadBitmap(NSString *filename, int texIndex)
{
BOOL success = FALSE;
NSBitmapImageRep *theImage;
int bitsPPixel, bytesPRow;
unsigned char *theImageData;
int rowNum, destRowNum;
theImage = [ NSBitmapImageRep imageRepWithContentsOfFile:filename ];
if( theImage != nil )
{
bitsPPixel = [ theImage bitsPerPixel ];
bytesPRow = [ theImage bytesPerRow ];
if( bitsPPixel == 24 ) // No alpha channel
texFormat[ texIndex ] = GL_RGB;
else if( bitsPPixel == 32 ) // There is an alpha channel
texFormat[ texIndex ] = GL_RGBA;
texSize[ texIndex ].width = [ theImage pixelsWide ];
texSize[ texIndex ].height = [ theImage pixelsHigh ];
texBytes[ texIndex ] = calloc( bytesPRow * texSize[ texIndex ].height,
1 );
if( texBytes[ texIndex ] != NULL )
{
success = TRUE;
theImageData = [ theImage bitmapData ];
destRowNum = 0;
for( rowNum = texSize[ texIndex ].height - 1; rowNum >= 0;
rowNum--, destRowNum++ )
{
// Copy the entire row in one shot
memcpy( texBytes[ texIndex ] + ( destRowNum * bytesPRow ),
theImageData + ( rowNum * bytesPRow ),
bytesPRow );
}
}
}
return success;
}
Boolean loadGLTextures()
{
BOOL status = FALSE;
NSString *path = [NSString stringWithFormat:@"%@/%s", [ [ NSBundle mainBundle ] resourcePath ], "background1.bmp" ];
int texnum = 0;
if (loadBitmap (path , texnum))
{
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 );
free( texBytes[ 0 ] );
}
return status;
}
Boolean bindTexture(int texNum)
{
glEnable( GL_TEXTURE_2D ); // Enable texture mapping
glBindTexture( GL_TEXTURE_2D, texture[ 0 ] );
glBegin( GL_QUADS );
glColor3f(1.0f,1.0f, 0.0f);
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 0.0f, 0.0f, 0.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 300.0f, 0.0f, 0.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 300.0f, 300.0f, 0.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 0.0f, 300.0f, 0.0f ); // Top left
glEnd();
return true;
}
I reenabled 2d textures before i try to bind them just incase but it wasnt the problem. But they are enabled when I init OpenGL. And i Just set the yellow color to make sure the Quad was visable. Thanks in advance guys
GLenum texFormat[ 1 ]; // Format of texture (GL_RGB, GL_RGBA)
NSSize texSize[ 1 ]; // Width and height
char *texBytes[ 1 ]; // Texture data
GLuint texture[ 1 ]; // Storage for one texture
Boolean loadBitmap(NSString *filename, int texIndex)
{
BOOL success = FALSE;
NSBitmapImageRep *theImage;
int bitsPPixel, bytesPRow;
unsigned char *theImageData;
int rowNum, destRowNum;
theImage = [ NSBitmapImageRep imageRepWithContentsOfFile:filename ];
if( theImage != nil )
{
bitsPPixel = [ theImage bitsPerPixel ];
bytesPRow = [ theImage bytesPerRow ];
if( bitsPPixel == 24 ) // No alpha channel
texFormat[ texIndex ] = GL_RGB;
else if( bitsPPixel == 32 ) // There is an alpha channel
texFormat[ texIndex ] = GL_RGBA;
texSize[ texIndex ].width = [ theImage pixelsWide ];
texSize[ texIndex ].height = [ theImage pixelsHigh ];
texBytes[ texIndex ] = calloc( bytesPRow * texSize[ texIndex ].height,
1 );
if( texBytes[ texIndex ] != NULL )
{
success = TRUE;
theImageData = [ theImage bitmapData ];
destRowNum = 0;
for( rowNum = texSize[ texIndex ].height - 1; rowNum >= 0;
rowNum--, destRowNum++ )
{
// Copy the entire row in one shot
memcpy( texBytes[ texIndex ] + ( destRowNum * bytesPRow ),
theImageData + ( rowNum * bytesPRow ),
bytesPRow );
}
}
}
return success;
}
Boolean loadGLTextures()
{
BOOL status = FALSE;
NSString *path = [NSString stringWithFormat:@"%@/%s", [ [ NSBundle mainBundle ] resourcePath ], "background1.bmp" ];
int texnum = 0;
if (loadBitmap (path , texnum))
{
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 );
free( texBytes[ 0 ] );
}
return status;
}
Boolean bindTexture(int texNum)
{
glEnable( GL_TEXTURE_2D ); // Enable texture mapping
glBindTexture( GL_TEXTURE_2D, texture[ 0 ] );
glBegin( GL_QUADS );
glColor3f(1.0f,1.0f, 0.0f);
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( 0.0f, 0.0f, 0.0f ); // Bottom left
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 300.0f, 0.0f, 0.0f ); // Bottom right
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 300.0f, 300.0f, 0.0f ); // Top right
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( 0.0f, 300.0f, 0.0f ); // Top left
glEnd();
return true;
}
I reenabled 2d textures before i try to bind them just incase but it wasnt the problem. But they are enabled when I init OpenGL. And i Just set the yellow color to make sure the Quad was visable. Thanks in advance guys