I'm trying to run the code from Anton Gerdelan's tutorial "hello triangle" that I basically just copy-pasted into QtCreator.
I included : GL/glew.h, GLFW/glfw3.h, stdio.h.
int main () {
// start GL context and O/S window using the GLFW helper library
if (!glfwInit ()) {
fprintf (stderr, "ERROR: could not start GLFW3\n");
return 1;
}
GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
if (!window) {
fprintf (stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent (window);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit ();
// get version info
const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
const GLubyte* version = glGetString (GL_VERSION); // version as a string
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported %s\n", version);
// tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable (GL_DEPTH_TEST); // enable depth-testing
glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
GLuint vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 120\n"
"in vec3 vp;"
"void main () {"
" gl_Position = vec4 (vp, 1.0);"
"}";
const char* fragment_shader =
"#version 120\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
GLuint shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);
while (!glfwWindowShouldClose (window)) {
// wipe the drawing surface clear
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_programme);
glBindVertexArray (vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays (GL_TRIANGLES, 0, 3);
// update other events like input handling
glfwPollEvents ();
// put the stuff we've been drawing onto the display
glfwSwapBuffers (window);
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
Having read the old similar topic Black screen on Anton's OpenGL Hello Triangle Tutorial, I followed the advice and changed the #version from 400 to 120 in the shaders, because the program tells me that :
Renderer: Mesa DRI Intel(R) G41
OpenGL version supported 2.1 Mesa 11.2.0
However I still get an empty black window. I used this to find out where the error is :
if (glGetError() == GL_NO_ERROR) {
qDebug() << "no errors";
}
else {
qDebug() << "errors" ;
}
And apparently it is in glUseProgram (shader_programme);
What can I do ? Is my GPU too weak to run this code or am I doing something wrong ?
Thank you for your help,
Lauriane.
EDIT :
I have added the following debug :
GLuint shader_programme = glCreateProgram ();
glAttachShader (shader_programme, vs);
glAttachShader (shader_programme, fs);
glLinkProgram (shader_programme);
GLint isCompiled ;
glGetShaderiv(shader_programme, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
qDebug() << "not compiled" ;
GLint maxLength = 0;
glGetShaderiv(shader_programme, GL_INFO_LOG_LENGTH, &maxLength);
qDebug() << maxLength ;
// The maxLength includes the NULL character
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(shader_programme, maxLength, &maxLength, &errorLog[0]);
}
GLint isLinked ;
glGetProgramiv( shader_programme, GL_LINK_STATUS, &isLinked); ;
if (isLinked == GL_FALSE) {
qDebug() << "not linked" ;
GLint maxLength2 = 0;
glGetShaderiv(shader_programme, GL_INFO_LOG_LENGTH, &maxLength2);
qDebug() << maxLength2 ;
// The maxLength includes the NULL character
std::vector<GLchar> errorLog(maxLength2);
glGetShaderInfoLog(shader_programme, maxLength2, &maxLength2, &errorLog[0]);
}
It returns "not linked", 0 .
GLint success;
glGetProgramiv(shader_programme, GL_LINK_STATUS, &success);
if(!success)
{
GLchar infoLog[512];
glGetProgramInfoLog(shader_programme, 512, NULL, infoLog);
qDebug() << infoLog ;
}
Returns : error: linking with uncompiled shadererror: linking with uncompiled shader.
Since the topic is a mess (because of me) I wanted to recall that, when I change my shaders to the following ones, it works fine :
const char* vertex_shader =
"#version 120\n"
"attribute vec3 vp;"
"void main () {"
" gl_Position = vec4 (vp, 1.0);"
"}";
const char* fragment_shader =
"#version 120\n"
"void main () {"
" gl_FragColor = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
Which makes me think it might be a compatibility issue...
EDIT EDIT : On BDL's advice I realised my debug was nonsense and changed it to :
GLint isCompiled ;
glGetShaderiv(vs, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
qDebug() << "vs not compiled" ;
GLint maxLength = 0;
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
GLchar errorLog[maxLength];
glGetShaderInfoLog(vs, maxLength, NULL, errorLog);
qDebug() << errorLog ;
}
glGetShaderiv(fs, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
qDebug() << "fs not compiled" ;
GLint maxLength = 0;
glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
GLchar errorLog[maxLength];
glGetShaderInfoLog(fs, maxLength, NULL, errorLog);
qDebug() << errorLog ;
}
The output is :
vs not compiled
0:1(10): error: GLSL 2.10 is not supported. Supported versions are: 1.10, 1.20, and 1.00 ES
fs not compiled
0:1(10): error: GLSL 2.10 is not supported. Supported versions are: 1.10, 1.20, and 1.00 ES
I asked for version 2.1 thanks to glWindowHint (which is the maximum I can have without being enable to open the window) and set #version 210 .
When I ask for version 1.2 and set #version 120, I get the error :
vs not compiled
0:2(1): error: in' qualifier in declaration of
vp' only valid for function parameters in GLSL 1.20
fs not compiled
0:2(1): error: out' qualifier in declaration of
frag_colour' only valid for function parameters in GLSL 1.20
Which leads me to change my shaders to the old syntax with "attribute" etc. I'm more and more convinced that I just cant run new OpenGL on this very cheap computer but if you guys say that I can I trust you.