Leonardo D. Mariscal
Leonardo D. Mariscal

Reputation: 82

Nothing is showing up in OpenGL

well, I have being working in java and c++ for a while, but Im new to OpenGL, so I started using a library called GLFW, I have being following a book called "OpenGL Super Bible 6th Edition" but in GLFW mode. The problem here is that I have rechecked all and watched other tutorials and my code seams to be alright but nothing from the shaders renders. I don't know if the part where I declare the shader src is okay or even a valid form.

Thank you for even read this :)

NOTE: I know it will render only a point but I resized it with "glPointSize(40.0f);".

#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 * fragmentShader_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 * vertexShader_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);
    glPointSize(40.0f);
}

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;
    glewInit ();

    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;
}

Upvotes: 0

Views: 314

Answers (1)

HolyBlackCat
HolyBlackCat

Reputation: 96013

The two variables that contain shaders' sources are named incorrectly. You've misplaced vertex source into fragmentShader_src and fragment source into vertexShader_src.

You would easily found the error if you checked shader compilation and linking status. You should add appropriate ifs and print logs if shader compilation or linking fails.

Also, you're missing an explicit OpenGL version selection. You should ask GLFW to give you 'OpenGL 4.3 compatibility profile' context. ('core profile' works too if you don't need any deprecated features.) Check GLFW docs for information how to do it.

Upvotes: 2

Related Questions