#ifndef FRUSTUM_H
#define FRUSTUM_H
#include "vector.h"
class Frustum {
public:
void initialize(float angle, float ratio, float nearDistance);
void destroy();
void update(VECTOR3D position);
VECTOR3D up;
VECTOR3D right;
VECTOR3D orientation;
VECTOR3D nearCenter;
VECTOR3D nearTopLeft;
VECTOR3D nearTopRight;
VECTOR3D nearBottomLeft;
VECTOR3D nearBottomRight;
float modelview[4*4];
float view[3];
float position[3];
float nearDistance;
float nearHeight;
float nearWidth;
private:
};
#endif
frustum.cpp
#include <gl.h>
#include <math.h>
#include "mymath.h"
#include "frustum.h"
void Frustum::initialize(float angle, float ratio, float nearDistance) {
// Register parameters
this->nearDistance = nearDistance;
// Compute the width and height of the near and far plane sections
float tang = (float) tan((PI / 180.0f) * angle);
nearHeight = nearDistance * tang;
nearWidth = nearHeight * ratio;
}
void Frustum::destroy() {
}
void Frustum::update(VECTOR3D position) {
// Calculate up- and right-vector
glGetFloatv(GL_MODELVIEW_MATRIX, modelview);
up.x = modelview[1];
up.y = modelview[5];
up.z = modelview[9];
right.x = modelview[0];
right.y = modelview[4];
right.z = modelview[8];
orientation.crossProduct(up,right);
// Normalize vectors
up.normalize();
right.normalize();
orientation.normalize();
// Calculate center of near frustum
nearCenter = (orientation * nearDistance * 1.2f);
// Calculate corners of near frustum
nearTopLeft = nearCenter + (up * (nearHeight / 2.0f)) - (right * (nearWidth / 2.0f));
nearTopRight = nearCenter + (up * (nearHeight / 2.0f)) + (right * (nearWidth / 2.0f));
nearBottomLeft = nearCenter - (up * (nearHeight / 2.0f)) - (right * (nearWidth / 2.0f));
nearBottomRight = nearCenter - (up * (nearHeight / 2.0f)) + (right * (nearWidth / 2.0f));
// Calculate view vector
view[0] = nearCenter.x;
view[1] = nearCenter.y;
view[2] = nearCenter.z;
// Calculate position vector
Frustum::position[0] = position.x;
Frustum::position[1] = position.y;
Frustum::position[2] = position.z;
}
Ingen kommentarer:
Send en kommentar