我正在使用 OpenGL 中的纹理,遇到了一个奇怪的问题。有时,加载的图像会稍微向右推。我将在下面提供代码和屏幕截图。
#include <gl/glew.h>
#include <gl/glut.h>
#include <windows.h>
#include <stdio.h>
#include <SOIL.h>
const int WINDOW_WIDTH = 1024;
const int WINDOW_HEIGHT = 512;
GLuint Tex;
GLuint Tex2;
GLuint LoadTexture( const char * filename, int w, int h )
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = w;
height = h;
data = (unsigned char *)malloc( width * height * 3);
fread( data, width * height * 3, 1, file );
fclose( file );
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_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST );
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 Reshape( int width, int height )
{
glViewport( 0, 0, (GLsizei)width, (GLsizei)height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0f, WINDOW_WIDTH, 0.0f, WINDOW_HEIGHT, 1.0f, 100.0f );
glMatrixMode( GL_MODELVIEW );
}
void lightInit( void )
{
GLfloat lightWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightWhite );
glMaterialf( GL_FRONT, GL_SHININESS, 30 );
glLightfv( GL_LIGHT0, GL_AMBIENT, lightBlack );
glLightfv( GL_LIGHT0, GL_SPECULAR, lightWhite );
glLightfv( GL_LIGHT0, GL_DIFFUSE, lightWhite );
glLightfv( GL_LIGHT0, GL_POSITION, lightPos );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glEnable( GL_DEPTH_TEST );
}
void drawTexObj()
{
Tex = LoadTexture( "texture2.bmp", 1024, 512 );
if( Tex == 0 )
{
exit(0);
}
glEnable( GL_TEXTURE_2D );
glPushAttrib( GL_CURRENT_BIT );
glBegin( GL_QUADS );
glTexCoord2d( 0.0f, 0.0f );
glVertex2f( 0.0f, 0.0f );
glTexCoord2d( 0.0f, 1.0f );
glVertex2f( 0.0f, 512.0f );
glTexCoord2d( 1.0f, 1.0f );
glVertex2f( 1024.0f, 512.0f );
glTexCoord2d( 1.0f, 0.0f );
glVertex2f( 1024.0f, 0.0f );
glEnd();
glPopAttrib();
glDisable( GL_TEXTURE_2D );
glDeleteTextures( (GLsizei)1, &Tex );
}
void drawTexObj2()
{
Tex2 = LoadTexture( "texture.bmp", 1024, 512 );
if( Tex2 == 0 )
{
exit(0);
}
glEnable( GL_TEXTURE_2D );
glBegin( GL_QUADS );
glTexCoord2d( 0.0f, 0.0f );
glVertex2f( 0.0f, 0.0f );
glTexCoord2d( 0.0f, 1.0f );
glVertex2f( 0.0f, 256.0f );
glTexCoord2d( 1.0f, 1.0f );
glVertex2f( 512.0f, 256.0f );
glTexCoord2d( 1.0f, 0.0f );
glVertex2f( 512.0f, 0.0f );
glEnd();
glDisable( GL_TEXTURE_2D );
glDeleteTextures( (GLsizei)1, &Tex2 );
}
void Display()
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -1.0f );
drawTexObj();
drawTexObj2();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glEnable( GL_DEPTH );
glutInitWindowPosition( 200, 100 );
glutInitWindowSize( 1024, 512 );
glutCreateWindow( "Texturing :D!" );
//lightInit();
glutDisplayFunc( Display );
glutIdleFunc( Display );
glutReshapeFunc( Reshape );
glutMainLoop();
}