1

我正在使用 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();
}
4

1 回答 1

2

这看起来像是首先读取文件的问题。BMP 文件不是简单整齐的像素字节数组,因此您不能只读取宽度*高度*3 字节。您将在其中看到图像数据(假设 24 bpp 存储),但还有标题信息会将像素数据推送到显示器上。我很惊讶第二个位图看起来不错,尽管这可能是标题与图像宽度匹配的巧合。

更好的解决方案是使用 Gdi 加载图像...

GdiplusStartupInput lstartup_input;
GdiplusStartupOutput lstartup_output;
ULONG_PTR   ltoken;

GdiplusStartup(&ltoken, &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++;
}

(请注意,我刚刚将此代码从我的项目中提取出来,因此可能需要进行一些清理。请告诉我。)

于 2013-06-21T04:15:47.163 回答