Danielle Hoopes
Danielle Hoopes

Reputation: 151

OpenGL not rendering colors

For some reason the colors are not rendering when I run my program. With the addition of glm, I have been running into issues with some strange images rendering on run. It might be a library, but it is highly doubtful. I have checked and rechecked my includes and libraries. I am using Eclipse.

Here is my code

/*
 * Module5.cpp
 *
 *  Created on: Aug 21, 2017
 *      Author: 
 */

#include <iostream>
#include <Gl/glew.h>
#include <GL/freeglut.h>

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


using namespace std;

#define WINDOW_TITLE "Window"

#ifndef GLSL
#define GLSL(Version, Source) "#version " #Version "\n" #Source
#endif

GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;

GLuint VBO, VAO; //Global variables for Buffer Object etc.

GLfloat cameraSpeed = 0.0005f;

GLchar currentKey;  //will store key pressed

glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 CameraUpY = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 CameraForwardZ = glm::vec3(0.0f, 0.0f, -1.0f);

void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UKeyboard(unsigned char key, int x, int y);
void UKeyReleased(unsigned char key, int x, int y);

const GLchar * vertexShaderSource = GLSL(330,

    layout(location=0) in vec3 position; //incoming data

    layout(location=1) in vec3 color;

    out vec4 mobileColor; //Attrib pointer 0
    //out vec4 colorFromVShader;

    //uniform mat4 primitiveTransform; //transform for shape

    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;

     void main()
     {
                 //gl_Position = primitiveTransform * vertex_Position; //move object on y-axis .5
                 gl_Position = projection * view * model * vec4(position, 1.0f); //move object on y-axis .5
                 //colorFromVShader = colorFromVBO;
                 mobileColor = color;
     }

);
const GLchar * fragmentShaderSource = GLSL(440,

        in vec3 mobileColor;
        out vec4 gpuColor;

         void main(){
            // gl_FragColor= vec4(1.0, 0.5, 0.0, 1.0);
            gpuColor= vec4(mobileColor, 1.0);
         }
);


//Main
int main(int argc, char* argv[])
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowSize(WindowWidth, WindowHeight);
        glutCreateWindow(WINDOW_TITLE);


        glutReshapeFunc(UResizeWindow);

        glewExperimental = GL_TRUE;

        if(glewInit() != GLEW_OK)
        {
            cout << "Failed to initialize glew!" << endl;
            return -1;
        }

        UCreateShader();

        UCreateBuffers();

        glUseProgram(shaderProgram);

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


        glutDisplayFunc(URenderGraphics);

        glutKeyboardFunc(UKeyboard);

        glutKeyboardUpFunc(UKeyReleased);

        glutMainLoop();

        glDeleteVertexArrays(1, &VAO);//cleanup
        glDeleteBuffers(1, &VBO);//cleanup



        return 0;
}
void UResizeWindow(int w, int h)
{
    WindowWidth = w;
    WindowHeight = h;
    glViewport(0, 0, WindowWidth, WindowHeight);
}

void URenderGraphics(void)
{
     glEnable(GL_DEPTH_TEST);

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clears screen

     glBindVertexArray(VAO); //activate vertex array to render the vertices that render our shape

     if(currentKey == 'w')
     cameraPosition += cameraSpeed * CameraForwardZ;

     if(currentKey == 's')
     cameraPosition -= cameraSpeed * CameraForwardZ;

     if(currentKey == 'a')
     cameraPosition -= cameraSpeed * CameraForwardZ;

     if(currentKey == 'd')
     cameraPosition += cameraSpeed * CameraForwardZ;

     glm::mat4 model;
     model = glm::translate(model,glm::vec3(0.0f, 0.0f, 0.0));
     model = glm::rotate(model, glm::radians(-45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); //rotate shape x-axis by 1.0f
     model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); //scale shape


     glm::mat4 view; //camera
     view = glm::lookAt(cameraPosition, cameraPosition + CameraForwardZ, CameraUpY); //move camera back by 5 (z)

     glm::mat4 projection;
     projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
     //projection = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f);


     GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
     GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
     GLint projLoc = glGetUniformLocation(shaderProgram, "projection");

     glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
     glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
     glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));

     //apply projection matrix
     /*

     glm::mat4 newTransform; //references 4 x 4 matrix

     newTransform = glm::translate(newTransform, glm::vec3(0.0f, 0.5f, 0.0)); //make square move up y-axis
     newTransform = glm::rotate(newTransform, glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); //rotate shape
     //newTransform = glm::scale(newTransform, glm::vec3(0.5f, 0.5f, 0.5f)); //rotate shape

     GLuint transformInfo = glGetUniformLocation(ProgramId, "primitiveTransform"); //id for shader, name of variable shader
     glUniformMatrix4fv(transformInfo, 1, GL_FALSE, glm::value_ptr(newTransform));
*/


     glutPostRedisplay();

     glDrawArrays(GL_TRIANGLES, 0 , 36);

     glBindVertexArray(0);

     glutSwapBuffers();

}
void UCreateShader()
{


    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

}
/*void applyDepthSettings() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClearDepth(1.0f);
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glShadeModel(GL_SMOOTH);
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}*/

void UCreateBuffers()
{
    //specify coords for creating square
    // Positon and Color data
        GLfloat vertices[] = {
    // Vertex Positions    // Colors
                           -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // Top Right Vertex 0
                            0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // Top Left Vertex 3
                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
                           -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,


                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,   // Top Right Vertex 0
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,     // Top Left Vertex 3
                           -0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f,
                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 0.0f,


                           -0.5f,  0.5f,  0.5f, 0.0f, 0.0f, 1.0f,   // Top Right Vertex 0
                           -0.5f,  0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // Bottom Right Vertex 1
                           -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,    // Bottom Left Vertex 2
                           -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,    // Top Left Vertex 3
                           -0.5f, -0.5f,  0.5f, 0.0f, 0.0f, 1.0f,
                           -0.5f,  0.5f,  0.5f, 0.0f, 0.0f, 1.0f,


                            0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 0.0f,   // Top Right Vertex 0
                            0.5f,  0.5f, -0.5f, 1.0f, 1.0f, 0.0f,  // Bottom Right Vertex 1
                            0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,    // Bottom Left Vertex 2
                            0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,     // Top Left Vertex 3
                            0.5f, -0.5f,  0.5f, 1.0f, 0.0f, 0.0f,
                            0.5f,  0.5f,  0.5f, 1.0f, 1.0f, 0.0f,


                           -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,  // Top Right Vertex 0
                            0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,  // Bottom Right Vertex 1
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,   // Bottom Left Vertex 2
                            0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,     // Top Left Vertex 3
                           -0.5f, -0.5f,  0.5f, 0.0f, 1.0f, 1.0f,
                           -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,

                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,   // Top Right Vertex 0
                            0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,  // Bottom Right Vertex 1
                            0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,    // Bottom Left Vertex 2
                            0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,     // Top Left Vertex 3
                           -0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 1.0f,
                           -0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 1.0f,



                               };


    //generate id's for buffer object
    glGenVertexArrays(1, &VAO); //generate for Vertex Array Object
    glGenBuffers(1, &VBO);      //generate for Vertex Buffer Object


    glBindVertexArray(VAO);      //activate text array object

    glBindBuffer(GL_ARRAY_BUFFER, VBO); //activating VBO buffer
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //pass in size of array from line 128

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
    glEnableVertexAttribArray(0);//enable vertex attribute pointer, starting position of x,y,z

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
    glEnableVertexAttribArray(1);//specify color starting point

    glBindVertexArray(0);  //deactivate vertex array object (VBO)

}
void UKeyboard(unsigned char key, GLint x, GLint y)
{
    switch(key)
    {

    case'w':
    cout<<"You pressed W!" <<endl;
    break;

    case 's':
    cout<<"You pressed S!"<<endl;
    break;

    case'a':
        cout<<"You pressed A!"<<endl;
        break;

    case 'd':
        cout<<"You pressed D!"<<endl;
    break;

    default:
        cout<<"Press a key!"<<endl;
    }
}

/*Implements the UKeyReleased function*/
void UKeyReleased(unsigned char key, GLint x, GLint y)
{
    cout<<"Key released"<<endl;
}

Upvotes: 3

Views: 916

Answers (1)

Rabbid76
Rabbid76

Reputation: 211220

Your vertex shader does not compile, because mobileColor is of type vec4 and color is of type vec3.

Change:

mobileColor = color;

to:

mobileColor = vec4(color, 1.0); 

Note, your shader program was not used, because it was not successfully built. Everything you have drawn was drawn by default OpenGL with the currently set glColor, which is by default white (1,1,1,1).

Whether the compilation of a shader succeeded can be checked by glGetShaderiv, and the error message can be retrieved with glGetShaderInfoLog:

GLint status = GL_TRUE;
glCompileShader( shaderStage );
glGetShaderiv( shaderStage, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetShaderiv( shaderStage, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen+1 );
    GLsizei written;
    glGetShaderInfoLog( shaderStage, logLen, &written, log.data() );
    std::cout << "compile error:" << std::endl << log.data() << std::endl;
}

Whether a program was linked successfully can be checked by glGetProgramiv, and the error message can be retrieved with glGetProgramInfoLog:

GLint status = GL_TRUE;
glLinkProgram( shaderProgram );
glGetProgramiv( shaderProgram, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
    GLint logLen;
    glGetProgramiv( shaderProgram, GL_INFO_LOG_LENGTH, &logLen );
    std::vector< char >log( logLen+1 );
    GLsizei written;
    glGetProgramInfoLog( shaderProgram, logLen, &written, log.data() );
    std::cout  << "link error:" << std::endl << log.data() << std::endl;
}

Upvotes: 3

Related Questions