OpenGL текстурное отображение пустого экрана - PullRequest
0 голосов
/ 09 февраля 2012

Итак, я пробежался по учебнику по наложению текстур в OpenGL, и я использую функцию из учебника, чтобы попытаться отобразить текстуру в моей собственной программе.

Я думаю, что, должно быть, я пропускаю некоторые необходимые звонки или что-то ужасно неправильно, но в данный момент мне удается добиться эффекта черного экрана, как обычно.

I 'Я пробежался по этому коду, и, насколько мне известно, я не вижу причин, по которым у меня должен быть пустой экран, и я надеялся, что кто-нибудь здесь сможет обнаружить, где я ошибаюсь

Я использую 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 ]

2 голосов
/ 09 февраля 2012

Что ж, одна проблема заключается в том, что вы не сбрасываете матрицу вида модели каждый раз, поэтому ваш квадрат быстро переводится из поля зрения (при условии, что он имел в виду, с которого я начал, что я не проверял). *

0 голосов
/ 09 февраля 2012

Сделайте снимок (текстурирование удалено для краткости и отсутствия ссылочного файла):

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