我刚刚实现了基于this solution的纹理解决方案,该解决方案正确编译,但是我没有看到我的场景发生任何变化。 。如果这很重要的话,相机目前在我画的四边形内。
下面是我映射纹理的代码。
GLuint LoadTexture( const char * filename )
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
//int size = fseek(file,);
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 quad(int a,int b,int c,int d,float scaleX,float scaleY,float scaleZ )
{
glBegin(GL_QUADS);
glVertex3f(ver[a][0]*scaleX,ver[a][1]*scaleY,ver[a][2]*scaleZ);
glVertex3f(ver[b][0]*scaleX,ver[b][1]*scaleY,ver[b][2]*scaleZ);
glVertex3f(ver[c][0]*scaleX,ver[c][1]*scaleY,ver[c][2]*scaleZ);
glVertex3f(ver[d][0]*scaleX,ver[d][1]*scaleY,ver[d][2]*scaleZ);
glEnd();
}
void room1()
{
GLuint texture;
texture = LoadTexture( "brick.bmp" );
glBindTexture (GL_TEXTURE_2D, texture);
glColor3fv(color[6]);
quad(0,3,2,1,10,5,11);
glColor3fv(color[1]);
quad(2,3,7,6,10,5,11);
glColor3fv(color[2]);
quad(0,4,7,3,10,5,11);
glColor3fv(color[3]);
quad(1,2,6,5,10,5,11);
glColor3fv(color[4]);
quad(4,5,6,7,10,5,11);
glColor3fv(color[5]);
quad(0,1,5,4,10,5,11);
}