无法透视我的图像

时间:2018-11-10 02:37:58

标签: opengl

所以我正在关注相机的LearnOpenGL教程。我正在尝试渲染一个多维数据集(定义为verts的向量),并在该多维数据集周围绕圈以确保其正确渲染。我可以很好地渲染立方体的正面,但是一旦我将着色器切换为使用MVP,就不会渲染

void GraphicsProgram::run_program() 
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    state = RUNNING;

    // TODO: Move these into their respective classes
    // Hardcoded for some testing
    // ----------------------------------------------
    const char *vertexShaderPath = "vertex_shader.vs";
    const char *fragmentShaderPath = "fragment_shader.fs";
    Shader firstShader = Shader(vertexShaderPath, fragmentShaderPath);

    std::vector<Vertex> verts;
    Vertex v1, v2, v3, v4, v5, v6, v7, v8;
    v1.position = { -0.5, -0.5, +0.5 };
    v2.position = { +0.5, -0.5, +0.5 };
    v3.position = { +0.5, +0.5, +0.5 };
    v4.position = { -0.5, +0.5, +0.5 };
    v5.position = { +0.5, -0.5, -0.5 };
    v6.position = { +0.5, +0.5, -0.5 };
    v7.position = { -0.5, +0.5, -0.5 };
    v8.position = { -0.5, -0.5, -0.5 };

    verts.push_back(v1);
    verts.push_back(v2);
    verts.push_back(v3);
    verts.push_back(v4);
    verts.push_back(v5);
    verts.push_back(v6);
    verts.push_back(v7);
    verts.push_back(v8);

    std::vector<unsigned int> indicies = { 
        0, 1, 2, 0, 2, 3, // front
        1, 4, 5, 1, 5, 2, // right
        4, 7, 6, 4, 6, 7, // back
        7, 0, 3, 7, 3, 6, // left
        3, 2, 5, 3, 5, 6, // top
        7, 4, 1, 7, 1, 0  // bottom
    };


    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex), &verts[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size() * sizeof(unsigned int), &indicies[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);


    glUseProgram(firstShader.shaderID);

    glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
    firstShader.set_mat4("projection", projection);

    while (state == RUNNING) {
        currentFrame = (float)SDL_GetTicks() / 1000;
        deltaTime = lastFrame - currentFrame;
        lastFrame = currentFrame;

        process_input();

        glUseProgram(firstShader.shaderID);

        glm::mat4 view;
        float radius = 10.0f;
        float camX = sin((float)SDL_GetTicks() / 1000) * radius;
        float camZ = cos((float)SDL_GetTicks() / 1000) * radius;
        view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
        firstShader.set_mat4("view", view);

        glBindVertexArray(VAO);

        glm::mat4 model;
        model = glm::translate(model, glm::vec3(0.0, 0.0, 0.0));
        float angle = 0.0;
        model = glm::rotate(model, glm::radians(angle), glm::vec3(0,0,0));
        firstShader.set_mat4("model", model);

        glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0);
        SDL_GL_SwapWindow(window);
    }

    shutdown_program();
}

顶点结构:

struct Vertex {
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 texCoord;
};

顶点着色器代码:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

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

void main() {
    gl_Position = projection * view * model * vec4(aPos, 1.0f);
    vertexColor = vec4(abs(aPos.x), abs(aPos.y), abs(aPos.z), 1.0);
}

片段着色器:

#version 330 core

in vec4 vertexColor;
out vec4 fragColor;

void main() {
    fragColor = vertexColor;
}

我最初使用的顶点着色器只是处于顶点位置。

1 个答案:

答案 0 :(得分:1)

您必须初始化矩阵变量glm::mat4 modelglm::mat4 view

glm API documentation是指The OpenGL Shading Language specification 4.20

  

5.4.2向量和矩阵构造器

     

如果向量构造函数只有一个标量参数,它将用于将构造的向量的所有分量初始化为该标量的值。 如果矩阵构造函数只有一个标量参数,则它将用于初始化矩阵对角线上的所有分量,并将其余分量初始化为0.0。

这意味着,可以通过单个参数1.0来初始化一个单位矩阵:

glm::mat4 model(1.0f);

....

glm::mat4 view(1.0f);

您错过了在glm::rotate中设置适当的旋转轴的操作:

model = glm::rotate(model, glm::radians(angle), 
    glm::vec3(1.0,0,0)); // e.g. (1, 0, 0) instead of (0, 0, 0)

您必须在每帧(glClear)之前清除默认的Farmebuffer:

glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)
{
    glClear(GL_COLOR_BUFFER_BIT);

    .....

您应该启用Depth Test并清除深度缓冲区:

glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);

while (state == RUNNING)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    .....