Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  GUI->Foreground Transform3D solution  (Read 503 times)
0 Members and 1 Guest are viewing this topic.
Offline JeramieHicks

Senior Newbie




Java games rock!


« Posted 2004-10-25 21:55:26 »

Just wanted to give ya'll food for thought, since it took me a couple days to work this out.

I'm designing a GUI system that works on a Foreground node at a distance of -0.6f from the camera.

First, using the screenToFG() function I previously figured out:

1  
2  
3  
4  
5  
6  
7  
Point3f screenToFG (Point screen, float depth)
{
float xfrac = (float)((screen.x / canvasWidth) - 0.5f) * -2.0f;
float yfrac = (float)((screen.y / canvasHeight) - 0.5f) * 2.0f;
float heightA = canvasTanFOV * depth;
return new Point3f(xfrac * heightA * canvasAspect, yfrac * heightA, depth);
}


where canvasTanFOV = Math.tan(canvasFOV)

Using this, I determined that at a depth of -0.6f, a quad that exactly covers the entire screen is of the following 3D coords in the Foreground node:

Pixel -> Foreground 3D coords
(0,0) -> (-0.559797, 0.41984773, -0.6)
(800,600) -> (0.559797, -0.41984773, -0.6)
Size: 1.119594 x 0.83969546

Then, to get even fancier, I figured rather than making custom fuctions to do all this math for me, why don't I just figure out a transformation matrix to do all this work for me. So I ended up with this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
float xscale = 1.119594f / canvasWidth;
float yscale = 0.83969546f / canvasHeight;

TransformGroup foregroundTG = new TransformGroup();

// This transform scales pixels to 3D size, and flips the Y coord    
Transform3D tmp = new Transform3D();
tmp.setScale(new Vector3f(xscale,-yscale,1));

// This transform recenters 0,0 from the middle of the
// screen (from 3D style) to the upper left (to screen
// style). It also drops everything back -0.6f to the GUI plate.

// Note that after the previous scaling, the X and Y
// coords are now in pixels!
         
Transform3D tmp2 = new Transform3D();
tmp2.setTranslation(new Vector3f(-canvasWidth/2,-canvasHeight/2,-0.6f));

tmp.mul(tmp2);
    
foregroundTG.setTransform(tmp);
      
foregroundBG.addChild(foregroundTG);
    
Foreground fg = new Foreground(foregroundBG, View.VIEW_FIXED);
addToScene(fg);


Now you can create quads with vertices in X, Y pixel coords (with Z=0) and it should map into the Foreground 3D coords automatically. Just make sure to add your quads to foregroundTG.

Another cool benefit is that with the entire GUI system based on a single TransformGroup, you can reposition the entire GUI in one step... like scrolling it off the screen, shaking it, etc.

Maybe a new ScreenGUI node (extends Foreground node) that does all this automatically? I couldn't even begin to put that into Xith, just an idea. I got my code working with the above anyway, so I'm good to go already.

Possible issues to be debugged... I might have one too many negative signs in there, flipping (and reflipping) things too many times but coming out correctly in the end. Also, the Foreground stack looks like this:

ForegroundBG -> ForegroundTG -> user quads

I dunno if the ForegroundBG is really necessary. The Foreground node only seems to accept BranchGroups, maybe it can be made to accept just Nodes and then we can skip the extraneous ForegroundBG? I don't know how to do any of this, I just know the code above works for my needs Smiley
Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #1 - Posted 2004-10-26 04:28:46 »

nice one.

Regarding BranchGroup - it is a slightly unusual setup I agree.  As Background and Foreground implement Leaf and not Group - you can't simply use the addChild method.  Just then I tried modifying it and adding a TG instead of a BG - interestingly the first transform was completely ignored so it's best to stick to a BG for now.  The performance hit of a single extra BG is negligible.  For source code neatness, we could add a TransformGroup constructor which automatically encapsulates it as a BranchGroup.

Will.

Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Reply #2 - Posted 2004-10-26 16:50:35 »

rocking!! I cant wait to try this out.

Sacramento Volleyball
"Whitty phrase goes here."
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (93 views)
2013-05-17 21:29:12

alaslipknot (101 views)
2013-05-16 21:24:48

gouessej (131 views)
2013-05-16 00:53:38

gouessej (126 views)
2013-05-16 00:17:58

theagentd (137 views)
2013-05-15 15:01:13

theagentd (125 views)
2013-05-15 15:00:54

StreetDoggy (166 views)
2013-05-14 15:56:26

kutucuk (188 views)
2013-05-12 17:10:36

kutucuk (191 views)
2013-05-12 15:36:09

UnluckyDevil (198 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.149 seconds with 21 queries.