Tamim Addari
Tamim Addari

Reputation: 7841

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

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 .

Upvotes: 0

Views: 110

Answers (1)

Hugh Fisher
Hugh Fisher

Reputation: 2516

The best way to do anything in OpenGL is usually to make the code as simple and clear as possible. This is also the most efficient way about 97% of the time (Knuth) because a program that works is better than one that doesn't. Modern CPU/GPUs are so fast that even "slow" code is usually good enough.

Comparing the two approaches, the first keeps the original data and transforms it each time. The second approach would replace the original data by the new transformed values. Because computer maths has finite precision, there's a chance that small errors will accumulate over time and distort the geometry. So I would recommend the first.

If you want to try the second approach, you get the vertex shader outputs by rendering with transform feedback enabled, where the results aren't drawn but instead stored in one or more VBOs you specify. Chapter 7 in the 6th edition SuperBible explains how to do it.

Hope this helps.

Upvotes: 2

Related Questions