-
Notifications
You must be signed in to change notification settings - Fork 0
/
fishA.cpp
282 lines (239 loc) · 9.73 KB
/
fishA.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef __APPLE__
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif
#include "fishA.h"
#include "math.h"
// setup the static variables
GLfloat fishA::material[4] = {1.f, 1.f, 1.f, 1.f};
GLfloat fishA::shininess = 120.f;
// Debug only, the other one should be used
fishA::fishA() {
// Fish Speed
xInc = 0.0;
yInc = 0.1;
zInc = -0.1;
// Fish Position
x = 0;
y = 0;
z = -15;
// Tail Animation
tailAngle = 40.0f;
modeTail=0;
angleMax = 45;
}
// position, speed and color as parameters
fishA::fishA(float posx, float posy, float posz, float velx, float vely, float velz, float A, float B, float C) {
x = posx;
y = posy;
z = posz; // position
xInc = velx;
yInc = vely;
zInc = velz; // speed
cpos1 = A;
cpos2 = B;
cpos3 = C; // color
// Tail Animation
tailAngle = 40.0f;
modeTail = 0;
angleMax = 45;
}
void fishA::draw(void) {
// increment the fish position
x += xInc;
y += yInc;
z += zInc;
glColor4f(cpos1, cpos2, cpos3,1); // semi-transparent blue bubble
glPushMatrix();
glTranslatef(x,y,z);
getCrossProduct(xInc,yInc,zInc,-1,0,0); // get cross product
// glRotatef(getAngle(xInc,yInc,zInc,1,0,0), xcp, ycp, zcp); // rotate to look to where it is moving
if((xcp!=0.0)||(ycp!=0.0)||(zcp!=0.0)) { // orient fish to look where it's going
glRotatef(-getAngle(xInc,yInc,zInc,-1,0,0), xcp, ycp, zcp);
}
else {
if(getAngle(xInc,yInc,zInc,-1,0,0)>179.5)
glRotatef(180.0, 0, 1.0, 0);
}
// getCrossProduct(-1,0.2,0,1,0,0); // get cross product
// glRotatef(getAngle(-1,0.2,0,1,0,0), xcp, ycp, zcp); // rotate to look to where it is moving
// std::cout << "Angle : " << getAngle(xInc,yInc,zInc,-1,0,0) << std::endl;
// std::cout << "vector : " << xcp << " " << ycp << " " << zcp << std::endl;
// set up the material properties (only front needs to be set)
glMaterialfv(GL_FRONT, GL_AMBIENT, material);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material);
glMaterialfv(GL_FRONT, GL_SPECULAR, material);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
glEnable(GL_LIGHTING);
GLfloat amDef[4], spDef[4];
GLfloat am[] = {1, 1, 0, 1};
GLfloat sp[] = {0, 0.5, 1, 0.5};
glGetMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amDef);
glGetMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spDef);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, am);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sp);
// set up vertex arrays
glVertexPointer(3, GL_FLOAT, 0, vertex);
glNormalPointer(GL_FLOAT, 0, normal);
glTexCoordPointer(2, GL_FLOAT, 0, texels);
// enable vertex arrays
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// draw first side of the fish
glFrontFace(GL_CCW);
drawSide();
// draw second side of the fish
glScalef(1.0f, 1.0f, -1.0f);
glFrontFace(GL_CW);
drawSide();
// work out new fish tail position
/*GLfloat pt = sin(tailAngle * 3.14159 / 180);
tailAngle += tailAngleInc;
if (tailAngle < -tailAngleCutOff || tailAngle > tailAngleCutOff)
tailAngleInc *= -1; */
if(modeTail==0) {
tailAngle = tailAngle+5;
if(tailAngle>=angleMax)
modeTail=1;
}
else {
tailAngle = tailAngle-5;
if (tailAngle<=-angleMax)
modeTail=0;
}
GLfloat pt = sin(tailAngle * PI / 180);
// draw one side of flexible part of the tail
vertex[143] = vertex[152] = vertex[149] = vertex[158] = vertex[167] = pt;
glDrawArrays(GL_TRIANGLES, 6 + (4 * 6) + (3 * 5), 3 * 4);
glScalef(1.0f, 1.0f, -1.0f);
// draw second side of flexible part of the tail
glFrontFace(GL_CCW);
vertex[143] = vertex[152] = vertex[149] = vertex[158] = vertex[167] = -pt;
glDrawArrays(GL_TRIANGLES, 6 + (4 * 6) + (3 * 5), 3 * 4);
// disable all vertex arrays and texturing
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glPopMatrix();
}
void fishA::drawSide(void) {
glDrawArrays(GL_TRIANGLES, 0, 3 * 2);
glDrawArrays(GL_QUADS, 6, 4 * 6);
glDrawArrays(GL_TRIANGLES, 6 + 4 * 6, 3 * 5);
}
GLfloat fishA::vertex[] ={
// 2 | | |
0.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 0.45f,
// 1 | | |
0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.45f, 0.5f, 0.5f, 0.0f,
// 3 | | |
0.5f, 0.0f, 0.45f, 0.5f, -0.5f, 0.0f, 1.5f, -1.0f, 0.0f, 1.5f, 0.0f, 0.45f,
// 4 | | |
0.5f, 0.5f, 0.0f, 0.5f, 0.0f, 0.45f, 1.5f, 0.0f, 0.45f, 1.5f, 1.0f, 0.0f,
// 6 | | |
1.5f, 0.0f, 0.45f, 1.5f, -1.0f, 0.0f, 2.5f, -1.0f, 0.0f, 2.5f, 0.0f, 0.45f,
// 5 | | |
1.5f, 1.0f, 0.0f, 1.5f, 0.0f, 0.45f, 2.5f, 0.0f, 0.45f, 2.5f, 1.0f, 0.0f,
// 8 | | |
2.5f, 0.0f, 0.45f, 2.5f, -1.0f, 0.0f, 3.5f, -0.5f, 0.0f, 3.5f, 0.0f, 0.2f,
// 7 | | |
2.5f, 1.0f, 0.0f, 2.5f, 0.0f, 0.45f, 3.5f, 0.0f, 0.2f, 3.5f, 0.5f, 0.0f,
// 9
// c | | |
3.5f, 0.5f, 0.0f, 3.5f, 0.0f, 0.2f, 3.75f, 0.4f, 0.1f,
// b | | |
3.5f, 0.0f, 0.2f, 3.75f, -0.4f, 0.1f, 3.75f, 0.4f, 0.1f,
// a | | |
3.5f, 0.0f, 0.2f, 3.5f, -0.5f, 0.0f, 3.75f, -0.4f, 0.1f,
// e | | |
3.5f, 0.5f, 0.0f, 3.75f, 0.4f, 0.1f, 3.75f, 0.4f, -0.1f,
// d | | |
3.5f, -0.5f, 0.0f, 3.75f, -0.4f, -0.1f, 3.75f, -0.4f, 0.1f,
// 10
// F | | |
3.75f, 0.4f, 0.1f, 3.75f, -0.4f, 0.1f, 4.3f, -0.8f, 0.0f,
// G | | |
3.75f, 0.4f, 0.1f, 4.3f, -0.8f, 0.0f, 4.3f, 0.8f, 0.0f,
// I | | |
3.75f, 0.4f, 0.1f, 4.3f, 0.8f, 0.0f, 3.75f, 0.4f, -0.1f,
// H | | |
3.75f, -0.4f, -0.1f, 4.3f, -0.8f, 0.0f, 3.75f, -0.4f, 0.1f
};
GLfloat fishA::normal[] ={
// 2 | | |
-1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f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f, 0.0f, 1.0f, 0.0f,
// H | | |
0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f
};
GLfloat fishA::texels[] ={
// 2 | | |
126.f / 128.f, 68.f / 128.0f, 119.f / 128.f, 47.f / 128.f, 112.f / 128.f, 68.f / 128.f,
// 1 | | |
126.f / 128.f, 68.f / 128.0f, 112.f / 128.f, 68.f / 128.f, 117.f / 128.f, 84.f / 128.f,
// 4 | | |
112.f / 128.f, 68.f / 128.f, 119.f / 128.f, 47.f / 128.f, 86.f / 128.f, 8.f / 128.f, 84.f / 128.f, 68.f / 128.f,
// 3 | | |
117.f / 128.f, 84.f / 128.f, 112.f / 128.f, 68.f / 128.f, 84.f / 128.f, 68.f / 128.f, 83.f / 128.f, 120.f / 128.f,
// 6 | | |
84.f / 128.f, 68.f / 128.f, 86.f / 128.f, 8.f / 128.f, 52.f / 128.f, 15.f / 128.f, 53.f / 128.f, 68.f / 128.f,
// 5 | | |
83.f / 128.f, 120.f / 128.f, 84.f / 128.f, 68.f / 128.f, 53.f / 128.f, 68.f / 128.f, 54.f / 128.f, 110.f / 128.f,
// 8 | | |
53.f / 128.f, 68.f / 128.f, 52.f / 128.f, 15.f / 128.f, 26.f / 128.f, 54.f / 128.f, 25.f / 128.f, 69.f / 128.f,
// 7 | | |
54.f / 128.f, 110.f / 128.f, 53.f / 128.f, 68.f / 128.f, 25.f / 128.f, 69.f / 128.f, 28.f / 128.f, 76.f / 128.f,
// 9
// c | | |
28.f / 128.f, 74.f / 128.f, 25.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// b | | |
25.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// a | | |
25.f / 128.f, 69.f / 128.f, 26.f / 128.f, 54.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// e | | |
26.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// d | | |
26.f / 128.f, 54.f / 128.f, 20.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// 10
// F | | |
20.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f, 5.f / 128.f, 69.f / 128.f,
// G | | |
20.f / 128.f, 69.f / 128.f, 5.f / 128.f, 69.f / 128.f, 5.f / 128.f, 69.f / 128.f,
// I | | |
20.f / 128.f, 69.f / 128.f, 5.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f,
// H | | |
20.f / 128.f, 69.f / 128.f, 5.f / 128.f, 69.f / 128.f, 20.f / 128.f, 69.f / 128.f
};