-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathproject1.cpp
575 lines (460 loc) · 15.7 KB
/
project1.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// fragment shading of sphere model
#include "Angel.h"
#include <iostream>
#include <cstdlib>
#include "SDL2/SDL.h"
#include "SDL2/SDL_opengl.h"
#include <chrono>
#include <thread>
#include <cmath>
typedef Angel::vec4 point4;
typedef Angel::vec4 color4;
// Constants
float SpeedFactorInitial = 0.2;
float SpeedFactorIncrement = 0.04;
float SpeedFactorMax = 1.2;
float BallTrajectoryInitial = -0.1;
const int ms_per_frame = 50; //20fps, my vm runs really slow
float speedFactor = SpeedFactorInitial;
float ballTrajectory = BallTrajectoryInitial; //slope of ball's path, factor of aspect ratio
int score[2] = {0,0}; //element 0 is player 1's score, element 1 is player 2's score
struct collisionInfo{
bool isColliding; //true if collision, false if no collision
bool isComingFromPlayer1; //true if player 1, false if player 2
float location; //location of the collision on the paddle. determines return path
collisionInfo() : isColliding(false), isComingFromPlayer1(false), location(0.0) {}
} collision;
void updateBallPosition(bool);
// Model and view matrices uniform location
GLuint mMatrix, vMatrix, pMatrix;
// Define global vaos and ebos
GLuint vao1, vao2, vao3, ebo1, ebo2, ebo3;
// Define programs
GLuint programP1, programP2, programB;
// Define model matrices
mat4 modelP1, modelP2, modelB;
// Create camera view variables
point4 at( 0.0, 0.0, 0.0, 1.0 );
point4 eye( 0.0, 0.0, 5.0, 1.0 );
vec4 up( 0.0, 10.0, 0.0, 0.0 );
GLfloat positionArray[]={
// Paddle
-0.5,-3.0,0.0,
-0.5,3.0,0.0,
0.5,3.0,0.0,
0.5,-3.0,0.0,
// Ball
-0.5,-0.5,0.0,
-0.5,0.5,0.0,
0.5,0.5,0.0,
0.5,-0.5,0.0
};
GLfloat colorArray[]={
// Paddle 1
1.0,0.0,0.0,0.0,
1.0,0.0,0.0,0.0,
1.0,0.0,0.0,0.0,
1.0,0.0,0.0,0.0,
// Paddle 2
0.0,0.0,1.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,1.0,0.0,
// Ball
1.0f,1.0f,0.0f,1.0f,
0.0f,0.0f,0.0f,1.0f,
0.0f,1.0f,1.0f,1.0f,
1.0f,0.0f,1.0f,1.0f
};
GLubyte elemsArray[]={
0,1,2,3
};
// Define Constants
GLuint NumVerticies = 4;
GLuint BallHeight = 1;
GLuint BallWidth = 1;
GLuint PaddleHeight = 6;
GLuint PaddleWidth = 1;
//----------------------------------------------------------------------------
// OpenGL initialization
void init()
{
// Load shaders and use the resulting shader program
programP1 = InitShader( "vshaderP1.glsl", "fshader.glsl" );
programP2 = InitShader( "vshaderP2.glsl", "fshader.glsl" );
programB = InitShader( "vshaderB.glsl", "fshader.glsl" );
// Define data members
GLuint vbo;
size_t posDataOffset, colorDataOffset, paddleElemsArray, ballElemsSize;
// --------------------------------------------------------------
// ------- V E R T E X A R R A Y O B J E C T 1 -------
// --------------------------------------------------------------
// Define offsets and sizes
posDataOffset = 0;
colorDataOffset = sizeof(positionArray);
// Use programP1
glUseProgram( programP1 );
// Generate and bind new vertex array object
glGenVertexArrays( 1,&vao1 );
glBindVertexArray( vao1 );
// Generate and bind new vertex buffer object and populate the buffer
glGenBuffers( 1,&vbo );
glBindBuffer( GL_ARRAY_BUFFER,vbo );
glBufferData( GL_ARRAY_BUFFER,sizeof(positionArray) + sizeof(colorArray),NULL,GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER,posDataOffset,sizeof(positionArray),positionArray );
glBufferSubData( GL_ARRAY_BUFFER,colorDataOffset,sizeof(colorArray),colorArray );
// Bind position attribute of vbo
GLuint in_position = glGetAttribLocation( programP1, "in_position" );
glVertexAttribPointer( in_position,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(posDataOffset) );
glEnableVertexAttribArray( in_position );
// Bind color attribute of vbo
GLuint in_color = glGetAttribLocation( programP1, "in_color" );
glVertexAttribPointer( in_color,4,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(colorDataOffset) );
glEnableVertexAttribArray( in_color );
// Generate and bind element buffer object
glGenBuffers( 1,&ebo1 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,ebo1 );
glBufferData( GL_ELEMENT_ARRAY_BUFFER,sizeof(elemsArray),elemsArray,GL_STATIC_DRAW );
// Release bind to vao1 and programP1
glBindVertexArray( 0 );
glUseProgram( 0 );
// --------------------------------------------------------------
// --------------------------------------------------------------
// ------- V E R T E X A R R A Y O B J E C T 2 -------
// --------------------------------------------------------------
// Define new offsets
posDataOffset += 0; // Duplicating last paddle
colorDataOffset += sizeof(GLfloat) * 4 * NumVerticies; // Different color
// Use programP2
glUseProgram( programP2 );
// Generate new vertex array object
glGenVertexArrays( 1,&vao2 );
glBindVertexArray( vao2 );
// Bind vertex buffer object
// --Use same vbo as vao1 (no new buffer has been bound)
// Bind attributes to vertex array
glVertexAttribPointer(in_position,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(posDataOffset));
glEnableVertexAttribArray( in_position );
glVertexAttribPointer(in_color,4,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(colorDataOffset));
glEnableVertexAttribArray( in_color );
// Generate and bind element buffer object
glGenBuffers( 1,&ebo2 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,ebo2 );
glBufferData( GL_ELEMENT_ARRAY_BUFFER,sizeof(elemsArray),elemsArray,GL_STATIC_DRAW );
// Release bind to vao2 and programP2
glBindVertexArray( 0 );
glUseProgram( 0 );
// --------------------------------------------------------------
// --------------------------------------------------------------
// ------- V E R T E X A R R A Y O B J E C T 3 -------
// --------------------------------------------------------------
// Define new offsets
posDataOffset += sizeof(GLfloat) * 3 * NumVerticies;
colorDataOffset += sizeof(GLfloat) * 4 * NumVerticies;
// Use programB
glUseProgram( programB );
// Generate new vertex array object
glGenVertexArrays( 1,&vao3 );
glBindVertexArray( vao3 );
// Bind vertex buffer object
// --Use same vbo as vao1 (no new buffer has been bound)
// Bind attributes to vertex array
glVertexAttribPointer(in_position,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(posDataOffset));
glEnableVertexAttribArray( in_position );
glVertexAttribPointer(in_color,4,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(colorDataOffset));
glEnableVertexAttribArray( in_color );
// Generate and bind element buffer object
glGenBuffers( 1,&ebo3 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,ebo3 );
glBufferData( GL_ELEMENT_ARRAY_BUFFER,sizeof(elemsArray),elemsArray,GL_STATIC_DRAW );
// Release bind to vao3 and programB
glBindVertexArray( 0 );
glUseProgram( 0 );
// --------------------------------------------------------------
// Retrieve transformation uniform variable locations
mMatrix = glGetUniformLocation( programP1, "modelMatrix" );
vMatrix = glGetUniformLocation( programP1, "viewMatrix" );
pMatrix = glGetUniformLocation( programP1, "projectionMatrix" );
// Initialize model matrices to their correct positions
modelP1 = modelP2 = modelB = identity();
modelP1 = modelP1 * Translate(-10.0,0.0,0.0);
modelP2 = modelP2 * Translate(10.0,0.0,0.0);
glEnable( GL_DEPTH_TEST );
glClearColor( 0.0, 0.0, 0.0, 1.0 ); // black background
}
//----------------------------------------------------------------------------
void printMat4(mat4 m){
std::cout<<" "<<m[0][0]<<" "<<m[0][1]<<" "<<m[0][2]<<" "<<m[0][3]<<std::endl;
std::cout<<" "<<m[1][0]<<" "<<m[1][1]<<" "<<m[1][2]<<" "<<m[1][3]<<std::endl;
std::cout<<" "<<m[2][0]<<" "<<m[2][1]<<" "<<m[2][2]<<" "<<m[2][3]<<std::endl;
std::cout<<" "<<m[3][0]<<" "<<m[3][1]<<" "<<m[3][2]<<" "<<m[3][3]<<std::endl;
}
//----------------------------------------------------------------------------
void resetGame(){
speedFactor=SpeedFactorInitial;
modelB = identity();
collision.isColliding = false;
collision.isComingFromPlayer1 = false;
collision.location = 0.0;
ballTrajectory = BallTrajectoryInitial;
updateBallPosition(true);
}
//----------------------------------------------------------------------------
void display( SDL_Window* screen ){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Define view
mat4 view = LookAt( eye, at, up );
// Draw elements of vao1
glUseProgram( programP1 );
glBindVertexArray( vao1 );
glUniformMatrix4fv( mMatrix, 1, GL_TRUE, modelP1 );
glUniformMatrix4fv( vMatrix, 1, GL_TRUE, view );
glDrawElements( GL_TRIANGLE_FAN,sizeof(elemsArray),GL_UNSIGNED_BYTE,0 );
glBindVertexArray( 0 );
glUseProgram( 0 );
// Draw elements of vao2
glUseProgram( programP2 );
glBindVertexArray( vao2 );
glUniformMatrix4fv( mMatrix, 1, GL_TRUE, modelP2 );
glUniformMatrix4fv( vMatrix, 1, GL_TRUE, view );
glDrawElements( GL_TRIANGLE_FAN,sizeof(elemsArray),GL_UNSIGNED_BYTE,0 );
glBindVertexArray( 0 );
glUseProgram( 0 );
// Draw elements of vao3
glUseProgram( programB );
glBindVertexArray( vao3 );
glUniformMatrix4fv( mMatrix, 1, GL_TRUE, modelB );
glUniformMatrix4fv( vMatrix, 1, GL_TRUE, view );
glDrawElements( GL_TRIANGLE_FAN,sizeof(elemsArray),GL_UNSIGNED_BYTE,0 );
glBindVertexArray( 0 );
glUseProgram( 0 );
// Release binds and swap buffers
glBindVertexArray( 0 );
glUseProgram( 0 );
glFlush();
SDL_GL_SwapWindow(screen);
}
//----------------------------------------------------------------------------
void input(SDL_Window* screen ){
SDL_Event event;
while (SDL_PollEvent(&event)){//Handling the keyboard
switch (event.type){
case SDL_QUIT:exit(0);break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:exit(0);
case SDLK_w://paddle 1 up;
if (modelP1[1][3] < 7.0) {
modelP1 = modelP1 * Translate(0.0,1.0,0.0);
}
//std::cout<<"modelP1="<<std::endl;
//printMat4(modelP1);
//std::cout<<std::endl;
break;
case SDLK_s://paddle 1 down;
if (modelP1[1][3] > -7.0) {
modelP1 = modelP1 * Translate(0.0,-1.0,0.0);
}
//std::cout<<"modelP1="<<std::endl;
//printMat4(modelP1);
//std::cout<<std::endl;
break;
case SDLK_i://paddle 2 up
if (modelP2[1][3] < 7.0) {
modelP2 = modelP2 * Translate(0.0,1.0,0.0);
}
break;
case SDLK_k://paddle 2 down
if (modelP2[1][3] > -7.0) {
modelP2 = modelP2 * Translate(0.0,-1.0,0.0);
}
break;
case SDLK_r://new game
std::cout<<"*new game*\n";
score[0]=0;
score[1]=0;
modelP1 = modelP2 = identity();
modelP1 = modelP1 * Translate(-10.0,0.0,0.0);
modelP2 = modelP2 * Translate(10.0,0.0,0.0);
resetGame();
break;
}
}
}
}
//----------------------------------------------------------------------------
collisionInfo detectCollision(){
// Get positions
int ballLx, ballRx, p1Lx, p1Rx, p2Lx, p2Rx;
ballLx = 10.0*(modelB[0][3] - BallWidth/2.0);
ballRx = 10.0*(modelB[0][3] + BallWidth/2.0);
p1Lx = 10.0*(modelP1[0][3] - PaddleWidth/2.0);
p1Rx = 10.0*(modelP1[0][3] + PaddleWidth/2.0);
p2Lx = 10.0*(modelP2[0][3] - PaddleWidth/2.0);
p2Rx = 10.0*(modelP2[0][3] + PaddleWidth/2.0);
float ballCy, p1Cy, p2Cy;
ballCy = modelB[1][3];
p1Cy = modelP1[1][3];
p2Cy = modelP2[1][3];
// Check for collision with paddle 1
if (ballLx >= p1Lx && ballLx <= p1Rx){
float heightOfImpact = ballCy - p1Cy;
if (abs(heightOfImpact) < ((PaddleHeight+BallHeight)/2)){
collision.isColliding=true;
collision.isComingFromPlayer1=true;
collision.location=heightOfImpact;
}
} // Check for collision with paddle 2
else if (ballRx >= p2Lx && ballRx <= p2Rx){
float heightOfImpact = ballCy - p2Cy;
if (abs(heightOfImpact) < ((PaddleHeight+BallHeight)/2)){
collision.isColliding=true;
collision.isComingFromPlayer1=false;
collision.location=heightOfImpact;
}
} // No collision detected
else {
collision.isColliding=false;
}
return collision;
}
//----------------------------------------------------------------------------
void updateScore(){
int ballPositionX = 10.0*(modelB[0][3]);
int leftWall = -130;
int rightWall = 130;
// Player 1 scores
if (ballPositionX >= rightWall){
score[0]++;
std::cout<<"Player 1 scored!\n";
std::cout<<"Score is "<<score[0]<<" : "<<score[1]<<"\n\n";
resetGame();
} // Player 2 scores
else if (ballPositionX <= leftWall){
score[1]++;
std::cout<<"Player 2 scored!\n";
std::cout<<"Score is "<<score[0]<<" : "<<score[1]<<"\n\n";
resetGame();
}
}
//----------------------------------------------------------------------------
void updateSpeed(){
if (collision.isColliding && speedFactor <= SpeedFactorMax){
speedFactor = speedFactor + SpeedFactorIncrement;
}
}
//----------------------------------------------------------------------------
vec3 calculateTrajectory(){
// Define data members
vec3 t(0.0);
int direction;
if (collision.isComingFromPlayer1){
direction = 1;
} else {
direction = -1;
}
t.x = direction*speedFactor;
t.y = collision.location/8.0;
return t;
}
//----------------------------------------------------------------------------
void updateBallPosition(bool forceUpdate){
static vec3 t(-speedFactor,BallTrajectoryInitial,0.0);
if (forceUpdate){
t = vec3(-speedFactor, BallTrajectoryInitial, 0.0);
} else if (collision.isColliding){
t = calculateTrajectory();
modelB = modelB * Translate(t.x, t.y, t.z);
} else {
int temp = 10.0*(modelB[1][3]);
if ((temp <= 100 && temp >= 92) || (temp >= -100 && temp <= -92)){
//hitting the ceiling or floor
t.y = -t.y;
}
modelB = modelB * Translate(t.x, t.y, t.z);
}
}
//----------------------------------------------------------------------------
void
reshape( int width, int height )
{
glViewport( 0, 0, width, height );
GLfloat left = -10.0, right = 10.0;
GLfloat top = 10.0, bottom = -10.0;
GLfloat zNear = -20.0, zFar = 20.0;
GLfloat aspect = GLfloat(width)/height;
if ( aspect > 1.0 ) {
left *= aspect;
right *= aspect;
} else {
top /= aspect;
bottom /= aspect;
}
mat4 projection = Ortho( left, right, bottom, top, zNear, zFar );
// Bind new projection to each program
glUseProgram( programP1 );
glUniformMatrix4fv( pMatrix, 1, GL_TRUE, projection );
glUseProgram( programP2 );
glUniformMatrix4fv( pMatrix, 1, GL_TRUE, projection );
glUseProgram( programB );
glUniformMatrix4fv( pMatrix, 1, GL_TRUE, projection );
glUseProgram( 0 );
}
//----------------------------------------------------------------------------
int main( int argc, char **argv )
{
//SDL window and context management
SDL_Window *window;
//used in main loop
int sleepTime = 0;
int ticks_0, ticks_1;
if(SDL_Init(SDL_INIT_VIDEO)<0){//initilizes the SDL video subsystem
fprintf(stderr,"Unable to create window: %s\n", SDL_GetError());
SDL_Quit();
exit(1);//die on error
}
//create window
window = SDL_CreateWindow(
"Beamer's Crew - Project 1", //Window title
SDL_WINDOWPOS_UNDEFINED, //initial x position
SDL_WINDOWPOS_UNDEFINED, //initial y position
512, //width, in pixels
384, //height, in pixels
SDL_WINDOW_OPENGL //flags to be had
);
//check window creation
if(window==NULL){
fprintf(stderr,"Unable to create window: %s\n",SDL_GetError());
}
//creates opengl context associated with the window
SDL_GLContext glcontext=SDL_GL_CreateContext(window);
//initializes glew
glewExperimental=GL_TRUE;
if(glewInit()){
fprintf(stderr, "Unable to initalize GLEW");
exit(EXIT_FAILURE);
}
init();
while (true) {
ticks_0 = SDL_GetTicks();
input(window);
collision = detectCollision();
updateScore();
updateSpeed();
updateBallPosition(false);
reshape(512,384);
display(window);
ticks_1 = SDL_GetTicks();
sleepTime = ms_per_frame - (ticks_1 - ticks_0);
while (sleepTime < 0){
sleepTime = sleepTime + ms_per_frame;
//std::cout<<"*Frame Dropped*\n";
}
std::chrono::milliseconds dura(sleepTime);
std::this_thread::sleep_for(dura);
}
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}