OpenGL程序不会显示任何对象?

时间:2018-11-13 23:26:54

标签: c++ opengl glfw glm-math

OpenGL项目不再显示任何对象。我试图从头开始重新制作所有内容,但仍然无法正常工作。

主要代码

#include <vector>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw_gl3.h>
#include "Loader.h"

void on_error(int error, const char* description)
{
    std::cout << "GLFW error " << error << " : \"" << description << "\"" << std::endl;
}

int main()
{
    //Init glfw
    glfwSetErrorCallback(on_error);
    if (!glfwInit()) return  -1;
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //Init window
    auto window = glfwCreateWindow(1920, 1080, "gl_Crane", NULL, NULL);
    if (!window) { glfwTerminate(); return -1; }
    glfwMakeContextCurrent(window);

    //Init glew
    glewExperimental = true;
    if (glewInit() != GLEW_OK) { glfwTerminate(); return -1; }

    //Some opengl options
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_DEBUG_OUTPUT);
    glDepthFunc(GL_LESS);
    //glEnable(GL_CULL_FACE);

    //matrices
    std::vector<glm::vec3> vertices = {
        {-.2f, -.2f, 0}, {0, .2f, 0}, {.2f, -.2f, 0}
    };
    std::vector<glm::vec3> colors = {
        {1, 0, 0}, {0, 1, 0}, {0, 0, 1}
    };
    std::vector<GLushort> indexes = {
        0, 1, 2
    };

    //vertexArray
    GLuint vertex_array;
    glGenVertexArrays(1, &vertex_array);
    glBindVertexArray(vertex_array);

    //vertexbuffer
    GLuint vertex_buffer;
    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

    //colorbuffer
    GLuint color_buffer;
    glGenBuffers(1, &color_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * colors.size(), colors.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

    //indexbuffer
    GLuint index_buffer;
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * indexes.size(), indexes.data(), GL_STATIC_DRAW);

    glBindVertexArray(0);

    //Init shader
    auto shader_program = new ShaderProgram;
    shader_program->initFromFiles("../Crane/simple.vert", "../Crane/simple.frag");
    //shader_program->addUniform("MVP");


    ImGui_ImplGlfwGL3_Init(window, true);
    glfwSwapInterval(1);

    while (!glfwWindowShouldClose(window))
    {
    ImGui_ImplGlfwGL3_NewFrame();
    ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

        //maj viewport
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_h, &display_w);
        glViewport(0, 0, display_w, display_h);

        //clear screen
        glClearColor(.2f, .2f, .2f, 0);
        glClear(GL_COLOR_BUFFER_BIT);

        //draw stuff
        shader_program->use();
        glBindVertexArray(vertex_array);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        //auto mvp = glm::mat4(1);
        //glUniformMatrix4fv(shader_program->uniform("MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
        glDrawElements(GL_TRIANGLES, indexes.size(), GL_UNSIGNED_SHORT, nullptr);

        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glBindVertexArray(0);

        shader_program->disable();
        ImGui::Render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    shader_program->disable();
    ImGui_ImplGlfwGL3_Shutdown();
    glfwTerminate();
    return 0;
}

片段着色器

#version 430
in vec3 fColors;
out vec4 fragColors;
void main()
{
    fragColors = vec4(fColors, 1.0);
}

顶点着色器

#version 430    
layout (location = 0) in vec4 vertexPosition; 
layout (location = 1) in vec3 vertexColor; 
out vec3 fColors; 
void main() 
{ 
    fColors = vertexColor; 
    gl_Position = vertexPosition; 
}

此外,我从这里使用着色器加载器: r3dux shader loader

1 个答案:

答案 0 :(得分:4)

在您的程序中,Depth TestglEnable(GL_DEPTH_TEST))。
片段的深度存储在单独的缓冲区中。在每帧开始时,也必须清除该缓冲区,就像使用颜色缓冲区一样。参见glClear

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

当然,如果您禁用深度测试,那么您也将“看到”三角形。