OpenGL - 错误的对象定位 - gluLookAt()

时间:2016-12-11 20:30:27

标签: opengl glulookat

我从大学获得了一个大学,必须做一个太阳系的小例子,对象必须旋转等问题是,当我不打电话给GluLookAt()时,一切看起来都不错,但我会喜欢改变视图,当我调用该函数时,会发生一个轨道完全奇怪地渲染。 我不知道问题是第一个轨道的错误创建,还是gluLookAt参数中的正确值。有人可以帮忙吗?

以下是调用gluLookAt()时的外观:

以下是关于gluLookAt()的看法:

以下是代码:

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

1 个答案:

答案 0 :(得分:0)

您的某些对象具有不同的z值,例如第一个轨道在-5.5,第二个在0,因为你“弹出”矩阵。

一般来说,不要这么多push \ pops互相嵌套,矩阵堆栈不是橡胶制成的。

有效的圆绘制程序比计算每个步骤的正弦和余弦更有效,例如:获得圆圈成为轮换的优势:

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

它可以通过使用对称性进一步优化,但这是基础。