5
$\begingroup$

I'm trying to learn GLSL, I'm following this 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.

$\endgroup$
  • $\begingroup$ your second glVertexAttribPointer doesn't look right, it's missing a *5 in the stride param $\endgroup$ – ratchet freak Oct 15 '15 at 10:31
  • $\begingroup$ @ratchetfreak Yes the *5 was missing, But the problem still exists. $\endgroup$ – lino Oct 15 '15 at 12:53
  • $\begingroup$ What if you remove the layout stuff from the shaders? $\endgroup$ – JesseTG Oct 22 '15 at 18:47
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.