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;
}