Automatical OpenGL layout

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Automatical OpenGL layout

Postby eugenk » Sun Feb 10, 2008 7:23 pm

Suppose that we have some complex object, consists of several colored 3D cubes for example. The number of cubes may be vary. Nevertheless, this object is only a part of scene, and must occupy only predefined screen area. How to place it to screen automatically, with manner independent with number of colored 3D cubes, in object ?

Remember about one of OpenGL basics - about projections. Look at start of onDraw() method in Colored 3D cube example.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //get screen physical size in pixels
  3.  
  4.             int w = getWidth();
  5.  
  6.             int h = getHeight();
  7.  
  8.  
  9.  
  10. //setup OpenGL raster area to whole screen
  11.  
  12.             gl.glViewport(0, 0, w, h);
  13.  
  14.  
  15.  
  16. //calculate the ratio between screen width and height (formfactor)            
  17.  
  18.             float ratio = (float)w / h;
  19.  
  20.  
  21.  
  22. //switch to operations with projection matrix
  23.  
  24.             gl.glMatrixMode(gl.GL_PROJECTION);
  25.  
  26.  
  27.  
  28. //init projection matrix with trivial matrix
  29.  
  30.             gl.glLoadIdentity();
  31.  
  32.  
  33.  
  34. //setup projection
  35.  
  36.             gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);
  37.  
  38.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The last call is very important. It setups viewing area. glFrustumf() method has the next semantics:
glFrustumf (float left, float right, float bottom, float top, float near, float far)
where left, right, top, bottom, near, far - corresponding bounds of viewing area. So, viewing area is some truncated pyramid, oblique-angled in general case(see picture).
Eye (or camera) is placed in the beginning of coordinates (0,0,0) or in imagine top of this truncated pyramid.
X-axes goes from left to right.
Y - from bottom to top.
Z - from near to far.

If right = - left and top = - bottom, pyramid will be symmetric. Else it will be oblique-angled. In colored 3D cube tutorial left=- ratio, right=ratio, bottom=-1. top=1, near=2, far=12. Hence pyramid is symmetric.

As you can see on the picture, any surface {Z=const} will be a plane, parallel near (z=near] and far(z=far) planes. Particularly near plane is a screen surface, or projection plane. Namely to near plane relays left, right, bottom and top parameters. It sign that if point lays at near plane (has coordinates (x,y,near)), then if x=left then point lays on left screen side, if x=right - on right, if y=bottom on bottom, if y=top on top. This allows to calculate screen coordinates of point that has any value of z.

Be continued tomorrow.
Attachments
viewarea.png
viewing area picture
viewarea.png (32.16 KiB) Viewed 7030 times
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Top

Postby eugenk » Mon Feb 11, 2008 3:59 pm

Try to calculate screen coordinates of point, with any Z-coordinate between near and far. Suppose for simplicity, that it's Y-coordinate is zero. I.e. point has coordinates (X0, 0, Z0). Dissect our pyramid (picture in previous post) with plane {y=0} (see attached picture). On picture, point OZ is coordinates beginning op plane {Z=Z0}, and ON is coordinates beginning in {Z=near}. Remember, that {Z=near} plane is our physical screen. Hence point ON represents screen coordinates beginning.


Sorry, I'm busy a little. Be continued
Attachments
tri2.png
tri2.png (23.98 KiB) Viewed 7021 times
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Postby tsipoura » Thu Feb 21, 2008 5:12 pm

Sorry, I'm busy a little. Be continued
:(
tsipoura
Freshman
Freshman
 
Posts: 2
Joined: Sat Feb 09, 2008 8:14 pm

Postby plusminus » Thu Feb 21, 2008 5:29 pm

tsipoura wrote:
Sorry, I'm busy a little. Be continued
:(

There is someone obviously addicted to you eugene ;)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby eugenk » Thu Feb 21, 2008 8:10 pm

plusminus wrote:
tsipoura wrote:
Sorry, I'm busy a little. Be continued
:(

There is someone obviously addicted to you eugene ;)


plusminus, sorry, sorry and once more sorry :( I have not so many time before April 14. But I decide to win. Hardly decide to do it... Today have just upgrade SDK. And application does not works with it:( Now learn about API changes. Maybe week or two and I'll work out at least draft game version. Then continue this topic and begin to say about other interesting things :) Unfortunately my English is not very well, and I spend some time and efforts to translate from Russian texts which I write... But time and efforts now is critical factor for me :(
eugenk
Developer
Developer
 
Posts: 29
Joined: Wed Jan 30, 2008 8:07 pm
Location: Russian Federation, Moscow

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests