andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Automatical OpenGL layout


 
       anddev.org - Android Development Community | Android Tutorials | Index -> Android 2D/3D Graphics - OpenGL Tutorials
Author Message
eugenk
Developer
Developer


Joined: 30 Jan 2008
Posts: 29
Location: Russian Federation, Moscow

PostPosted: Sun Feb 10, 2008 7:23 pm    Post subject: Automatical OpenGL layout Reply with quote

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.

Java:

//get screen physical size in pixels
            int w = getWidth();
            int h = getHeight();

//setup OpenGL raster area to whole screen
            gl.glViewport(0, 0, w, h);

//calculate the ratio between screen width and height (formfactor)            
            float ratio = (float)w / h;

//switch to operations with projection matrix
            gl.glMatrixMode(gl.GL_PROJECTION);

//init projection matrix with trivial matrix
            gl.glLoadIdentity();

//setup projection
            gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);


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.



viewarea.png
 Description:
viewing area picture
 Filesize:  32.16 KB
 Viewed:  1969 Time(s)

viewarea.png


Back to top
View user's profile Send private message Send e-mail
eugenk
Developer
Developer


Joined: 30 Jan 2008
Posts: 29
Location: Russian Federation, Moscow

PostPosted: Mon Feb 11, 2008 3:59 pm    Post subject: Reply with quote

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



tri2.png
 Description:
 Filesize:  23.98 KB
 Viewed:  1960 Time(s)

tri2.png


Back to top
View user's profile Send private message Send e-mail
tsipoura
Freshman
Freshman


Joined: 09 Feb 2008
Posts: 2

PostPosted: Thu Feb 21, 2008 5:12 pm    Post subject: Reply with quote

Quote:
Sorry, I'm busy a little. Be continued
Sad
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2439
Location: College Park, MD

PostPosted: Thu Feb 21, 2008 5:29 pm    Post subject: Reply with quote

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

There is someone obviously addicted to you eugene Wink

_________________
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
eugenk
Developer
Developer


Joined: 30 Jan 2008
Posts: 29
Location: Russian Federation, Moscow

PostPosted: Thu Feb 21, 2008 8:10 pm    Post subject: Reply with quote

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

There is someone obviously addicted to you eugene Wink


plusminus, sorry, sorry and once more sorry Sad 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 Smile 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 Sad
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Android 2D/3D Graphics - OpenGL Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.