1

I'm trying to render a square using two triangles, and then applying some transformations, but for whatever reason it isn't showing up. Here is the code:

#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 "Modern OpenGL 3D Cube"


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


GLint shaderProgram, windowWidth = 800, windowHeight = 600;
GLuint VBO, VAO, EBO, texture;


void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);




const GLchar* vertexShaderSource = GLSL(330,
    layout(location = 0) in vec3 position; 
layout(location = 1) in vec3 color;

out vec3 mobileColor; 

uniform mat4 shaderTransform;

void main() {
    gl_Position = shaderTransform * vec4(position, 1.0f); 
    mobileColor = color;
});


const GLchar* fragmentShaderSource = GLSL(330,
    in vec3 mobileColor; 
out vec4 gpuColor; 

void main() {
    gpuColor = vec4(mobileColor, 1.0);
});



//main program
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, 0, 0, 1);
    glutDisplayFunc(URenderGraphics);
    glutMainLoop();

    
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);

    return 0;
}


void UResizeWindow(int w, int h) {
    windowWidth = w;
    windowHeight = h;
    glViewport(0, 0, windowWidth, windowHeight);
}


void URenderGraphics(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glBindVertexArray(VAO);

    glm::mat4 currentTransform; 

    
    currentTransform = glm::translate(currentTransform, glm::vec3(0.0f, 0.5f, 0.0f));

    
    currentTransform = glm::rotate(currentTransform, 45.0f, glm::vec3(0.0f, 0.0f, 1.0f));

    
    currentTransform = glm::scale(currentTransform, glm::vec3(0.5f, 0.5f, 0.5f));

    
    GLuint transformLocation = glGetUniformLocation(shaderProgram, "shaderTransform");
    glUniformMatrix4fv(transformLocation, 1, GL_FALSE, glm::value_ptr(currentTransform));

    
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0); 
    glutSwapBuffers(); 
}


void UCreateShader() {
    
    GLint vertexShader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader); 

    
    GLint 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 UCreateBuffers() {
    
    GLfloat vertices[] = {
        0.5f, 0.5f, 0.0f,   1, 0, 0, //top right vertex 0
        0.5f, -0.5f, 0.0f,    0, 1, 0, //bottom right vertex 1
        -0.5f, -0.5f, 0.0f,   0, 0, 1, //bottom left vertex 2
        -0.5f, 0.5f, 0.0f,    1, 0, 1 //top left vertex 3
    };

    GLuint indices[] = {
        0, 1, 3, //triangle 1
        1, 2, 3 //triangle 2
    };

    
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    
    glBindVertexArray(VAO);

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

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

    
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1); 

    glBindVertexArray(0);
}

It compiles and runs just fine, but it only opens a blank window. No graphics are shown on that window like it normally would. The output is supposed to look like a slightly rotated square rotated about its z-axis, but nothing appears at all. All help appreciated.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
CodyT96
  • 165
  • 1
  • 3
  • 12
  • 1
    Nobody's square shows up the first time. ;) What was the last version of your program that worked correctly? In other words, spend some time trying to identify the smallest code change that resulted in unexpected behaviour. Begin at the beginning. Can you get glClearColor, glClear and glutSwapBuffers to output the clear color? Incrementally add functionality from there. – Wyck Nov 22 '20 at 01:08
  • Is the issue solved? – Rabbid76 Nov 26 '20 at 16:08
  • Nope.. I completely deleted the previous project, started a new one, typed the same source code letter for letter, and now it renders a white square that isn't transformed in any way. The code did not change a single bit.. – CodyT96 Nov 28 '20 at 23:02

2 Answers2

0

Some common things I check, when I get the infamous blank screen:

  • forgetting to call glUseProgram(shaderID) before you start passing uniforms (done)
  • pass the model matrix as an identity matrix.
  • double checking if the VBO/EBO are laid out in memory correctly (also, simplifying them during this step, ie. removing the color attribute, just pass position, and hard code the color in the fragment shader)
  • doing some error checking to see if there is a problem with the shader compilation using glGetShaderInfoLog and glGetProgramInfoLog.
jackw11111
  • 1,457
  • 1
  • 17
  • 34
0

The model matrix variable glm::mat4 currentTransform has to be initialized by the Identity matrix.

The OpenGL Mathematics (GLM) API documentation is based on OpenGL Shading Language (GLSL) and refers to The OpenGL Shading Language specification.

5.4.2 Vector and Matrix Constructors

[...] If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix's diagonal, with the remaining components initialized to 0.0.

An Identity matrix can be initialized by the single parameter 1.0:

glm::mat4 currentTransform;

glm::mat4 currentTransform(1.0f);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174