Public Group

# How to efficiently transform and use the resulting coordinates in later iteration opengl?

This topic is 866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a wheel that would be rotating and advancing through the x axis . The wheel is made up of a simple circle and 4 diameter lines. Now in each iteration the lines translated and rotated by a small degree , and the circle and center should be only translated along x-axis.

What would be the best way to achieve this in opengl? I have 2 methods in mind

1) create an initial array of vertices and line co-ordinates and then create two shaders for circle and the lines. And then in each iteration calculate the rotation and translation matrix and pass to the vertex shader this matrix and the initial vertices through uniform at each iteration.

2) calculate the transformation matrix once , and then in each iteration update the vertex array with the new coordinates ( I don't know how to return data from.vertex shader though) .

So which would be most efficient way of doing this animation and if it is 2 please explain how I can achieve this as I don't know how to get the vertex shader output of one iteration to use in the next iteration .

##### Share on other sites

Option 2 is very very inefficient compared to option 1. You would effectively have to do it on the cpu since you can't really return vertex data from the shader (and it would likely be in a different space if you could).

Define your wheel so that it's axis is at 0, 0 (i.e. top might be at (0, 1), right (0, 1), bottom (0, -1) and left (-1, 0). Now you rotate it using whatever method you choose (a single axis rotation is quite simple). Then you translate it to it's actual position. A rotate->translate matrix is quite neat to do when you are dealing with 2D, you could make it in one go rather than making 2 separate matrices and multiplying them. All you'd need to store is position and angle and build it from there each time one of the values changes (but no more than once per frame)

It's important you define your vertices like that and rotate first BEFORE translating as otherwise you will be rotating about the wrong point.

A matrix to do that would look like this:

cos(A)   -Sin(A)  x
sin(A)   cos(A)   y
0        0        1


Pad it out with extra zeros and a 1 (for the bottom right element) if you need a 4x4.

Depending on how you want your circle to look you can probably use the same shader for both parts (especially if it's just made of lines). As for using your points later, you wouldn't really be moving them, you'd be keeping track of just the position and angle which is then used to move the original vertices to their current position each time.

Edited by Nanoha

##### Share on other sites
Option 3) if your wheel needs to revolve once in 60 frames, create a rotation matrix for 360/60 degrees, and multiply that matrix with your wheels' transformation matrix each frame. It avoids the need for calls to sin/cos.

Really though, stop worrying. If you're starting out in games programming, the first aim should be to get *something* working. Later on you can discover ways to improve performance. Even later still, you can discover the best ways to exploit hardware performance to the full.

As a rule of thumb, always choose the simple option. Is it going to be easier to rotate 400 vertices, or modify one transformation matrix? Code optimisation is the art of always choosing the simplest option. Simpler code = faster code (generally speaking)

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631702
• Total Posts
3001810
×