Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Frustrum culling example: for basic translation.  (Read 1589 times)
0 Members and 1 Guest are viewing this topic.
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Posted 2009-01-30 13:29:03 »

hey guys. I wanted to post my approach to frustrum culling, in perspective mode.
The code takes into account a few assumptions, like the order of placement for the camera.

It can be made alot more robust, but I'v put in the main logic. In doesnt take into account any sorting of objects.

1  
2  
public class Frustrum {
  private static float xGradient, yGradient, distance, a1, b1, a2, b2, temp, x, y, z;


1  
2  
3  
4  
  public static void init(float fieldOfView, float aspectRatio) {
    xGradient = (float)Math.tan(Math.toRadians(fieldOfView)/2f);
    yGradient = (float)Math.tan(Math.toRadians(fieldOfView*aspectRatio)/2f);
  }

run at the start of program

1  
2  
3  
4  
5  
6  
  public static void update() {
    a1 = (float)Math.cos(-Math.toRadians(Camera.yRot));
    b1 = (float)Math.sin(-Math.toRadians(Camera.yRot));
    a2 = (float)Math.cos(-Math.toRadians(Camera.xRot));
    b2 = (float)Math.sin(-Math.toRadians(Camera.xRot));
  }

update should be called, when the camera rotation changes (or every cycle).

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  
28  
29  
30  
31  
32  
33  
   
  //Returns mapObjects distance from the camera, if mapObject is not in frustrum will return 0
 public static float check(MapObject o, float radius) {
    //UN TRANSLATE CAMERA POSITION
   x = o.xPos - Camera.trueX;
    y = o.yPos - Camera.trueY;
    z = o.zPos - Camera.trueZ;
     
    //UN ROTATE CAMERA Y ROTATION
   temp = (x * a1) - (z * b1);
    z =    (x * b1) + (z * a1);
    x = temp;

    //UN ROTATE CAMERA X ROTATION
   temp = (z * b2) + (y * a2);
    z =    (z * a2) - (y * b2);
    y = temp;
         
    //CHECK SIDE OF SCREEN BOOLEAN EQUATIONS
   if (y + radius < xGradient*z || x + radius < yGradient*z) return 0;
    z = -z;
    if (y - radius > xGradient*z || x - radius > yGradient*z) return 0;
   
    //* DISTANCE FROM CAMERA BOOLEAN CHECK - RADIAL VERSION - SLOWER!
   z = (float)Math.sqrt((x*x) + (y*y) + (z*z));
    if (z > Settings.fDistance || z < Settings.cDistance) return 0;

    //* DISTANCE FROM CAMERA BOOLEAN CHECK - FAST VERSION
   //if (z > Settings.fDistance || z < Settings.cDistance) return 0;

    return z;
  }
}

this is the actual frustrum code check.

ASSUMES CAMERA IS POSITION IN THE FOLLOWING WAY:
ratate(camera.xRot, 1, 0, 0);
ratate(camera.yRot, 0, 1, 0);
translate(-camera.x, -camera.y, -camera.z); //notice negative translation


Sure the order of rotation and translation can be changed depending on the camera placement.

My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Offline broumbroum

Junior Member





« Reply #1 - Posted 2009-01-30 15:18:49 »

Thx for this, interesting. Can you add some more about the var you init in the code please ? I'm sure if I can put it in my code. I'm looking for a way to place x.y.z my camera and look in the x.y.z right direction.

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline bobjob

JGO Knight


Medals: 10
Projects: 4


David Aaron Muhar


« Reply #2 - Posted 2009-01-31 00:47:14 »

Thx for this, interesting. Can you add some more about the var you init in the code please ? I'm sure if I can put it in my code. I'm looking for a way to place x.y.z my camera and look in the x.y.z right direction.
the init code variables refer to the variables used in the openGL function
gluPerspective(fieldOfView, aspectRatio, closeDistance, farDistance);
the number that is calculated is just a basic gradient value (used in a basic linear equation y = mx + c) m is the gradient.

this example is only for basic translations, and rotations. if doesnt really work if the rotations are all done in one call.

Basically what this code does, is positions the scene as if the camera were at position 0, 0, 0 with no rotation (camera normal, looking out at 0, 0, -1).
then the line equation test is run (y = mx + c) in this case there is no yIntercept (c) there fore the test run is:
y = mx, but to take into account the models/objects radius the test thats run is
y+radius = gradient * x. (this example checks if the object is ON the line, so less than or greather than signs are used to check if its in the screen)

this test is run for the top, bottom, left and right of the screen.

Assuming you camera is placed in the following way
translate(x, y, z);
rotate(yRot, 0, 1, 0);
here is a version that should work for you:
1  
2  
3  
4  
  public static void update() {
    a1 = (float)Math.cos(-Math.toRadians(Camera.yRot));
    b1 = (float)Math.sin(-Math.toRadians(Camera.yRot));
  }


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  
28  
29  
30  
31  
  //Returns mapObjects distance from the camera, if mapObject is not in frustrum will return 0
 public static float check(MapObject o, float radius) {

    //UN ROTATE CAMERA Y ROTATION
   x = (o.xPos * a1) - (o.zPos * b1);
    z = (o.xPos * b1) + (o.zPos * a1);
   
    //UN TRANSLATE CAMERA POSITION
   x -= Camera.xPos;
    y = o.yPos - Camera.yPos;
    z -= Camera.zPos;

    //OR UNDO BOTH IN 1 STEP
   //x = (o.xPos * a1) - (o.zPos * b1) - Camera.xPos;
   //y = o.yPos - Camera.yPos;
   //z = (o.xPos * b1) + (o.zPos * a1) - Camera.zPos;

    //CHECK SIDE OF SCREEN BOOLEAN EQUATIONS
   if (y + radius < xGradient*z || x + radius < yGradient*z) return 0;
    z = -z;
    if (y - radius > xGradient*z || x - radius > yGradient*z) return 0;
   
    //* DISTANCE FROM CAMERA BOOLEAN CHECK - RADIAL VERSION - SLOWER!
   z = (float)Math.sqrt((x*x) + (y*y) + (z*z));
    if (z > farDistance || z < closeDistance) return 0;

    //* DISTANCE FROM CAMERA BOOLEAN CHECK - FAST VERSION
   //if (z > farDistance || z < closeDistance) return 0;

    return z;
  }


My Projects
Games, Webcam chat, Video screencast, PDF tools.

Javagaming.org with chat room
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!