1

Well, as in the title the Shaders aren't doing a thing they should draw a point but it isn't appearing in the screen :/. I have being checking for solutions but it appears that they don't work. Also glfw and glew are initialising okay and the red color is appearing.

#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h> 
#include <stdio.h>
#include <iostream> 
#include "jelly/lua_manager.h"
#include "jelly/keysManager.h"

jelly::keys_buttons::KeysManager km; 

GLuint vertex_array_obj;
GLuint program;

GLuint startRender(GLFWwindow* window)
{
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    std::cout << "ASD" << std::endl;

    static const GLchar * vertexShader_src[] =
    {
        "#version 430 core                                                      \n"
        "                                                                       \n"
        "void main(void)                                                        \n"
        "{                                                                      \n"
        "   gl_Position = vec4(0, 0.5, 0.0, 1);                                 \n"
        "}                                                                      \n"
    };

    static const GLchar * fragmentShader_src[] =
    {
        "#version 430 core                          \n"
        "                                           \n"
        "out vec4 color;                            \n"
        "                                           \n"
        "void main(void)                            \n"
        "{                                          \n"
        "   color = vec4(0.0, 0.8, 1.0, 1.0);       \n"
        "}                                          \n"
    };

    glShaderSource(vertexShader, 1, vertexShader_src, NULL);
    glCompileShader(vertexShader);

    glShaderSource(fragmentShader, 1, fragmentShader_src, NULL);
    glCompileShader(fragmentShader);

    GLuint tprogram = glCreateProgram();
    glAttachShader(tprogram, vertexShader);
    glAttachShader(tprogram, fragmentShader);
    glLinkProgram(tprogram);
    glValidateProgram(tprogram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glGenVertexArrays(1, &vertex_array_obj);
    glBindVertexArray(vertex_array_obj);

    return tprogram;
}

void render(GLFWwindow* window)
{
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_POINTS, 0, 1);
}

void mouseCallback(GLFWwindow* window, int button, int action, int mods)
{
    km.mouseClick(button, action, mods);
}

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    km.keyPressed(key, action, mods);
}

int main()
{
    jelly::lua::LuaManager lm;

    // 0 = Build | 1 = Release | 2 = Alpha | 3 = Beta
    int buildType = 0;

    std::string title = "Relieved";

    if (buildType != 1)
    {
        switch (buildType) {
            case 0 :
                title += " | Build Version";
            break;
            case 2 :
                title += " | Alpha Version";
            break;
            case 3 :
                title += " | Beta Version";
            break;
            default :
            break;
        }
    }

    GLFWwindow* window;

    if (!glfwInit()) {
        glfwTerminate();
        return -1;
    }

    window = glfwCreateWindow(640, 400, title.c_str(), NULL, NULL);

    if (!window) {
        glfwTerminate();  
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
      fprintf(stderr, "Error: %s\n", glewGetErrorString(err));  
    }

    glLoadIdentity();

    program = startRender(window);
    glUseProgram(program);

    glfwSetKeyCallback(window, keyCallback);
    glfwSetMouseButtonCallback(window, mouseCallback);

    while(!glfwWindowShouldClose(window))
    {
        render(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &vertex_array_obj);
    glDeleteProgram(program);
    glDeleteVertexArrays(1, &vertex_array_obj);

    glfwTerminate();    

    return 0;
}
  • Have you checked if the shaders are compiling and linking correctly? – BDL Dec 27 '15 at 20:30
  • glValidateProgram(tprogram);? or how do I check if they are linking correctly? – Leonardo D. Mariscal Dec 27 '15 at 20:41
  • `glValidateProgram` does NOT check whether a shader was successfully build. It checks whether a shader can be RUN in the current state. Check [this page](http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/) to see how you can query compilation and linking status. – BDL Dec 27 '15 at 20:49
  • 1
    Just double checking: You're completely sure that it's not rendering? You're only drawing a single pixel, which can be hard to spot on a high resolution display. – Reto Koradi Dec 27 '15 at 22:22
  • @RetoKoradi well, here in the post I forgot to put the line "glPointSize(40.0f);" but it should do the trick for the small pixel, but still not working :/. And I will read BDL comment – Leonardo D. Mariscal Dec 27 '15 at 23:00
  • @BDL Oh, I think I found the problem, I programming in a mac but it only supports until opengl 4.1 and well, I don't know why but it says that "ERROR: 0:1: '' : version '410' is not supported" So, please can you help me setting up the version glfw should use? – Leonardo D. Mariscal Dec 27 '15 at 23:15
  • 1
    According to [this](http://antongerdelan.net/opengl/hellotriangle.html) and [this](http://www.glfw.org/docs/latest/compat.html) this commands should do the trick: `glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);` This will create OGL 3.2 core context. – mafian89 Dec 29 '15 at 12:05

1 Answers1

1

You are creating your vertexarray

glGenVertexArrays(1, &vertex_array_obj);
glBindVertexArray(vertex_array_obj);

but there is not data in it. You need to add some data so that the shaders are fired (even if the value is hardcoded in the vertex shader, as it is your case)

So, add some vertices to your vertex array like this:

std::vector<float> v({0.0f,0.0f,0.0f}); 

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, v.size() *sizeof(float), &v[0], GL_STATIC_DRAW); //v is a std::vector<float>, it size is the size of the data (buffer), and &v[0] gives the pointer of the data

glEnableVertexAttribArray(0);       //layout 0. In this example, position
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(
0,                  // layout in shader
3,                  // number of elements
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  
reinterpret_cast<void*>(0)
);

A good way to send data is to pack everything in a std::vector and just arrange the layouts

GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, v.size() *sizeof(float), &v[0], GL_STATIC_DRAW); //v is a std::vector<float>, it size is the size of the data (buffer), and &v[0] gives the pointer of the data

const int s[] = { 3, 3, 2 };        //size of data, in my case 3 floats for position, 3 for normals and 2 for texture coordinates
size_t accum = 0;
for (int z = 0; z < 3; ++z){
glEnableVertexAttribArray(z);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(
z,                  // layout in shader
s[z],                  // number of elements
GL_FLOAT,           // type
GL_FALSE,           // normalized?
8 * sizeof(float),                  // stride (3+3+2)
reinterpret_cast<void*>(accum*sizeof(float))    //shift from the previous data [3,3,2]
);
accum += s[z];
}

Read about vertex arrays here: https://stackoverflow.com/a/17517103/5729376

Community
  • 1
  • 1
  • Vertex arrays are not required. If no vertex array is enabled for an attribute, the current value of the attribute is used. In this code, the vertex shader has no inputs in the first place, so no attributes are needed at all. – Reto Koradi Dec 30 '15 at 07:01