简体   繁体   中英

OpenGL texture mapping blank screen

So I've been running through a tutorial on texture mapping in OpenGL and I'm using the function from the tutorial to attempt to map a texture in my own program.

I think I must be missing some necessary calls to something or going horribly wrong somewhere, but at the moment, I'm managing to achieve the black screen effect as per usual.

I've ran through this code and to my knowledge I see no reason why i should be getting a blank screen and was hoping anybody around here could spot where I'm going wrong

I'm using SDL just to clarify which I think might have some relevance to the problem maybe:

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

}

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

Give this a shot (texturing removed for brevity and lack of referenced file):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM