texture not showing

I have a 2d orthogonal projection and im trying to texture map a bmp on the whole screen as a background. However when I run this code, nothing gets mapped, all white. The front portion of this code is something i got from Nehe that grabs info from the bmp into the byte array. I checked the byte array and it looks fine.

 
#include <stdio.h> 
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define WINDOW_WIDTH 430
#define WINDOW_HEIGHT 360


/* storage for one texture  */
int texture[1];

/* Image type - contains height, width, and data */
struct Image {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
};
typedef struct Image Image;

// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.  
// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
int ImageLoad(char *filename, Image *image) {
    FILE *file;
    unsigned long size;                 // size of the image in bytes.
    unsigned long i;                    // standard counter.
    unsigned short int planes;          // number of planes in image (must be 1) 
    unsigned short int bpp;             // number of bits per pixel (must be 24)
    char temp;                          // temporary color storage for bgr-rgb conversion.
    float ff;
    int ii;
    int j, k;
    char c;
    // make sure the file is there.
    if ((file = fopen(filename, "rb"))==NULL)
    {
	printf("File Not Found : %s
",filename);
	return 0;
    }
    
    // seek through the bmp header, up to the width/height:
    fseek(file, 18, SEEK_CUR);

    // read the width
    if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
	printf("Error reading width from %s.
", filename);
	return 0;
    }
    printf("Width of %s: %lu
", filename, image->sizeX);
    
    // read the height 
    if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
	printf("Error reading height from %s.
", filename);
	return 0;
    }
    printf("Height of %s: %lu
", filename, image->sizeY);
    
    // calculate the size (assuming 24 bits or 3 bytes per pixel).
    size = image->sizeX * image->sizeY * 3;

    // read the planes
    if ((fread(&planes, 2, 1, file)) != 1) {
	printf("Error reading planes from %s.
", filename);
	return 0;
    }
    if (planes != 1) {
	printf("Planes from %s is not 1: %u
", filename, planes);
	return 0;
    }


    // read the bpp
    if ((i = fread(&bpp, 2, 1, file)) != 1) {
	printf("Error reading bpp from %s.
", filename);
	return 0;
    }
    if (bpp != 24) {
	printf("Bpp from %s is not 24: %u
", filename, bpp);
	return 0;
    }
	
    // seek past the rest of the bitmap header.
    fseek(file, 24, SEEK_CUR);

    // read the data. 
    image->data = (char *) malloc(size);
    if (image->data == NULL) {
	printf("Error allocating memory for color-corrected image data");
	return 0;	
    }

    
    if ((i = fread(image->data, size, 1, file)) != 1) {
      printf("Error reading image data from %s.
", filename);
      return 0;
    }
   /

    
   for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
	temp = image->data[i];
	image->data[i] = image->data[i+2];
	image->data[i+2] = temp;
    }
    // we're done.
    return 1;
}
    
// Load Bitmaps And Convert To Textures
void LoadGLTextures(char *filename) {	
    // Load Texture
    Image *image1;
    
    // allocate space for texture
    image1 = (Image *) malloc(sizeof(Image));
    if (image1 == NULL) {
	printf("Error allocating space for image");
	exit(0);
    }

    if (!ImageLoad(filename, image1)) {
	exit(1);
    }        

    // Create Texture	
    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)
    
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
    
    // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, 
    // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
    glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};


void myinit(char* filename)
{
  LoadGLTextures(filename);		 
  glEnable(GL_TEXTURE_2D);
  glClearColor(1.0, 1.0, 1.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);

}

// Draw ground
void drawGround()
{
        glBindTexture(GL_TEXTURE_2D, texture[0]);   // choose the texture to use. glBegin(GL_POLYGON);	
	
    glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0);	// Bottom Left Of The Texture and Quad
     glTexCoord2f(0.0f, 1.0f); glVertex2i( 0, WINDOW_HEIGHT);	// Top Left Of The Texture and Quad  
    glTexCoord2f(1.0f, 1.0f); glVertex2i( WINDOW_WIDTH, WINDOW_HEIGHT);	// Top Right Of The Texture and Quad
 glTexCoord2f(1.0f, 0.0f); glVertex2i( WINDOW_WIDTH, 0);	// Bottom Right Of The Texture and Quad
    glEnd();
   
   glFlush();
}



// Display function
void display()
{
  // Clear buffer
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
  // Set up texture and draw ground
  drawGround();
  glutSwapBuffers();
}


int main (int argc, char ** argv)
{
  /* Initialize glut */
  glutInit(&argc,argv);
  
  /* Set up window modes */
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  /* Set window position (where it will appear when it opens) */
  glutInitWindowPosition(0,0);

  /* Set size of window */
  glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);

  /* Create the window */
  glutCreateWindow("War Simulator");

  /* Turn on depth testing */
  glEnable(GL_DEPTH_TEST);
  
  /* tells glut to use a particular display function to redraw */
  glutDisplayFunc(display);

  /* Set idle function.  You can change this to call code for your animation,
   * or place your animation code in doIdle */
  glutIdleFunc(doIdle);

  /* do initialization */
  myinit(argv[1]);

  /* Begin GL loop */
  glutMainLoop();

  return 0;
}
 

OK… I think I know why it didnt work. My bmp picture was 430 X360 pixels. Is that why nothing showed up? So I need to add padding to make it 512X512?

Unfortunately… its still nothing even when I used a 512X512 bitmap

you can use gluScaleImage to scale any image to 256x256 or 512x512 etc.

by the way, you don’t have to rearrange the bitmap data if you use GL_BGR instead of GL_RGB in glTexImage2D.

concerning the textures- i usually use these settings:

GLuint tex;
char tex_data[];

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_BGR, GL_UNSIGNED_BYTE, tex_data);

i see you defined your texture name as ‘int textures[]’- it should be GLuint.

Thats not the problem (defining it as texture[], im just using the first number in the array.
Anyone have any ideas? Im sure I did the steps right…

well, look at this line from drawGround():

glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use. glBegin(GL_POLYGON);

the two slashes make the rest of the line a comment. if you put glBegin(GL_POLYGON) in a new line, it should work.

I love you GreetingsFromMunich. you are awesome!

:slight_smile: thank you very much. to avoid this in the future, a good idea would be to use an editor which can do syntax highlighting (like nedit, if you program in linux). these editors usually put c/c++ -keywords in different colors, maybe in a bold font, and comments are displayed in italic font. in nedit, your code would look something like

void drawGround() {
glBindTexture(GL_TEXTURE_2D, texture[0]); // choose the texture to use. glBegin(GL_POLYGON);

you can immediately see if there’s a syntax error- if you type ‘vodi’ instead of ‘void’, it is not displayed in bold. and you see that glBegin(GL_POLYGON) is interpreted as a comment.