簡體   English   中英

OpenGL 着色器編譯但 window 中未出現渲染

[英]OpenGL shaders compile but no rendering appears in the window

頂點和片段着色器似乎都在編譯,window 出現並被賦予清晰的顏色,但我無法渲染三角形。 我試圖只使用 glBegin(); 頂點(); glEnd(); 作為替代方案,但這也導致沒有渲染三角形。

main.c

#include <stdio.h>
#include <unistd.h>
#include <gfx/shader.h>
#include <gfx/vbo.h>
#include <gfx/vao.h>

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

int main(void){
    if(!glfwInit()){
        fprintf(stderr, "Unable to init GLFW.\n");
        exit(1);
    }
    GLFWwindow *win;
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    win = glfwCreateWindow(SCREENWIDTH, SCREENHEIGHT, "Terraria", NULL, NULL);
    if(win==NULL){
        fprintf(stderr, "Error creating opengl window\n");
        glfwTerminate();
        exit(1);
    }
    glfwMakeContextCurrent(win);
    if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
        fprintf(stderr, "Unable to init glad\n");
        glfwTerminate();
        exit(1);
    }
    glViewport(0, 0, 800, 600);
    glfwSetFramebufferSizeCallback(win, frame_buffer_size_cb);

    struct Shader s = create_shader("resources/shaders/vec2.vs", "resources/shaders/vec2.fs");
    uint32_t vaoID, vboID;
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);
    glGenBuffers(1, &vboID);
    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(vboID, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW);

    while(!glfwWindowShouldClose(win)){
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(s.sh);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glVertexAttribPointer(
            0,
            3,
            GL_FLOAT,
            GL_FALSE,
            3*sizeof(GLfloat),
            (void*)0
        );
        glDrawArrays(GL_TRIANGLES, 0,3);
        glDisableVertexAttribArray(0);


        glfwSwapBuffers(win);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

着色器.h

#include <gfx/gfx.h>

struct Shader{
    uint32_t sh;
    uint32_t vsh;
    uint32_t fsh;
};

struct Shader create_shader(const char *vs_path, const char *fs_path);

着色器.c

#include <gfx/shader.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

static uint32_t _compile(int8_t id, const char *path, uint16_t type){
    uint32_t sh; //Shader handle or ID
    FILE *fp = fopen(path, "rb"); //Open the shader source code file
    long sz = file_size(fp); //Get the file size
    char *shaderSource; //Create object to holder shader source code
    shaderSource = calloc(sz+1, 1);
    fread(shaderSource, 1, sz, fp); //Read the shader source code into new buffer
    fclose(fp);
    assert(strlen(shaderSource)>0);
    sh = glCreateShader(type);
    glShaderSource(sh, 1, (const GLchar **)&shaderSource, NULL);
    glCompileShader(sh);
    int success;
    char infoLog[512];
    glGetShaderiv(sh, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(sh, 512, NULL, infoLog);
        printf("Unable to compile shader id %d\n", id);
    }
    free(shaderSource);
    return sh;
}

struct Shader create_shader(const char *vs_path, const char *fs_path){
    struct Shader h;
    h.vsh = _compile(1,vs_path, GL_VERTEX_SHADER);
    h.fsh = _compile(0,fs_path, GL_FRAGMENT_SHADER);
    h.sh = glCreateProgram();
    glAttachShader(h.sh, h.vsh);
    glAttachShader(h.sh, h.fsh);
    glLinkProgram(h.sh);
    // check for linking errors
    int success;
    char infoLog[512];
    glGetProgramiv(h.sh, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(h.sh, 512, NULL, infoLog);
        printf("Unable to link gl program\n");
    }
    glDetachShader(h.sh, h.vsh);
    glDetachShader(h.sh, h.fsh);
    glDeleteShader(h.vsh);
    glDeleteShader(h.fsh);
    return h;
}

頂點着色器

#version 330 core
layout (location = 0) in vec3 aPos;
void main(){
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

片段着色器

#version 330 core

out vec4 FragColor;

void main(){
    FragColor = vec4(1.0,1.0,1.0,1.0);
}

我找到了答案。 如果您仔細查看 main,似乎您實際上應該將 glBindBuffer 調用到宏 GL_ARRAY_BUFFER 而不是像我想的那樣調用 vbo 句柄。 修復此問題導致我的程序為 function。 我的 window 提示也沒有生效,因為我在創建 window 后調用了它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM