🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Adding Multiple Objects in 1 Scene OpenGL

Started by
10 comments, last by zX_Flame_Xz 3 years ago

I'm having trouble adding two objects onto one OpenGL scene. What are the basic steps when it comes to adding multiple objects in a scene. I have to add two eyes to a skull, and parent it to each other. I'm a beginner, so if you could explain in baby steps, that'd be nice.

Advertisement

The link to the files: https://drive.google.com/drive/folders/1T3IrFMRx9cdDpoJOEDpqdDvdC_NxqTqN?usp=sharing

If you are just toying around with OGL, you can draw your meshes in the order you need it. Lets say you want your eyes be part of the skull so you first create the modelview matrix for your skull. The matrix is usually a 4x4 Matrix witch is the product of your skulls position in the world and the camera. Then you draw your skull with that matrix.

Next step is to calculate the modelview matrix for each of your eyes. To do that, you perform the same computation of location in 3D space and camera but you also add the skull's modelview matrix to the multiplication. By doing this, you are moving the pivot point of the eyes from center of the world (0,0,0) to center of the skull. So your eyes are relative to the parent mesh.

There is usually a system involved which does all those parenting transitions and calculation for you. The system is called a Scene Graph and everything drawn in your scene is a node of that graph. The graph then computes from the root node (usually the scene) down to every leaf node, the final matrix needed to place the nodes in the scene properly.

In professional software, this looks for example like this

Could you go over the functions I have to use, I'm new to OpenGL and the tutorials are confusing.

Need more info first:

  • What programming language do you use?
  • What OpenGL version do you use (anything up to 3.3 or “modern” GL 3.5+)?

Because this changes how you handle scene configurations. For example in legacy GL 2.0 you had the functions glPushMatrix and glPopMatrix to keep matrices for further computation and glTranslate, glRotate, glScale to modify the current matrix while in modern GL 3.5 and beyond, you need to use a shader to render (the functions I mentioned are depricated and won't work anymore) and so you need to perform all the matrix calculations on your own (and even implement the linear algebra code on your own when not using a 3rd party library)

Its latest one, I just downloaded it for class

I sent a link with the files

Looks like you already have a framework for setting up your skull model and rendering it, why wouldn't you be able to repeat the same step for the eye models?

It's rednering one eyeball nd I can't change the size of the eyeball by itself, I only have one projection matrix, so I think that this is the problem

The projection matrix has nothing to do with object placement. It is responsible finally to bring your pixels onto the screen, as the name says, to project a 3D point to a flat 2D surface.

What you need is to define the model-view matrices for every object you want to render. So starting with the skull and the identity matrix, you add translation, rotation and scale to that matrix

This places your object at the right location, rotation and scale without affecting anything else. Then you need a second matrix for the 1st eye and do the same but finally multiply the result by the model-view matrix of the skull, this translates the origin of the eye from your game world center (0, 0, 0) to the skull as parent. So keep in mind that it isn't game world center anymore and you need to translate, rotate and scale based on the skull, not the origin! Repeat that process with the other eye and you'll get the “scene graph”.

This is called “local transform”, if you now want to have the skull for example always look into the camera, you need to also apply “global transform” to it. I'd do this with another matrix for simplicity.

zX_Flame_Xz said:
Its latest one, I just downloaded it for class

I assume that you then have a shader to display everything on screen. Add 4 matrices to your shader in order to place the meshes properly in your game world:

  • Projection Matrix
  • Camera Matix
  • Model-View Matrix
  • Global Transform Matrix
uniform mat4 projection;
uniform mat4 camera;
uniform mat4 modelView;
uniform mat4 transform;
  
void main()
{
  //calculate mesh origin (like exported from Blender) * transform (the final placement in the world) * camera (where the user currently is) * projection (to map 3D to screen space coords)
  gl_Position = (((modelView * transform) * camera) * projection) * vec4(vertexPosition_modelspace,1);
}

This topic is closed to new replies.

Advertisement