我正在使用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();
}
答案 0 :(得分:2)
这看起来像是首先阅读文件的问题。 BMP文件不是简单的像素字节整齐数组,因此您不能只读取宽度*高度* 3字节。您将在那里看到图像数据(假设24 bpp存储),但是还有标题信息将在显示屏上推动像素数据。我很惊讶第二个位图看起来还不错,尽管这可能与标题与图像宽度匹配一致。
更好的解决方案是使用Gdi加载图像......
GdiplusStartupInput lstartup_input;
GdiplusStartupOutput lstartup_output;
ULONG_PTR ltoken;
GdiplusStartup(<oken, &lstartup_input, &lstartup_output);
Bitmap *lbitmap;
lbitmap = new Bitmap(wszPath);
GdiplusShutdown(ltoken);
RGBQUAD *pBits = (RGBQUAD *) malloc(lbitmap->GetWidth() * lbitmap->GetHeight() * 4 * sizeof(BYTE));
lbitmapdata.Scan0 = pBits;
lbitmapdata.Width = lbitmap->GetWidth();
lbitmapdata.Height = lbitmap->GetHeight();
lbitmapdata.PixelFormat = PixelFormat32bppARGB;
lbitmapdata.Stride = lbitmap->GetWidth()*4;
Rect lrect(0,0,lbitmap->GetWidth(), lbitmap->GetHeight());
st = lbitmap->LockBits(&lrect, ImageLockModeUserInputBuf | ImageLockModeRead,
PixelFormat32bppARGB, &lbitmapdata);
ASSERT(st == Ok);
st = lbitmap->UnlockBits(&lbitmapdata); // Unlock straightaway, because we just want to extract the pixel data
ASSERT(st == Ok);
这个剪辑使用每像素4个字节,因为我使用alpha通道,但从那里你应该能够根据需要转换它。这是一个将BGRA转换为RGB的代码片段。 (您可以将UnlockBits的像素格式更改为PixelFormat24bppRGB,但您可能仍需要交换颜色 - 尝试并查看。)
BYTE *rgbBits = (malloc lbitmap->GetHeight() * lbitmap->GetWidth() * 3);
BYTE *rgbPtr = rgbBits;
pPixel = (RGBQUAD *)m_pBits;
for (li = 0; li < lbitmap->GetHeight() * lbitmap->GetWidth(); li++)
{
*(rgbPtr++) = pPixel->rgbRed;
*(rgbPtr++) = pPixel->rgbGreen;
*(rgbPtr++) = pPixel->rgbBlue;
pPixel++;
}
(请注意,我刚从项目中删除了此代码,因此可能需要进行一些清理。请告诉我们。)