<p>There are two problems:</p>

<ol>
<li><p>Your indices are unsigned integers (4 bytes per index) but the call to glDrawElements specifies GL_UNSIGNED_SHORT. You need to change this to GL_UNSIGNED_INT.</p></li>
<li><p>You are drawing two red triangles onto a buffer that has been cleared to red.</p></li>
</ol>

CC BY-SA 3.0
2015-10-20T21:21:24.110Z
2015-10-20T21:21:24.110Z
Jason Allen
<p>I'm trying to learn GLSL, I'm following <a href="https://www.youtube.com/playlist?list=PLRwVmtr-pp06qT6ckboaOhnm9FxmzHpbY" rel="nofollow">this</a> tutorial. I've done everything according to the tutorials but I can't get any output from my code. As you see I'm checking for both compile errors and linker errors and everything is fine, GLSL code compiles and links fine but there is no output but a red window. Here is my code:</p>

<p>main.cpp </p>

<pre><code>#include &lt;QApplication&gt;
#include &lt;GlWindow.h&gt;
#include &lt;iostream&gt;

int main(int argc, char *argv[]){
    QApplication app(argc, argv);

    GlWindow glWindow;
    glWindow.show();

    return app.exec();
}
</code></pre>

<p>GLWindow.h</p>

<pre><code>#ifndef GLWINDOW_H
#define GLWINDOW_H
#include &lt;QtOpenGL/QGLWidget&gt;
#include &lt;QOpenGLWidget&gt;

class GlWindow : public QGLWidget{
public:

protected:
    void initializeGL();
    void paintGL();
};

#endif // GLWINDOW_H
</code></pre>

<p>GLWindow.cpp</p>

<pre><code>#include &lt;GL/glew.h&gt;
#include "GlWindow.h"
#include &lt;iostream&gt;

extern const char* vertexShaderCode;
extern const char* fragmentShaderCode;

void sendDataToOpenGL(){
    GLfloat verts[] = {
        +0.0f, +0.0f,
             1.0f, 0.0f, 0.0f,
        +1.0f, +1.0f,
             1.0f, 0.0f, 0.0f,
        -1.0f, +1.0f,
             1.0f, 0.0f, 0.0f,
        -1.0f, -1.0f,
             1.0f, 0.0f, 0.0f,
        +1.0f, -1.0f,
             1.0f, 0.0f, 0.0f,

    };

    GLuint verexBufferID;
    glGenBuffers(1, &amp;verexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, verexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5,0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (char*)(sizeof(GLfloat) * 2));

    GLuint indices [] = {0,1,2, 0,3,4};
    GLuint indexBufferID;
    glGenBuffers(1,&amp;indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}


bool checkShaderStatus(GLuint shaderID){
    GLint compileStatus;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &amp;compileStatus);


    if(compileStatus != GL_TRUE){
        std::cerr&lt;&lt;"failed"&lt;&lt;std::endl;
        GLint infoLogLength;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &amp;infoLogLength);
        GLchar* buffer = new GLchar [infoLogLength];

        GLsizei bufferSize;
        glGetShaderInfoLog(shaderID, infoLogLength, &amp;bufferSize, buffer);
        std::cout&lt;&lt;buffer&lt;&lt;std::endl;
        return false;
        delete []buffer;
    }
    return true;
}

bool checkProgramStatus(GLuint programID){
    GLint linkStatus;
    glGetProgramiv(programID, GL_LINK_STATUS, &amp;linkStatus);


    if(linkStatus != GL_TRUE){
        std::cerr&lt;&lt;"failed"&lt;&lt;std::endl;
        GLint infoLogLength;
        glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &amp;infoLogLength);
        GLchar* buffer = new GLchar [infoLogLength];

        GLsizei bufferSize;
        glGetProgramInfoLog(programID, infoLogLength, &amp;bufferSize, buffer);
        std::cout&lt;&lt;buffer&lt;&lt;std::endl;
        return false;

        delete []buffer;
    }
    return true;
}


void installShaders(){

    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    const char *adapter[1];
    adapter[0] = vertexShaderCode;

    glShaderSource(vertexShaderID, 1, adapter, 0);
    adapter[0] = fragmentShaderCode;
    glShaderSource(fragmentShaderID, 1, adapter, 0);



    glCompileShader(vertexShaderID);
    glCompileShader(fragmentShaderID);

    if(!checkShaderStatus(vertexShaderID)){
        std::cerr&lt;&lt;"Sth wrong with vertex shader"&lt;&lt;std::endl;
        return;
    }

    if(!checkShaderStatus(fragmentShaderID)){
       std::cerr&lt;&lt;"Sth wrong with fragment shader"&lt;&lt;std::endl;
       return;
    }


    GLuint programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);

    glLinkProgram(programID);

    if(!checkProgramStatus(programID)){
        std::cerr&lt;&lt;"failed to link"&lt;&lt;std::endl;
        return;
    }

    glUseProgram(programID);
}

void GlWindow::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0,0,width(), height());

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

}


void GlWindow::initializeGL(){
    glewInit();

    const GLubyte *shaderVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
    std::cout&lt;&lt;shaderVersion&lt;&lt;std::endl;

    sendDataToOpenGL();
    installShaders();
}
</code></pre>

<p>and finally my shader code: </p>

<pre><code>const char* vertexShaderCode  =
        "#version 430\r\n"
        "\n"
        "in layout(location=0) vec2 position;\n"
        "in layout(location=1) vec3 vertexColor;\n"
        ""
        "out vec3 theColor;\n"
        "\n"
        "void main()\n"
        "{\n"
        "\n"
        "\n"
        "gl_Position = vec4(position, 0.0, 1.0);\n"
        "theColor = vertexColor; \n"
        "\n"
        "}\n"
        ;


const char* fragmentShaderCode =
        "#version 430\r\n"
        "\n"
        "\n"
        "\n"
        "out vec4 daColor;\n"
        "in vec3 theColor;\n"
        "\n"
        "void main()\n"
        "{\n"
        "\n"
        " daColor = vec4(theColor,1.0);\n"
        "}\n"
        ;
</code></pre>

<p>I know that my shader code looks stupid but I'm just trying to learn. </p>

<p><strong>EDIT:</strong> I don't know if thats relevant, but I'm using Ubuntu 15.0 and <code>glGetString(GL_SHADING_LANGUAGE_VERSION)</code> returns: <code>4.4 nvidia via cg compiler</code>.  </p>

CC BY-SA 3.0
2015-10-15T10:31:15.700Z
your second `glVertexAttribPointer` doesn't look right, it's missing a *5 in the stride param
ratchet freak
CC BY-SA 3.0
2015-10-15T12:53:33.407Z
@ratchetfreak Yes the *5 was missing, But the problem still exists.
lino
CC BY-SA 3.0
2015-10-22T18:47:39.383Z
What if you remove the `layout` stuff from the shaders?
JesseTG
<opengl><shader><glsl>
Can't get output from a GLSL code
lino
# Can't get output from a GLSL code

(Asked by: username_0 on 2015-10-15)

I'm trying to learn GLSL, I'm following [this](https://www.youtube.com/playlist?list=PLRwVmtr-pp06qT6ckboaOhnm9FxmzHpbY) tutorial. I've done everything according to the tutorials but I can't get any output from my code. As you see I'm checking for both compile errors and linker errors and everything is fine, GLSL code compiles and links fine but there is no output but a red window. Here is my code:

main.cpp

```
#include <QApplication>
#include <GlWindow.h>
#include <iostream>

int main(int argc, char *argv[]){
    QApplication app(argc, argv);

    GlWindow glWindow;
    glWindow.show();

    return app.exec();
}

```

GLWindow.h

```
#ifndef GLWINDOW_H
#define GLWINDOW_H
#include <QtOpenGL/QGLWidget>
#include <QOpenGLWidget>

class GlWindow : public QGLWidget{
public:

protected:
    void initializeGL();
    void paintGL();
};

#endif // GLWINDOW_H

```

GLWindow.cpp

```
#include <GL/glew.h>
#include "GlWindow.h"
#include <iostream>

extern const char* vertexShaderCode;
extern const char* fragmentShaderCode;

void sendDataToOpenGL(){
    GLfloat verts[] = {
        +0.0f, +0.0f,
             1.0f, 0.0f, 0.0f,
        +1.0f, +1.0f,
             1.0f, 0.0f, 0.0f,
        -1.0f, +1.0f,
             1.0f, 0.0f, 0.0f,
        -1.0f, -1.0f,
             1.0f, 0.0f, 0.0f,
        +1.0f, -1.0f,
             1.0f, 0.0f, 0.0f,

    };

    GLuint verexBufferID;
    glGenBuffers(1, &verexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, verexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5,0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (char*)(sizeof(GLfloat) * 2));

    GLuint indices [] = {0,1,2, 0,3,4};
    GLuint indexBufferID;
    glGenBuffers(1,&indexBufferID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}

bool checkShaderStatus(GLuint shaderID){
    GLint compileStatus;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileStatus);

    if(compileStatus != GL_TRUE){
        std::cerr<<"failed"<<std::endl;
        GLint infoLogLength;
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar* buffer = new GLchar [infoLogLength];

        GLsizei bufferSize;
        glGetShaderInfoLog(shaderID, infoLogLength, &bufferSize, buffer);
        std::cout<<buffer<<std::endl;
        return false;
        delete []buffer;
    }
    return true;
}

bool checkProgramStatus(GLuint programID){
    GLint linkStatus;
    glGetProgramiv(programID, GL_LINK_STATUS, &linkStatus);

    if(linkStatus != GL_TRUE){
        std::cerr<<"failed"<<std::endl;
        GLint infoLogLength;
        glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLogLength);
        GLchar* buffer = new GLchar [infoLogLength];

        GLsizei bufferSize;
        glGetProgramInfoLog(programID, infoLogLength, &bufferSize, buffer);
        std::cout<<buffer<<std::endl;
        return false;

        delete []buffer;
    }
    return true;
}

void installShaders(){

    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    const char *adapter[1];
    adapter[0] = vertexShaderCode;

    glShaderSource(vertexShaderID, 1, adapter, 0);
    adapter[0] = fragmentShaderCode;
    glShaderSource(fragmentShaderID, 1, adapter, 0);

    glCompileShader(vertexShaderID);
    glCompileShader(fragmentShaderID);

    if(!checkShaderStatus(vertexShaderID)){
        std::cerr<<"Sth wrong with vertex shader"<<std::endl;
        return;
    }

    if(!checkShaderStatus(fragmentShaderID)){
       std::cerr<<"Sth wrong with fragment shader"<<std::endl;
       return;
    }

    GLuint programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);

    glLinkProgram(programID);

    if(!checkProgramStatus(programID)){
        std::cerr<<"failed to link"<<std::endl;
        return;
    }

    glUseProgram(programID);
}

void GlWindow::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0,0,width(), height());

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);

}

void GlWindow::initializeGL(){
    glewInit();

    const GLubyte *shaderVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
    std::cout<<shaderVersion<<std::endl;

    sendDataToOpenGL();
    installShaders();
}

```

and finally my shader code:

```
const char* vertexShaderCode  =
        "#version 430\r\n"
        "\n"
        "in layout(location=0) vec2 position;\n"
        "in layout(location=1) vec3 vertexColor;\n"
        ""
        "out vec3 theColor;\n"
        "\n"
        "void main()\n"
        "{\n"
        "\n"
        "\n"
        "gl_Position = vec4(position, 0.0, 1.0);\n"
        "theColor = vertexColor; \n"
        "\n"
        "}\n"
        ;

const char* fragmentShaderCode =
        "#version 430\r\n"
        "\n"
        "\n"
        "\n"
        "out vec4 daColor;\n"
        "in vec3 theColor;\n"
        "\n"
        "void main()\n"
        "{\n"
        "\n"
        " daColor = vec4(theColor,1.0);\n"
        "}\n"
        ;

```

I know that my shader code looks stupid but I'm just trying to learn.

**EDIT:** I don't know if thats relevant, but I'm using Ubuntu 15.0 and `glGetString(GL_SHADING_LANGUAGE_VERSION)` returns: `4.4 nvidia via cg compiler`.

**Tags:** opengl, shader, glsl

> **Comments:**
> **username_1**: your second `glVertexAttribPointer` doesn't look right, it's missing a *5 in the stride param  
> **username_0**: @username_1 Yes the *5 was missing, But the problem still exists.  
> **username_2**: What if you remove the `layout` stuff from the shaders?  

---

## 1 answers

### Answer by username_3 on 2015-10-20. Score: 3 | Accepted answer

There are two problems:

1. Your indices are unsigned integers (4 bytes per index) but the call to glDrawElements specifies GL\_UNSIGNED\_SHORT. You need to change this to GL\_UNSIGNED\_INT.
2. You are drawing two red triangles onto a buffer that has been cleared to red.


---

