-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincludes.h
executable file
·262 lines (221 loc) · 5.53 KB
/
includes.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef INCLUDES
#define INCLUDES
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<sstream>
#include<ctime>
#include<cstdarg>
#include<cstdio>
#include<vector>
#define GL_GLEXT_PROTOTYPES
#include<GL/glut.h>
#include<AL/al.h>
#include<AL/alc.h>
#include<AL/alut.h>
#define NUM_BUFFERS 5
#define NUM_SOURCES 5
#define START 0
#define IDLE 1
#define COIN 2
#define ROOSTER 3
#define FOSS_TOTAL 25
#define WOLF 4
#define G 9.8
#define FRIC_CONST 0.1
#define BIKE_ACC 1.5
#define BIKE_INIT 0.0
#define BIKE_LEN 1.0
#define BIKE_WID 0.5
#define BIKE_HIGH 0.7
#define BIKE_MASS 1.0
#define TYRE_RADIUS 0.3
#define RIM_RADIUS 0.1
#define PI 3.141592653589
#define DEG2RAD(x) (x*PI/180)
#define RAD2DEG(x) (x*180/PI)
#define sine(th) sin( th*PI/180)
#define cosine(th) cos(th*PI/180)
#define dis(a,b,c,d) sqrt( (a-c)*(a-c) + (b-d)*(b-d) )
void project();
void initFossils();
void printv(va_list args, const char * format);
void print(const char * format, ...);
void printAt(int x,int y, const char * format, ...);
void drawBike(float x, float y, float z,
float len, float wid, float high,
float x_deg, float y_deg, float z_deg,
float tyre,float tyre_turn);
void drawFossil(float x,float y,float z,float angle,float color[3]);
GLuint loadBMP_custom(const char *imagepath);
class Vec3f {
private:
float v[3];
public:
Vec3f();
Vec3f(float x, float y, float z);
float &operator[](int index);
float operator[](int index) const;
Vec3f operator*(float scale) const;
Vec3f operator/(float scale) const;
Vec3f operator+(const Vec3f &other) const;
Vec3f operator-(const Vec3f &other) const;
Vec3f operator-() const;
const Vec3f &operator*=(float scale);
const Vec3f &operator/=(float scale);
const Vec3f &operator+=(const Vec3f &other);
const Vec3f &operator-=(const Vec3f &other);
float magnitude() const;
float magnitudeSquared() const;
Vec3f normalize() const;
float dot(const Vec3f &other) const;
Vec3f cross(const Vec3f &other) const;
};
Vec3f operator*(float scale, const Vec3f &v);
std::ostream &operator<<(std::ostream &output, const Vec3f &v);
//Represents an image
class Image {
public:
Image(char* ps, int w, int h);
~Image();
/* An array of the form (R1, G1, B1, R2, G2, B2, ...) indicating the
* color of each pixel in image. Color components range from 0 to 255.
* The array starts the bottom-left pixel, then moves right to the end
* of the row, then moves up to the next column, and so on. This is the
* format in which OpenGL likes images.
*/
char* pixels;
int width;
int height;
};
//Reads a bitmap image from file.
Image* loadBMP(const char* filename);
class Terrain {
private:
int w; //Width
int l; //Length
float** hs; //Heights
Vec3f** normals;
bool computedNormals; //Whether normals is up-to-date
public:
Terrain(int w2, int l2) {
w = w2;
l = l2;
hs = new float*[l];
for(int i = 0; i < l; i++) {
hs[i] = new float[w];
}
normals = new Vec3f*[l];
for(int i = 0; i < l; i++) {
normals[i] = new Vec3f[w];
}
computedNormals = false;
}
~Terrain() {
for(int i = 0; i < l; i++) {
delete[] hs[i];
}
delete[] hs;
for(int i = 0; i < l; i++) {
delete[] normals[i];
}
delete[] normals;
}
int width() {
return w;
}
int length() {
return l;
}
//Sets the height at (x, z) to y
void setHeight(int x, int z, float y) {
hs[z][x] = y;
computedNormals = false;
}
//Returns the height at (x, z)
float getHeight(int x, int z) {
return hs[z][x];
}
//Computes the normals, if they haven't been computed yet
void computeNormals() {
if (computedNormals) {
return;
}
//Compute the rough version of the normals
Vec3f** normals2 = new Vec3f*[l];
for(int i = 0; i < l; i++) {
normals2[i] = new Vec3f[w];
}
for(int z = 0; z < l; z++) {
for(int x = 0; x < w; x++) {
Vec3f sum(0.0f, 0.0f, 0.0f);
Vec3f out;
if (z > 0) {
out = Vec3f(0.0f, hs[z - 1][x] - hs[z][x], -1.0f);
}
Vec3f in;
if (z < l - 1) {
in = Vec3f(0.0f, hs[z + 1][x] - hs[z][x], 1.0f);
}
Vec3f left;
if (x > 0) {
left = Vec3f(-1.0f, hs[z][x - 1] - hs[z][x], 0.0f);
}
Vec3f right;
if (x < w - 1) {
right = Vec3f(1.0f, hs[z][x + 1] - hs[z][x], 0.0f);
}
if (x > 0 && z > 0) {
sum += out.cross(left).normalize();
}
if (x > 0 && z < l - 1) {
sum += left.cross(in).normalize();
}
if (x < w - 1 && z < l - 1) {
sum += in.cross(right).normalize();
}
if (x < w - 1 && z > 0) {
sum += right.cross(out).normalize();
}
normals2[z][x] = sum;
}
}
//Smooth out the normals
const float FALLOUT_RATIO = 0.5f;
for(int z = 0; z < l; z++) {
for(int x = 0; x < w; x++) {
Vec3f sum = normals2[z][x];
if (x > 0) {
sum += normals2[z][x - 1] * FALLOUT_RATIO;
}
if (x < w - 1) {
sum += normals2[z][x + 1] * FALLOUT_RATIO;
}
if (z > 0) {
sum += normals2[z - 1][x] * FALLOUT_RATIO;
}
if (z < l - 1) {
sum += normals2[z + 1][x] * FALLOUT_RATIO;
}
if (sum.magnitude() == 0) {
sum = Vec3f(0.0f, 1.0f, 0.0f);
}
normals[z][x] = sum;
}
}
for(int i = 0; i < l; i++) {
delete[] normals2[i];
}
delete[] normals2;
computedNormals = true;
}
//Returns the normal at (x, z)
Vec3f getNormal(int x, int z) {
if (!computedNormals) {
computeNormals();
}
return normals[z][x];
}
};
#endif