OpenGL:鱼眼效应以及如何避免这种情况

时间:2015-05-07 14:17:03

标签: opengl

我正在尝试模拟太阳系,我正在经历鱼眼效应。

从图中可以看出,太阳位于中心,地球被转换为x轴20个单位,它看起来不再是一个球体。

enter image description here

glm::lookAt函数中,我设置视野(这是FoVx?,我从wiki读取,这是水平视野,所以我认为这是FoVx。)是80度,宽高比是5.4f / 3.0f(大多数应用程序将宽高比设置为4/3,但如果我使用此比率,这些行星看起来就像一个椭圆!)。

那我怎么解决这个问题呢?由于地球将围绕太阳旋转,并且它应始终是任何角度和视距的球体。也许我还没有完全理解视角,视图矩阵等。真的需要一些帮助。

以下是我用来渲染太阳和地球的代码:

void renderSun(int i){
    glPushMatrix();
    glLoadIdentity();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_ID[0]);
    glUniform1i(texture_Location, 0);
    glm::mat4 Projection = glm::perspective(90.0f, 5.4f / 3.0f, 1.0f, 100.0f);
    glm::mat4 View = glm::lookAt(
        glm::vec3(0, 30, 1),
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0)
    );

    /* Animations */
    GLfloat angle = (GLfloat) (i);
    //View = glm::translate(View, glm::vec3(-2.0f, 0.0f, 0.0f));
    View = glm::rotate(View, angle * 0.5f, glm::vec3(0.0f, 0.0f, 1.0f));
    /* ******* */
    glm::mat4 Model = glm::mat4(1.0f);
    glm::mat4 MVP = Projection * View * Model;

    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "mvpMatrix"), 1, GL_FALSE, glm::value_ptr(MVP));
    glDrawElements(GL_TRIANGLES, numsToDraw, GL_UNSIGNED_INT, NULL);
    glPopMatrix();
}


void renderEarth(int i){
    glPushMatrix();
    glLoadIdentity();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture_ID[3]);
    glUniform1i(texture_Location, 0);
    //glm::mat4 Projection = glm::ortho(0.f, 800.f, 0.f, 400.f, -5.f, 5.f );
    glm::mat4 Projection = glm::perspective(80.0f, 5.4f / 3.0f, 1.0f, 100.0f);
    glm::mat4 View = glm::lookAt(
        glm::vec3(0, 30, 2),
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0)
    );

    /* Animations */
    GLfloat angle = (GLfloat) (i);
    View = glm::translate(View, glm::vec3(30.0f, 0.0f, 0.0f));
    //View = glm::scale(View, glm::vec3(4.0f, 5.0f, 4.0f));
    View = glm::rotate(View, angle * 0.5f, glm::vec3(0.0f, 0.0f, 1.0f));
    /* ******* */
    glm::mat4 Model = glm::mat4(1.0f);
    glm::mat4 MVP = Projection * View * Model;

    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "mvpMatrix"), 1, GL_FALSE, glm::value_ptr(MVP));
    glDrawElements(GL_TRIANGLES, numsToDraw, GL_UNSIGNED_INT, NULL);
    glPopMatrix();
}

0 个答案:

没有答案