Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Need help calculating a frustum with a fixed centralized plane

This thread is locked; no one can reply to it. rss feed Print
Need help calculating a frustum with a fixed centralized plane
Mark Oates
Member #1,146
March 2001
avatar

What’s working:

So I have a basic tile map that is 25 tiles across and 15 tiles high. The tiles themselves are 16x16 pixels making the full size of the tilemap 400x240 in pixels. On a display that is always assumed to be 1920x1080, that 400x240 tilemap is scaled up by 4.8 x 4.5 in order to fill the screen (with some negligible distortion).

Problem:

The above works for a 2D projection, however, I want this arrangement to exist within a 3D projection, such that other tilemaps can be drawn at different levels of depth, in front of the main tile map and/or behind it.

Most importantly, I’d like to be able to flexibly experiment with the depth of field, (and possibly the positions of the near and far planes), while still having the primary 400x240px tile map set in a fixed position and not have it distort as depth of field is being changed. Also, being able to “zoom in” to different scales than 4.8 x 4.5 would be a good feature, if I wanted to add focusing effects on certain parts of the action on the tile map.

As an example, I found this picture online and, as an analogy, you might imagine the z plane is in the position where the dimensions are always fixed at 1920x1080 regardless of the change in depth of field, near and far planes:

{"name":"613166","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6df9ab1a817e10d51c3d8f2a49935e4f.jpg","w":405,"h":203,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6df9ab1a817e10d51c3d8f2a49935e4f"}613166

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

frank.n
Member #16,879
July 2018

Hmm, not sure if I understand correctly.
I think I hacked together something related to have animation effects (rotation around X / Y axes) in lgui tests / demo:

https://github.com/frank256/lgui/blob/develop/src/tests/lguitest.cpp#L254

Note sure if I did it the "correct" way, but basically it puts the usual 2D screen on a plane on z=0 within a frustum so that you can draw as you would in 2D, however, different Z coordinates lead to some (slight) perspective effects.

It's been a while, would need to wrap my head around it again.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Hi frank n o/

Oates - basically you take 1920x1080 and set it as your width and height. you'll determine the proper depth to render to in a moment.

First, you need your horizontal field of view and your aspect ratio. Typical is 90^ and 1.33333 for a quarter view on a 4 by 3 screen.

These determine your depth, which is the desired view 'width' divided by the tangent of HFOV/2.0 . This is the desired depth to render to to make everything look like it was normal size on the tilemap.

So now you need parameters to pass to al_perspective_transform. l and r and t and b are just half your width and height to either side of the origin. Eg. -960,960 and -540,540 to center 1920x1080 on the screen. The only other parameters you need are the near and far clipping planes, but they are arbitrary in size depending on the field of view. They can be set to any reasonable near plane for the camera to have space behind whatever it is looking at. The far plane can just be again, another reasonable value say some small multiple of the 'screen' depth.

Mark Oates
Member #1,146
March 2001
avatar

frank.n said:

I think I hacked together something related to have animation effects (rotation around X / Y axes) in lgui tests / demo:

Ok, yea this actually works quite seamlessly!

Quote:

Note sure if I did it the "correct" way, but basically it puts the usual 2D screen on a plane on z=0 within a frustum so that you can draw as you would in 2D, however, different Z coordinates lead to some (slight) perspective effects.

Yea this is basically exactly what I'm looking for. I thought I'd have to figure it out myself but your code worked for me out of the box and the comments were very nice.

I only had to tweak the w / h variables to 1920x1080, and then I only needed to change the Z_DIST_FACTOR to expand/contract the focal length depth effect. I did have to add the depth rendering flags to get the depth buffer:

      // before display creation:
      al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 32, ALLEGRO_SUGGEST);

      // before renders:
      al_set_render_state(ALLEGRO_DEPTH_TEST, true);
      al_clear_depth_buffer(1000);

Not only did it work well, it also worked seamlessly with the different camera control strategies that I had in place. So zooming in/out, changing the camera position to lock to different "rooms", etc, in the game integrated perfectly.

Thank you :)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Go to: