Texture Bug

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 &gt;= 0; r-- ) 
    {
        for ( int c = 0; c &lt; texWidth; c++ ) 
         { in.read( reinterpret_cast&lt;char*&gt;(&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 &lt; texHeight; r++ ) {
        for ( int c = 0; c &lt; 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( );

Edited for misinformation!!

You want to call glTexEnv and glTexParameter functions just before you make the call to glTexImage2D otherwise the texture environment you set up will have no effect. You should go and read the chapter on texturing in the online Red Book. It will help allot.

Old GLman

[This message has been edited by Old GLman (edited 11-02-2002).]

You are storing your image in a 2d array and OpenGL does not accept 2d arrays.

There’s nothing wrong with a 2D array. In memory, all elements are stored directly after each other, exactly the same as with a 1D array. If you look at the memory layout on byte level, they will look exactly the same. So it doesn’t make any differnce to OpenGL.

Furthermore, you want to call glTexEnv and glTexParameter functions just before you make the call to glTexImage2D otherwise the texture environment you set up will have no effect.

You can have glTexEnv and glTexParameter anywhere you like, before or after glTexImage2D, it doesn’t matter. As long as you have the correct texture bound. Ane by the way, glTexEnv sets states for the active texture unit, and does not affect, nor is afefcted by, the currently active texture.

Bob, your absolutely right ofcourse. Ive been programming with arrays all day so my brain is array fried. Thanks for pointing it out. However, I still think that making calls to glTexParameter and glTexEnv randomly is sloppy and can lead to errors when working with multiple texture units. So OpenGL applies the texture environment after uploading it to OpenGL?

Well I dropped the prior way of using .bmp files to texture map and adopted nehe’s example in tut #24 for texture mapping .TGA files. I can get it to texture map using the Floor.TGA file but File.TGA does not work. I tried using teh File.TGA in textures[0].textID and the base won’t texture map. I’m suspecting something is wrong with my File.TGA but I’m not completely sure.
here is my code http://sourcepost.sytes.net/sourcepost/sourceview.aspx?source_id=1573