1

I've loaded a texture's ID into the tex variable, but when I bind the texture with this ID just before drawing a QUAD (seen in code below inside the renderScene function surrounded by the xxxxxxxxx... comments), no texture is getting applied to the QUAD; it remains colorless. No error messages either.

I've included all the texture-related things I've written in my program.

What else do I need to add/change?

#include<windows.h>
#include <GL/glut.h>
#include<iostream>
#include <stdlib.h>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>

// (...)



GLuint loadBMP_custom(const char * imagepath);
GLuint tex = loadBMP_custom("texture.bmp");



GLuint loadBMP_custom( const char * imagepath )
{

   GLuint texture;

   int width, height;

   unsigned char * data;

   FILE * file;

   file = fopen( imagepath, "rb" );

   if ( file == NULL ) return 0;

   width = 256;
   height = 256;
   data = (unsigned char *)malloc( width * height * 3 );

   fread( data, width * height * 3, 1, file );
   fclose( file );

  for(int i = 0; i < width * height ; ++i)
  {
    int index = i*3;
    unsigned char B,R;
    B = data[index];
    R = data[index+2];

    data[index] = R;
    data[index+2] = B;

  }



  glGenTextures( 1, &texture );
  glBindTexture( GL_TEXTURE_2D, texture );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
  free( data );

  return texture;
}



// (...)



void renderScene(void)
{

    glMatrixMode(GL_MODELVIEW);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(  posX, 1.0f, posZ,
            posX+vlx, posY+vly,  posZ+vlz,
            0.0f, 1.0f,  0.0f);




    // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

    glBindTexture(GL_TEXTURE_2D, tex);

    glBegin(GL_QUADS);
        glTexCoord2f (0.0, 0.0);
        glVertex3f(-100.0f, -0.02f, -100.0f);
        glTexCoord2f (0.0, 1.0);
        glVertex3f(-100.0f, -0.02f,  100.0f);
        glTexCoord2f (1.0, 1.0);
        glVertex3f( 100.0f, -0.02f,  100.0f);
        glTexCoord2f (1.0, 0.0);
        glVertex3f( 100.0f, -0.02f, -100.0f);
    glEnd();

    // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


    // [Also display 3D blocks in space besides the above QUAD, which serves as ground]

    glFlush();
    glutSwapBuffers();

}

//(...)

int main(int argc, char **argv)
{

    // (...)

    glEnable (GL_TEXTURE_2D) ;

    glutMainLoop();
    return 1;
}
dziadek1990
  • 277
  • 2
  • 10

1 Answers1

0

As mentioned in the comments, your method to load a BMP file is incorrect: even if you know how big the image is (256x256), you would still need to skip ahead to the pixel data part (usually 54 bytes, which is the usual size of the header).

So now you could write your own importer by reading the official spec over here: https://en.wikipedia.org/wiki/BMP_file_format

...Or also find many implementations on the web such as the one mentioned in this question: C++: Read bitmap wrong

Unless you find some easy-to-plug header(s)-only library, I would personally go against using a library to specifically deal with BMP files since it introduces extra dependencies for something quite easy to roll on your own (and at the same time, learn how to implement a simple file loader).

Community
  • 1
  • 1
Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47
  • 1
    This can't be the only problem. Failing to skip the header would just shift the data by 18 pixels. The OP is saying that the result is not textured at all. – Reto Koradi Jan 08 '16 at 04:04