0

I'm new to opengl, install GLFW and GLEW by following https://medium.com/@bhargav.chippada/how-to-setup-opengl-on-mingw-w64-in-windows-10-64-bits-b77f350cea7e, I'm using mingw and geany as Text Editor and created one Makefile to build and run code. I have create one opengl program where I'm able to draw one 2d triangle with GLSL. Everything seems working fine. Then I install GLM, I am able to install it, when I try to add GLM code, there is no compilation/build error. But when I try to execute program, it is displaying blank screen.

My CPP is given below:


#include <stdio.h>
#include <string.h>

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

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

const GLint WIDTH = 920, HEIGHT = 600;

GLuint VAO, VBO, shader, uniformModel;

bool direction = true;
float triOffset = 0.0f;
float triMaxOffset = 0.7f;
float triIncrement = 0.0005f;

// Vertex Shader
const GLchar* vShader = R"glsl(
#version 330 core

layout(location = 0) in vec3 pos;

uniform mat4 model;

void main(){
   gl_Position = model * vec4(0.4 *pos.x, 0.4 * pos.y, pos.z, 1.0);
}
)glsl";

// Fragment
const GLchar * fShader = R"glsl(
#version 330 core

out vec4 color;

void main(){
  color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
)glsl";


void CreateTriangle() {
    // Set up vertex data (and buffer(s)) and attribute pointers
    GLfloat vertices[] = {
        -1.0f, -1.0f, 0.f, // left
         1.0f, -1.0f, 0.f, // right
         0.0f,  1.0f, 0.f, // top
    };

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
            glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}

void CompileShaders() {
    shader = glCreateProgram();
    /* shader compilation code */
    uniformModel = glGetUniformLocation(shader, "model");
}

int main () {

    /* GLFW initialization */

    CreateTriangle();
    CompileShaders();

        // Define a loop which terminates when the window is closed
    while(!glfwWindowShouldClose(window)) {
        // clean screen
        //for handling events such as keyboard input
        glfwPollEvents();
        
        if(direction) {
            triOffset += triIncrement; 
        } else {
            triOffset -= triIncrement;
        }

        if(abs(triOffset) >= triMaxOffset) {
            direction = !direction;
        }

        glUseProgram(shader);

        glm::mat4 model;
        model = glm::translate(model, glm::vec3(triOffset, 0.0f, 0.0f));
        glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
    
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);
        glUseProgram(0);
        
        //Swap the draw buffer
    }

    // Properly de-allocate all resource once they've outlive
    
}

Makefile to build code

CC=g++

OPENGL=-lopengl32 -lglew32 -lglfw3 -lglu32 -lgdi32 

$(file) = main
build:
    $(CC) -v $(file)  -o output $(OPENGL) -Wall -Werror
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
sonnet
  • 15
  • 4

1 Answers1

0

Change the following line:

glm::mat4 model;

To

glm::mat4 model(1.0f);

It should work.

sabbir
  • 2,020
  • 3
  • 26
  • 48