Optimizations and cleanup in view and env:

- Removed unused code and variables
- Declare variables when they are needed
- Avoid copying TPlane by using references

git-svn-id: https://svn.code.sf.net/p/extremetuxracer/code/trunk@340 0420edf4-82e4-42fc-9478-35b55e6d67a3
master
pkeus 2013-05-23 09:40:48 +00:00
parent eb95b614bd
commit 786b3e70cb
7 changed files with 93 additions and 119 deletions

14
env.cpp
View File

@ -282,15 +282,13 @@ void CEnvironment::DrawFog () {
TPlane bottom_plane, top_plane;
TVector3 left, right, vpoint;
TVector3 topleft, topright;
TVector3 bottomleft = NullVec;
TVector3 bottomright = NullVec;
float height;
TVector3 bottomleft, bottomright;
// the clipping planes are calculated by view frustum (view.cpp)
leftclip = get_left_clip_plane ();
rightclip = get_right_clip_plane ();
farclip = get_far_clip_plane ();
bottomclip = get_bottom_clip_plane ();
const TPlane& leftclip = get_left_clip_plane ();
const TPlane& rightclip = get_right_clip_plane ();
const TPlane& farclip = get_far_clip_plane ();
const TPlane& bottomclip = get_bottom_clip_plane ();
// --------------- calculate the planes ---------------------------
@ -299,7 +297,7 @@ void CEnvironment::DrawFog () {
// TPlane right_edge_plane = MakePlane (-1.0, 0.0, 0.0, Course.width);
bottom_plane.nml = MakeVector (0.0, 1, -slope);
height = Course.GetBaseHeight (0);
float height = Course.GetBaseHeight (0);
bottom_plane.d = -height * bottom_plane.nml.y;
top_plane.nml = bottom_plane.nml;

5
env.h
View File

@ -62,11 +62,6 @@ private:
TFog fog;
TFog default_fog;
TPlane leftclip;
TPlane rightclip;
TPlane farclip;
TPlane bottomclip;
map<string, size_t> EnvIndex;
map<string, size_t> LightIndex;

4
gui.h
View File

@ -62,8 +62,8 @@ public:
TCheckbox(int x, int y, int width_, const string& tag_)
: TWidget(x, y, 32, 32)
, width(width_)
, tag(tag_)
, width(width_)
, checked(false)
{
mouseRect.left = x+width-32;
@ -82,8 +82,8 @@ class TIconButton : public TWidget {
public:
TIconButton(int x, int y, TTexture* texture_, double size_, int max_, int value_)
: TWidget(x, y, 32, 32)
, texture(texture_)
, size(size_)
, texture(texture_)
, maximum(max_)
, value(value_)
{}

View File

@ -155,7 +155,7 @@ TPlane MakePlane (double nx, double ny, double nz, double d){
return tmp;
}
bool IntersectPlanes (TPlane s1, TPlane s2, TPlane s3, TVector3 *p){
bool IntersectPlanes (const TPlane& s1, const TPlane& s2, const TPlane& s3, TVector3 *p){
double A[3][4];
double x[3];
double retval;
@ -187,8 +187,8 @@ bool IntersectPlanes (TPlane s1, TPlane s2, TPlane s3, TVector3 *p){
}
}
double DistanceToPlane (TPlane plane, const TVector3& pt) {
return
double DistanceToPlane (const TPlane& plane, const TVector3& pt) {
return
plane.nml.x * pt.x +
plane.nml.y * pt.y +
plane.nml.z * pt.z +
@ -252,28 +252,28 @@ void MakeRotationMatrix (TMatrix mat, double angle, char axis){
mat[2][2] = cosv;
break;
case 'z':
case 'z':
mat[0][0] = cosv;
mat[1][0] = -sinv;
mat[0][1] = sinv;
mat[1][1] = cosv;
break;
}
}
}
void MakeTranslationMatrix (TMatrix mat, double x, double y, double z){
MakeIdentityMatrix (mat);
mat[3][0] = x;
mat[3][1] = y;
mat[3][2] = z;
}
}
void MakeScalingMatrix (TMatrix mat, double x, double y, double z){
MakeIdentityMatrix (mat);
mat[0][0] = x;
mat[1][1] = y;
mat[2][2] = z;
}
}
void MakeBasisMat (TMatrix mat, const TVector3& w1, const TVector3& w2, const TVector3& w3) {
MakeIdentityMatrix (mat);
@ -286,9 +286,9 @@ void MakeBasisMat (TMatrix mat, const TVector3& w1, const TVector3& w2, const TV
mat[2][0] = w3.x;
mat[2][1] = w3.y;
mat[2][2] = w3.z;
}
}
void MakeBasismatrix_Inv (TMatrix mat, TMatrix invMat,
void MakeBasismatrix_Inv (TMatrix mat, TMatrix invMat,
const TVector3& w1, const TVector3& w2, const TVector3& w3){
MakeIdentityMatrix (mat);
mat[0][0] = w1.x;
@ -311,7 +311,7 @@ void MakeBasismatrix_Inv (TMatrix mat, TMatrix invMat,
invMat[0][2] = w3.x;
invMat[1][2] = w3.y;
invMat[2][2] = w3.z;
}
}
void RotateAboutVectorMatrix (TMatrix mat, const TVector3& u, double angle) {
TMatrix rx, irx, ry, iry;
@ -324,12 +324,12 @@ void RotateAboutVectorMatrix (TMatrix mat, const TVector3& u, double angle) {
d = sqrt (b*b + c*c);
if (d < EPS) {
if (a < 0)
if (a < 0)
MakeRotationMatrix (mat, -angle, 'x');
else
MakeRotationMatrix (mat, angle, 'x');
return;
}
}
MakeIdentityMatrix (rx);
MakeIdentityMatrix (irx);
@ -362,7 +362,7 @@ void RotateAboutVectorMatrix (TMatrix mat, const TVector3& u, double angle) {
MultiplyMatrices (mat, mat, rx);
MultiplyMatrices (mat, iry, mat);
MultiplyMatrices (mat, irx, mat);
}
}
TQuaternion MakeQuaternion (double x, double y, double z, double w){
TQuaternion q;
@ -383,7 +383,7 @@ TQuaternion MultiplyQuaternions (const TQuaternion& q, const TQuaternion& r){
}
TQuaternion AddQuaternions (const TQuaternion& q, const TQuaternion& r){
TQuaternion res;
TQuaternion res;
res.x = q.x + r.x;
res.y = q.y + r.y;
res.z = q.z + r.z;
@ -445,7 +445,7 @@ TQuaternion MakeQuaternionFromMatrix (TMatrix m){
res.x = (m[1][2] - m[2][1]) * s;
res.y = (m[2][0] - m[0][2]) * s;
res.z = (m[0][1] - m[1][0]) * s;
} else {
} else {
i = 0;
if (m[1][1] > m[0][0]) i = 1;
if (m[2][2] > m[i][i]) i = 2;
@ -453,9 +453,9 @@ TQuaternion MakeQuaternionFromMatrix (TMatrix m){
k = nxt[j];
s = sqrt (m[i][i] - m[j][j] - m[k][k] + 1.0);
q[i] = s * 0.5;
if (s != 0.0) s = 0.5 / s;
q[3] = (m[j][k] - m[k][j]) * s;
@ -496,7 +496,7 @@ TQuaternion MakeRotationQuaternion (const TVector3& s, const TVector3& t){
return res;
}
TQuaternion InterpolateQuaternions (const TQuaternion& q,
TQuaternion InterpolateQuaternions (const TQuaternion& q,
TQuaternion r, double t){
TQuaternion res;
double cosphi;
@ -524,10 +524,10 @@ TQuaternion InterpolateQuaternions (const TQuaternion& q,
scale1 = t;
}
res.x = scale0 * q.x + scale1 * r.x;
res.y = scale0 * q.y + scale1 * r.y;
res.z = scale0 * q.z + scale1 * r.z;
res.w = scale0 * q.w + scale1 * r.w;
res.x = scale0 * q.x + scale1 * r.x;
res.y = scale0 * q.y + scale1 * r.y;
res.z = scale0 * q.z + scale1 * r.z;
res.w = scale0 * q.w + scale1 * r.w;
return res;
}
@ -610,7 +610,7 @@ void elim (double *matrix, int n, int pivot){
double factor = (*(matrix+row*(n+1)+pivot))/(*(matrix+pivot*(n+1)+pivot));
*(matrix+row*(n+1)+pivot)=0.0;
for (int col=pivot+1l; col<n+1; col++) {
*(matrix+row*(n+1)+col) = *(matrix+row*(n+1)+col) -
*(matrix+row*(n+1)+col) = *(matrix+row*(n+1)+col) -
(*(matrix+pivot*(n+1)+col))*factor;
}
}
@ -620,7 +620,7 @@ void elim (double *matrix, int n, int pivot){
void backsb (double *matrix, int n, double *soln){
for (int row = n-1; row >=0; row--){
for (int col = n-1; col >= row+1; col--) {
*(matrix+row*(n+1)+(n)) = *(matrix+row*(n+1)+n) -
*(matrix+row*(n+1)+(n)) = *(matrix+row*(n+1)+n) -
(*(soln+col))*(*(matrix+row*(n+1)+col));
}
*(soln+row) = (*(matrix+row*(n+1)+n))/(*(matrix+row*(n+1)+row));
@ -643,8 +643,8 @@ bool IntersectPolygon (const TPolygon& p, TVector3 *v) {
if (fabs(nuDotProd) < EPS)
return false;
d = - (nml.x * v[p.vertices[0]].x +
nml.y * v[p.vertices[0]].y +
d = - (nml.x * v[p.vertices[0]].x +
nml.y * v[p.vertices[0]].y +
nml.z * v[p.vertices[0]].z);
if (fabs (d) > 1) return false;
@ -653,7 +653,7 @@ bool IntersectPolygon (const TPolygon& p, TVector3 *v) {
TVector3 *v0, *v1;
v0 = &v[p.vertices[i]];
v1 = &v[p.vertices[ (i+1) % p.num_vertices ]];
v1 = &v[p.vertices[ (i+1) % p.num_vertices ]];
TVector3 edge_vec = SubtractVectors (*v1, *v0);
edge_len = NormVector (edge_vec);
@ -676,23 +676,23 @@ bool IntersectPolygon (const TPolygon& p, TVector3 *v) {
TVector3 pt = AddVectors (ray.pt, ScaleVector (s, ray.vec));
for (int i=0; i < p.num_vertices; i++) {
TVector3 edge_nml = CrossProduct (nml,
TVector3 edge_nml = CrossProduct (nml,
SubtractVectors (v[p.vertices[ (i+1) % p.num_vertices ]], v[p.vertices[i]]));
wec = DotProduct (SubtractVectors (pt, v[p.vertices[i]]), edge_nml);
if (wec < 0) return false;
}
}
return true;
}
}
bool IntersectPolyhedron (const TPolyhedron& p) {
bool hit = false;
for (size_t i=0; i<p.num_polygons; i++) {
hit = IntersectPolygon (p.polygons[i], p.vertices);
if (hit == true) break;
}
}
return hit;
}
}
TVector3 MakeNormal (const TPolygon& p, TVector3 *v) {
double old_len;
@ -703,7 +703,7 @@ TVector3 MakeNormal (const TPolygon& p, TVector3 *v) {
old_len = NormVector (normal);
return normal;
}
}
TPolyhedron CopyPolyhedron (const TPolyhedron& ph) {
@ -711,16 +711,16 @@ TPolyhedron CopyPolyhedron (const TPolyhedron& ph) {
newph.vertices = new TVector3[ph.num_vertices];
copy_n(ph.vertices, ph.num_vertices, newph.vertices);
return newph;
}
}
void FreePolyhedron (const TPolyhedron& ph) {
delete[] ph.vertices;
}
}
void TransPolyhedron (TMatrix mat, const TPolyhedron& ph) {
for (size_t i=0; i<ph.num_vertices; i++)
ph.vertices[i] = TransformPoint (mat, ph.vertices[i]);
}
}
// --------------------------------------------------------------------
// ode solver
@ -731,7 +731,7 @@ const double ode23_coeff_mat[][4] = {
{0.0, 1./2., 0.0, 2./9.},
{0.0, 0.0, 3./4., 1./3.},
{0.0, 0.0, 0.0, 4./9.},
{0.0, 0.0, 0.0, 0.0}
{0.0, 0.0, 0.0, 0.0}
};
const double ode23_error_mat[] = {-5./72., 1./12., 1./9., -1./8. };
@ -781,7 +781,7 @@ double ode23_TimestepExponent(){
}
TOdeSolver NewOdeSolver23() {
TOdeSolver s;
TOdeSolver s;
s.NumEstimates = ode23_NumEstimates;
s.InitOdeData = ode23_InitOdeData;
s.NextTime = ode23_NextTime;
@ -808,11 +808,11 @@ double LinearInterp (const double x[], const double y[], double val, int n){
}
double XRandom (float min, float max) {
return (double)rand () / RAND_MAX * (max - min) + min;
return (double)rand () / RAND_MAX * (max - min) + min;
}
double FRandom () {
return (double)rand () / RAND_MAX;
return (double)rand () / RAND_MAX;
}
int IRandom (int min, int max) {

View File

@ -50,8 +50,8 @@ TVector3 TransformVector (TMatrix mat, const TVector3& v);
TVector3 TransformNormal (const TVector3& n, TMatrix mat); // not used ?
TVector3 TransformPoint (TMatrix mat, const TVector3& p);
TPlane MakePlane (double nx, double ny, double nz, double d);
bool IntersectPlanes (TPlane s1, TPlane s2, TPlane s3, TVector3 *p);
double DistanceToPlane (TPlane plane, const TVector3& pt);
bool IntersectPlanes (const TPlane& s1, const TPlane& s2, const TPlane& s3, TVector3 *p);
double DistanceToPlane (const TPlane& plane, const TVector3& pt);
void MakeIdentityMatrix (TMatrix h);
void MakeRotationMatrix (TMatrix mat, double angle, char axis);
@ -66,8 +66,8 @@ void RotateAboutVectorMatrix (TMatrix mat, const TVector3& u, double angle);
TQuaternion MakeQuaternion (double x, double y, double z, double w);
TQuaternion AddQuaternions (const TQuaternion& q, const TQuaternion& r); // not used?
TQuaternion MultiplyQuaternions (const TQuaternion& q, const TQuaternion& r); // not used?
TQuaternion ScaleQuaternion (double s, TQuaternion q);
TQuaternion MultiplyQuaternions (const TQuaternion& q, const TQuaternion& r);
TQuaternion ScaleQuaternion (double s, const TQuaternion& q);
TQuaternion ConjugateQuaternion (const TQuaternion& q);
void MakeMatrixFromQuaternion (TMatrix mat, const TQuaternion& q);
TQuaternion MakeQuaternionFromMatrix (TMatrix mat);

View File

@ -166,9 +166,7 @@ void setup_view_matrix (CControl *ctrl, bool save_mat) {
view_mat[3][2] = -viewpt_in_view_frame.z;
if (save_mat) {
for (int xx=0; xx<4; xx++) {
for (int yy=0; yy<4; yy++) stationary_matrix[xx][yy] = view_mat[xx][yy];
}
memcpy(stationary_matrix, view_mat, 16*sizeof(*view_mat));
}
glLoadIdentity();
glMultMatrixd ((double*) view_mat);
@ -176,18 +174,12 @@ void setup_view_matrix (CControl *ctrl, bool save_mat) {
TVector3 MakeViewVector () {
double course_angle = Course.GetCourseAngle();
TVector3 res
= MakeVector (0,
sin (ANGLES_TO_RADIANS (
double rad = ANGLES_TO_RADIANS (
course_angle -
CAMERA_ANGLE_ABOVE_SLOPE +
PLAYER_ANGLE_IN_CAMERA)),
cos (ANGLES_TO_RADIANS (
course_angle -
CAMERA_ANGLE_ABOVE_SLOPE +
PLAYER_ANGLE_IN_CAMERA)));
res = ScaleVector (camera_distance, res);
return res;
PLAYER_ANGLE_IN_CAMERA);
TVector3 res = MakeVector (0, sin(rad), cos(rad));
return ScaleVector (camera_distance, res);
}
void update_view (CControl *ctrl, double dt) {
@ -198,41 +190,34 @@ void update_view (CControl *ctrl, double dt) {
}
TVector3 view_pt = MakeVector (0,0,0);
TVector3 view_dir, view_vec;
double ycoord;
double course_angle;
TVector3 axis;
TVector3 view_dir;
TMatrix rot_mat;
TVector3 y_vec;
TVector3 mz_vec;
TVector3 vel_proj;
TQuaternion rot_quat;
static const TVector3 y_vec = MakeVector (0.0, 1.0, 0.0);
static const TVector3 mz_vec = MakeVector (0.0, 0.0, -1.0);
TVector3 vel_cpy = ctrl->cvel;
double speed = NormVector (vel_cpy);
double time_constant_mult = 1.0 /
min (1.0, max (0.0,
(speed - NO_INTERPOLATION_SPEED) /
(BASELINE_INTERPOLATION_SPEED - NO_INTERPOLATION_SPEED)));
TVector3 up_dir = MakeVector (0, 1, 0);
min (1.0, max (0.0,
(speed - NO_INTERPOLATION_SPEED) /
(BASELINE_INTERPOLATION_SPEED - NO_INTERPOLATION_SPEED)));
TVector3 vel_dir = ctrl->cvel;
NormVector (vel_dir);
course_angle = Course.GetCourseAngle();
double course_angle = Course.GetCourseAngle();
switch (ctrl->viewmode) {
case BEHIND: {
view_vec = MakeViewVector ();
TVector3 view_vec = MakeViewVector ();
y_vec = MakeVector (0.0, 1.0, 0.0);
mz_vec = MakeVector (0.0, 0.0, -1.0);
vel_proj = ProjectToPlane (y_vec, vel_dir);
TVector3 vel_proj = ProjectToPlane (y_vec, vel_dir);
NormVector (vel_proj);
rot_quat = MakeRotationQuaternion (mz_vec, vel_proj);
view_vec = RotateVector (rot_quat, view_vec);
view_pt = AddVectors (ctrl->cpos, view_vec);
ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
double ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
@ -254,34 +239,31 @@ void update_view (CControl *ctrl, double dt) {
}
view_vec = SubtractVectors (view_pt, ctrl->cpos);
axis = CrossProduct (y_vec, view_vec);
TVector3 axis = CrossProduct (y_vec, view_vec);
NormVector (axis);
RotateAboutVectorMatrix (rot_mat, axis, PLAYER_ANGLE_IN_CAMERA);
view_dir = ScaleVector (-1.0, TransformVector (rot_mat, view_vec));
if (ctrl->view_init) {
for (int i=0; i<2; i++) {
TVector3 up_dir = MakeVector (0, 1, 0);
interpolate_view_frame (ctrl->viewup, ctrl->viewdir,
&up_dir, &view_dir, dt,
BEHIND_ORIENT_TIME_CONSTANT);
up_dir = MakeVector (0.0, 1.0, 0.0);
}
}
break;
}
case FOLLOW: { // normale Einstellung
up_dir = MakeVector (0, 1, 0);
view_vec = MakeViewVector ();
y_vec = MakeVector (0.0, 1.0, 0.0);
mz_vec = MakeVector (0.0, 0.0, -1.0);
TVector3 view_vec = MakeViewVector ();
vel_proj = ProjectToPlane (y_vec, vel_dir);
TVector3 vel_proj = ProjectToPlane (y_vec, vel_dir);
NormVector (vel_proj);
rot_quat = MakeRotationQuaternion (mz_vec, vel_proj);
view_vec = RotateVector (rot_quat, view_vec);
view_pt = AddVectors (ctrl->cpos, view_vec);
ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
double ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
}
@ -289,41 +271,40 @@ void update_view (CControl *ctrl, double dt) {
if (ctrl->view_init) {
for (int i=0; i<2; i++) {
view_pt = interpolate_view_pos (ctrl->plyr_pos, ctrl->cpos,
MAX_CAMERA_PITCH, ctrl->viewpos,
view_pt, camera_distance, dt,
FOLLOW_ORBIT_TIME_CONSTANT *
time_constant_mult);
MAX_CAMERA_PITCH, ctrl->viewpos,
view_pt, camera_distance, dt,
FOLLOW_ORBIT_TIME_CONSTANT *
time_constant_mult);
}
}
ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
if (view_pt.y < ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT) {
if (view_pt.y < ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT) {
view_pt.y = ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT;
}
view_vec = SubtractVectors (view_pt, ctrl->cpos);
axis = CrossProduct (y_vec, view_vec);
TVector3 axis = CrossProduct (y_vec, view_vec);
NormVector (axis);
RotateAboutVectorMatrix (rot_mat, axis,
PLAYER_ANGLE_IN_CAMERA);
PLAYER_ANGLE_IN_CAMERA);
view_dir = ScaleVector (-1.0,
TransformVector (rot_mat, view_vec));
if (ctrl->view_init) {
for (int i=0; i<2; i++) {
TVector3 up_dir = MakeVector (0, 1, 0);
interpolate_view_frame (ctrl->viewup, ctrl->viewdir,
&up_dir, &view_dir, dt,
FOLLOW_ORIENT_TIME_CONSTANT);
up_dir = MakeVector (0.0, 1.0, 0.0);
}
}
break;
}
case ABOVE: {
up_dir = MakeVector (0, 1, 0);
view_vec = MakeViewVector ();
TVector3 view_vec = MakeViewVector ();
view_pt = AddVectors (ctrl->cpos, view_vec);
ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
double ycoord = Course.FindYCoord (view_pt.x, view_pt.z);
if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
}
@ -331,7 +312,7 @@ void update_view (CControl *ctrl, double dt) {
view_vec = SubtractVectors (view_pt, ctrl->cpos);
MakeRotationMatrix (rot_mat, PLAYER_ANGLE_IN_CAMERA, 'x');
view_dir = ScaleVector (-1.0,
TransformVector (rot_mat, view_vec));
TransformVector (rot_mat, view_vec));
break;
}
@ -341,7 +322,7 @@ void update_view (CControl *ctrl, double dt) {
ctrl->viewpos = view_pt;
ctrl->viewdir = view_dir;
ctrl->viewup = up_dir;
ctrl->viewup = MakeVector (0, 1, 0);
ctrl->plyr_pos = ctrl->cpos;
ctrl->view_init = true;
@ -438,7 +419,7 @@ clip_result_t clip_aabb_to_view_frustum (const TVector3& min, const TVector3& ma
return NoClip;
}
TPlane get_far_clip_plane() {return frustum_planes[1]; }
TPlane get_left_clip_plane() {return frustum_planes[2]; }
TPlane get_right_clip_plane() {return frustum_planes[3]; }
TPlane get_bottom_clip_plane() {return frustum_planes[5]; }
const TPlane& get_far_clip_plane() {return frustum_planes[1]; }
const TPlane& get_left_clip_plane() {return frustum_planes[2]; }
const TPlane& get_right_clip_plane() {return frustum_planes[3]; }
const TPlane& get_bottom_clip_plane() {return frustum_planes[5]; }

8
view.h
View File

@ -39,9 +39,9 @@ enum clip_result_t {
void SetupViewFrustum (CControl *ctrl);
clip_result_t clip_aabb_to_view_frustum (const TVector3& min, const TVector3& max );
TPlane get_far_clip_plane();
TPlane get_left_clip_plane();
TPlane get_right_clip_plane();
TPlane get_bottom_clip_plane();
const TPlane& get_far_clip_plane();
const TPlane& get_left_clip_plane();
const TPlane& get_right_clip_plane();
const TPlane& get_bottom_clip_plane();
#endif