source: opengl-game/pong.cpp@ 7ee66ea

feature/imgui-sdl points-test
Last change on this file since 7ee66ea was 9d22ee9, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Finish implementing pong by allowing the ball to bounce off at an angle if it hits a corner of the paddle

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[9e81839]1#include "logger.h"
2
3#include <GL/glew.h>
4#include <GLFW/glfw3.h>
5
6#include <cstdio>
7#include <iostream>
8#include <fstream>
9#include <cmath>
10
11using namespace std;
12
13GLuint loadShader(GLenum type, string file);
14GLuint createDataBuffer(size_t size, GLfloat* data);
15GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo);
16
[4046b51]17GLfloat* createCirclePoints(unsigned int smoothness, GLfloat centerX, GLfloat centerY, GLfloat radius);
18GLfloat* createCircleColors(unsigned int smoothness);
19
20const double PI = 3.1415926535897;
[9e81839]21const bool FULLSCREEN = false;
22
23void glfw_error_callback(int error, const char* description) {
24 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
25}
26
27int main(int argc, char* argv[]) {
28 cout << "New OpenGL Game" << endl;
29
30 if (!restart_gl_log()) {}
31 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
32
33 glfwSetErrorCallback(glfw_error_callback);
34 if (!glfwInit()) {
35 fprintf(stderr, "ERROR: could not start GLFW3\n");
36 return 1;
37 }
38
39#ifdef __APPLE__
40 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
41 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
42 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
43 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
44#endif
45
46 glfwWindowHint(GLFW_SAMPLES, 4);
47
48 GLFWwindow* window = NULL;
49
50 int width = 640;
51 int height = 480;
52
53 if (FULLSCREEN) {
54 GLFWmonitor* mon = glfwGetPrimaryMonitor();
55 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
56
57 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
58 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
59
60 width = vmode->width;
61 height = vmode->height;
62 } else {
63 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
64 }
65
66 if (!window) {
67 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
68 glfwTerminate();
69 return 1;
70 }
71 glfwMakeContextCurrent(window);
72 glewExperimental = GL_TRUE;
73 glewInit();
74
75 // glViewport(0, 0, width*2, height*2);
76
77 const GLubyte* renderer = glGetString(GL_RENDERER);
78 const GLubyte* version = glGetString(GL_VERSION);
79 printf("Renderer: %s\n", renderer);
80 printf("OpenGL version supported %s\n", version);
81
82 glEnable(GL_DEPTH_TEST);
83 glDepthFunc(GL_LESS);
84
85 glEnable(GL_CULL_FACE);
86 // glCullFace(GL_BACK);
87 // glFrontFace(GL_CW);
88
[237ec28]89 unsigned int numBallPoints = 36;
[9e81839]90
[237ec28]91 GLfloat ballRadius = 0.2f;
92 GLfloat paddleThickness = 0.1f;
93
94 GLfloat* points = createCirclePoints(numBallPoints, 0.0f, 0.0f, ballRadius);
[4046b51]95 GLfloat* colors = createCircleColors(numBallPoints);
[9e81839]96
97 GLfloat points_paddle[] = {
[363d5ff]98 -1.0f, 0.15f, 0.0f,
99 -1.0f, -0.15f, 0.0f,
100 -0.9f, -0.15f, 0.0f,
101 -1.0f, 0.15f, 0.0f,
102 -0.9f, -0.15f, 0.0f,
103 -0.9f, 0.15f, 0.0f,
[9e81839]104 };
105
106 GLfloat colors_paddle[] = {
107 1.0, 0.0, 0.0,
[eeece4e]108 1.0, 0.0, 0.0,
109 1.0, 0.0, 0.0,
110 1.0, 0.0, 0.0,
111 1.0, 0.0, 0.0,
112 1.0, 0.0, 0.0,
[9e81839]113 };
114
115 GLfloat model[] = {
116 1.0f, 0.0f, 0.0f, 0.0f, // column 1
117 0.0f, 1.0f, 0.0f, 0.0f, // column 2
118 0.0f, 0.0f, 1.0f, 0.0f, // column 3
[eeece4e]119 0.0f, 0.0f, 0.0f, 1.0f, // column 4
[9e81839]120 };
121
[237ec28]122 GLuint ball_points_vbo = createDataBuffer(numBallPoints*9*sizeof(GLfloat), points);
123 GLuint ball_colors_vbo = createDataBuffer(numBallPoints*9*sizeof(GLfloat), colors);
[9e81839]124 GLuint ball_vao = createArrayBuffer(ball_points_vbo, ball_colors_vbo);
125
126 GLuint paddle_points_vbo = createDataBuffer(sizeof(points_paddle), points_paddle);
127 GLuint paddle_colors_vbo = createDataBuffer(sizeof(colors_paddle), colors_paddle);
128 GLuint paddle_vao = createArrayBuffer(paddle_points_vbo, paddle_colors_vbo);
129
130 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
131 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
132
133 GLuint shader_program = glCreateProgram();
134 glAttachShader(shader_program, vs);
135 glAttachShader(shader_program, fs);
136
137 glLinkProgram(shader_program);
[eeece4e]138 glUseProgram(shader_program);
[9e81839]139
140 GLint location = glGetUniformLocation(shader_program, "model");
141
[9d22ee9]142 float speedX = 1.0f;
143 float speedY = 0.0f;
144 float last_position_x = 0.0f;
145 float last_position_y = 0.0f;
[363d5ff]146 float last_paddle_pos = 0.0f;
[9e81839]147
148 double previous_seconds = glfwGetTime();
149 while (!glfwWindowShouldClose(window)) {
150 double current_seconds = glfwGetTime();
151 double elapsed_seconds = current_seconds - previous_seconds;
152 previous_seconds = current_seconds;
153
[9d22ee9]154 if (last_position_x > 1.0f-ballRadius) {
155 speedX = -speedX;
[237ec28]156 }
[9d22ee9]157 if (last_position_y+ballRadius>1.0f || last_position_y-ballRadius<-1.0f) {
158 speedY = -speedY;
[9e81839]159 }
160
[9d22ee9]161 if (last_position_x < -1.0f+ballRadius+paddleThickness &&
162 last_position_x > -1.0f) {
163 if (fabs(last_paddle_pos-last_position_y) < 0.15f) {
164 speedX = -speedX;
165 } else {
166 // if the ball hits the paddle on a corner, make it bounce off
167 // at an angle
168 float horDist = fabs(paddleThickness-1.0f-last_position_x);
169 float verDist1 = fabs(last_paddle_pos+0.15f-last_position_y);
170 float verDist2 = fabs(last_paddle_pos-0.15f-last_position_y);
171
172 float dist1 = pow(horDist, 2)+pow(verDist1, 2);
173 float dist2 = pow(horDist, 2)+pow(verDist2, 2);
174
175 if (dist1 < ballRadius) {
176 if (speedX == -1.0f || speedY == 0.7f) {
177 speedX = 0.7f;
178 speedY = 0.7f;
179 } else {
180 speedX = 1.0f;
181 speedY = 0.0f;
182 }
183 } else if (dist2 < ballRadius) {
184 if (speedX == -1.0f || speedY == -0.7f) {
185 speedX = 0.7f;
186 speedY = -0.7f;
187 } else {
188 speedX = 1.0f;
189 speedY = 0.0f;
190 }
191 }
192 }
193 }
[237ec28]194
[9e81839]195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196
[eeece4e]197 model[12] = 0.0f;
[363d5ff]198 model[13] = last_paddle_pos;
[eeece4e]199 glUniformMatrix4fv(location, 1, GL_FALSE, model);
200
201 glBindVertexArray(paddle_vao);
[9e81839]202 glEnableVertexAttribArray(0);
203 glEnableVertexAttribArray(1);
204
[eeece4e]205 glDrawArrays(GL_TRIANGLES, 0, sizeof(points_paddle)/sizeof(GLfloat)/3);
[9e81839]206
[9d22ee9]207 model[12] = last_position_x + speedX*elapsed_seconds;
208 last_position_x = model[12];
209 model[13] = last_position_y + speedY*elapsed_seconds;
210 last_position_y = model[13];
[eeece4e]211 glUniformMatrix4fv(location, 1, GL_FALSE, model);
212
213 glBindVertexArray(ball_vao);
[9e81839]214 glEnableVertexAttribArray(0);
215 glEnableVertexAttribArray(1);
216
[237ec28]217 glDrawArrays(GL_TRIANGLES, 0, numBallPoints*3);
[9e81839]218
219 glfwPollEvents();
220 glfwSwapBuffers(window);
221
222 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
223 glfwSetWindowShouldClose(window, 1);
224 }
[363d5ff]225 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_UP)) {
226 last_paddle_pos += 1.0f * elapsed_seconds;
227 if (last_paddle_pos > 0.85f) {
228 last_paddle_pos = 0.85f;
229 }
230 }
231 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_DOWN)) {
232 last_paddle_pos -= 1.0f * elapsed_seconds;
233 if (last_paddle_pos < -0.85f) {
234 last_paddle_pos = -0.85f;
235 }
236 }
[9e81839]237 }
238
239 glfwTerminate();
[4046b51]240
241 delete points;
242 delete colors;
243
[9e81839]244 return 0;
245}
246
247GLuint loadShader(GLenum type, string file) {
248 cout << "Loading shader from file " << file << endl;
249
250 ifstream shaderFile(file);
251 GLuint shaderId = 0;
252
253 if (shaderFile.is_open()) {
254 string line, shaderString;
255
256 while(getline(shaderFile, line)) {
257 shaderString += line + "\n";
258 }
259 shaderFile.close();
260 const char* shaderCString = shaderString.c_str();
261
262 shaderId = glCreateShader(type);
263 glShaderSource(shaderId, 1, &shaderCString, NULL);
264 glCompileShader(shaderId);
265
266 cout << "Loaded successfully" << endl;
267 } else {
268 cout << "Failed to loade the file" << endl;
269 }
270
271 return shaderId;
272}
273
274GLuint createDataBuffer(size_t size, GLfloat* data) {
275 GLuint vbo = 0;
276 glGenBuffers(1, &vbo);
277 glBindBuffer(GL_ARRAY_BUFFER, vbo);
278 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
279 return vbo;
280}
281
282GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo) {
283 GLuint vao = 0;
284 glGenVertexArrays(1, &vao);
285 glBindVertexArray(vao);
286 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
287 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
288 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
289 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
290 return vao;
291}
[4046b51]292
293GLfloat* createCirclePoints(unsigned int smoothness, GLfloat centerX, GLfloat centerY, GLfloat radius) {
[237ec28]294 GLfloat* points = new GLfloat[smoothness*9];
[4046b51]295
[237ec28]296 GLfloat curX, curY, nextX, nextY;
297 curX = centerX;
298 curY = centerY+radius;
[4046b51]299
300 for (unsigned int i=0; i<smoothness; i++) {
[237ec28]301 double radians = PI/2 + 2*PI * (i+1)/smoothness;
302 nextX = centerX + cos(radians)*radius;
303 nextY = centerY + sin(radians)*radius;
304
305 points[i*9] = curX;
306 points[i*9+1] = curY;
307 points[i*9+2] = 0.5f;
308 points[i*9+3] = nextX;
309 points[i*9+4] = nextY;
310 points[i*9+5] = 0.5f;
311 points[i*9+6] = centerX;
312 points[i*9+7] = centerY;
313 points[i*9+8] = 0.5f;
314
315 curX = nextX;
316 curY = nextY;
[4046b51]317 }
318
319 return points;
320}
321
322GLfloat* createCircleColors(unsigned int smoothness) {
[237ec28]323 GLfloat* colors = new GLfloat[smoothness*9];
[4046b51]324
[237ec28]325 for (unsigned int i=0; i<smoothness*3; i++) {
[4046b51]326 colors[i*3] = 0.0f;
327 colors[i*3+1] = 1.0f;
328 colors[i*3+2] = 0.0f;
329 }
330
331 return colors;
332}
Note: See TracBrowser for help on using the repository browser.