Karlos Otruba
Karlos Otruba

Reputation: 79

Using OpenGL functions in object constructor without context loaded

I have a problem with creating object before the context is initialized. My code looks something like this:

..
Vertex vertices[] = { ... } // declaration of object Vertices
Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]));
..
..
void onDisplay()
{
    ..
    mesh.Draw();
    ..
}
void init() { .. }
int main(int argc, char** argv)
{
    ..
    glutDisplayFunc(onDisplay);
    ..
}

As I have read on OpenGL site, the problem is that an Object is created before the context is initialized (because of the code in constructor). If i would put the "mesh" and "vertices" in "void init()", that would solve the problem, but I couldn't use method "mesh.Draw()" in "void onDisplay" - because the object mesh is not global, it is declared only in init().

On OpenGL site they tell about how to fix this, but I really don't understand what I should do.

The solutions from OpenGL site are these:

Solution 1 is not recommended, solution 2 wouldn't help me (at least I think not) and I have no idea how I would make solution 3

Upvotes: 1

Views: 1042

Answers (1)

genpfault
genpfault

Reputation: 52165

Change vertices and mesh to be (smart) pointers and construct the objects in main() after you secure a GL context.

If you don't want to change your existing code (. to -> for member access) you can call them verticesPtr and meshPtr and create local references (Mesh& mesh = *meshPtr;) at the top of the functions where you reference them.

Upvotes: 1

Related Questions