无法在 OpenGL 中渲染旋转立方体

问题描述

我正在尝试获得一个旋转的 3D 立方体,但无法渲染。没有错误,但只是黑屏。此外,没有检查 Fragment 着色器是否正常工作,但我可以向您保证,我只是没有将其包含在此处。

这是我的代码的样子:

#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

#include <cglm/cglm.h>
#include <cglm/mat4.h>
#include <cglm/vec3.h>

#include <stdio.h>
#include <stdlib.h>

float vertices[] = {
    -0.5f,-0.5f,0.0f,0.5f,1.0f,1.0f};

gluint elements[] = {
    0,1,2,3,0};

static const char *vertex_shader_text =
    "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "layout (location = 1) in vec2 aTexCoord;\n"
    "out vec2 TexCoord;\n"
    "uniform mat4 model;\n"
    "uniform mat4 view;\n"
    "uniform mat4 projection;\n"

    "void main()\n"
    "{\n"
    "   gl_Position = projection * view * model * vec4(aPos,1.0f);\n"
    "   TexCoord = vec2(aTexCoord.x,aTexCoord.y);\n"
    "}";

static const char *fragment_shader_text =
    "#version 330 core\n"
    "varying vec3 color;\n"
    "void main()\n"
    "{\n"
    "    gl_FragColor = vec4(color,1.0);\n"
    "}\n";

static void error_callback(int error,const char *description) {
    fprintf(stderr,"Error: %s\n",description);
}

static void key_callback(GLFWwindow *window,int key,int scancode,int action,int mods) {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetwindowShouldClose(window,GLFW_TRUE);
}

int main(void) {
    GLFWwindow *window;
    gluint vbo,vertex_shader,fragment_shader,program;
    GLint mvp_location,vpos_location,vcol_location;

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);

    window = glfwCreateWindow(640,480,"Simple example",NULL,NULL);
    if (!window) {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(window,key_callback);

    glfwMakeContextCurrent(window);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        fprintf(stderr,"Could Not Load GLFW\n");
        exit(1);
    }

    glfwSwapInterval(1);

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

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    // position attribute
    glVertexAttribPointer(0,GL_FLOAT,GL_FALSE,5 * sizeof(float),(void *)0);
    glEnabLevertexAttribArray(0);
    // texture coord attribute
    glVertexAttribPointer(1,(void *)(3 * sizeof(float)));
    glEnabLevertexAttribArray(1);


    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader,&vertex_shader_text,NULL);
    glCompileShader(vertex_shader);

    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader,&fragment_shader_text,NULL);
    glCompileShader(fragment_shader);

    GLint status;
    glGetShaderiv(vertex_shader,GL_COMPILE_STATUS,&status);
    if(!status){
        char buffer[512];
        glGetShaderInfoLog(vertex_shader,512,buffer);
        puts(buffer);
    }

    program = glCreateProgram();
    glAttachShader(program,vertex_shader);
    glAttachShader(program,fragment_shader);
    glLinkProgram(program);

    while (!glfwWindowShouldClose(window)) {
        float ratio;
        int width,height;

        glfwGetFramebufferSize(window,&width,&height);
        ratio = width / (float)height;

        glViewport(0,width,height);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        mat4 model = {{1.f,1.f,1.f},{1.f,1.f}};
        mat4 view = {{1.f,1.f}};
        mat4 projection = {{1.f,1.f}};

        vec3 axis = {0.5f,0.0f};
        vec3 dist = {0.0f,-3.0f};

        glm_rotate(model,glfwGetTime(),axis);
        glm_translate(view,dist);
        glm_perspective(0.79,width / height,0.1f,100.0f,projection);

        unsigned int modelLoc = glGetUniformlocation(program,"model");
        unsigned int viewLoc = glGetUniformlocation(program,"view");

        gluniformMatrix4fv(modelLoc,(const GLfloat *)model);
        gluniformMatrix4fv(viewLoc,(const GLfloat *)view);

        unsigned int projectionLoc = glGetUniformlocation(program,"projection");
        glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);
}

解决方法

您必须使用 Identity matrix 初始化矩阵:

mat4 model = {
    {1.f,0.f,0.f},{0.f,1.f,1.f}};

viewprojection 也这样做。

glm_rotateglm_perspective 的参数的数据类型必须是 float

glm_rotate(model,(float)glfwGetTime(),axis);
glm_translate(view,dist);
glm_perspective((float)0.79,(float)width / (float)height,0.1f,100.0f,projection);

您还忘记创建 Index buffer

glBindVertexArray(VAO);

// [...]

unsigned int IBO;
glGenBuffers(1,&IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elements),elements,GL_STATIC_DRAW);

安装程序:

glUseProgram(program);

设置projection矩阵统一。

unsigned int projectionLoc = glGetUniformLocation(program,"projection");
glUniformMatrix4fv(projectionLoc,1,GL_FALSE,(const GLfloat *)projection);

最后,您必须更改片段着色器。例如:

#version 330 core
in vec2 TexCoord;
void main()
{                            
    gl_FragColor = vec4(TexCoord.xy,0.5,1.0);
};