# OpenGL Detachable hierarchical objects

## Recommended Posts

I'm assuming only rotations and translations are used in this. The easiest way to have a hierarchical object is to just store local transforms in the hierarchy, and in rendering the usual OpenGL transforms can be applied then popped when going up the hierarchy. But what if I want to have objects be able to detach and reattach to/from the hierarchy? When detaching, the object should be in world coordinates, with a rotation (quaternion) and translation. How do I extract that from the combined transforms of the hierarchy? Reattaching seems even more complex to me, as in I'm not at all clear how to approach the issue. Suggestions are greatly appreciated.

##### Share on other sites
Quote:
 Original post by PruneHow do I extract that from the combined transforms of the hierarchy?

Just start at the root and multiply the matrices until you get to the leaf (just like OpenGL does). Then store the result in the object's orientation matrix.

To attach just set your local matrix to be = identity * attachPointOffsetMatrix.

-me

##### Share on other sites
Quote:
 Original post by PalidineJust start at the root and multiply the matrices until you get to the leaf (just like OpenGL does). Then store the result in the object's orientation matrix.

How would I extract from that matrix a single translation and single rotation to represent it, when that matrix has resulted from a composition of rotations around different points and directions? I need to extract this since I need to do physical simulation on the detached object.

Quote:
 To attach just set your local matrix to be = identity * attachPointOffsetMatrix.

But, as above, how would I find the local rotation that, when transformed by the sequence of transforms of the hierarchy, makes the object have the same orientation in world coordinate basis as when it was detached?

If hierarchy and global are something like this
R1*T1*R2*T2*R3*T3 <--> R0*T0
First case is how do I decompose the multiplied matrices from the hierarchy into R0 and T0.
Second case is, how do I find R3 and T3... OK the translation seems easy, but what about R3?

##### Share on other sites
Quote:
 Original post by PruneHow would I extract from that matrix a single translation and single rotation to represent it, when that matrix has resulted from a composition of rotations around different points and directions? I need to extract this since I need to do physical simulation on the detached object.

I'm not sure I see the problem. If you can render it you have the necessary matrices. Just multiply them together starting from the root and you'll end up with the appropriate world-relative transform for the attached object

Quote:
 Original post by PruneBut, as above, how would I find the local rotation that, when transformed by the sequence of transforms of the hierarchy, makes the object have the same orientation in world coordinate basis as when it was detached?

This, I suppose is a bit more complex

First, to back up let's make sure we've got the correct architecture:

Root - world-relative matrix (orientation plus translation
|
|
Child - Root-relative orientation matrix (includes root relative translation)
|
|
Child of Child - Child-relative orientation matrix (includes child-relative translation)

So to detach Child of Child:

matrix = rootMatrix * childMatrix * childOfChildMatrix
this now contains the proper world-relative orientation and translation. this is how you're already rendering it so it shouldn't look confusing.

To attach child of child back (now I'm unsure of this [smile]):

matrix = invRootMatrix * invChildMatrix * childOfChildMatrix;
If I'm not mistaken this will transform Child of Child back into Child relative object space (which is what you want).

[EDIT: a little shaky on the math but what you want is inverse(rootMatrix * childMatrix) * childOfChildMatrix. I believe that what I wrote above is equivalent but my matrix math is a tad weak [smile]. If that doesn't work, what you want to do conceptually is: when detaching transform CoC from child relative space into world-relative space; when attaching transform CoC from world-relative space into child-relative space]

-me

##### Share on other sites
OK, that makes sense.
From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation. I assume that if a transform matrix was composed of only translation and rotation matrix multiplications, I can use the usual matrix to axis angle conversion on the 3x3 submatrix?

##### Share on other sites
Quote:
 Original post by PruneOK, that makes sense.From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation. I assume that if a transform matrix was composed of only translation and rotation matrix multiplications, I can use the usual matrix to axis angle conversion on the 3x3 submatrix?

I guess... I mean you'll always need to work with the world-relative matrix representation to extract the axis-angle or euler angles.

But, why do you need to extract anything? You'll have the complete orientation matrix, that's all you need to render or perform game logic. You can directly load a matrix in both OpenGL and DirectX for rendering, you don't need axis-angle representations.

-me

##### Share on other sites
I need it for some simulation and animation stuff where it's most convenient.

##### Share on other sites
Quote:
 Original post by PruneOK, that makes sense.From the composed matrix resulting from the multiplications, I know translation is just entries 13, 14, and 15, but I also need to extract the rotation...

You're determined to think in terms of translations and rotations. Stop it. For this sort of thing, all that matters is the 4x4 affine transformation matrix. Translations and rotations are merely an artifact of the transformation. Multiply two transformations, and you get the composition of the two transformations. If you like, you can extract translation and rotation from that, though for most purposes it's not necessary.

##### Share on other sites
It may not matter for rendering, but when specifying a keyframe in an animation to/from which to interpolate it's very useful. It's easier for me to specify, be at such and such angle at this point, then interpolate the rotation to get there from a previous one; likewise for translation. I'm also mixing such animation with simplified simulation in other parts of the timeline where again an orientation conceptually represented in terms of the object's axes which are aligned with the object in physically meaningful ways is useful.

##### Share on other sites
Quote:
 Original post by PruneIt may not matter for rendering, but when specifying a keyframe in an animation to/from which to interpolate it's very useful. It's easier for me to specify, be at such and such angle at this point, then interpolate the rotation to get there from a previous one; likewise for translation. I'm also mixing such animation with simplified simulation in other parts of the timeline where again an orientation conceptually represented in terms of the object's axes which are aligned with the object in physically meaningful ways is useful.

But all you're doing is complicating the math that is more easily done with matrices. What Sneftel is saying is that you already have the perfect tool for this job: a matrix. That you don't yet know the math is irrelivant. Better to learn the appropriate tool than whittling down your square peg so it can go in the round hole. =)

For interpolation you may find quaternions more elegant; but they are easily generated from that same matrix you already have.

A corollary would be trying to determine the angle between a unit's facing orientation and it's target using trigonometry instead of just the dot product of the two vectors. Sure it'll work, but it gets confusing fast and is not easy to implement.

-me

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627740
• Total Posts
2978883
• ### Similar Content

• Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
So, here's what the plan is so far as far as loading goes:
Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!

• I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.

• 10
• 10
• 21
• 14
• 13