计算顶点着色器opengl中的MVP矩阵

时间:2015-05-10 12:02:43

标签: c++ opengl

我在主代码中计算了MVP矩阵,然后我将计算移动到顶点着色器中。

最后它不再起作用了。为什么?计算没有什么不同,除了我放置它们的位置。

有人可以解释一下吗?我认为在着色器中计算MVP矩阵是在openGL版本中做事的正确方法> 3.0对吗?但我无法让它发挥作用。当我将新代码应用到它时,太阳消失了。

旧计算:

void renderSun(int i){
    glPushMatrix();
    glLoadIdentity();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, sun_TextureID);
    glUniform1i(sun_TextureLocation, 0);
    glm::mat4 Projection = glm::ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height), 1000.0f, -1000.0f);
    glm::mat4 View = glm::lookAt(
        glm::vec3(0, 120, 1),
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0)
    );
    /* Animations */
    GLfloat angle = (GLfloat) (i);
    glm::mat4 Model = glm::mat4(1.0f);
    Model = glm::translate(Model, glm::vec3(CENTRE_X, 0.0f, CENTRE_Z));
    Model = glm::scale(Model, SUN_SIZE);
    Model = glm::rotate(Model, angle * 0.5f, glm::vec3(0.0f, 0.0f, 1.0f));
    /* ******* */

    glm::mat4 MVP = Projection * View * Model;
    glUniformMatrix4fv(glGetUniformLocation(shaderProgramSun, "mvpMatrix"), 1, GL_FALSE, glm::value_ptr(MVP));
    glDrawElements(GL_TRIANGLES, numsToDraw, GL_UNSIGNED_INT, NULL);
    glPopMatrix();
}

新计算:

void renderSun(int i){
    glPushMatrix();
    glLoadIdentity();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, sun_TextureID);
    glUniform1i(sun_TextureLocation, 0);
    glm::mat4 Projection = glm::ortho(0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height), 1000.0f, -1000.0f);
    glm::mat4 View = glm::lookAt(
        glm::vec3(0, 120, 1),
        glm::vec3(0, 0, 0),
        glm::vec3(0, 1, 0)
    );
    /* Animations */
    GLfloat angle = (GLfloat) (i);
    glm::mat4 Model = glm::mat4(1.0f);
    Model = glm::translate(Model, glm::vec3(CENTRE_X, 0.0f, CENTRE_Z));
    Model = glm::scale(Model, SUN_SIZE);
    Model = glm::rotate(Model, angle * 0.5f, glm::vec3(0.0f, 0.0f, 1.0f));

    GLint modelLocation = glGetUniformLocation(shaderProgramSun, "model");
    GLint viewLocation = glGetUniformLocation(shaderProgramSun, "view");
    GLint projLocation = glGetUniformLocation(shaderProgramSun, "projection");

    glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(View));
    glUniformMatrix4fv(projLocation, 1, GL_FALSE, glm::value_ptr(Projection));
    glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(Model));
    /* ******* */

    glDrawElements(GL_TRIANGLES, numsToDraw, GL_UNSIGNED_INT, NULL);
    glPopMatrix();
}

我的顶点着色器:

#version 330 core
precision highp float;
layout (location = 0) in vec3 sun_Position;
varying vec4 Sun_Texture_Coord;

uniform mat4 mvpMatrix;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

void main(void){
    gl_Position = projection * view * model * vec4(sun_Position, 1.0f);
    //gl_Position = mvpMatrix * vec4(sun_Position, 1.0);
    Sun_Texture_Coord = vec4(sun_Position, 1.0);
}

0 个答案:

没有答案