Opengl code encapsulation in C++ classes

Asked

Viewed 176 times

1

I am currently starting a SDL / Opengl-based project, using the code as a basis of that Article. At the moment, I’m trying to find the best way to encapsulate the example’s Opengl code. My main function is the following:

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
    SDL_GLContext context = SDL_GL_CreateContext(window);

    //glewExperimental = GL_TRUE;
    glewInit();

    SDL_Event windowEvent;
    while (1)
    {
        if (SDL_PollEvent(&windowEvent))
        {
            if (windowEvent.type == SDL_QUIT) break;
            if (windowEvent.type == SDL_KEYUP && windowEvent.key.keysym.sym == SDLK_ESCAPE) break;
        }

        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DeleteContext(context);
    SDL_Quit();
    return 0;
}

In this code, my intention is to instantiate the Opengl objects that will represent the elements of my scene. I intend to implement the following types:

1- land 2- sky (panoramic JPEG image) 3- constructions (based on Autocad projects) 4- vehicles (also based on Autocad projects) 5- characters (Blender projects)

each element is implemented by objects that are all derived from a common class (being implemented), which will have some common methods that will return vertex array and other elements required for Opengl code.

It is in this class that I wanted to implement method(s) to create/compile/use the shaders needed for the graphics card to render the 3D object. My question is how to implement this: I put all the code in the constructor/destructor or it is better to divide it into several methods (in this case, what is the best way to distribute this code)?

Also, with these classes ready, what is the best way to integrate the instantiated objects of these classes into the code above? My initial idea (which I do not know if it is correct or the best), is to instantiate the object outside the event loop, within it call a method like "display(...)" of the class, and outside it, before the exclusion of the context, call another method of the class (like a "Destroy"(...)").

Does anyone have any suggestions on this topic?

  • Um... in a way what you want to do is build your own 3D rendering engine. Nothing wrong, and nothing against. But is there a reason for this (maybe study Opengl)? Why, from a productivity point of view, it would be better to use something already ready, like some more general graphical library like the Allegro or even a 3D engine like Unity.

  • @Luizvieira Much more than learning about Opengl, my goal is to create something optimized for the file formats I intend to use (in addition to the ones I mentioned in the topic). In another forum, someone suggested me to create classes for Shader entries and other similar items, and use these classes in the classes that represent my 3D scene elements. Would you have any suggestions of what types of Shader are used for each type of element listed above?

  • I got Kleber. Well, I’m not going to give an answer because I haven’t touched Opengl directly in a few good years. But encapsulating objects in classes is a good idea. Their standard interface can not only facilitate access to Shaders but also centralize other important issues (from a physics library, for example, including necessary definitions such as mass, dimension, etc.). My suggestion is to explore the concepts of OO well, and even look for some interesting design patterns.

  • Maybe the Bridge is useful to separate the general construction (the vertices) from the object of the Shader used (as the vertices will actually be rendered). Good luck! :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.