OpenGL纹理映射空白屏幕

时间:2012-02-09 02:50:47

标签: c++ opengl sdl texture-mapping

所以我一直在运行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;

}

2 个答案:

答案 0 :(得分:2)

一个问题是你每次都没有重置你的模型视图矩阵,所以你的四边形很快被翻译出视图(假设它在视图中开始,我还没有检查过)。

答案 1 :(得分:0)

给它一个镜头(为了简洁而缺少参考文件,删除了纹理):

#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;
}