无法使OpenGL 3.3绘制三角形,不会抛出任何错误

时间:2015-09-07 16:43:57

标签: c opengl

第二天我尝试使用着色器使用OpenGL 3.3绘制三角形,但我无法使其工作。没有打印出错误消息。除了GL_GetError()调用之后,我还调试了调试器和0一直glewInit()(没有错误)的每一行,但人们说它没问题。我不知道代码或我的电脑有什么问题。我使用lubuntu 15.04glxinfo | grep "OpenGL"打印

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Bay Trail 
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.5.9
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 10.5.9
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 10.5.9
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
OpenGL ES profile extensions:

这是我的程序,只有100 LOC,没有任何外部依赖:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

GLuint VBO;
GLuint prog;

const GLchar *vert_shader_src =
    "#version 330\n"
    "layout (location = 0) in vec3 Position;\n"
    "void main() {\n"
    "gl_Position = vec4(0.5*Position.x, 0.5*Position.y, Position.z, 1.0); }\n";
const GLchar *frag_shader_src =
    "#version 330\n"
    "out vec4 FragColor;\n"
    "void main() {\n"
    "FragColor = vec4(1.0, 0.0, 0.0, 1.0); }";

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(prog);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glutSwapBuffers(); }

void initGlut(int *argc_ptr, char **argv, void (*renderCallback)(void)) {
    glutInit(argc_ptr, argv);
    glutInitContextVersion(3, 3);
    glutInitWindowSize(600, 600);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("minigl");
    glutDisplayFunc(renderCallback); }

void initVBO(void) {
    float pts[9] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(pts), pts, GL_STATIC_DRAW); }

int initProg(void) {
    prog = glCreateProgram();
    GLuint vshade = glCreateShader(GL_VERTEX_SHADER);
    GLuint fshade = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar **v_src = &vert_shader_src;
    const GLchar **f_src = &frag_shader_src;
    int v_src_len_ptr[1] = { strlen(vert_shader_src) };
    int f_src_len_ptr[1] = { strlen(frag_shader_src) };

    glShaderSource(vshade, 1, v_src, v_src_len_ptr);
    glShaderSource(fshade, 1, f_src, f_src_len_ptr);
    glCompileShader(vshade);
    glCompileShader(fshade);
    GLint success;
    glGetShaderiv(vshade, GL_COMPILE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetShaderInfoLog(vshade, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
    glGetShaderiv(fshade, GL_COMPILE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetShaderInfoLog(fshade, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
    glAttachShader(prog, vshade);
    glAttachShader(prog, fshade);
    glLinkProgram(prog);
    glGetProgramiv(prog, GL_LINK_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Error linking shader program: '%s'\n", infolog); }
    glValidateProgram(prog);
    glGetProgramiv(prog, GL_VALIDATE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Error validating shader program: '%s'\n", infolog); }
    glDeleteShader(vshade);
    glDeleteShader(fshade);
    return 1; }

int main(int argc, char **argv) {
    initGlut(&argc, argv, render);
    // GL_GetError throws 1280 (invalid enum) after glewInit but all say it's OK
    if(glewInit() != GLEW_OK) {
        puts("GLEW failed to initialize!");
        return EXIT_FAILURE; }
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    initProg();

    glutMainLoop();
    glDeleteProgram(prog);
    glDeleteBuffers(1, &VBO);
    return EXIT_SUCCESS; }

顺便说一下,我已经下载了Github repo这个最小的OpenGL 3.3示例,它运行得很好。我看到的唯一主要区别是除了VBO之外还使用了VAO。

编辑:添加了VAO,移植到GLFW - 仍然是同样的问题。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

GLuint VBO;
GLuint VAO;
GLuint prog;

const GLchar *vert_shader_src =
    "#version 330\n"
    "layout (location = 0) in vec3 Position;\n"
    "void main() {\n"
    "gl_Position = vec4(0.5*Position.x, 0.5*Position.y, Position.z, 1.0); }\n";
const GLchar *frag_shader_src =
    "#version 330\n"
    "out vec4 FragColor;\n"
    "void main() {\n"
    "FragColor = vec4(1.0, 0.0, 0.0, 1.0); }";

void render(GLFWwindow *window) {
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(prog);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glfwSwapBuffers(window); }

void initVBO(void) {
    float pts[9] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(pts), pts, GL_STATIC_DRAW); }

int initProg(void) {
    prog = glCreateProgram();
    GLuint vshade = glCreateShader(GL_VERTEX_SHADER);
    GLuint fshade = glCreateShader(GL_FRAGMENT_SHADER);
    const GLchar **v_src = &vert_shader_src;
    const GLchar **f_src = &frag_shader_src;
    int v_src_len_ptr[1] = { strlen(vert_shader_src) };
    int f_src_len_ptr[1] = { strlen(frag_shader_src) };

    glShaderSource(vshade, 1, v_src, v_src_len_ptr);
    glShaderSource(fshade, 1, f_src, f_src_len_ptr);
    glCompileShader(vshade);
    glCompileShader(fshade);
    GLint success;
    glGetShaderiv(vshade, GL_COMPILE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetShaderInfoLog(vshade, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
    glGetShaderiv(fshade, GL_COMPILE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetShaderInfoLog(fshade, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
    glAttachShader(prog, vshade);
    glAttachShader(prog, fshade);
    glLinkProgram(prog);
    glGetProgramiv(prog, GL_LINK_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Error linking shader program: '%s'\n", infolog); }
    glValidateProgram(prog);
    glGetProgramiv(prog, GL_VALIDATE_STATUS, &success);
    if (!success) {
        GLchar infolog[1024];
        glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
        fprintf(stderr, "Error validating shader program: '%s'\n", infolog); }
    glDeleteShader(vshade);
    glDeleteShader(fshade);
    return 1; }

int main() {
    if(!glfwInit()) {
        puts("GLFW failed to initialize!");
        return EXIT_FAILURE; }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow *window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if(window == NULL) {
        puts("Failed to create window!");
        glfwTerminate();
        return EXIT_FAILURE; }
    glfwMakeContextCurrent(window);

    if(glewInit() != GLEW_OK) {
        puts("GLEW failed to initialize!");
        glfwDestroyWindow(window);
        glfwTerminate();
        return EXIT_FAILURE; }

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    initProg();

    while(!glfwWindowShouldClose(window)) {
        render(window); }

    glDeleteProgram(prog);
    glDeleteBuffers(1, &VBO);
    glfwDestroyWindow(window);
    glfwTerminate();
    return EXIT_SUCCESS; }

1 个答案:

答案 0 :(得分:0)

事实证明我创建了一个初始化VBO和VAO的函数......但忘了调用它。两天。