开发者

Using a global vertex cache and still take advantage of glRotate?

开发者 https://www.devze.com 2023-01-16 02:34 出处:网络
I have a simple Android OpenGL-ES app, and as all th开发者_高级运维e models are very simple (<10 vertices!) I have implemented a global \"world\" class that tracks all the vertices, and performs a

I have a simple Android OpenGL-ES app, and as all th开发者_高级运维e models are very simple (<10 vertices!) I have implemented a global "world" class that tracks all the vertices, and performs a single pair of GL commands on the frame rendering.

Each model object adds it's vertices to the global buffers, and these these are 'sent' to GL in one operation:

gl.glVertexBuffer(...);
gl.glDrawElements(...);

My question (perhaps an obvious answer, but I want to be sure) is, does this mean I have to do all my own rotations manually?

My base objects just define a bunch of vertices that get added to the cache, for example a triangle, a square, a pentagram, etc. My World object then takes the big array of vertices, and dumps them out to GL. If I want to rotate all those, am I correct in thinking I have to perform my own vertex coordinate manipulations (trigonometry!)?

I guess it's not the end of the world to have to create some utility functions to rotate all the vertices in my models, but I'd rather not if it's not necessary.


Yes, unfortunately that is the price to pay for performing a single draw callfor multiple models. The calculations are really quite simple, if you use the standard Scale-Rotate-Translate order. For every vertex:

  • Determine the distance to the center: Xrel = X-Xcenter and Yrel = Y-Ycenter
  • Multiply that by the scale for x and y. Xscaled = scalex*Xrel and Yscaled = scalex*Yrel.
  • Determine the new positions relative to the center after rotation: Xrot = Xscaled*cos(d)-Yscaled*sin(d) and Ynew = Xscaled*sin(d)+Yscaled*cos(d).
  • Move the vertices with the translation: Xnew = Xrot + translatex and Ynew = Yrot+translatey.

Easy! Cheers, Aert.


No, using glRotate will work fine with vertex arrays.

0

精彩评论

暂无评论...
验证码 换一张
取 消