绘图三角形不起作用:我只看到黑屏和鼠标

时间:2014-05-23 15:26:10

标签: c++ opengl glfw

我正在使用GLEW用于现代OpenGL功能,而GLFW用于窗口。我正在尝试绘制一个三角形,但是编译和运行有些错误,但是没有绘制三角形,我只看到黑屏和鼠标。 这是我的代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>

const GLchar* vertexSource =
"#version 150 core\n"
"in ver2 position;"
"void main(){"
"   gl_Position = vec4(position, 0.0, 1.0);"
"}";

const GLchar* fragmentSource =
"#version 150 core\n"
"out vec4 outColor;"
"void main(){"
"   outColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";

int main(){

    //GLFW init:
    if (!glfwInit()){
        fprintf(stderr, "Failed to init GLFW");
        glfwTerminate();
    }

    //Hints:
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    //Creating window:
    GLFWwindow* window = glfwCreateWindow(800, 600, "Engine", glfwGetPrimaryMonitor(), nullptr);
    glfwMakeContextCurrent(window);

    //Glew initialize:
    glewExperimental = GL_TRUE;
    glewInit();

    //Create vertex array object:
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    //Create vertex buffer object:
    GLuint vbo;
    glGenBuffers(1, &vbo);

    //Our triangle data:
    GLfloat vertices[] = {
         0.0f,  0.5f, //Vertex 1 (X, Y)
         0.5f, -0.5f, //Vertex 2 (X, Y)
        -0.5f, -0.5f  //Vertex 3 (X, Y)
    };

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

    //Create and compile the vertex shader:
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);

    //Create and compile the fragment shader:
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
    glCompileShader(fragmentShader);

    //Creating shaders program:
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);



    //The loop:
    while (!glfwWindowShouldClose(window)){

        //Escape key:
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
            glfwSetWindowShouldClose(window, GL_TRUE);
        }

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }



    return 0;
}

2 个答案:

答案 0 :(得分:3)

"in ver2 position;"
    ^^^^

ver2是什么?我怀疑你的GLSL编译器是否也知道。

尝试vec2并检查着色器编译/链接状态/日志:

struct Program
{
    static GLuint Load( const char* vert, const char* geom, const char* frag )
    {
        GLuint prog = glCreateProgram();
        if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
        if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
        if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
        glLinkProgram( prog );
        CheckStatus( prog );
        return prog;
    }

private:
    static void CheckStatus( GLuint obj )
    {
        GLint status = GL_FALSE;
        if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
        if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
        if( status == GL_TRUE ) return;
        GLchar log[ 1 << 15 ] = { 0 };
        if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
        if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
        std::cerr << log << std::endl;
        exit( -1 );
    }

    static void AttachShader( GLuint program, GLenum type, const char* src )
    {
        GLuint shader = glCreateShader( type );
        glShaderSource( shader, 1, &src, NULL );
        glCompileShader( shader );
        CheckStatus( shader );
        glAttachShader( program, shader );
        glDeleteShader( shader );
    }
};

答案 1 :(得分:0)

您不需要使用glBindFragDataLocation,而是在片段着色器中写入gl_FragColor

const GLchar* fragmentSource =
"#version 150 core\n"
"void main(){"
"   gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";

否则您需要使用glDrawBuffers

指定要写入的缓冲区