0
我開始學習OpenGL,並決定在VirtualBox上使用Ubuntu 15.10來完成此操作。我安裝的軟件包檯面,共開發(gl.h),libglew-dev的(glew.h)和libglfw3-dev的(glfw3.h)及以下this教程,我想出了這個代碼:OpenGL程序不是在Ubuntu上用VirtualBox渲染
#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時我錯過了什麼?編譯參數是否正確?
在虛擬硬件上進行OpenGL編程的第一步是...勇敢,我猜:)。黑屏聽起來很有希望,但首先,通過運行任何已知的工作預編譯程序,確保OpenGL在Virtual Box中正常工作。如果這不起作用,它不再是一個編程問題。下一個完整性檢查是設置一個非黑色的glClearColor,並執行glClear()每幀...顏色顯示出來嗎? –
@ Paul-Jan感謝您的回答!將glClearColor和glClear()設置爲白色可以看到三角形。我沒有意識到片段着色器將多邊形設置爲黑色。我並不打算在Virtual Box上採取很多步驟進入OpenGL,它更像是一個臨時解決方案,因爲我的主要硬件正在維護中。但是如果我在虛擬機中堅持一點時間,我會遇到什麼問題? –
虛擬機非常適合僅限CPU的工作負載,GPU虛擬化是一個相對較新的增加,支持稍微不穩定,最終可能會發生虛擬機中發生錯誤但在外部正常運行。這可能會導致嘗試在現實世界中調試一個不存在的問題的時間損失 – rlam12