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?

Jean Catanho
  • 326
  • 7
  • 18
  • 1
    Taking your first steps into OpenGL programming on virtual hardware is... brave, I guess :). The black screen sounds very promising, but first, make sure OpenGL is working properly in the Virtual Box by running any known to work precompiled program. If that doesn't work, it is no longer a programming problem. The next sanity check is setting a non-black glClearColor and doing a glClear() each frame... does the color show up? – Paul-Jan Dec 19 '15 at 06:36
  • @Paul-Jan Thanks for the answer! Setting glClearColor and glClear() to white made it possible to see the triangle. I didn't realize that the fragment shader was setting the polygon to black. I don't intend to take many steps into OpenGL on the Virtual Box, it's more like a temporary solution as my main hardware is at maintenance. But what problems would I have if I insisted a little bit longer with it in the virtual machine? – Jean Catanho Dec 19 '15 at 07:05
  • 1
    Virtual machines are excellent for CPU-only workloads, GPU virtualization is a relatively new addition and support is a little bit unstable, you could end up with bugs just happening in the VM but running fine outside. That could lead to lost hours trying to debug a nonexistent problem in the real world – rlam12 Dec 19 '15 at 18:16
  • @rlam12 I see.. thanks for the answer, it helped a lot! :) – Jean Catanho Dec 19 '15 at 19:55

1 Answers1

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.

Jean Catanho
  • 326
  • 7
  • 18