1
votes

can someone for goodness sake tell me what is wrong with this code, it is not showing a simple cube with simple vertex and fragment shader, It seems that I've initialized gl and GLFW correctly, created shaders comiled them and linked them, created model view projection matrix, and passed them to the shader, I cannot see why it is not rendering.

#include <iostream>
#include <fstream>

#include <GL\glew.h>
#include <GLFW\glfw3.h>

#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\type_ptr.hpp>

using namespace std;
using namespace glm;

int g_gl_width = 800;
int g_gl_height = 600;
GLFWwindow* g_window = NULL;

void glfw_error_callback(int error, const char* description) {
    cerr << description << endl;
}

void glfw_window_size_callback(GLFWwindow* window, int width, int height) {
    g_gl_width = width;
    g_gl_height = height;
}

void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{

}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{

}

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

    glfwSetErrorCallback(glfw_error_callback);
    if (!glfwInit()) {
        cerr << "ERROR: could not start GLFW3\n" << endl;
        return false;
    }
    g_window = glfwCreateWindow(
        g_gl_width, g_gl_height, "Extended Init.", NULL, NULL
        );
    if (!g_window) {
        cerr << "ERROR: could not open window with GLFW3\n" << endl;
        glfwTerminate();
        return false;
    }
    glfwSetWindowSizeCallback(g_window, glfw_window_size_callback);
    glfwMakeContextCurrent(g_window);
    glfwSetMouseButtonCallback(g_window, mouse_button_callback);
    glfwSetScrollCallback(g_window, scroll_callback);

    glfwWindowHint(GLFW_SAMPLES, 4);

    glewExperimental = GL_TRUE;
    glewInit();

    const GLubyte* renderer = glGetString(GL_RENDERER); 
    const GLubyte* version = glGetString(GL_VERSION);
    cout << "Renderer: " << renderer;
    cout << "OpenGL version supported: " << version;

    glEnable(GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
    glEnable(GL_CULL_FACE); // cull face
    glCullFace(GL_BACK); // cull back face

    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glViewport(0, 0, g_gl_width, g_gl_height);

    GLfloat points[] = {
        -1.0f, -1.0f, -1.0f, // triangle 1 : begin
        -1.0f, -1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f, // triangle 1 : end
        1.0f, 1.0f, -1.0f, // triangle 2 : begin
        -1.0f, -1.0f, -1.0f,
        -1.0f, 1.0f, -1.0f, // triangle 2 : end

        1.0f, -1.0f, 1.0f,
        -1.0f, -1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,
        1.0f, 1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f, -1.0f,

        -1.0f, -1.0f, -1.0f,
        -1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, -1.0f,
        1.0f, -1.0f, 1.0f,
        -1.0f, -1.0f, 1.0f,
        -1.0f, -1.0f, -1.0f,

        -1.0f, 1.0f, 1.0f,
        -1.0f, -1.0f, 1.0f,
        1.0f, -1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, -1.0f, -1.0f,
        1.0f, 1.0f, -1.0f,

        1.0f, -1.0f, -1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, -1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, -1.0f,
        -1.0f, 1.0f, -1.0f,

        1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, -1.0f,
        -1.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f,
        -1.0f, 1.0f, 1.0f,
        1.0f, -1.0f, 1.0f
    };

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(
        GL_ARRAY_BUFFER,
        3 * 36 * sizeof(GLfloat),
        points,
        GL_STATIC_DRAW
        );
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);

    const char* vertex_shader =
        "#version 410\n"
        "layout(location = 0) in vec3 vertex_position;\n"
        "uniform mat4 model, view, projection;\n"
        "void main() {\n"
        "   gl_Position = projection * view * model * vec4(vertex_position, 1.0);\n"
        "}\n";

    const char* fragment_shader =
        "#version 410\n"
        "void main() {\n"
        "   gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0);\n"
        "}\n";

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

    mat4 projection = glm::perspective(90.0f, (float)(g_gl_width / g_gl_height), 0.1f, 100.0f);

mat4 view = glm::mat4(1.0f);
view = glm::translate(view, vec3(0.0f, 0.0f, 0.0f));
view = glm::rotate(view, 0.0f, vec3(0.0f, 0.0f, 1.0f));
view = glm::rotate(view, 0.0f, vec3(0.0f, 1.0f, 0.0f));
view = glm::rotate(view, 0.0f, vec3(1.0f, 0.0f, 0.0f));

    mat4 model = glm::mat4(1.0f);
    model = glm::translate(model, vec3(0.0f, 0.0f, 0.0f));
    model = glm::rotate(model, 0.0f, vec3(0.0f, 0.0f, 1.0f));
    model = glm::rotate(model, 0.0f, vec3(0.0f, 1.0f, 0.0f));
    model = glm::rotate(model, 0.0f, vec3(1.0f, 0.0f, 0.0f));

    int model_mat_location = glGetUniformLocation(shader_programme, "model");
    glUniformMatrix4fv(model_mat_location, 1, GL_FALSE, glm::value_ptr(model));
    int view_mat_location = glGetUniformLocation(shader_programme, "view");
    glUniformMatrix4fv(view_mat_location, 1, GL_FALSE, glm::value_ptr(view));
    int proj_mat_location = glGetUniformLocation(shader_programme, "projection");
    glUniformMatrix4fv(proj_mat_location, 1, GL_FALSE, glm::value_ptr(projection));
    glUseProgram(shader_programme);

    while (!glfwWindowShouldClose(g_window)) {
        glfwMakeContextCurrent(g_window);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glViewport(0, 0, g_gl_width, g_gl_height);
        glBindVertexArray(vao);
        glUseProgram(shader_programme);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        if (GLFW_PRESS == glfwGetKey(g_window, GLFW_KEY_ESCAPE)) {
            glfwSetWindowShouldClose(g_window, 1);
        }
        glfwPollEvents();
        glfwSwapBuffers(g_window);
    }
    glfwTerminate();
    return 0;
}

EDIT:

I also wanted to point out that I had another error in the code where

mat4 view = glm::mat4(1.0f);
glm::translate(view, vec3(0.0f, 0.0f, 0.0f));
glm::rotate(view, 0.0f, vec3(0.0f, 0.0f, 1.0f));
glm::rotate(view, 0.0f, vec3(0.0f, 1.0f, 0.0f));
glm::rotate(view, 0.0f, vec3(1.0f, 0.0f, 0.0f));

should have been:

mat4 view = glm::mat4(1.0f);
view = glm::translate(view, vec3(0.0f, 0.0f, 0.0f));
view = glm::rotate(view, 0.0f, vec3(0.0f, 0.0f, 1.0f));
view = glm::rotate(view, 0.0f, vec3(0.0f, 1.0f, 0.0f));
view = glm::rotate(view, 0.0f, vec3(1.0f, 0.0f, 0.0f));

and model also.

1

1 Answers

2
votes

The problem is probably in this code here.

int model_mat_location = glGetUniformLocation(shader_programme, "model");
glUniformMatrix4fv(model_mat_location, 1, GL_FALSE, glm::value_ptr(model));
int view_mat_location = glGetUniformLocation(shader_programme, "view");
glUniformMatrix4fv(view_mat_location, 1, GL_FALSE, glm::value_ptr(view));
int proj_mat_location = glGetUniformLocation(shader_programme, "projection");
glUniformMatrix4fv(proj_mat_location, 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(shader_programme);

You need to call glUseProgram before you attempt to make any changes to the uniform values. location variables are independently assigned for each shader, which means for program 1, the locations will be in the range [0,1,2,3,...], and for program 2, the locations will also be in the range [0,1,2,3,...]. Because of this, calling glUniform* without an active program means that OpenGL doesn't know which program you're updating the uniforms for, and thus doesn't know how to apply the values you're passing.

Also, you might need to change this code as well:

mat4 view = glm::mat4(1.0f);
glm::translate(view, vec3(0.0f, 0.0f, 0.0f));
glm::rotate(view, 0.0f, vec3(0.0f, 0.0f, 1.0f));
glm::rotate(view, 0.0f, vec3(0.0f, 1.0f, 0.0f));
glm::rotate(view, 0.0f, vec3(1.0f, 0.0f, 0.0f));

While there's nothing fundamentally wrong with this code, this is going to put the camera inside the cube. With face culling enabled, you probably won't be able to actually see anything, since all the faces will be facing away from you. For testing purposes, consider using glm::lookAt.

mat4 view = glm::lookAt(vec3(3, 3, 1), vec3(0, 0, 0), vec3(0, 1, 0));