2016-12-11 6 views
1

J'ai une leçon de l'université et je dois faire un petit exemple de système solaire, les objets doivent tourner etc. Le problème est que lorsque je n'appelle pas GluLookAt() tout a l'air bien, mais je voudrais changer la vue et quand j'appelle la fonction, il se produit qu'une orbite rend complètement étrangement. Je ne sais pas si le problème est dû à une mauvaise création de la première orbite, ou aux bonnes valeurs dans les paramètres de gluLookAt. Quelqu'un peut-il aider?OpenGL - mauvais positionnement des objets - gluLookAt()

Voilà comment il semble sans appeler gluLookAt():

Voilà comment il ressemble après gluLookAt():

Voici le code:

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

Répondre

0

Certains de vos objets sont à des valeurs z différentes, par ex. 1ère orbite à -5.5, seconde à 0, parce que vous avez "sauté" la matrice.

En général, ne faites pas tant de push \ pops imbriqués les uns dans les autres, la pile de matrice n'est pas faite de caoutchouc.

Il existe une procédure de tracé de cercle plus efficace que pour calculer le sinus et le cosinus pour chaque étape, par ex. pour obtenir l'avantage de cercle étant un chiffre de rotation:

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(); 
} 

il peut être optimisé davantage en utilisant la symétrie, mais celui-ci est à la base.

+0

Merci :) J'ai changé la traduction et dessiné une orbite de rayon rayon1/2. Maintenant c'est parfait. // première orbite \t glColor3f (1, 1, 1); \t glPushMatrix(); \t glTranslatef (0, 0, 0); \t drawCercle (rayon1/2); –