为什么我的OpenGL代码未呈现方格纹理

时间:2019-01-14 17:25:24

标签: c opengl glfw

为什么我的屏幕只有黑色?

out <- Reduce(function(...) merge(..., by = "row.names", all = TRUE), Bpam)
row.names(out) <- out$Row.names
out <- out[, -1]

因此,我试图创建一个方格图案,然后在屏幕上绘制它。 我已经在learningopengl.com上进行了练习……我不明白我在做什么错。 有帮助吗?

我正在使用C语言和OpenGL 4.5

我正在尝试学习现代OpenGL,我使用的是固定管道,在此之前,它在许多方面都大不相同。

1 个答案:

答案 0 :(得分:6)

程序本身不会用数据填充元素索引缓冲区(ebo)。如果没有元素索引缓冲区,glDrawElements将无法从中提取元素索引,因此将无济于事。

编辑1:此外,顶点缓冲对象(vbo)仅用于着色器所需的3个属性之一。您只需要设置纹理坐标属性指针,而不能设置位置或颜色。

编辑2:另外,您没有将采样器设置为统一。

编辑3:最后但并非最不重要的一点是,您正在创建核心配置文件上下文,但是使用不推荐使用的格式(因此核心不可用)纹理格式GL_LUMINANCE。您必须为单个通道纹理使用GL_RED


确实呈现棋盘格纹理的固定程序的源代码:

#include <epoxy/gl.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
//#include "include/state.h"
//#include "include/state_machine.h"
//#include "include/renderable.h"


const int WIDTH = 800;
const int HEIGHT = 600;

// called when user resizes window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// called when we receive input
void processInput(GLFWwindow *window) {
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, 1);
}

GLuint get_checker_texture() {
    unsigned char texDat[64];
    for (int i = 0; i < 64; ++i)
        texDat[i] = ((i + (i / 8)) % 2) * 128 + 127;

    //upload to GPU texture
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 8, 8, 0, GL_RED, GL_UNSIGNED_BYTE, texDat);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    return tex;
}

//void render_box(renderable* this, unsigned int vbo, unsigned int vao, unsigned int ebo) {
//    draw_texture(this->texture, this->x, this->y, this->z, vbo, vao, ebo);
//}

int main(int argc, char* argv[]) {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    #ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // only on MACOS
    #endif

    // creating the window
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL App", NULL, NULL);
    if (window == NULL) {
        printf("Failed to create GLFW window");
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    // hook on window resize
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    glViewport(0, 0, WIDTH, HEIGHT);

    // just making sure the state machine works
    // for testing purposes only
    /*state_machine* sm = init_state_machine();
    state* state0 = init_state();

    state* state1 = init_state();
    state1->background_color->r = 1.0f;

    state* state2 = init_state();
    state2->background_color->g = 1.0f;

    ss_vector_append(sm->states, state0);
    ss_vector_append(sm->states, state1);
    ss_vector_append(sm->states, state2);*/

    //renderable* box = init_renderable(0.0f, 0.0f, 0.0f, &render_box);
    unsigned int tex = get_checker_texture();

    const char* vertex_shader_src =
        "#version 330 core\n"
        "layout (location = 0) in vec3 aPos;\n"
        "layout (location = 1) in vec3 aColor;\n"
        "layout (location = 2) in vec2 aTexCoord;\n"
        "\n"
        "out vec3 ourColor;\n"
        "out vec2 TexCoord;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    gl_Position = vec4(aPos, 1.0);\n"
        "    ourColor = aColor;\n"
        "    TexCoord = aTexCoord;\n"
        "}";

    unsigned int vertex_shader;
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
    glCompileShader(vertex_shader); 

    int  success;
    char infoLog[512];
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);

    if (!success) {
        glGetShaderInfoLog(vertex_shader, 512, NULL, infoLog);
        printf("%s\n", infoLog);
    }

    const char* fragment_shader_src = 
        "#version 330 core\n"
        "out vec4 FragColor;\n"
        "\n" 
        "in vec3 ourColor;\n"
        "in vec2 TexCoord;\n"
        "\n"
        "uniform sampler2D ourTexture;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    FragColor = vec4(vec3(texture(ourTexture, TexCoord).r), 1.);\n"
        "}";

    unsigned int fragment_shader;
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_src, NULL);
    glCompileShader(fragment_shader);

    int  success0;
    char infoLog0[512];
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success0);

    if (!success0) {
        glGetShaderInfoLog(fragment_shader, 512, NULL, infoLog0);
        printf("%s\n", infoLog0);
    }

    unsigned int shaderProgram;
    shaderProgram = glCreateProgram();

    glAttachShader(shaderProgram, vertex_shader);
    glAttachShader(shaderProgram, fragment_shader);
    glLinkProgram(shaderProgram);

    unsigned uniform_sampler_ourTexture = glGetUniformLocation(shaderProgram, "ourTexture");

    int success1;
    char infoLog1[512];

    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success1);
    if(!success1) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog1);
        printf("%s\n", infoLog1);
    }

    float vertices[] = {
         // positions          // colors           // texture coords
         0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // top right
         0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // bottom right
        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // bottom left
        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // top left 
    }; 

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

    unsigned int vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    unsigned int vbo;
    glGenBuffers(1, &vbo);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(0 * sizeof(float)));
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);

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

    // render loop
    while(!glfwWindowShouldClose(window)) {
        processInput(window);

        //state* current_state = ((state*)sm->states->items[sm->stateindex]);

        glClear(GL_COLOR_BUFFER_BIT);
        // render here
        glClearColor(
            0.2f,//current_state->background_color->r,        
            0.3f,//current_state->background_color->g, 
            0.3f,//current_state->background_color->b,
            1.0f//current_state->background_color->a
        );

        glUseProgram(shaderProgram);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, tex);
    glUniform1i(uniform_sampler_ourTexture, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();    
    }

    glfwTerminate();
    return 0;
}