Help end child hunger

Radar Approach – Implementation

Prev: Testing Points II Next: Testing Spheres

A class to implement the frustum functionality based on the radar approach is now presented. Note that the code itself is not optimized in any way, as the goal is only to serve as the tutorial companion. As opposed to the previous implementations, this class contains only the required data to make it work.

The class header file

class FrustumR{


	Vec3 cc; // camera position
	Vec3 X,Y,Z; // the camera referential
	float nearD, farD, width, height;


	void setCamInternals(float angle, float ratio, float nearD, float farD);
	void setCamDef(Vec3 &p, Vec3 &l, Vec3 &u);
	int pointInFrustum(Vec3 &p);

The Methods: setCamInternals

This function takes exactly the same parameters as the function gluPerspective. Each time the perspective definitions change, for instance when a window is resized, this function should be called as well.

#define ANG2RAD 3.14159265358979323846/180.0

void FrustumR::setCamInternals(float angle, float ratio, float nearD, float farD) {

	// store the information
	this->ratio = ratio;
	this->nearD = nearD;
	this->farD = farD;

	// compute width and height of the near section
	tang = (float)tan(ANG2RAD * angle * 0.5) ;
	heigth = nearD * tang;
	width = height * ratio;

The function stores all the information, and computes the width and height of the rectangular sections of the near plane and stores them in height (near height) and width (near width).

The Methods: setCamDef

This function takes three vectors that contain the information for the gluLookAt function: the position of the camera, a point to where the camera is pointing and the up vector. Each time the camera position or orientation changes, this function should be called as well. Notice how the following function is much simpler than for the other methods of view frustum culling. There is no need to compute the planes anymore.

void FrustumR::setCamDef(Vec3 &p, Vec3 &l, Vec3 &u) {

	cc = p.copy();

	// compute the Z axis of the camera referential
	// this axis points in the same direction from
	// the looking direction
	Z = l - p;

	// X axis of camera with given "up" vector and Z axis
	X = Z * u;

	// the real "up" vector is the dot product of X and Z
	Y = X * Z;

The Methods: pointInFrustum

Testing a point is also much simpler and more efficient:

int FrustumR::pointInFrustum(Vec3 &p) {

	float pcz,pcx,pcy,aux;

	// compute vector from camera position to p
	Vec3 v = p-camPos;

	// compute and test the Z coordinate
	pcz = v.innerProduct(-Z);
	if (pcz > farD || pcz < nearD)


 	// compute and test the Y coordinate
 	pcy = v.innerProduct(Y);
 	aux = pcz * tang;
 	if (pcy > aux || pcy < -aux)

 	// compute and test the X coordinate
 	pcx = v.innerProduct(X);
 	aux = aux * ratio;
 	if (pcx > aux || pcx < -aux)



Prev: Testing Points II Next: Testing Spheres

One Response to “Radar Approach – Implementation”

  1. Am I correct in assuming that

    pcz = v.innerProduct(-Z);

    is -Z because of right-handedness of your example?
    So If I were to try this in left-handed I should use Z instead of -Z

Leave a Reply




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 Suffusion theme by Sayontan Sinha