1

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.

Community
  • 1
  • 1
Lauriane.C
  • 100
  • 10
  • Are you checking somewhere whether your shaders compiled and linked without errors? When you get compile errors, then `glUseShader` will also throw an error since `shader_programme` is no valid program object. – BDL Jul 11 '16 at 08:02
  • I checked that bool is_program = glIsProgram(shader_programme); qDebug() << is_program ; right before the loop and it returns true so I guess that the shaders are fine, but is that what you meant ? – Lauriane.C Jul 11 '16 at 08:09
  • @Lauriane.C No, it's not. `glIs*()` checks if an object was created with `glGen*()` (or `glCreate*()`), which is not very helpful, because you already know that it's true. You need to use `glGetShaderiv(..., GL_COMPILE_STATUS, ...)` and `glGetProgramiv(..., GL_LINK_STATUS, ...)`. If they return false, you need to print error (and info logs that you can get from those functions). – HolyBlackCat Jul 11 '16 at 08:33
  • Also, try following: replace `in` with `attribute`, remove `out vec4 frag_colour;` and replace `frag_colour = ` with `gl_FragColor = `. – HolyBlackCat Jul 11 '16 at 08:35
  • As I told you already: One cannot query the compile state of a shader program object. And you are still not checking the compile state of the two shader objects... – BDL Jul 11 '16 at 10:31
  • @BDL : so sorry I did not understand at all what you meant but I think now I did and edited the question. – Lauriane.C Jul 11 '16 at 12:32
  • Yes, now it is correct :) What graphics hardware do you actually have? – BDL Jul 11 '16 at 12:40
  • From sudo lshw -C display I get : description: VGA compatible controller produit: 4 Series Chipset Integrated Graphics Controller fabriquant: Intel Corporation identifiant matériel: 2 information bus: pci@0000:00:02.0 version: 03 bits: 64 bits horloge: 33MHz fonctionnalités: msi pm vga_controller bus_master cap_list rom configuration: driver=i915 latency=0 ressources: irq:26 mémoire:fe400000-fe7fffff mémoire:d0000000-dfffffff portE/S:ec00(taille=8) – Lauriane.C Jul 11 '16 at 12:44

1 Answers1

1
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.

Community
  • 1
  • 1
derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thank you for this answer, very clear. And sorry for the #version 210 it was indeed stupid. For the rest, you say the error messages are clear and indeed I thought they were until I was told in this topic I could still use new syntax (so >= 1.30) even though the computer told me supported versions were 1.1 and 1.2 . From this, I started to try everything I could think of to make it work. Finally yesterday I set up my hard drive in a computer that has a proper GPU, now it tell me "supported version 3.0" and with the specification #version 130 and the new syntax it works just fine :) – Lauriane.C Jul 12 '16 at 07:49