2016-05-09 7 views
0

Ich versuche ein rechteckiges Prisma zu machen, aber endete mit einer Pyramide. Was ist falsch an meinem Code?Wie macht man ein rechteckiges Prisma in OpenGL?

float ax, ay, dx, dy, dz; 

float color[3] = {0.1, 0.6, 0.1 }; 

float v[8][3] = { 
    -1, 1, -1, 
    1, 1, -1, 
    1, -1, -1, 
    -1, -1, -1, 
    -1, 1, 1, 
    -1, -1, 1, 
    1, -1, 1, 
    1, 1, 1 
}; 

float norm[6][3] = { 
    0, 0, -1, 
    0, 0, 1, 
    -1, 0, 0, 
    1, 0, 0, 
    0, 1, 0, 
    0, -1, 0 
}; 

int idx[6][4] = 
{ 
0, 1, 2, 3, // Rear 

4, 5, 6, 7, // Front 

0, 3, 5, 4, // Left 

7, 6, 2, 1, // Right 

0, 4, 7, 1, // Top 

5, 3, 2, 6, // Bottom 
}; 

- (void) prepareOpenGL { 

    [super prepareOpenGL]; 

    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_COLOR_MATERIAL); 
    glShadeModel(GL_SMOOTH); 
} 


- (void) drawRect:(NSRect)rect { 
    [super drawRect:rect]; 

    glClearColor(1, 1, 1, 0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glTranslatef(dx, dy, dz); 
    glRotatef(ay, 0, 1, 0); 
    glRotatef(ax, 1, 0, 0); 

    glColor3fv(color); 
    glBegin(GL_QUADS); 

    for (int i = 0; i < 6; i++) { 
     glNormal3fv(norm[i]); 
     for (int j = 0; j < 4; j++) 
      glVertex3fv(v[idx[i][j]]); 
     } 
    glEnd(); 
    glFlush(); 
} 

My "Rectangle"

Antwort

1

Sie haben nicht die GL_PROJECTION Matrix so die Hälfte des Modells weg abgeschnitten eingerichtet. Was Sie sehen, ist keine Pyramide, sondern eine Ecke Ihres Würfels, wobei der Rest unter der fernen Ebene liegt und abgeschnitten ist.

Sehen Sie sich auch Ihr Leben zu erleichtern, indem die Kamera durch GLKMatrix4MakeLookAt positionieren, so dass Ihre Zeichnung Code würde wie folgt aussehen:

glMatrixMode(GL_PROJECTION); 
projectionMatrix = GLKMatrix4MakePerspective(...); 
glLoadMatrix(projectionMatrix); 

glMatrixMode(GL_MODELVIEW); 
viewMatrix = GLKMatrix4MakeLookAt(...); 
glLoadMatrix(viewMatrix); 

... // model transformation, vertices etc. 

Wenn Sie diese Arbeit bekommen, die nächste Aufgabe wäre es neu zu schreiben, die programmierbare Verwendung pipeline, b/c Niemand schreibt Software mit Legacy OpenGL so.

+0

Ja, viel besser. ABER! Ich fügte hinzu: 'GlLoadMatrixf (GLKMatrix4MakePerspective (45,1,1.0f, 1000.0f). M);' aber ich habe immer noch ein Problem. [Bild] (https://pp.vk.me/c633831/v633831763/291c8/CzZrNkqvZac.jpg). Brauche ich: 'glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);'? –

+0

Sie haben kein GLUT auf iOS, das ist also nicht verfügbar. Was Sie vermissen, ist wahrscheinlich glEnable (GL_DEPTH_TEST) –

+0

Kein Effekt mit glEnable (GL_DEPTH_TEST). Ich spreche über OSX nicht, iOS :) –

Verwandte Themen