I'm currently starting an SDL / OpenGL-based project based on the code for this article . At the moment, I am trying to find the best way to encapsulate the OpenGL code of the example. My main function is as follows:
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 want to implement the following types:
1- ground 2- sky (panoramic JPEG image) 3- Building (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 array of vertices and other elements needed for OpenGL code.
It is in this class that I wanted to implement method (s) to create / compile / use the shaders needed for the video 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 split into several methods (in this case, how best to distribute this code)?
In addition, with these classes ready, what is the best way to integrate the instantiated objects of these classes in the above code? My initial idea (which I do not know is correct or best) is to instantiate the object outside the event loop, inside it call a method like "display (...)" of the class, and out of it, before deleting the context, call another class method (such as a "destroy (...)").
Anyone have any suggestions on this topic?