I've been working through a few OpenGL tutorials, and decided to give myself the exercise of trying to draw a grid, with the added requirement of being able to press the -
and +
keys to change the dimensions of the grid. I've achieved what I set out to, however, it's left me with a few questions on how to go about separating some of the logic.
My biggest concern is to do with the input handling. Right now, I'm handling it like so:
Grid grid;
void key_callback(GLFWwindow* window, int key, int scanCode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_MINUS && action == GLFW_PRESS)
grid.DecrementGridSize();
if (key == GLFW_KEY_EQUAL && action == GLFW_PRESS)
grid.IncrementGridSize();
grid.Build();
glBufferData(GL_ARRAY_BUFFER, grid.Size() * sizeof(Vector3D), grid.Data(), GL_STATIC_DRAW);
}
There are several things I don't like about this:
- This is at the top of my
main.cpp
file, sogrid
is global, purely so it can be used in the input handler. - I've coupled the glBufferData() call with the input handling.
I'm not sure how to go about solving point 1, as I'm inexperienced with C++, but, for 2, I've considered the idea of having a separate Update()
method that would be called at the end of the handler, rather than calling glBufferData()
directly. Update()
would then take care of updating the contents of the buffer.
The problem I see with that is how you would pass the data from grid
to it. In my simple program, passing one set of vertices is absolutely no problem, but I'm not sure how you'd handle doing that when you have a whole bunch of different objects with their own vertices. I assume there are different patterns that already exist for handling this, so I'm more looking for some phrases I can Google really.
What can I do to improve the situation?