OpenGL Newbie is the best way to move objects in the scene.

I am new to OpenGL and graphical programming in general, although I have always been interested in this topic, so I have a foundation in theory.

I would like to create a scene in which many objects move. In particular, they are soccer robots on the field. Objects:

  • Lighting, field and goals that do not change
  • The ball, which is the only net that will undergo translation and rotation, but does not scale.
  • Players, each of which consists of body parts, each of which is translated and rotated to create the illusion of a connected body.

So, for my newbie GL, I would like to load these objects into the scene and then just move them. No properties of the vertices will change, neither their positioning, nor texture / normals / etc. Just transforming their "parent" object as a whole.

In addition, players have the same bodies. Can I somehow optimize loading the model into memory once, and then draw it several times using a different transformation matrix each time?

I am currently playing with OpenTK, which is a lightweight shell on top of OpenGL libraries.

Therefore, a useful answer to this question would be:

  • What parts of OpenGL give me what I need? Do I need to redraw all faces in each frame? Only those who move? Can I just update some transformation matrices? How to just do it with OpenTK? What would psuedocode look like? Or,
  • Is there a better framework free (ideally open source) and providing this level of abstraction?

Please note that I need any solution to run in .NET on multiple platforms .

+4
source share
3 answers

Using so-called vertex arrays is probably the most reliable way to optimize such a scene. Here is a good tutorial:

http://www.songho.ca/opengl/gl_vertexarray.html

A vertex array or, more generally, a gl data array stores data, such as vertex positions, normals, colors. You can also have an array containing indexes for these buffers to indicate in which order to draw them.
Then you have several closely related functions that manage these arrays, distribute them, set data for them, and draw them. You can render a complex grid with just one OpenGL command, for example glDrawElements()

These arrays are usually found in host memory. Another optimization is the use of vertex buffer objects, which are the same concept as regular arrays, but are located in the GPU memory and can be somewhat faster. Here is abit about it:

http://www.songho.ca/opengl/gl_vbo.html

Working with buffers unlike the good old glBegin() .. glEnd() has the advantage of being compatible with OpenGL ES. in OpenGL ES, arrays and buffers are the only way to draw stuff.

--- EDIT

Moving things, turning them and transforming them into a scene is performed using the View Model matrix and does not require any changes to the grid data. To illustrate:

you have initialization:

 void initGL() { // create set of arrays to draw a player // set data in them // create set of arrays for ball // set data in them } void drawScene { glMatrixMode(GL_MODEL_VIEW); glLoadIdentity(); // set up view transformation gluLookAt(...); drawPlayingField(); glPushMatrix(); glTranslate( player 1 position ); drawPlayer(); glPopMatrix(); glPushMatrix(); glTranslate( player 2 position ); drawPlayer(); glPopMatrix(); glPushMatix(); glTranslate( ball position ); glRotate( ball rotation ); drawBall(); glPopMatrix(); } 
+5
source

As you get started, I suggest sticking to the immediate rendering mode and getting it to work in the first place. If you become more comfortable, you can upgrade to vertex arrays. If you feel even more comfortable, VBOs. And finally, if you get a super convenient, instancing, which is the fastest solution for your case (no deformation, only whole transformations of objects).

If you are not trying to implement something like Fifa 2009, it is best to follow simple methods until you have an obvious performance problem. No need to give headaches prematurely.

For whole object transformations, you usually transform the model view matrix.

 glPushMatrix(); // do gl transforms here and render your object glPopMatrix(); 

To load objects, you even need to come up with some format or implement something that can load mesh formats (obj is one of the easiest formats to support). There are high-level libraries to simplify this language, but I recommend using OpenGL to gain the experience and control that you have.

+4
source

I was hoping the OpenGL API would be easy to navigate through IDE support (intellisense, etc.). After a few hours, it became obvious that some basic rules needed to be established. So I stopped typing and RTFM.

http://www.glprogramming.com/red/

The best advice I could give anyone who finds this question when they find their OpenGL framework. Long reading, but empowerment.

+1
source

Source: https://habr.com/ru/post/1314001/


All Articles