【发布时间】:2020-01-22 03:31:55
【问题描述】:
我已经阅读了很多关于此的内容,但还没有解决任何问题。我正在尝试使用 OpenGL3 绘制彩色三角形,但出现以下错误:
在抛出 'std::runtime_error' what() 实例后调用终止:顶点着色器的编译错误(来自文件 ./TP1/shaders/triangle.vs.glsl):0:1(10) : 错误:不支持 GLSL 3.30。 支持的版本有:1.10、1.20、1.30、1.00 ES、3.00 ES、3.10 ES 和 3.20 ES
当我运行 glxinfo | grep -i opengl 我明白了:
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 6000 (Broadwell GT3)
OpenGL core profile version string: 3.3 (Core Profile) Mesa 19.0.8
OpenGL core profile shading language version string: 4.50
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL version string: 3.0 Mesa 19.0.8
OpenGL shading language version string: 4.50
OpenGL context flags: (none)
OpenGL profile mask: compatibility profile
OpenGL extensions:
OpenGL ES profile version string:
OpenGL ES 3.1 Mesa 19.0.8
OpenGL ES profile shading language version string:
OpenGL ES GLSL ES 3.10
OpenGL ES profile extensions:
我尝试了 export MESA_GL_VERSION_OVERRIDE=3.3,这使我能够执行代码,但我只是得到一个奇怪的三角形,而不是一个漂亮的等边多色三角形。 这是我的完整代码:
#include <glimac/SDLWindowManager.hpp>
#include <GL/glew.h>
#include <iostream>
#include <glimac/Program.hpp>
#include <glimac/FilePath.hpp>
using namespace glimac;
int main(int argc, char** argv) {
// Initialize SDL and open a window
SDLWindowManager windowManager(800, 600, "GLImac");
// Initialize glew for OpenGL3+ support
GLenum glewInitError = glewInit();
if(GLEW_OK != glewInitError) {
std::cerr << glewGetErrorString(glewInitError) << std::endl;
return EXIT_FAILURE;
}
std::cout << "OpenGL Version : " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLEW Version : " << glewGetString(GLEW_VERSION) << std::endl;
//load shaders and tell OpenGL to use them
FilePath applicationPath(argv[0]);
Program program = loadProgram(applicationPath.dirPath() + "shaders/triangle.vs.glsl",
applicationPath.dirPath() + "shaders/triangle.fs.glsl");
program.use();
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
//triangle data
GLfloat vertices[] = { -0.5f, -0.5f, 1.f, 0.f, 0.f, //2 coordinates + 1 0 0 color
0.5f, -0.5f, 0.f, 1.f, 0.f,
0.0f, 0.5f, 0.f, 0.f, 1.f };
glBufferData(GL_ARRAY_BUFFER, (15*(sizeof(float))), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
const GLuint VERTEX_ATTR_POSITION = 3;
const GLuint VERTEX_ATTR_COLOR = 8;
glEnableVertexAttribArray(VERTEX_ATTR_POSITION);
glEnableVertexAttribArray(VERTEX_ATTR_COLOR);
const GLvoid* bouche;
glVertexAttribPointer(VERTEX_ATTR_POSITION, 2, GL_FLOAT, GL_FALSE, (0*sizeof(GL_FLOAT)), bouche);
glVertexAttribPointer(VERTEX_ATTR_COLOR, 3, GL_FLOAT, GL_FALSE, (2*sizeof(GL_FLOAT)), bouche);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(VERTEX_ATTR_POSITION, 2, GL_FLOAT, GL_FALSE, (2*sizeof(GL_FLOAT)), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Application loop:
bool done = false;
while(!done) {
// Event loop:
SDL_Event e;
while(windowManager.pollEvent(e)) {
if(e.type == SDL_QUIT) {
done = true; // Leave the loop after this iteration
}
}
//clean window
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
// Update the display
windowManager.swapBuffers();
}
//liberate allocated memory on GPU (the vbo and vao)
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
return EXIT_SUCCESS;
}
【问题讨论】: