OpenGL程序不能在Ubuntu上使用VirtualBox进行渲染

时间:2015-12-19 05:33:37

标签: c++ opengl glfw glew ubuntu-15.10

我开始学习OpenGL,我决定在VirtualBox上使用Ubuntu 15.10来完成这项工作。我安装了包 mesa-common-dev (gl.h), libglew-dev (glew.h)和 libglfw3-dev (glfw3) .h)以及this教程之后我想出了这段代码:

#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;
}

我正在将其编译为g++ test.cpp -o test -lglfw -lGLEW -lGL,没有任何错误。

但是,当我执行程序时,它会打开一个黑色屏幕的窗口,而不会渲染三角形。我尝试执行有人在教程的评论中发布的this代码,但是我得到了相同的黑屏并且没有渲染多边形。问题是代码吗?设置OpenGL时我错过了什么吗?编译参数是否正确?

1 个答案:

答案 0 :(得分:0)

由于我没有为背景和三角形声明颜色,因此默认情况下它们都是黑色。因此,三角形虽然在渲染时是不可见的。为它们设置颜色解决了这个问题。