Lauriane.C
Lauriane.C

Reputation: 100

OpenGL "Hello Triangle" results in black window, version issue?

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 ofvp' only valid for function parameters in GLSL 1.20 fs not compiled 0:2(1): error: out' qualifier in declaration offrag_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.

Upvotes: 1

Views: 644

Answers (1)

derhass
derhass

Reputation: 45342

vs not compiled
0:1(10): error: GLSL 2.10 is not supported. Supported versions are: 1.10, 1.20, and 1.00 ES

That error message is quite clear. You should be aware the GLSL 2.10 does not even exist. OpenGL 2.1 comes with GLSL version 1.20, so that is what you should use. The mapping between GLSL version and GL version is a bit uninituitive for historical reasons. Have a look at this answer for details.

When I ask for version 1.2

First of all, you can't do that either. Asking for specific versions was introduced in GL 3.x. Asking for GL 2.1 or asking for 1.2 does not make a difference, you get the same as you would get when not asking for a specific version at all. And that will typically be the highest supported GL version in compatibility profile. In practice, that means you most likely get up to 2.1 on implementations not supporting the compatibility profile, or just the highest version supported by your GPU, even up to the recent 4.5 on nvidia's or AMD's proprietary drivers. You might have a look at this answer for further details.

and set #version 120, I get the error:

vs not compiled 0:2(1): error: in' qualifier in declaration ofvp' only valid for function parameters in GLSL 1.20 fs not compiled 0:2(1): error: out' qualifier in declaration offrag_colour' only valid for function parameters in GLSL 1.20

This error message is also quite clear. As @HolyBlackCat already pointed out in the comments, you cannot use the generic in and out syntax in GLSL 1.20; that syntax was introduced in GLSL 1.30 / OpenGL 3.0 (with the introduction of the geometry shader as a thrid programmable stage). You cannot just take some GLSL >= 1.30 code, change the version back to GLSL 1.20, and just hope that it will work.

Upvotes: 1

Related Questions