OpenGL 程序未使用 VirtualBox 在 Ubuntu 上呈现
OpenGL program isn't rendering on Ubuntu with VirtualBox
我开始学习 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
,没有任何错误。
但是,当我执行该程序时,它打开 window 时黑屏,没有渲染三角形。我尝试执行有人在教程评论中发布的 this 代码,但我得到了相同的黑屏并且没有呈现多边形。问题出在代码上吗?设置 OpenGL 时我错过了什么吗?编译参数是否正确?
由于我既没有为背景也没有为三角形声明颜色,所以默认情况下它们都是黑色的。因此,尽管正在渲染三角形,但它是不可见的。为它们设置颜色解决了问题。
我开始学习 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
,没有任何错误。
但是,当我执行该程序时,它打开 window 时黑屏,没有渲染三角形。我尝试执行有人在教程评论中发布的 this 代码,但我得到了相同的黑屏并且没有呈现多边形。问题出在代码上吗?设置 OpenGL 时我错过了什么吗?编译参数是否正确?
由于我既没有为背景也没有为三角形声明颜色,所以默认情况下它们都是黑色的。因此,尽管正在渲染三角形,但它是不可见的。为它们设置颜色解决了问题。