如何在openGL中沿着Z轴绘制直线

ccrfmcuu  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(190)

我在Z轴上画了一条线。我已经检查了相关的主题OpenGL Can't draw z axis,但是即使我改变了相机的位置,我仍然看不到我的线;同时,我可以在XZ平面上看到一个正方形图形。
下面是我代码:

int main()
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

    GLFWwindow* window = glfwCreateWindow(1024, 768, "some program", NULL, NULL);

    glfwMakeContextCurrent(window);

    glewExperimental = true; // Needed for core profile
    if (glewInit() != GLEW_OK) {
        std::cerr << "Failed to create GLEW" << std::endl;
        glfwTerminate();
        return -1;
    }

    glClearColor(0.2f, 0.298f, 0.298f, 1.0f);

    // Compile and link shaders here ...
    int shaderProgram = compileAndLinkShaders();

    int lineVao = createLineVertexArrayObject(LineArray, sizeof(LineArray));

    while (!glfwWindowShouldClose(window))
    {
        GLuint worldMatrixLocation = glGetUniformLocation(shaderProgram, "worldMatrix");
        // Each frame, reset color of each pixel to glClearColor
        glClear(GL_COLOR_BUFFER_BIT);
        glm::mat4 translationMatrix = glm::mat4(1.0f);
        glUniformMatrix4fv(worldMatrixLocation, 1, GL_FALSE, &translationMatrix[0][0]);

        glUseProgram(shaderProgram);

        glBindVertexArray(lineVao);

        glDrawArrays(GL_LINES, 0, 2);

        glBindVertexArray(0);

        glfwSwapBuffers(window);

        // Detect inputs
        glfwPollEvents();

        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);

        if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)//see the object in other direction
        {
            glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0.0f, 1.0f, 0.0f),  // eye
                glm::vec3(0.0f, 0.0f, -1.0f),  // center
                glm::vec3(0.0f, 1.0f, 0.0f));// up

            GLuint viewMatrixLocation = glGetUniformLocation(shaderProgram, "viewMatrix");
            glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]);
        }
    }
    // Shutdown GLFW
    glfwTerminate();
    return 0;
}

创建一个数组对象

int createLineVertexArrayObject(const glm::vec3* vertexArray, int arraySize)
{
    // Create a vertex array
    GLuint vertexArrayObject;
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    // Upload Vertex Buffer to the GPU, keep a reference to it (vertexBufferObject)
    GLuint vertexBufferObject;
    glGenBuffers(1, &vertexBufferObject);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, arraySize, vertexArray, GL_STATIC_DRAW);

    glVertexAttribPointer(0,                   // attribute 0 matches aPos in Vertex Shader
        2,                   // size
        GL_FLOAT,            // type
        GL_FALSE,            // normalized?
        2 * sizeof(glm::vec3), // stride - each vertex contain 2 vec3 (position, color)
        (void*)0             // array buffer offset
    );
    glEnableVertexAttribArray(0);

    glVertexAttribPointer(1,                            // attribute 1 matches aColor in Vertex Shader
        2,
        GL_FLOAT,
        GL_FALSE,
        2 * sizeof(glm::vec3),
        (void*)sizeof(glm::vec3)      // color is offseted a vec3 (comes after position)
    );
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    return vertexArrayObject;
}

我的lineArray[]是这样的

glm::vec3 LineArray[] = {
        glm::vec3(-0.5f, 0.0f, -0.5f), // position
        glm::vec3(1.0f, 0.0f, 0.0f), // color (yellow)
        glm::vec3(0.0f, 0.0f, 0.0f), // position
        glm::vec3(1.0f, 0.0f, 0.0f), // color (yellow)
};

但是如果我改变相机的位置,我只能看到一条水平线。

pgx2nnw8

pgx2nnw81#

假设你想画一条3D线,那么glVertexAttribPointer的size参数是错误的。把它设置为2,你告诉OpenGL只读取两个值,并为z加0。
将大小设置为3,如

glVertexAttribPointer(0,                   // attribute 0 matches aPos in Vertex Shader
    3,                   // size
    GL_FLOAT,            // type
    GL_FALSE,            // normalized?
    2 * sizeof(glm::vec3), // stride - each vertex contain 2 vec3 (position, color)
    (void*)0             // array buffer offset
);

并且还将读取z值。
请注意,您的彩色绑定也有同样的问题。您只读取了读取值和绿色值,而不是蓝色值。

相关问题