2016-12-11 3 views
1

Ich habe eine Taks von der Universität und muss ein kleines Beispiel für Solarsystem machen, die Objekte müssen sich drehen usw. Das Problem ist, dass wenn ich GluLookAt() nicht aufrufen alles sieht gut aus, aber ich würde gerne die Ansicht wechseln und wenn ich die Funktion aufrufen, kommt es vor, dass eine Umlaufbahn völlig seltsam rendert. Ich weiß nicht, ob das Problem mit der falschen Erzeugung der ersten Umlaufbahn oder mit den richtigen Werten in den gluLookAt-Parametern ist. Kann jemand helfen?OpenGL - falsche Objekte Positionierung - gluLookAt()

Hier ist, wie es aussieht, ohne gluLookAt() aufrufen:

Hier ist, wie es aussieht, nachdem gluLookAt():

Hier ist der Code ist:

#include "stdafx.h" 
#include <GL\glut.h> 
#include <math.h> 

GLfloat yRotated=1; 
GLfloat movement = 0; 


void drawCircle(float r) { // radius 

    glBegin(GL_LINE_LOOP); 
    for (int i = 0; i <= 300; i++) { 
     double angle = 2 * 3.14 * i/300; 
     double x = r*cos(angle); 
     double y = r*sin(angle); 
     glVertex3d(x, y, -5.5); 
    } 
    glEnd(); 

} 



void display(void) { 
    glMatrixMode(GL_MODELVIEW); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glLoadIdentity(); 
    //gluLookAt(5, 5, 5, 0, 0, -8, 0, 1, 0); // 3rd coordinate - depth 

    float radius1 = 6; 
    float radius2 = 1; 

    //first orbit 
    glColor3f(1, 1, 1); 
    glPushMatrix(); 
    glTranslatef(0, 0, -5.5); 
    drawCircle(radius1); 
    glPopMatrix(); 


    //second orbit with rotation 
    glPushMatrix(); 
    glRotatef(yRotated, 0, 0, 1); 
    glPushMatrix(); 
    glTranslatef(radius1/2, 0, 0); 
    drawCircle(radius2); 
    glPopMatrix(); 
    glPopMatrix(); 


    //first czajnik 
    glColor3f(0.8, 0.2, 0.1); 
    glPushMatrix(); 
    glTranslatef(0.0, 0.0, -5.5); 
// glScalef(1.0, 1.0, 1.0); 
    glRotatef(yRotated, 0, 0, 1); 
    glRotatef(90, 1, 0, 0); 
    glutSolidSphere(1,20,20); 


    //second czajnik 
    glPushMatrix(); 
    glColor3f(0, 0, 1); 
    glTranslatef(radius1/2, 0, 0); 
    glRotatef(yRotated, 0, 1, 0); 
    glutSolidSphere(0.5, 20, 20); 

    //third czajnik 
    glPushMatrix(); 
    glTranslatef(radius2, 0, 0); 
    glColor3f(1, 1, 0); 
    glRotatef(yRotated, 0, 1, 0); 
    glutSolidSphere(0.2, 20, 20); 
    glPopMatrix(); 


    //second czajnik pop 
    glPopMatrix(); 


    //first czajnik pop 
    glPopMatrix(); 





    glFlush(); 
} 

void idle() { 

     yRotated += 0.1; 
     Sleep(2); 
     display(); 

} 

void myReshape(int w, int h) { 

    if (w == 0 || h == 0) return; 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(70.0, (GLdouble)w/(GLdouble)h, 0.5, 20.0); 

    glViewport(0, 0, w, h); 
} 



int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(900, 600); 
    glutCreateWindow("Solar system"); 
    //window with a title 
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
    glClearColor(0, 0, 0, 1.0); 
    glutDisplayFunc(display); 
    glutReshapeFunc(myReshape); 
    glutIdleFunc(idle); 
    glutMainLoop(); 
    return 0; 
} 

Antwort

0

Einige Ihrer Objekte haben unterschiedliche z-Werte, z. 1. Umlaufbahn bei -5.5, Sekunde bei 0, weil du die Matrix "aufgeplatzt" hast.

Im Allgemeinen nicht so viele Push \ Pops ineinander verschachtelt, Matrix-Stack besteht nicht aus Gummi.

Es gibt eine effizientere Zirkelzeichnungsprozedur als die Berechnung von Sinus und Kosinus für jeden Schritt, z. zu bekommen Vorteil des Kreises eine Rotationsfigur zu sein:

inline void circle(F32 r, U32 quality) 
{ 
    if (r < F_ALMOST_ZERO) return; 

    F32 th = M_PI /(quality-1); 
    F32 s = sinf(th); 
    F32 c = cosf(th); 
    F32 t; 

    F32 x = r; 
    F32 y = 0; 

    ::glBegin (GL_LINE_LOOP); 
    for(U32 i = 0; i < quality; i++) 
    { 
     glVertex2f(x, y); 
     t = x; 
     x = c*x + s*y; 
     y = -s*t + c*y; 
    } 
    ::glEnd(); 
} 

kann sie weiter durch die Verwendung Symmetrie optimiert werden, aber dies ist die Basis.

+0

Thanks :) Ich habe die Übersetzung und zog eine Umlaufbahn mit dem Radius radius1/2 geändert. Jetzt ist es perfekt. // erste Umlaufbahn \t glColor3f (1, 1, 1); \t glPushMatrix(); \t glTranslatef (0, 0, 0); \t DrawCircle (Radius1/2); –

Verwandte Themen