-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain.h
148 lines (119 loc) · 4.18 KB
/
terrain.h
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// -----------------------------------------------------------------
// Learning Team B
// Members:
// Adam LeMmon
// Faith Satterthwaite
// Tom Fletcher
// Justin Ball
// CS 4280 – 11:30 am
// Final Project
// Dr. Rague
// Due: 12/06/12
// Version: 2.4
// -----------------------------------------------------------------
// We made five major improvements to this game
// 1) New controls
// 2) Enemy attack
// 3) HUD (heads up display)
// 4) Enemy health bars
// 5) New Weapon
// -----------------------------------------------------------------
#ifndef __TERRAIN_H
#define __TERRAIN_H
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
/*
TERRAIN.H
The CTerrain class interface
OpenGL Game Programming
Author: Kevin Hawkins
Date: 3/29/2001
Description: The CTerrain class is derived from CObject, and represents
the main world. Randomly generated terrain and frustrum clipping
are provided.
The random terrain is created with the midpoint
displacement algorithm.
Random terrain generation Copyright (C) Jason Shankel, 2000
*/
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
//#include <gl/glext.h>
#include "camera.h"
#include "object.h"
// Phase 13 - Add following include
#include "texture.h"
#include "vector.h"
class CTerrain : public CObject
{
private:
int width; // terrain is of size width X width
// preferrably with 2^n = width
float terrainMul;
float heightMul;
float scanDepth;
float textureMul;
float RangedRandom(float v1,float v2);
void NormalizeTerrain(float field[],int size);
void FilterHeightBand(float *band,int stride,int count,float filter);
void FilterHeightField(float field[],int size,float filter);
void MakeTerrainPlasma(float field[],int size,float rough);
protected:
// terrain doesn't move, so no physics animations
void OnAnimate(scalar_t deltaTime) {}
void OnDraw(CCamera *camera);
void OnCollision(CObject *collisionObject);
public:
float *heightMap; // dynamic heightmap
// Phase 13 - Uncomment the following
CTexture terrainTex[5]; // for multiple textures on the terrain
float fogColor[4]; // color of the fog/sky
CTerrain();
CTerrain(int width, float rFactor);
~CTerrain() { delete [] heightMap; }
void Load() {}
void Unload() {}
void BuildTerrain(int width, float rFactor);
int GetWidth() { return width; }
float GetMul() { return terrainMul; }
float GetScanDepth() { return scanDepth; }
float GetHeight(double x, double z)
{
float projCameraX, projCameraZ;
// divide by the grid-spacing if it is not 1
projCameraX = static_cast<float>(x / terrainMul);
projCameraZ = static_cast<float>(z / terrainMul);
// compute the height field coordinates (Col0, Row0)
// and (Col1, Row1) that identify the height field cell
// directly below the camera.
int col0 = int(projCameraX);
int row0 = int(projCameraZ);
int col1 = col0 + 1;
int row1 = row0 + 1;
// make sure that the cell coordinates don't fall
// outside the height field.
if (col1 > width)
col1 = 0;
if (row1 > width)
row1 = 0;
// get the four corner heights of the cell from the height field
float h00 = heightMul * (float)heightMap[col0 + row0*width];
float h01 = heightMul * (float)heightMap[col1 + row0*width];
float h11 = heightMul * (float)heightMap[col1 + row1*width];
float h10 = heightMul * (float)heightMap[col0 + row1*width];
// calculate the position of the camera relative to the cell.
// note, that 0 <= tx, ty <= 1.
float tx = projCameraX - float(col0);
float ty = projCameraZ - float(row0);
// the next step is to perform a bilinear interpolation
// to compute the height of the terrain directly below
// the object.
float txty = tx * ty;
float final_height = h00 * (1.0f - ty - tx + txty)
+ h01 * (tx - txty)
+ h11 * txty
+ h10 * (ty - txty);
return final_height;
}
};
#endif