/
Asteroid3D.cpp
323 lines (223 loc) · 8.18 KB
/
Asteroid3D.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//OLD
class Asteroid{
public:
Asteroid();
~Asteroid();
void Render(vect2,bool center);
void DoPhysics(float ticks);
private:
vect2 position;
vect3 orient;
float angledeg;
float anglerad;
float rotation;
uint32_t num_faces;
vect3 vface[20];
uint32_t num_bfaces;
vect3 vbface[20];
GLuint textureid;
uint8_t bmpdata[BMP_SIZE_128];
float ticks_normal;
};
Asteroid::Asteroid(){
position.x = 0;
position.y = 0;
ticks_normal = 20000;
rotation = 2;
angledeg = 0;
//Generate texture
LoadFileBitmap("bitmaps/rock1.bmp",bmpdata, BMP_SIZE_128);
glGenTextures(1, &textureid);
glBindTexture(GL_TEXTURE_2D, textureid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_BGR, GL_UNSIGNED_BYTE, bmpdata);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
num_faces = 13;
vface[0].z = 0.8f;
float step = (360.0f/num_faces);
float param = 0;
float scale = 1;
for (uint8_t i=1;i<=num_faces;i++){
param += step;
scale = (rand()%200)/200.0f + 0.9;
vface[i].x = scale*sinf(param*PI/180);
vface[i].y = scale*cosf(param*PI/180);
vface[i].z = 0.2f;
}
num_bfaces = 13;
vbface[0].z = -0.8f;
step = (360.0f/num_bfaces);
param = 0;
scale = 1;
for (uint8_t i=1;i<=num_bfaces;i++){
param += step;
scale = (rand()%200)/200.0f + 0.9;
vbface[i].x = scale*sinf(param*PI/180);
vbface[i].y = scale*cosf(param*PI/180);
vbface[i].z = -0.2f;
}
/*
vface[1].x = 1;
vface[2].y = 1;
vface[3].x = -1;
vface[4].y = -1;
vface[5].y = -1;
vface[5].x = -1;*/
}
Asteroid::~Asteroid(){
}
void Asteroid::Render(vect2 relative,bool center){
glPushMatrix();
//glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
//glLoadIdentity ();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
//glLoadIdentity(); // Reset The Current Matrix
//glTranslatef(0.0f,0.0f,-5.0f); // Move Into The Screen 5 Units
//glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
//glShadeModel(GL_SMOOTH); // Enable Smooth Shading
//glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
//glClearDepth(1.0f); // Depth Buffer Setup
//glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective
if (center){
glTranslatef(-relative.x,-relative.y,0);
}else{
glTranslatef(position.x-relative.x,position.y-relative.y,0);
}
glRotatef(angledeg,1.0f,1.0f,1.0f);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_BGR, GL_UNSIGNED_BYTE, bmpdata);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // Set The Blending Function For Translucency
glEnable(GL_BLEND);
//glCullFace(GL_FRONT_AND_BACK);
//glDisable(GL_DEPTH_TEST);
//glBindTexture(GL_TEXTURE_2D, textureid);
glColor4f(1,1,1,1);
/*
glBegin(GL_TRIANGLES);
glTexCoord2f(0,0);
glVertex3f(-1, -1, -0.1f);
glTexCoord2f(1.0, 0);
glVertex3f(1.0, -1, -0.1f);
glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, -0.0f);
glEnd();
*/
uint8_t texture_swap = 1;
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0 , 0); glVertex3fv((GLfloat*)&vface[0]);
glTexCoord2f(1.0, 0); glVertex3fv((GLfloat*)&vface[1]);
for (int i=2;i<=num_faces;i++){
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vface[i]);
}
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vface[1]);
glEnd();
//Bottom
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0 , 0); glVertex3fv((GLfloat*)&vbface[0]);
glTexCoord2f(1.0, 0); glVertex3fv((GLfloat*)&vbface[1]);
for (int i=2;i<=num_bfaces;i++){
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vbface[i]);
}
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vbface[1]);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0 , 0); glVertex3fv((GLfloat*)&vface[1]);
glTexCoord2f(1.0, 0); glVertex3fv((GLfloat*)&vbface[1]);
for (int i=2;i<=num_bfaces;i++){
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vface[i]);
glVertex3fv((GLfloat*)&vbface[i]);
}
if (texture_swap){
texture_swap = 0;
glTexCoord2f(1.0, 1.0);
}else{
texture_swap = 1;
glTexCoord2f(1.0, 0);
}
glVertex3fv((GLfloat*)&vface[1]);
glEnd();
/*
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();
*/
//glDisable(GL_TEXTURE_2D);
//glEnable(GL_DEPTH_TEST);
glPopMatrix();
}
void Asteroid::DoPhysics(float ticks){
float dt = ticks/ticks_normal;
angledeg += (rotation * dt);
anglerad = (angledeg / 360.0f)*2*3.1415926535;
//printf("ticks : %8.3f\n",ticks);
//printf("Angledeg: %8.3f\n",angledeg);
}