2011-11-23 36 views
10

Estoy tratando de establecer una imagen de fondo de mi ventana openGL creando un quad en una matriz de proyección ortogonal y agregando textura a la misma. También estoy haciendo uso de herramientas GLUT en mi aplicación.Establecer imagen de fondo de una ventana OpenGL

Sin embargo, estoy teniendo varios problemas. A continuación, se muestra una captura de pantalla que ilustra el problema: la imagen es en escala de grises y se repite en xey, aunque el tamaño de la textura se establece de la misma manera que el cuadrángulo.

Eso es lo que estoy tratando de lograr: Desired result

Y eso es lo que estoy haciendo: Actual result

Código para la representación y la textura de carga se ve así:

void orthogonalStart (void) { 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    gluOrtho2D(0, w1, 0, h1); 
    glScalef(1, -1, 1); 
    glTranslatef(0, -h1, 0); 
    glMatrixMode(GL_MODELVIEW); 
} 

void orthogonalEnd (void) { 
    glMatrixMode(GL_PROJECTION); 
    glPopMatrix(); 
    glMatrixMode(GL_MODELVIEW); 
} 

GLuint LoadTexture(const char * filename, int width, int height) 
{ 
    GLuint texture; 
    unsigned char * data; 
    FILE * file; 

    //The following code will read in our RAW file 
    file = fopen(filename, "rb"); 
    if (file == NULL) return 0; 
    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); 


    //even better quality, but this will do for now. 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); 


    //to the edge of our shape. 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 

    //Generate the texture 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, data); 
    free(data); //free the texture 
    return texture; //return whether it was successful 
} 

void FreeTexture(GLuint texture) 
{ 
    glDeleteTextures(1, &texture); 
} 

void background (void) { 
    glBindTexture(GL_TEXTURE_2D, texture); 

    orthogonalStart(); 

    glBegin(GL_QUADS); 
     glTexCoord2d(0.0,0.0); glVertex2f(0, 0); 
     glTexCoord2d(1.0,0.0); glVertex2f(1024, 0); 
     glTexCoord2d(1.0,1.0); glVertex2f(1024, 1024); 
     glTexCoord2d(0.0,1.0); glVertex2f(0, 1024); 
     glEnd(); 

    orthogonalEnd(); 
} 

void display (void) { 
    glClearColor (1.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glEnable(GL_TEXTURE_2D); 

    background(); 
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

    glutSwapBuffers(); 
} 

Para convertir la textura de PNG a formato RAW utilizo photoshop, por lo que pensé que la imagen en escala de grises podría deberse a la conversión.

Nota: Acabo de empezar a aprender abierto, por lo que este podría no ser el mejor enfoque. Por favor, hágamelo saber si hay una manera mejor

Editar: La imagen tiene que permanecer en un lugar y no rescalse

+0

Nota al margen: Creo que quería decir glTexCoord2f (la "d" significa "doble", no "dimensiones"). –

+0

@MarceloCantos Correcto, pero probablemente no debería marcar la diferencia, ¿verdad? – Vitalij

+0

No, no espero que esto afecte el resultado. –

Respuesta

12

me había avoid doing anything other than a glFrustum() or glOrtho() call para su matriz de proyección.

Además, establecer GL_UNPACK_ALIGNMENT a 1 para la textura de su carga, si está haciendo píxeles de 3 componentes:

#include <GL/glut.h> 

int w1 = 0; 
int h1 = 0; 
void reshape(int w, int h) 
{ 
    w1 = w; 
    h1 = h; 
    glViewport(0, 0, w, h); 
} 


void orthogonalStart() 
{ 
    glMatrixMode(GL_PROJECTION); 
    glPushMatrix(); 
    glLoadIdentity(); 
    gluOrtho2D(-w1/2, w1/2, -h1/2, h1/2); 
    glMatrixMode(GL_MODELVIEW); 
} 

void orthogonalEnd() 
{ 
    glMatrixMode(GL_PROJECTION); 
    glPopMatrix(); 
    glMatrixMode(GL_MODELVIEW); 
} 


GLuint texture = 0; 
void background() 
{ 
    glBindTexture(GL_TEXTURE_2D, texture); 

    orthogonalStart(); 

    // texture width/height 
    const int iw = 500; 
    const int ih = 500; 

    glPushMatrix(); 
    glTranslatef(-iw/2, -ih/2, 0); 
    glBegin(GL_QUADS); 
     glTexCoord2i(0,0); glVertex2i(0, 0); 
     glTexCoord2i(1,0); glVertex2i(iw, 0); 
     glTexCoord2i(1,1); glVertex2i(iw, ih); 
     glTexCoord2i(0,1); glVertex2i(0, ih); 
    glEnd(); 
    glPopMatrix(); 

    orthogonalEnd(); 
} 


void display() 
{ 
    glClearColor (1.0,0.0,0.0,1.0); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glEnable(GL_TEXTURE_2D); 

    background(); 
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

    glutSwapBuffers(); 
} 


GLuint LoadTexture() 
{ 
    unsigned char data[] = { 255,0,0, 0,255,0, 0,0,255, 255,255,255 }; 

    glGenTextures(1, &texture); 
    glBindTexture(GL_TEXTURE_2D, texture); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

    //even better quality, but this will do for now. 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); 

    //to the edge of our shape. 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 

    //Generate the texture 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,GL_RGB, GL_UNSIGNED_BYTE, data); 
    return texture; //return whether it was successful 
} 


int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 

    glutInitWindowSize(800,600); 
    glutCreateWindow("Aspect Ratio"); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    texture = LoadTexture(); 
    glutMainLoop(); 
    return 0; 
} 
Cuestiones relacionadas