I’m trying to texture map the top of a quad using a .bmp image that is mostly grey color and looks like a roadway. When I run my project the entire quad is black. It’s not supposed to be. Can anyone explain why and offer some solutions for my problem? I’m not a confident OpenGL user so I found a sample that does texture mapping and I used it. I’m not too sure how I can go about this. I want to be able to texture map other objects as well. Here is the segment that sets up everything.
#include “model.h”
#include <iostream>
#include <fstream>
const char* const fileName = “wall.bmp”; // The name of the image file
const int texHeight = 128; // The image’s height, in pixels
const int texWidth = 128; // The image’s width, in pixels
struct RGB { // The data on one RGB pixel
unsigned char red;
unsigned char green;
unsigned char blue;
};
RGB image[texHeight][texWidth]; // The image
GLuint texture; // Identifier for the image’s texture
void initTexture( void )
{
// Try to open the file:
ifstream in( fileName );
// If the file opened OK, read the texture from it. Read rows from
// highest-indexed to lowest, since in the file the first row is
// the top of the image, which OpenGL expects to have in the
// highest-indexed part of the texture array:
if ( in )
{
for ( int r = texHeight-1; r >= 0; r-- )
{
for ( int c = 0; c < texWidth; c++ )
{ in.read( reinterpret_cast<char*>(&image[r][c]), sizeof(RGB) ); }
}
}
else {
// If the file didn't open, tell the user and set the texture
// to a uniform medium gray:
printf("Couldn't Open File");
for ( int r = 0; r < texHeight; r++ ) {
for ( int c = 0; c < texWidth; c++ ) {
image[r][c].red = image[r][c].green = image[r][c].blue = 127;
}
}
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
}
void init ( void )
{
glClearColor ( 1.0, 1.0, 1.0, 0.0 );
glEnable ( GL_DEPTH_TEST );
}
void display ( void )
{
glClearDepth( 1.0 );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture );
Draw_Base( );
glDisable(GL_TEXTURE_2D);
Draw_Outside_Wall( );
Draw_Inside_Walls( );
glutSwapBuffers ( );
}
void reshape(int w, int h)
{
GLfloat x1, y1, z1, x2, y2, z2, x3, y3, z3;
/* origin of camera /
x1 = 5.25f;
y1 = 1.0f;
z1 = 4.25f;
/ point looking at /
x2 = 0.0f;
y2 = 0.1f;
z2 = 0.0f;
/ camera rotation */
x3 = 0.0f;
y3 = 1.0f;
z3 = 0.0f;
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
glFrustum( -0.33, 0.33, -0.33, 0.33, 0.5, 20.0 );
gluLookAt( x1, y1, z1, x2, y2, z2, x3, y3, z3);
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}
#pragma argsused
void keyboard ( unsigned char key, int x, int y )
{
switch ( key ) {
case 27: /* Escape key */
exit ( 0 );
break;
case ‘f’:
glutFullScreen ( );
break;
case ‘w’:
glutReshapeWindow ( 250, 250 );
break;
default:
break;
}
}
/* Main Loop
- Open window with initial window size, title bar,
- RGBA display mode, and handle input events.
/
int main ( int argc, char* argv )
{
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize ( 800, 600 );
glutCreateWindow ( “3D Maze” );
init ( );
glutReshapeFunc ( reshape );
glutKeyboardFunc ( keyboard );
glutDisplayFunc ( display );
initTexture();
glEnable( GL_DEPTH_TEST );
glutMainLoop ( );
return 0;
}
this is what my base looks like I’m trying to map:
glBegin(GL_QUADS);
glTexCoord2f( 1.0f, 1.0f );glVertex3f( 5.1f, 0.1f, -4.1f );// back right
glTexCoord2f( 0.0f, 1.0f );glVertex3f( -5.1f, 0.1f, -4.1f );//back left
glTexCoord2f( 0.0f, 0.0f );glVertex3f( -5.1f, 0.1f, 4.1f );//front left
glTexCoord2f( 1.0f, 0.0f );glVertex3f( 5.1f, 0.1f, 4.1f ); //front right
glEnd( );