0

I'm starting to learn OpenGL and I decided to use Ubuntu 15.10 on a VirtualBox to do this. I installed the packages mesa-common-dev (gl.h), libglew-dev (glew.h) and libglfw3-dev (glfw3.h) and following this tutorial I came up with this code:

#define GLEW_STATIC

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

using namespace std;

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

const GLchar* fragmentSource =
    "#version 130\n"
    "out vec4 outColor;"
    "uniform vec3 triangleColor;"
    "void main() {"
    "    outColor = vec4(triangleColor, 1.0);"
    "}";

int main(int argc, char *argv[]) {
    // GLFW initialization
    if (!glfwInit()) {
        cout << "Failed to initialize GLFW." << endl;
        return -1;

    }
    cout << "GLFW initialized." << endl;

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    cout << "Window and context created." << endl;

    // GLEW initialization
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        cout << "Failed to initialize GLEW." << endl;
        return -1;

    }
    cout << "GLEW initialized." << endl;

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

    // Create Vertex Array Object
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    cout << "VAO created and binded." << endl;

    //Vertex Buffer Object
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    cout << "VBO created and binded." << endl;

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

    if (!status) {
        // Vertex shader error handling
        char errorLog[512];
        glGetShaderInfoLog(vertexShader, 512, NULL, errorLog);

        cout << errorLog << endl;
        glfwTerminate();
        return -1;
    }
    cout << "Vertex shader created and compiled." << endl;

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

    if (!status) {
        // Fragment shader error handling
        char errorLog[512];
        glGetShaderInfoLog(fragmentShader, 512, NULL, errorLog);

        cout << errorLog << endl;
        glfwTerminate();
        return -1;
    }
    cout << "Fragment shader created and compiled." << endl;

    // Link the vertex and fragment shader into a shader program
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);
    cout << "Shaders linked." << endl;

    // Specify the layout of the vertex data
    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
    cout << "Layout of the vertex data specified." << endl;

    while( !glfwWindowShouldClose(window) ) {
        glDrawArrays(GL_TRIANGLES, 0, 3);

        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // Prepare to close the application
    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vbo);
    glDeleteBuffers(1, &vao);
    glfwTerminate();

    return 0;
}

I'm compiling it as g++ test.cpp -o test -lglfw -lGLEW -lGL with no errors.

But, when I execute the program, it opens the window with a black screen without rendering the triangle. I tried to execute this code that someone posted on the comments of the tutorial but I got the same black screen and no polygons rendered. Is the problem on the code? Did I miss something when setting OpenGL up? Are the compiling parameters correct?

4

1 回答 1

0

Since I didn't declare colors neither for the background nor for the triangle, both of them were black by default. Thus, the triangle was not visible althought it was being rendered. Setting up colors for both of them solved the problem.

于 2016-01-20T10:17:55.507 回答