Solitary Reaper
Solitary Reaper

Reputation: 11

GLFW window only displaying Clear Color and nothing else

I have issue in that the window is displaying the clear color i.e. (Orange) I tried all answers on the web but they didn't seem to help me. I'm using GLFW 3.3 precompiled binaries. I'm using GLAD for OpenGL functions.

The code does not give any errors or warnings and also glGetError() returns 0 at all points of the code.

I've already tried changing the order and gDEBugger gives me error (that is, PRIV_INSTRUCTION) and the VAO and VBO tabs are empty.

Here's my code


#define GLFW_DLL

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

using namespace std;

static const char* vshSrc = "#version 330 core \n layout (location = 0) in vec3 tPos; void main(){gl_Position = tPos.x, tPos.y, tPos.z;} \0";
static const char* fshSrc = "#version 330 core \n layout (location = 0) out vec4 FragOut; void main(){FragOut = vec4(1.0f,0.4f,0.3f,1.0f);} \0";


/** Processes the input.
    *Edit the fuction to add more features. */
void getInput( GLFWwindow *win ){
    if(glfwGetKey(win,GLFW_KEY_ESCAPE) == GLFW_PRESS){
            glfwSetWindowShouldClose(win,true);
    }
}


int main( int argc, char **argv )
{
        //Initialization
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
        glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

        //Make compatible for MacOSX
        #ifdef __APPLE__
            glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
        #endif // __APPLE__


        //Window creation

        GLFWwindow* window = glfwCreateWindow(640,480,"Illuminati",NULL,NULL);
        if (window == NULL)
        {
            cout << "Window creation failed" << endl;
            return -1; 
        }


        //GLAD

        glfwMakeContextCurrent(window);

        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            cout << "GLAD load error." << endl;
            return -1;
        }

        //glViewport( 0, 0, 640, 480);


//==============================================================//

        //Shader programs


        //Vertex Shader
        unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vshSrc, NULL);
        glCompileShader(vertexShader);

        //Check for compile errors
        int compilationSuccesful;

        char InfoLog[512];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful ){
                    glGetShaderInfoLog(vertexShader, 512, NULL, InfoLog);
                    cout << "Vertex Shader compilation Failed. ERROR. \n \n" << InfoLog << endl;
        }

        //Fragment Shader
        unsigned int fragmentShader;
        fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fshSrc, NULL);
        glCompileShader(fragmentShader);

        //error checking for fragment shader
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful ){
                    glGetShaderInfoLog(fragmentShader, 512, NULL, InfoLog);
                    cout << "Fragment Shader compilation Failed. ERROR. \n \n" << InfoLog << endl;
        }

        //Shader Program

        unsigned int shaderProgram;
        shaderProgram = glCreateProgram();

        //Shader attachments
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);

        //Checking for link errors
        glGetProgramiv(shaderProgram, GL_LINK_STATUS, &compilationSuccesful);
        if ( !compilationSuccesful ){
                    glGetProgramInfoLog(shaderProgram, 512, NULL, InfoLog);
                    cout << "The program refused to link. ERROR. \n \n" << InfoLog << endl;
        }

        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);

//----------------------------------------------------------------------------------------------------------//      

        //Vertex data and elements

        float rect[ ] {

            /* 0 bottom left*/       -0.5f, -0.5f,  0.0f,

            /* 1  top left*/             -0.5,   0.5f,  0.0f,

            /* 2 bottom right*/      0.5f,   -0.5f, 0.0f,

            /* 3 top right*/             0.5f,   0.5f,  0.0f

            };

        unsigned int indices[] {

            0,1,2,  //first triangle
            1,3,2  //second triangle

        };


//==============================================================//  

        //Buffers and VAO

        //VAO
        unsigned int VAO;
        glGenVertexArrays(1, &VAO);
        glBindVertexArray(VAO);

        //VBO
        unsigned int VBO;
        glGenBuffers(1,&VBO);

        //EBO
        unsigned int EBO;
        glGenBuffers(1, &EBO);



        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(rect), rect, GL_STATIC_DRAW);


        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);


//================================================================//

        //Vertex Attributes

        //glBindVertexArray(0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);

        //for safety
        //glBindBuffer(GL_ARRAY_BUFFER, 0);

        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        //A useless variable
        //unsigned int ui;

//===============================================================//

        //Render Loop


        while ( !glfwWindowShouldClose(window) )
        {

            //Clear the buffer
            glClearColor(0.9f,0.7f,0.2f,1.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            ////////////////////////////////////////////////////////////////////////////////////////////////
            //////////////////////////////////////////DRAWING//////////////////////////////////////////


            glBindVertexArray(VAO);
            glUseProgram(shaderProgram);
            //glDrawArrays(GL_TRIANGLES, 0, 4);
            glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT, 0);
            glBindVertexArray(0);
            //////////////////////////////////////////////END/////////////////////////////////////////////

            getInput(window);
            glfwSwapBuffers(window);
            glfwPollEvents();
        }

        //Exit and destructors

            glfwTerminate();
            return 0;
    }

//End

/*
       __
   <( .   )____
    /_______/
~~~~~~~~~
~~~~~~~~~

   */


Upvotes: 1

Views: 429

Answers (1)

datenwolf
datenwolf

Reputation: 162317

In your shader this expression will try to assign the value of tPos.z and only tPos.z to gl_Position

gl_Position = tPos.x, tPos.y, tPos.z;

The reason for that is the semantics of the Comma operator, where all expressions delimited by , are evaluated, but only the result of the very last one is used as the whole expression's R-value.

You probably just want to swizzle it, also you must assign .w:

gl_Position = vec4(tPos.xyz, 1);

Upvotes: 4

Related Questions