I am writing a simple graphics library in C++ that uses freeglut. I have multiple source files, one handling colors, one handling shapes, and one handling OpenGL functions. I have a base Shape Class, and shapes deriving from it (Triangle, Quad, etc.). I initialize the GL from a .cpp, and the glutDisplayFunc calls a method which iterates through a list of shapes, which in return calls the virtual flush function of triangles, quads, etc.
The thing is, when I write a test flush function inside the same .cpp with init and display functions, the triangle gets rendered. But when I call the flush functions of each shape from inside the seperate Shape.cpp file, it does not work. Is it possible that the glVertex2f's fall out of scope when used accross different source files?
//Glut_Functions.cpp
#include "Glut_Functions.h"
using namespace Cors;
static int shapeCount = 0;
static Shape** shapeList = nullptr;
void Cors::display()
{
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < Cors::shapeCount; i++)
{
Cors::shapeList[i]->flush();
}
glutSwapBuffers();
}
void Cors::init(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(700, 100);
glutInitWindowSize(800, 600);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glutCreateWindow("whatevs");
glutDisplayFunc(Cors::display);
glutIdleFunc(Cors::display);
}
//Glut_Functions.h
#ifndef GLUT_FUNCTIONS_H_INCLUDED
#define GLUT_FUNCTIONS_H_INCLUDED
#include <freeglut.h>
#include "Shape.h"
namespace Cors
{
static Shape **shapeList; //not used in this example for simplification of code.
static int shapeCount; //ditto...
void init(int*, char**);
void display(void);
}
#endif
//Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <cmath>
#include <freeglut.h>
#include "Vector.h"
#include "Color.h"
namespace Cors
{
class Shape
{
protected:
Vector position;
Vector *localVertexList;
int localVertexCount;
Color color;
public:
Shape();
Shape(Vector argPosition);
virtual void flush() = 0;
//some other functions...
};
class Triangle : public Shape
{
private:
void flush();
public:
Triangle();
Triangle(Vector argVertex0, Vector argVertex1, Vector argVertex2);
};
}
#endif
//Shape.cpp
#include "Shape.h"
using namespace Cors;
using namespace std;
//Shape Base Class
Shape::Shape() : position(0.0f, 0.0f) {}
Shape::Shape(Vector argPosition) : position(argPosition) {}
//Triangle Class
Triangle::Triangle()
{
//some stuff
}
Triangle::Triangle(Vector argVertex0, Vector argVertex1, Vector argVertex2)
{
//some other stuff
}
void Triangle::flush()
{
//The triangle flush method has been simplified for test purposes.
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.0f, 0.2f);
glEnd();
}
Note that I have posted a reduced code for simplicity, constructors and some other derived classes are excluded.
I am certain that Triangle::flush() is being called, I have put a std::cout in it and it does get invoked.
Also note that I am always welcome to any suggestions related to my coding style too :)
PS: To clear things out again, when I make a flush function inside the Glut_Functions.cpp, the triangle gets rendered. When I do it in Shape.cpp, it does not.