source: opengl-game/pong.cpp@ 9e81839

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

Start implementing pong and encapsulating some of the OpenGL code into reusable functions

  • Property mode set to 100644
File size: 5.9 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
17const bool FULLSCREEN = false;
18
19void glfw_error_callback(int error, const char* description) {
20 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
21}
22
23int main(int argc, char* argv[]) {
24 cout << "New OpenGL Game" << endl;
25
26 if (!restart_gl_log()) {}
27 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
28
29 glfwSetErrorCallback(glfw_error_callback);
30 if (!glfwInit()) {
31 fprintf(stderr, "ERROR: could not start GLFW3\n");
32 return 1;
33 }
34
35#ifdef __APPLE__
36 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
37 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
38 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
39 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
40#endif
41
42 glfwWindowHint(GLFW_SAMPLES, 4);
43
44 GLFWwindow* window = NULL;
45
46 int width = 640;
47 int height = 480;
48
49 if (FULLSCREEN) {
50 GLFWmonitor* mon = glfwGetPrimaryMonitor();
51 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
52
53 cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
54 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
55
56 width = vmode->width;
57 height = vmode->height;
58 } else {
59 window = glfwCreateWindow(width, height, "Hello Triangle", NULL, NULL);
60 }
61
62 if (!window) {
63 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
64 glfwTerminate();
65 return 1;
66 }
67 glfwMakeContextCurrent(window);
68 glewExperimental = GL_TRUE;
69 glewInit();
70
71 // glViewport(0, 0, width*2, height*2);
72
73 const GLubyte* renderer = glGetString(GL_RENDERER);
74 const GLubyte* version = glGetString(GL_VERSION);
75 printf("Renderer: %s\n", renderer);
76 printf("OpenGL version supported %s\n", version);
77
78 glEnable(GL_DEPTH_TEST);
79 glDepthFunc(GL_LESS);
80
81 glEnable(GL_CULL_FACE);
82 // glCullFace(GL_BACK);
83 // glFrontFace(GL_CW);
84
85 GLfloat points[] = {
86 0.0f, 0.5f, 0.5f,
87 -0.5f, -0.5f, 0.5f,
88 0.5f, -0.5f, 0.5f,
89 };
90
91 GLfloat colors[] = {
92 1.0, 0.0, 0.0,
93 0.0, 0.0, 1.0,
94 0.0, 1.0, 0.0,
95 };
96
97 GLfloat points_paddle[] = {
98 0.0f, -0.5f, 0.0f,
99 0.5f, 0.5f, 0.0f,
100 -0.5f, 0.5f, 0.0f,
101 };
102
103 GLfloat colors_paddle[] = {
104 1.0, 0.0, 0.0,
105 0.0, 0.0, 1.0,
106 0.0, 1.0, 0.0,
107 };
108
109 GLfloat model[] = {
110 1.0f, 0.0f, 0.0f, 0.0f, // column 1
111 0.0f, 1.0f, 0.0f, 0.0f, // column 2
112 0.0f, 0.0f, 1.0f, 0.0f, // column 3
113 0.5f, 0.0f, 0.0f, 1.0f, // column 4
114 };
115
116 GLuint ball_points_vbo = createDataBuffer(sizeof(points), points);
117 GLuint ball_colors_vbo = createDataBuffer(sizeof(colors), colors);
118 GLuint ball_vao = createArrayBuffer(ball_points_vbo, ball_colors_vbo);
119
120 GLuint paddle_points_vbo = createDataBuffer(sizeof(points_paddle), points_paddle);
121 GLuint paddle_colors_vbo = createDataBuffer(sizeof(colors_paddle), colors_paddle);
122 GLuint paddle_vao = createArrayBuffer(paddle_points_vbo, paddle_colors_vbo);
123
124 GLuint vs = loadShader(GL_VERTEX_SHADER, "./test.vert");
125 GLuint fs = loadShader(GL_FRAGMENT_SHADER, "./test.frag");
126
127 GLuint shader_program = glCreateProgram();
128 glAttachShader(shader_program, vs);
129 glAttachShader(shader_program, fs);
130
131 glLinkProgram(shader_program);
132
133 GLint location = glGetUniformLocation(shader_program, "model");
134
135 float speed = 1.0f;
136 float last_position = 0.0f;
137
138 double previous_seconds = glfwGetTime();
139 while (!glfwWindowShouldClose(window)) {
140 double current_seconds = glfwGetTime();
141 double elapsed_seconds = current_seconds - previous_seconds;
142 previous_seconds = current_seconds;
143
144 if (fabs(last_position) > 1.0f) {
145 speed = -speed;
146 }
147
148 model[12] = last_position + speed*elapsed_seconds;
149 last_position = model[12];
150 glUseProgram(shader_program);
151 glUniformMatrix4fv(location, 1, GL_FALSE, model);
152
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154
155 glBindVertexArray(ball_vao);
156 glEnableVertexAttribArray(0);
157 glEnableVertexAttribArray(1);
158
159 glDrawArrays(GL_TRIANGLES, 0, 3);
160
161 glBindVertexArray(paddle_vao);
162 glEnableVertexAttribArray(0);
163 glEnableVertexAttribArray(1);
164
165 glDrawArrays(GL_TRIANGLES, 0, 3);
166
167 glfwPollEvents();
168 glfwSwapBuffers(window);
169
170 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
171 glfwSetWindowShouldClose(window, 1);
172 }
173 }
174
175 glfwTerminate();
176 return 0;
177}
178
179GLuint loadShader(GLenum type, string file) {
180 cout << "Loading shader from file " << file << endl;
181
182 ifstream shaderFile(file);
183 GLuint shaderId = 0;
184
185 if (shaderFile.is_open()) {
186 string line, shaderString;
187
188 while(getline(shaderFile, line)) {
189 shaderString += line + "\n";
190 }
191 shaderFile.close();
192 const char* shaderCString = shaderString.c_str();
193
194 shaderId = glCreateShader(type);
195 glShaderSource(shaderId, 1, &shaderCString, NULL);
196 glCompileShader(shaderId);
197
198 cout << "Loaded successfully" << endl;
199 } else {
200 cout << "Failed to loade the file" << endl;
201 }
202
203 return shaderId;
204}
205
206GLuint createDataBuffer(size_t size, GLfloat* data) {
207 GLuint vbo = 0;
208 glGenBuffers(1, &vbo);
209 glBindBuffer(GL_ARRAY_BUFFER, vbo);
210 glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
211 return vbo;
212}
213
214GLuint createArrayBuffer(GLuint points_vbo, GLuint colors_vbo) {
215 GLuint vao = 0;
216 glGenVertexArrays(1, &vao);
217 glBindVertexArray(vao);
218 glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
219 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
220 glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
221 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
222 return vao;
223}
Note: See TracBrowser for help on using the repository browser.