使用 OpenGL 绘制一个点

Drawing a dot with OpenGL

我的程序应该打开一个 window 并在中心绘制一个蓝点,但未能绘制该点。

这是我正在使用的完整代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram()
{
    const char* vshaderSource = "#version 320 \n"
        "void main(void) \n"
        "{gl_Position = vec4(0.0,0.0,0.0,1.0);}";

    const char* fshaderSource = "#version 320 \n"
        "out vec4 color; \n"
        "void main(void) \n"
        "{ color = vec4(0.0,0.0,1.0,1.0);}";

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(vShader, 1, &vshaderSource, NULL);
    glShaderSource(fShader, 1, &fshaderSource, NULL);
    glCompileShader(vShader);
    glCompileShader(fShader);

    GLuint vfProgram = glCreateProgram();
    glAttachShader(vfProgram, vShader);
    glAttachShader(vfProgram, fShader);
    glLinkProgram(vfProgram);

    return vfProgram;
}

void init(GLFWwindow* window) 
{
    renderingProgram = createShaderProgram();
    glGenVertexArrays(numVAOs, vao);
    glBindVertexArray(vao[0]);
}

void display(GLFWwindow* window, double currentTime)
{
   
    glUseProgram(renderingProgram);
    glDrawArrays(GL_POINT, 0, 1);
}


int main(void)
{
    if (!glfwInit())
    {
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);

    GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter2-program1", NULL, NULL);

    glfwMakeContextCurrent(window);

    //Window or OpenGL context creation failed
    if (!window)
    {
        glfwTerminate();        
        return -1;
    }


    if (glewInit() != GLEW_OK)                 
    {
        std::cout << "Error!" << std::endl;
        exit(EXIT_FAILURE);
    }

    glfwSwapInterval(1);

    init(window);   


    /* Rendering loop- until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        
        /* Render here */
        display(window, glfwGetTime());

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();   
    exit(EXIT_SUCCESS);
    return 0;
}

几个问题:

总计:

// g++ main.cpp `pkg-config glfw3 glew --cflags --libs`
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];

void CheckStatus( GLuint obj, bool isShader )
{
    GLint status = GL_FALSE, log[ 1 << 11 ] = { 0 };
    ( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
    if( status == GL_TRUE ) return;
    ( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
    std::cerr << (GLchar*)log << "\n";
    std::exit( EXIT_FAILURE );
}

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

const char* const vert = R"GLSL(
#version 150
void main(void)
{
    gl_Position = vec4(0.0,0.0,0.0,1.0);
}
)GLSL";

const char* const frag = R"GLSL(
#version 150
out vec4 color;
void main(void)
{
    color = vec4(0.0,0.0,1.0,1.0);
}    
)GLSL";

int main(void)
{
    if (!glfwInit())
    {
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);

    GLFWwindow* window = glfwCreateWindow(600, 600, "Chapter2-program1", NULL, NULL);

    //Window or OpenGL context creation failed
    if (!window)
    {
        glfwTerminate();        
        return -1;
    }

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)                 
    {
        std::cout << "Error!" << std::endl;
        exit(EXIT_FAILURE);
    }

    glfwSwapInterval(1);

    GLuint renderingProgram = glCreateProgram();
    AttachShader( renderingProgram, GL_VERTEX_SHADER, vert );
    AttachShader( renderingProgram, GL_FRAGMENT_SHADER, frag );
    glLinkProgram( renderingProgram );
    CheckStatus( renderingProgram, false );
    
    glGenVertexArrays(numVAOs, vao);
    glBindVertexArray(vao[0]);

    /* Rendering loop- until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        
        glUseProgram(renderingProgram);
        glDrawArrays(GL_POINTS, 0, 1);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();   
    exit(EXIT_SUCCESS);
    return 0;
}