【问题标题】:OpenGL texture mapping blank screenOpenGL纹理映射黑屏
【发布时间】:2012-02-09 20:09:11
【问题描述】:

所以我一直在学习 OpenGL 中的纹理映射教程,我正在使用教程中的函数尝试在我自己的程序中映射纹理。

我想我一定是错过了一些必要的调用,或者在某个地方出现了可怕的错误,但目前,我正设法像往常一样实现黑屏效果。

我已经浏览了这段代码,据我所知,我认为我没有理由得到一个空白屏幕,并希望这里的任何人都能发现我哪里出错了

我使用 SDL 只是为了澄清我认为可能与问题相关的内容:

#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>

void Draw()

{

glTranslatef(320,240,0);
glBegin(GL_QUADS);
glColor3f(1,0,0);

    glTexCoord2f(0,0); glVertex2f(0,0);
    glTexCoord2f(100,0); glVertex2f(100,0);
    glTexCoord2f(100,100); glVertex2f(100,100);
    glTexCoord2f(0,100); glVertex2f(0,100);

glEnd();

glLoadIdentity();

}

void Init(void)

{

SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption("Texture Test", NULL);

}

void Set_States(void)

{

glClearColor(0,0,0,0);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

GLuint LoadTextureRAW( const char * filename, int wrap )

{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

// allocate buffer
width = 256;
height = 256;
data = (BYTE*)malloc( width * height * 3 );

// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );

// allocate a texture name
glGenTextures( 1, &texture );

// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                 GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
//       ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                 wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                 wrap ? GL_REPEAT : GL_CLAMP );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                   GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );

return texture;

}

int main (int argc, char ** argv)

{

GLuint texture;
SDL_Event event;
Init();
Set_States();
bool running = true;
glEnable( GL_TEXTURE_2D );
texture = LoadTextureRAW("texture.raw", 1);
glBindTexture( GL_TEXTURE_2D, texture );

while (running == true)

{

    while (SDL_PollEvent(&event))

    {

        switch (event.type)

        {

            case SDL_QUIT: running = false; break;

        }

        glClear(GL_COLOR_BUFFER_BIT);
        Draw();
        SDL_GL_SwapBuffers();

    }

}

SDL_Quit();
glDeleteTextures( 1, &texture );
return 0;

}

【问题讨论】:

    标签: c++ opengl sdl texture-mapping


    【解决方案1】:

    一个问题是您不是每次都重置模型视图矩阵,因此您的四边形很快就会被移出视图(假设它在一开始就在视图中,我还没有检查过)。

    【讨论】:

    • 解决了正方形不出现的问题。我现在有一个 100x100 像素的红色填充正方形,没有纹理
    【解决方案2】:

    试一试(为了简洁和缺少参考文件,纹理被删除):

    #include <SDL/SDL.h>
    #include <SDL/SDL_opengl.h>
    
    void Draw()
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 0, 480, -1, 1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glPushMatrix();
        glTranslatef(320,240,0);
        glScalef( 100, 100, 0 );
        glBegin(GL_QUADS);
        glColor3f(1,0,0);
        glTexCoord2f(0,0); glVertex2f(0,0);
        glTexCoord2f(1,0); glVertex2f(1,0);
        glTexCoord2f(1,1); glVertex2f(1,1);
        glTexCoord2f(0,1); glVertex2f(0,1);
        glEnd();
        glPopMatrix();
    }
    
    int main (int argc, char ** argv)
    {
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_SetVideoMode(640,480,32,SDL_OPENGL);
        SDL_WM_SetCaption("Texture Test", NULL);
    
        glClearColor(0,0,0,0);
        glViewport(0, 0, 640, 480);
    
        bool running = true;
        while (running == true)
        {
            SDL_Event event;
            while (SDL_PollEvent(&event))
            {
                switch (event.type)
                {
                    case SDL_QUIT: running = false; break;
                }
            }
    
            Draw();
            SDL_GL_SwapBuffers();
        }
    
        SDL_Quit();
        return 0;
    }
    

    【讨论】:

    • glOrthoglTranslate 是怎么回事?只需绘制世界坐标进行第一次测试。 glPushMatrixglPopMatrix 也是没用的,因为在恢复原始矩阵后什么都不做。
    • 两个答案都解决了这个问题,我现在有一个没有纹理的 100x100 红色填充正方形。我已经编辑了代码以显示更改。
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2012-07-28
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多