OpenGL/GLFW C++ program doesn't draw anything












-2















So, I made this program that has three main files:




  • a main.cpp file, which contains the program itself

  • one which contains GLObject class, which is the base class for every object

  • one that contains the Ellipse class, a derivated object


While I was implementing the object class (before I only had the ellipse one, then I decided to make a rectangle and found some pieces in common) I run into a problem, which is that in the screen nothing appears.



The GLObject.cpp contains the definitions of the "GLObjectsInit()" functions, which sets up the drawing program (draw_program) and all the offsets and matrix strides of the unfiorm block declared inside that program.



void GLObjectsInit()
{
CompileDrawProgram(); // Compiles the draw_program

coord_loc = glGetAttribLocation(draw_program, "coord");
color_loc = glGetAttribLocation(draw_program, "color");

static const GLchar* uniformNames[4] = {
"TransformBlock.res",
"TransformBlock.tsl",
"TransformBlock.rot",
"TransformBlock.scale"
};
GLuint uniformIndices[4];
glGetUniformIndices(draw_program, 4, uniformNames, uniformIndices);

GLint uniformOffsets[4];
GLint matrixStrides[4];
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_MATRIX_STRIDE, matrixStrides);

resolution_offset = uniformOffsets[0];
translation_offset = uniformOffsets[1];
rotation_matrix_offset = uniformOffsets[2];
rotation_matrix_stride = uniformOffsets[2];
scale_offset = uniformOffsets[3];
}


Drawing program:



Vertex shader:



#version 450 core
in vec4 coord;
in vec4 color;
layout(std140, binding = 0) uniform TransformBlock
{
vec4 res;
vec4 tsl;
vec4 scale;
mat4 rot;
} trs;

out vec4 color_vs;
void main(void)
{
color_vs = color;
gl_Position = ((coord * trs.scale * trs.rot) + trs.tsl) * trs.res;
}


Fragment shader:



#version 450 core

out vec4 color;
in vec4 color_vs;

void main(void)
{
color = color_vs;
}


The .h files contains only the headers to include and the declaration of some static variables (declared as static in order to avoid a multiple definition error):



#define _USE_MATH_DEFINES
#include <math.h>
#include <glm.hpp>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

static GLuint draw_program;

static GLuint coord_loc, color_loc;
static GLuint resolution_offset, translation_offset, scale_offset;
static GLuint rotation_matrix_offset, rotation_matrix_stride;


The ellipse object has some methods to modify the object position and scale and also has a draw() method (I will write only the draw() method and the constructor function, because I don't think the other ones are involved in the problem).



Ellipse.h:



#include "GLObject.h"

class Ellipse//: public GLObject
{
protected:
GLuint points_buffer, uniforms_buffer, vao;
vertex* points;
unsigned int size;
bool fill;

double ang;
glm::mat4 rotation_matrix;
glm::vec4 center, scale;

public:
void Draw();

//Other methods

Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1));
Ellipse(double r, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1)) :
Ellipse(r, r, cen, fill, col) {}

~Ellipse();
};


Ellipse.cpp:



#include "Ellipse.h"
using namespace std;

void Ellipse::Draw()
{
GLFWwindow* window = glfwGetCurrentContext();

glUseProgram(draw_program);
glBindVertexArray(vao);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniforms_buffer);

int w, h; glm::vec4 res;
glfwGetFramebufferSize(window, &w, &h);
glViewport(0, 0, w, h);

if (w > h) res = glm::vec4(static_cast<float>(h) / w, 1, 1, 1);
else res = glm::vec4(1, static_cast<float>(w) / h, 1, 1);

glNamedBufferSubData(uniforms_buffer, resolution_offset, sizeof(glm::vec4), &res);

if (fill) glDrawArrays(GL_TRIANGLES, 0, size);
else glDrawArrays(GL_LINES, 0, size);
}

Ellipse::Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col)
{
// Calculate the ellipse

glCreateVertexArrays(1, &vao);

glCreateBuffers(1, &uniforms_buffer);
glNamedBufferStorage(uniforms_buffer, sizeof(GLfloat) * 112, NULL, GL_DYNAMIC_STORAGE_BIT);

glCreateBuffers(1, &points_buffer);
glNamedBufferStorage(points_buffer, sizeof(vertex) * size, points, GL_DYNAMIC_STORAGE_BIT);
glVertexArrayVertexBuffer(vao, 0, points_buffer, 0, sizeof(vertex));

glVertexArrayAttribBinding(vao, coord_loc, 0);
glVertexArrayAttribFormat(vao, coord_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, coord));
glEnableVertexArrayAttrib(vao, coord_loc);

glVertexArrayAttribBinding(vao, color_loc, 0);
glVertexArrayAttribFormat(vao, color_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, color));
glEnableVertexArrayAttrib(vao, color_loc);

center = cen; ang = 0;
scale = glm::vec4(w, h, 1, 1);
rotation_matrix = glm::mat4(1);

glNamedBufferSubData(uniforms_buffer, scale_offset, sizeof(glm::vec4), &scale);
glNamedBufferSubData(uniforms_buffer, translation_offset, sizeof(glm::vec4), &center);
glNamedBufferSubData(uniforms_buffer, rotation_matrix_offset, sizeof(glm::mat4), &rotation_matrix[0]);
}

Ellipse::~Ellipse()
{
glDeleteBuffers(1, &points_buffer);
glDeleteBuffers(1, &uniforms_buffer);
glDeleteVertexArrays(1, &vao);
delete points;
}


The main is only that:



#include "Ellipse.h"
using namespace std;

int main()
{
glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

GLFWwindow* window = glfwCreateWindow(500, 500, "Title", NULL, NULL);
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glfwSwapInterval(1);

GLObjectsInit();
Ellipse ball1(0.25, 0.25, glm::vec4(0, 0, 0.5, 1), true);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
ball1.Draw();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();

return 0;
}









share|improve this question




















  • 2





    Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

    – hidefromkgb
    Nov 21 '18 at 19:17






  • 1





    That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

    – tadman
    Nov 21 '18 at 19:18











  • @hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

    – Riccardo Ripanti
    Nov 21 '18 at 20:01











  • @tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

    – Riccardo Ripanti
    Nov 21 '18 at 20:03











  • You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

    – Rabbid76
    Nov 21 '18 at 20:42
















-2















So, I made this program that has three main files:




  • a main.cpp file, which contains the program itself

  • one which contains GLObject class, which is the base class for every object

  • one that contains the Ellipse class, a derivated object


While I was implementing the object class (before I only had the ellipse one, then I decided to make a rectangle and found some pieces in common) I run into a problem, which is that in the screen nothing appears.



The GLObject.cpp contains the definitions of the "GLObjectsInit()" functions, which sets up the drawing program (draw_program) and all the offsets and matrix strides of the unfiorm block declared inside that program.



void GLObjectsInit()
{
CompileDrawProgram(); // Compiles the draw_program

coord_loc = glGetAttribLocation(draw_program, "coord");
color_loc = glGetAttribLocation(draw_program, "color");

static const GLchar* uniformNames[4] = {
"TransformBlock.res",
"TransformBlock.tsl",
"TransformBlock.rot",
"TransformBlock.scale"
};
GLuint uniformIndices[4];
glGetUniformIndices(draw_program, 4, uniformNames, uniformIndices);

GLint uniformOffsets[4];
GLint matrixStrides[4];
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_MATRIX_STRIDE, matrixStrides);

resolution_offset = uniformOffsets[0];
translation_offset = uniformOffsets[1];
rotation_matrix_offset = uniformOffsets[2];
rotation_matrix_stride = uniformOffsets[2];
scale_offset = uniformOffsets[3];
}


Drawing program:



Vertex shader:



#version 450 core
in vec4 coord;
in vec4 color;
layout(std140, binding = 0) uniform TransformBlock
{
vec4 res;
vec4 tsl;
vec4 scale;
mat4 rot;
} trs;

out vec4 color_vs;
void main(void)
{
color_vs = color;
gl_Position = ((coord * trs.scale * trs.rot) + trs.tsl) * trs.res;
}


Fragment shader:



#version 450 core

out vec4 color;
in vec4 color_vs;

void main(void)
{
color = color_vs;
}


The .h files contains only the headers to include and the declaration of some static variables (declared as static in order to avoid a multiple definition error):



#define _USE_MATH_DEFINES
#include <math.h>
#include <glm.hpp>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

static GLuint draw_program;

static GLuint coord_loc, color_loc;
static GLuint resolution_offset, translation_offset, scale_offset;
static GLuint rotation_matrix_offset, rotation_matrix_stride;


The ellipse object has some methods to modify the object position and scale and also has a draw() method (I will write only the draw() method and the constructor function, because I don't think the other ones are involved in the problem).



Ellipse.h:



#include "GLObject.h"

class Ellipse//: public GLObject
{
protected:
GLuint points_buffer, uniforms_buffer, vao;
vertex* points;
unsigned int size;
bool fill;

double ang;
glm::mat4 rotation_matrix;
glm::vec4 center, scale;

public:
void Draw();

//Other methods

Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1));
Ellipse(double r, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1)) :
Ellipse(r, r, cen, fill, col) {}

~Ellipse();
};


Ellipse.cpp:



#include "Ellipse.h"
using namespace std;

void Ellipse::Draw()
{
GLFWwindow* window = glfwGetCurrentContext();

glUseProgram(draw_program);
glBindVertexArray(vao);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniforms_buffer);

int w, h; glm::vec4 res;
glfwGetFramebufferSize(window, &w, &h);
glViewport(0, 0, w, h);

if (w > h) res = glm::vec4(static_cast<float>(h) / w, 1, 1, 1);
else res = glm::vec4(1, static_cast<float>(w) / h, 1, 1);

glNamedBufferSubData(uniforms_buffer, resolution_offset, sizeof(glm::vec4), &res);

if (fill) glDrawArrays(GL_TRIANGLES, 0, size);
else glDrawArrays(GL_LINES, 0, size);
}

Ellipse::Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col)
{
// Calculate the ellipse

glCreateVertexArrays(1, &vao);

glCreateBuffers(1, &uniforms_buffer);
glNamedBufferStorage(uniforms_buffer, sizeof(GLfloat) * 112, NULL, GL_DYNAMIC_STORAGE_BIT);

glCreateBuffers(1, &points_buffer);
glNamedBufferStorage(points_buffer, sizeof(vertex) * size, points, GL_DYNAMIC_STORAGE_BIT);
glVertexArrayVertexBuffer(vao, 0, points_buffer, 0, sizeof(vertex));

glVertexArrayAttribBinding(vao, coord_loc, 0);
glVertexArrayAttribFormat(vao, coord_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, coord));
glEnableVertexArrayAttrib(vao, coord_loc);

glVertexArrayAttribBinding(vao, color_loc, 0);
glVertexArrayAttribFormat(vao, color_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, color));
glEnableVertexArrayAttrib(vao, color_loc);

center = cen; ang = 0;
scale = glm::vec4(w, h, 1, 1);
rotation_matrix = glm::mat4(1);

glNamedBufferSubData(uniforms_buffer, scale_offset, sizeof(glm::vec4), &scale);
glNamedBufferSubData(uniforms_buffer, translation_offset, sizeof(glm::vec4), &center);
glNamedBufferSubData(uniforms_buffer, rotation_matrix_offset, sizeof(glm::mat4), &rotation_matrix[0]);
}

Ellipse::~Ellipse()
{
glDeleteBuffers(1, &points_buffer);
glDeleteBuffers(1, &uniforms_buffer);
glDeleteVertexArrays(1, &vao);
delete points;
}


The main is only that:



#include "Ellipse.h"
using namespace std;

int main()
{
glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

GLFWwindow* window = glfwCreateWindow(500, 500, "Title", NULL, NULL);
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glfwSwapInterval(1);

GLObjectsInit();
Ellipse ball1(0.25, 0.25, glm::vec4(0, 0, 0.5, 1), true);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
ball1.Draw();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();

return 0;
}









share|improve this question




















  • 2





    Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

    – hidefromkgb
    Nov 21 '18 at 19:17






  • 1





    That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

    – tadman
    Nov 21 '18 at 19:18











  • @hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

    – Riccardo Ripanti
    Nov 21 '18 at 20:01











  • @tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

    – Riccardo Ripanti
    Nov 21 '18 at 20:03











  • You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

    – Rabbid76
    Nov 21 '18 at 20:42














-2












-2








-2








So, I made this program that has three main files:




  • a main.cpp file, which contains the program itself

  • one which contains GLObject class, which is the base class for every object

  • one that contains the Ellipse class, a derivated object


While I was implementing the object class (before I only had the ellipse one, then I decided to make a rectangle and found some pieces in common) I run into a problem, which is that in the screen nothing appears.



The GLObject.cpp contains the definitions of the "GLObjectsInit()" functions, which sets up the drawing program (draw_program) and all the offsets and matrix strides of the unfiorm block declared inside that program.



void GLObjectsInit()
{
CompileDrawProgram(); // Compiles the draw_program

coord_loc = glGetAttribLocation(draw_program, "coord");
color_loc = glGetAttribLocation(draw_program, "color");

static const GLchar* uniformNames[4] = {
"TransformBlock.res",
"TransformBlock.tsl",
"TransformBlock.rot",
"TransformBlock.scale"
};
GLuint uniformIndices[4];
glGetUniformIndices(draw_program, 4, uniformNames, uniformIndices);

GLint uniformOffsets[4];
GLint matrixStrides[4];
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_MATRIX_STRIDE, matrixStrides);

resolution_offset = uniformOffsets[0];
translation_offset = uniformOffsets[1];
rotation_matrix_offset = uniformOffsets[2];
rotation_matrix_stride = uniformOffsets[2];
scale_offset = uniformOffsets[3];
}


Drawing program:



Vertex shader:



#version 450 core
in vec4 coord;
in vec4 color;
layout(std140, binding = 0) uniform TransformBlock
{
vec4 res;
vec4 tsl;
vec4 scale;
mat4 rot;
} trs;

out vec4 color_vs;
void main(void)
{
color_vs = color;
gl_Position = ((coord * trs.scale * trs.rot) + trs.tsl) * trs.res;
}


Fragment shader:



#version 450 core

out vec4 color;
in vec4 color_vs;

void main(void)
{
color = color_vs;
}


The .h files contains only the headers to include and the declaration of some static variables (declared as static in order to avoid a multiple definition error):



#define _USE_MATH_DEFINES
#include <math.h>
#include <glm.hpp>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

static GLuint draw_program;

static GLuint coord_loc, color_loc;
static GLuint resolution_offset, translation_offset, scale_offset;
static GLuint rotation_matrix_offset, rotation_matrix_stride;


The ellipse object has some methods to modify the object position and scale and also has a draw() method (I will write only the draw() method and the constructor function, because I don't think the other ones are involved in the problem).



Ellipse.h:



#include "GLObject.h"

class Ellipse//: public GLObject
{
protected:
GLuint points_buffer, uniforms_buffer, vao;
vertex* points;
unsigned int size;
bool fill;

double ang;
glm::mat4 rotation_matrix;
glm::vec4 center, scale;

public:
void Draw();

//Other methods

Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1));
Ellipse(double r, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1)) :
Ellipse(r, r, cen, fill, col) {}

~Ellipse();
};


Ellipse.cpp:



#include "Ellipse.h"
using namespace std;

void Ellipse::Draw()
{
GLFWwindow* window = glfwGetCurrentContext();

glUseProgram(draw_program);
glBindVertexArray(vao);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniforms_buffer);

int w, h; glm::vec4 res;
glfwGetFramebufferSize(window, &w, &h);
glViewport(0, 0, w, h);

if (w > h) res = glm::vec4(static_cast<float>(h) / w, 1, 1, 1);
else res = glm::vec4(1, static_cast<float>(w) / h, 1, 1);

glNamedBufferSubData(uniforms_buffer, resolution_offset, sizeof(glm::vec4), &res);

if (fill) glDrawArrays(GL_TRIANGLES, 0, size);
else glDrawArrays(GL_LINES, 0, size);
}

Ellipse::Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col)
{
// Calculate the ellipse

glCreateVertexArrays(1, &vao);

glCreateBuffers(1, &uniforms_buffer);
glNamedBufferStorage(uniforms_buffer, sizeof(GLfloat) * 112, NULL, GL_DYNAMIC_STORAGE_BIT);

glCreateBuffers(1, &points_buffer);
glNamedBufferStorage(points_buffer, sizeof(vertex) * size, points, GL_DYNAMIC_STORAGE_BIT);
glVertexArrayVertexBuffer(vao, 0, points_buffer, 0, sizeof(vertex));

glVertexArrayAttribBinding(vao, coord_loc, 0);
glVertexArrayAttribFormat(vao, coord_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, coord));
glEnableVertexArrayAttrib(vao, coord_loc);

glVertexArrayAttribBinding(vao, color_loc, 0);
glVertexArrayAttribFormat(vao, color_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, color));
glEnableVertexArrayAttrib(vao, color_loc);

center = cen; ang = 0;
scale = glm::vec4(w, h, 1, 1);
rotation_matrix = glm::mat4(1);

glNamedBufferSubData(uniforms_buffer, scale_offset, sizeof(glm::vec4), &scale);
glNamedBufferSubData(uniforms_buffer, translation_offset, sizeof(glm::vec4), &center);
glNamedBufferSubData(uniforms_buffer, rotation_matrix_offset, sizeof(glm::mat4), &rotation_matrix[0]);
}

Ellipse::~Ellipse()
{
glDeleteBuffers(1, &points_buffer);
glDeleteBuffers(1, &uniforms_buffer);
glDeleteVertexArrays(1, &vao);
delete points;
}


The main is only that:



#include "Ellipse.h"
using namespace std;

int main()
{
glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

GLFWwindow* window = glfwCreateWindow(500, 500, "Title", NULL, NULL);
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glfwSwapInterval(1);

GLObjectsInit();
Ellipse ball1(0.25, 0.25, glm::vec4(0, 0, 0.5, 1), true);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
ball1.Draw();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();

return 0;
}









share|improve this question
















So, I made this program that has three main files:




  • a main.cpp file, which contains the program itself

  • one which contains GLObject class, which is the base class for every object

  • one that contains the Ellipse class, a derivated object


While I was implementing the object class (before I only had the ellipse one, then I decided to make a rectangle and found some pieces in common) I run into a problem, which is that in the screen nothing appears.



The GLObject.cpp contains the definitions of the "GLObjectsInit()" functions, which sets up the drawing program (draw_program) and all the offsets and matrix strides of the unfiorm block declared inside that program.



void GLObjectsInit()
{
CompileDrawProgram(); // Compiles the draw_program

coord_loc = glGetAttribLocation(draw_program, "coord");
color_loc = glGetAttribLocation(draw_program, "color");

static const GLchar* uniformNames[4] = {
"TransformBlock.res",
"TransformBlock.tsl",
"TransformBlock.rot",
"TransformBlock.scale"
};
GLuint uniformIndices[4];
glGetUniformIndices(draw_program, 4, uniformNames, uniformIndices);

GLint uniformOffsets[4];
GLint matrixStrides[4];
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets);
glGetActiveUniformsiv(draw_program, 4, uniformIndices, GL_UNIFORM_MATRIX_STRIDE, matrixStrides);

resolution_offset = uniformOffsets[0];
translation_offset = uniformOffsets[1];
rotation_matrix_offset = uniformOffsets[2];
rotation_matrix_stride = uniformOffsets[2];
scale_offset = uniformOffsets[3];
}


Drawing program:



Vertex shader:



#version 450 core
in vec4 coord;
in vec4 color;
layout(std140, binding = 0) uniform TransformBlock
{
vec4 res;
vec4 tsl;
vec4 scale;
mat4 rot;
} trs;

out vec4 color_vs;
void main(void)
{
color_vs = color;
gl_Position = ((coord * trs.scale * trs.rot) + trs.tsl) * trs.res;
}


Fragment shader:



#version 450 core

out vec4 color;
in vec4 color_vs;

void main(void)
{
color = color_vs;
}


The .h files contains only the headers to include and the declaration of some static variables (declared as static in order to avoid a multiple definition error):



#define _USE_MATH_DEFINES
#include <math.h>
#include <glm.hpp>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>

static GLuint draw_program;

static GLuint coord_loc, color_loc;
static GLuint resolution_offset, translation_offset, scale_offset;
static GLuint rotation_matrix_offset, rotation_matrix_stride;


The ellipse object has some methods to modify the object position and scale and also has a draw() method (I will write only the draw() method and the constructor function, because I don't think the other ones are involved in the problem).



Ellipse.h:



#include "GLObject.h"

class Ellipse//: public GLObject
{
protected:
GLuint points_buffer, uniforms_buffer, vao;
vertex* points;
unsigned int size;
bool fill;

double ang;
glm::mat4 rotation_matrix;
glm::vec4 center, scale;

public:
void Draw();

//Other methods

Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1));
Ellipse(double r, glm::vec4 cen, bool fill, glm::vec4 col = glm::vec4(1, 1, 1, 1)) :
Ellipse(r, r, cen, fill, col) {}

~Ellipse();
};


Ellipse.cpp:



#include "Ellipse.h"
using namespace std;

void Ellipse::Draw()
{
GLFWwindow* window = glfwGetCurrentContext();

glUseProgram(draw_program);
glBindVertexArray(vao);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniforms_buffer);

int w, h; glm::vec4 res;
glfwGetFramebufferSize(window, &w, &h);
glViewport(0, 0, w, h);

if (w > h) res = glm::vec4(static_cast<float>(h) / w, 1, 1, 1);
else res = glm::vec4(1, static_cast<float>(w) / h, 1, 1);

glNamedBufferSubData(uniforms_buffer, resolution_offset, sizeof(glm::vec4), &res);

if (fill) glDrawArrays(GL_TRIANGLES, 0, size);
else glDrawArrays(GL_LINES, 0, size);
}

Ellipse::Ellipse(double w, double h, glm::vec4 cen, bool fill, glm::vec4 col)
{
// Calculate the ellipse

glCreateVertexArrays(1, &vao);

glCreateBuffers(1, &uniforms_buffer);
glNamedBufferStorage(uniforms_buffer, sizeof(GLfloat) * 112, NULL, GL_DYNAMIC_STORAGE_BIT);

glCreateBuffers(1, &points_buffer);
glNamedBufferStorage(points_buffer, sizeof(vertex) * size, points, GL_DYNAMIC_STORAGE_BIT);
glVertexArrayVertexBuffer(vao, 0, points_buffer, 0, sizeof(vertex));

glVertexArrayAttribBinding(vao, coord_loc, 0);
glVertexArrayAttribFormat(vao, coord_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, coord));
glEnableVertexArrayAttrib(vao, coord_loc);

glVertexArrayAttribBinding(vao, color_loc, 0);
glVertexArrayAttribFormat(vao, color_loc, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, color));
glEnableVertexArrayAttrib(vao, color_loc);

center = cen; ang = 0;
scale = glm::vec4(w, h, 1, 1);
rotation_matrix = glm::mat4(1);

glNamedBufferSubData(uniforms_buffer, scale_offset, sizeof(glm::vec4), &scale);
glNamedBufferSubData(uniforms_buffer, translation_offset, sizeof(glm::vec4), &center);
glNamedBufferSubData(uniforms_buffer, rotation_matrix_offset, sizeof(glm::mat4), &rotation_matrix[0]);
}

Ellipse::~Ellipse()
{
glDeleteBuffers(1, &points_buffer);
glDeleteBuffers(1, &uniforms_buffer);
glDeleteVertexArrays(1, &vao);
delete points;
}


The main is only that:



#include "Ellipse.h"
using namespace std;

int main()
{
glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

GLFWwindow* window = glfwCreateWindow(500, 500, "Title", NULL, NULL);
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glfwSwapInterval(1);

GLObjectsInit();
Ellipse ball1(0.25, 0.25, glm::vec4(0, 0, 0.5, 1), true);

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
ball1.Draw();
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();

return 0;
}






c++ opengl glfw glew






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 19:45







Riccardo Ripanti

















asked Nov 21 '18 at 18:24









Riccardo RipantiRiccardo Ripanti

143




143








  • 2





    Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

    – hidefromkgb
    Nov 21 '18 at 19:17






  • 1





    That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

    – tadman
    Nov 21 '18 at 19:18











  • @hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

    – Riccardo Ripanti
    Nov 21 '18 at 20:01











  • @tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

    – Riccardo Ripanti
    Nov 21 '18 at 20:03











  • You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

    – Rabbid76
    Nov 21 '18 at 20:42














  • 2





    Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

    – hidefromkgb
    Nov 21 '18 at 19:17






  • 1





    That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

    – tadman
    Nov 21 '18 at 19:18











  • @hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

    – Riccardo Ripanti
    Nov 21 '18 at 20:01











  • @tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

    – Riccardo Ripanti
    Nov 21 '18 at 20:03











  • You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

    – Rabbid76
    Nov 21 '18 at 20:42








2




2





Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

– hidefromkgb
Nov 21 '18 at 19:17





Thought #1: You are requesting OpenGL v2.0 but are trying to make it compile and run shaders designed for OpenGL 4.5 (at least the #version directive suggests so).

– hidefromkgb
Nov 21 '18 at 19:17




1




1





That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

– tadman
Nov 21 '18 at 19:18





That is quite a mountain of code to review. Can you cut that down to size, something more minimal we can inspect?

– tadman
Nov 21 '18 at 19:18













@hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

– Riccardo Ripanti
Nov 21 '18 at 20:01





@hidefromkgb Are the glfwWindowHint() functions that specify that? cause I didn't really understand their meaning and if it is like that, how should I write? (anyway that's not the problem, cause it was previously working with that)

– Riccardo Ripanti
Nov 21 '18 at 20:01













@tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

– Riccardo Ripanti
Nov 21 '18 at 20:03





@tadman I tried to write only the most important things, probably the only thing I can remove is the Ellipse constructor, but then you have to trust me when I say I initialized all the buffers correctly

– Riccardo Ripanti
Nov 21 '18 at 20:03













You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

– Rabbid76
Nov 21 '18 at 20:42





You specify vertex coordinates with tuple size of 4 (x, y, z, w - homogeneous coordinates) - glVertexArrayAttribFormat(vao, coord_loc, 4, ...). How do you initialize the w component of the vertices?

– Rabbid76
Nov 21 '18 at 20:42












1 Answer
1






active

oldest

votes


















1














After many tries I finally found what the problem was:



In the GLObjects.h file I declare some variables as static, because, if I don't do so, it generates some multiple definition errors. Declaring them as static solves that problem, but creates another one, because now each file has it's own copy of those variables.



So what I did, instead, to solve the problem, is to declare those variables as extern, and then define them in the GLObject.cpp file.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418369%2fopengl-glfw-c-program-doesnt-draw-anything%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    After many tries I finally found what the problem was:



    In the GLObjects.h file I declare some variables as static, because, if I don't do so, it generates some multiple definition errors. Declaring them as static solves that problem, but creates another one, because now each file has it's own copy of those variables.



    So what I did, instead, to solve the problem, is to declare those variables as extern, and then define them in the GLObject.cpp file.






    share|improve this answer




























      1














      After many tries I finally found what the problem was:



      In the GLObjects.h file I declare some variables as static, because, if I don't do so, it generates some multiple definition errors. Declaring them as static solves that problem, but creates another one, because now each file has it's own copy of those variables.



      So what I did, instead, to solve the problem, is to declare those variables as extern, and then define them in the GLObject.cpp file.






      share|improve this answer


























        1












        1








        1







        After many tries I finally found what the problem was:



        In the GLObjects.h file I declare some variables as static, because, if I don't do so, it generates some multiple definition errors. Declaring them as static solves that problem, but creates another one, because now each file has it's own copy of those variables.



        So what I did, instead, to solve the problem, is to declare those variables as extern, and then define them in the GLObject.cpp file.






        share|improve this answer













        After many tries I finally found what the problem was:



        In the GLObjects.h file I declare some variables as static, because, if I don't do so, it generates some multiple definition errors. Declaring them as static solves that problem, but creates another one, because now each file has it's own copy of those variables.



        So what I did, instead, to solve the problem, is to declare those variables as extern, and then define them in the GLObject.cpp file.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 11:40









        Riccardo RipantiRiccardo Ripanti

        143




        143
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418369%2fopengl-glfw-c-program-doesnt-draw-anything%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?