2013-03-18 9 views
5

Il mio programma di seguito deve visualizzare un cubo in rotazione illuminato con una luce semplice. Il problema è che il cubo lampeggia. Quando ritiro la chiamata glEnable (GL_DEPTH_TEST) il cubo non sta lampeggiando ma posso vedere le facce al suo interno (è normale perché non c'è un test di profondità). Tuttavia questa chiamata è essenziale. Quindi non capisco perché la chiamata di questa funzione non funzioni correttamente.Visualizzazione non corretta utilizzando glEnable (GL_DEPTH_TEST)

Ecco il mio codice:

#include <iostream> 
#include <SDL/SDL.h> 
#include <gl/glut.h> 

const static int WIDTH = 640; 
const static int HEIGHT = 480; 

GLfloat angle = 0.0f; 

static GLfloat position[4] = {0.0, 50.0, -50.0, 1.0}; 
static GLfloat diffuse[3] = {0.64, 0.64, 0.64}; 
static GLfloat specular[3] = {0.64, 0.64, 0.64}; 
static GLfloat emissive[3] = {0.0, 0.0, 1.0}; 

static GLfloat vertices[72] = 
{ 
    1.000000, -1.000000, -1.000000,  //V1 
    1.000000, -1.000000, 1.000000,  //V2 
    -1.000000, -1.000000, 1.000000,  //V3 
    -1.000000, -1.000000, -1.000000, //V4 

    1.000000, 1.000000, -0.999999,  //V5 
    -1.000000, 1.000000, -1.000000,  //V8 
    -1.000000, 1.000000, 1.000000,  //V7 
    0.999999, 1.000000, 1.000001,  //V6 

    1.000000, -1.000000, -1.000000,  //V1 
    1.000000, 1.000000, -0.999999,  //V5 
    0.999999, 1.000000, 1.000001,  //V6 
    1.000000, -1.000000, 1.000000,  //V2 

    1.000000, -1.000000, 1.000000,  //V2 
    0.999999, 1.000000, 1.000001,  //V6 
    -1.000000, 1.000000, 1.000000,  //V7 
    -1.000000, -1.000000, 1.000000,  //V3 

    -1.000000, -1.000000, 1.000000,  //V3 
    -1.000000, 1.000000, 1.000000,  //V7 
    -1.000000, 1.000000, -1.000000,  //V8 
    -1.000000, -1.000000, -1.000000, //V4 

    1.000000, 1.000000, -0.999999,  //V5 
    1.000000, -1.000000, -1.000000,  //V1 
    -1.000000, -1.000000, -1.000000, //V4 
    -1.000000, 1.000000, -1.000000  //V8 
}; 

static GLfloat normals[72] = 
{ 
    0.000000, -1.000000, 0.000000, 
    0.000000, -1.000000, 0.000000, 
    0.000000, -1.000000, 0.000000, 
    0.000000, -1.000000, 0.000000, 

    0.000000, 1.000000, 0.000000, 
    0.000000, 1.000000, 0.000000, 
    0.000000, 1.000000, 0.000000, 
    0.000000, 1.000000, 0.000000, 

    1.000000, 0.000000, 0.000000, 
    1.000000, 0.000000, 0.000000, 
    1.000000, 0.000000, 0.000000, 
    1.000000, 0.000000, 0.000000, 

    -0.000000, -0.000000, 1.000000, 
    -0.000000, -0.000000, 1.000000, 
    -0.000000, -0.000000, 1.000000, 
    -0.000000, -0.000000, 1.000000, 

    -1.000000, -0.000000, -0.000000, 
    -1.000000, -0.000000, -0.000000, 
    -1.000000, -0.000000, -0.000000, 
    -1.000000, -0.000000, -0.000000, 

    0.000000, -0.000000, -1.000000, 
    0.000000, -0.000000, -1.000000, 
    0.000000, -0.000000, -1.000000, 
    0.000000, -0.000000, -1.000000 
}; 

static void  eventListener(SDL_Event *pEvent, bool *pContinue) 
{ 
    while (SDL_PollEvent(pEvent)) 
    { 
     switch(pEvent->type) 
     { 
     case SDL_QUIT: 
      *pContinue = false; 
      break; 
     case SDL_KEYDOWN: 
      switch (pEvent->key.keysym.sym) 
      { 
      case SDLK_ESCAPE: 
       *pContinue = false; 
       break; 
      } 
     } 
    } 
} 

static void  beginRender(void) 
{ 
    /*Perspective*/ 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glViewport(0, 0, WIDTH, HEIGHT); 
    gluPerspective(60.0, (float)(WIDTH/HEIGHT), 0.0f, 1000.0f); 
    gluLookAt(0.0f, 0.0, 7.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 

    /*Clear screen*/ 

    glClearDepth(1.0f); 
    glClearColor(0.13f, 0.12f, 0.13f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    /*Prepare model transformations*/ 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    /*Light settings*/ 

    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 

    /*Depth test*/ 

    glEnable(GL_DEPTH_TEST); //PROBLEM COMES FROM HERE 
} 

static void  renderFrame(void) 
{ 
    /*light position*/ 

    glLightfv(GL_LIGHT0, GL_POSITION, position); 

    /*Model transformations*/ 

    glPushMatrix(); 
    glRotatef(angle, 0.0f, 1.0f, 1.0f); 

    /*Light materials*/ 

    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); 
    glMaterialfv(GL_FRONT, GL_SPECULAR, specular); 
    glMaterialfv(GL_FRONT, GL_EMISSION, emissive); 
    glMateriali(GL_FRONT, GL_SHININESS, 10); 

    /*Model rendering*/ 

    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnableClientState(GL_NORMAL_ARRAY); 

    glVertexPointer(3, GL_FLOAT, 0, vertices); 
    glNormalPointer(GL_FLOAT, 0, normals); 

    glDrawArrays(GL_QUADS, 0, 24); 

    glDisableClientState(GL_NORMAL_ARRAY); 
    glDisableClientState(GL_VERTEX_ARRAY); 

    angle += 0.010f; 
    glPopMatrix(); 
} 

static void  endRender(void) 
{ 
    glFlush(); 
    SDL_GL_SwapBuffers(); 
} 

static void  startRendering(void) 
{ 
    bool  continuer = true; 
    SDL_Event event; 

    while (continuer) 
    { 
     eventListener(&event, &continuer); 
     beginRender(); 
     renderFrame(); 
     endRender(); 
    } 
} 

int    main(int ac, char **av) 
{ 
    SDL_Init(SDL_INIT_VIDEO); 
    SDL_WM_SetCaption("Test luminosity",NULL); 
    SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL | SDL_DOUBLEBUF); 

    startRendering(); 
    SDL_Quit(); 
    return (0); 
} 

Ecco lo schermo con glEnable (GL_DEPTH_TEST) (facce lampeggiano)

enter image description here

E senza questa chiamata (nessuna visualizzazione lampeggiante ma non prova profondità)

enter image description here

Ho provato diverse combinazioni di codice senza successo.

Qualcuno può aiutarmi?

Grazie in anticipo per il vostro aiuto.

risposta

8

Il piano di ritaglio vicino non è impostato correttamente, quindi il buffer Z non funziona.

gluPerspective(60.0, (float)(WIDTH/HEIGHT), 0.0f, 1000.0f); 
              ^^^^ 

Posizionarlo a una distanza ragionevole, il più lontano possibile.

Inoltre, non mettono l'orientamento della telecamera (cioè la vista trasformare) nella matrice di proiezione ...

Infine, si noti che la maggior parte di questa funzionalità è obsoleto e non più disponibile in versioni più recenti di GL . L'approccio consigliato consiste nel gestire i propri calcoli a matrice e utilizzare la pipeline programmabile per il rendering della geometria.

+0

Ciao, grazie mille per la risposta. A proposito di gestione di matrici e geometrie, userò la mia matrice di gestione e gli shader come hai detto tu. È il mio obiettivo principale Grazie di tutto. Ciao. – user1364743

Problemi correlati