-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
295 lines (226 loc) · 8.05 KB
/
main.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
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#define LOG(argument) std::cout << argument << '\n'
#define GL_GLEXT_PROTOTYPES 1
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
#include <SDL.h>
#include <SDL_opengl.h>
#include "glm/mat4x4.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "ShaderProgram.h"
#include "stb_image.h"
enum AppStatus { RUNNING, TERMINATED };
constexpr int WINDOW_WIDTH = 1920,
WINDOW_HEIGHT = 1080;
constexpr float BG_RED = 0.0f,
BG_GREEN = 0.0f,
BG_BLUE = 0.0f,
BG_OPACITY = 1.0f;
constexpr int VIEWPORT_X = 0,
VIEWPORT_Y = 0,
VIEWPORT_WIDTH = WINDOW_WIDTH,
VIEWPORT_HEIGHT = WINDOW_HEIGHT;
constexpr char V_SHADER_PATH[] = "shaders/vertex_textured.glsl",
F_SHADER_PATH[] = "shaders/fragment_textured.glsl";
constexpr float MILLISECONDS_IN_SECOND = 1000.0;
constexpr GLint NUMBER_OF_TEXTURES = 1, // to be generated, that is
LEVEL_OF_DETAIL = 0, // mipmap reduction image level
TEXTURE_BORDER = 0; // this value MUST be zero
// source: https://yorukura-anime.com/
constexpr char PAIMON_SPRITE_FILEPATH[] = "paimon.png",
TRAVELER_SPRITE_FILEPATH[] = "traveler.png";
constexpr glm::vec3 //INIT_SCALE = glm::vec3(2.5f, 5.263f, 0.0f),
INIT_SCALE_PAIMON = glm::vec3(1.25f, 2.0f, 0.0f),
INIT_SCALE_TRAVELER = glm::vec3(3.0f, 5.0f, 0.0f),
INIT_POS_PAIMON = glm::vec3(-2.0f, 0.0f, 0.0f),
INIT_POS_TRAVELER = glm::vec3(2.0f, 0.0f, 0.0f);
constexpr float ROT_INCREMENT = 1.0f;
constexpr float TRANS_INCREMENT = 1.0f;
constexpr float SCALE_INCREMENT = 0.5f;
constexpr float RADIUS = 2.0f; // radius of your circle
float g_angle = 0.0f; // current angle
SDL_Window* g_display_window;
AppStatus g_app_status = RUNNING;
ShaderProgram g_shader_program = ShaderProgram();
glm::mat4 g_view_matrix,
g_paimon_matrix,
g_traveler_matrix,
g_projection_matrix;
float g_previous_ticks = 0.0f;
glm::vec3 g_rotation_paimon = glm::vec3(0.0f, 0.0f, 0.0f),
g_rotation_traveler = glm::vec3(0.0f, 0.0f, 0.0f),
g_translation_paimon = glm::vec3(0.0f, 0.0f, 0.0f),
g_translation_traveler = glm::vec3(0.0f, 0.0f, 0.0f),
g_scale_paimon = INIT_SCALE_PAIMON,
g_scale_traveler = INIT_SCALE_TRAVELER;
GLuint g_kano_texture_id,
g_mahiru_texture_id;
GLuint load_texture(const char* filepath)
{
// STEP 1: Loading the image file
int width, height, number_of_components;
unsigned char* image = stbi_load(filepath, &width, &height, &number_of_components, STBI_rgb_alpha);
if (image == NULL)
{
LOG("Unable to load image. Make sure the path is correct.");
assert(false);
}
// STEP 2: Generating and binding a texture ID to our image
GLuint textureID;
glGenTextures(NUMBER_OF_TEXTURES, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, LEVEL_OF_DETAIL, GL_RGBA, width, height, TEXTURE_BORDER, GL_RGBA, GL_UNSIGNED_BYTE, image);
// STEP 3: Setting our texture filter parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// STEP 4: Releasing our file from memory and returning our texture id
stbi_image_free(image);
return textureID;
}
void initialise()
{
// Initialise video and joystick subsystems
SDL_Init(SDL_INIT_VIDEO);
g_display_window = SDL_CreateWindow("Hello, Textures!",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(g_display_window);
SDL_GL_MakeCurrent(g_display_window, context);
if (g_display_window == nullptr)
{
std::cerr << "Error: SDL window could not be created.\n";
SDL_Quit();
exit(1);
}
#ifdef _WINDOWS
glewInit();
#endif
glViewport(VIEWPORT_X, VIEWPORT_Y, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
g_shader_program.load(V_SHADER_PATH, F_SHADER_PATH);
g_paimon_matrix = glm::mat4(1.0f);
g_traveler_matrix = glm::mat4(1.0f);
g_view_matrix = glm::mat4(1.0f);
g_projection_matrix = glm::ortho(-5.0f, 5.0f, -3.75f, 3.75f, -1.0f, 1.0f);
g_shader_program.set_projection_matrix(g_projection_matrix);
g_shader_program.set_view_matrix(g_view_matrix);
glUseProgram(g_shader_program.get_program_id());
glClearColor(BG_RED, BG_BLUE, BG_GREEN, BG_OPACITY);
g_kano_texture_id = load_texture(PAIMON_SPRITE_FILEPATH);
g_mahiru_texture_id = load_texture(TRAVELER_SPRITE_FILEPATH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void process_input()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE)
{
g_app_status = TERMINATED;
}
}
}
void update()
{
/* Delta time calculations */
float ticks = (float)SDL_GetTicks() / MILLISECONDS_IN_SECOND;
float delta_time = ticks - g_previous_ticks;
g_previous_ticks = ticks;
/* Game logic */
//paimon rotation
g_rotation_paimon.y += ROT_INCREMENT * delta_time;
//paimon translation
g_angle += TRANS_INCREMENT * delta_time;
g_translation_paimon.x = RADIUS * glm::cos(g_angle);
g_translation_paimon.y = RADIUS * glm::sin(g_angle);
//paimon scale
g_scale_paimon.x += SCALE_INCREMENT * delta_time;
g_scale_paimon.y += SCALE_INCREMENT * delta_time;
//LEFT UP
g_translation_traveler.x += -0.25f * TRANS_INCREMENT * delta_time;
g_translation_traveler.y += 0.25f * TRANS_INCREMENT * delta_time;
////LEFT DOWN
//g_translation_traveler.x += -0.25f * TRANS_INCREMENT * delta_time;
//g_translation_traveler.y += -0.25f * TRANS_INCREMENT * delta_time;
////RIGHT DOWN
//g_translation_traveler.x += 0.25f * TRANS_INCREMENT * delta_time;
//g_translation_traveler.y += -0.25f * TRANS_INCREMENT * delta_time;
////RIGHT UP
//g_translation_traveler.x += 0.25f * TRANS_INCREMENT * delta_time;
//g_translation_traveler.y += 0.25f * TRANS_INCREMENT * delta_time;
/* Model matrix reset */
g_paimon_matrix = glm::mat4(1.0f);
g_traveler_matrix = glm::mat4(1.0f);
/* Transformations */
//Initial position
g_traveler_matrix = glm::translate(g_traveler_matrix, INIT_POS_TRAVELER);
g_paimon_matrix = glm::translate(g_paimon_matrix, INIT_POS_PAIMON);
//Traveler
//Translate diagonally
g_traveler_matrix = glm::translate(g_traveler_matrix,
glm::vec3(g_translation_traveler.x, g_translation_traveler.y, 0.0f));
//Paimon
//Rotation around traveler
g_paimon_matrix = glm::translate(g_traveler_matrix,
glm::vec3(g_translation_paimon.x, g_translation_paimon.y, 0.0f));
//Paimon self rotate
g_paimon_matrix = glm::rotate(g_paimon_matrix,
g_rotation_paimon.y,
glm::vec3(0.0f, 1.0f, 0.0f));
//Scale
g_paimon_matrix = glm::scale(g_paimon_matrix,
glm::vec3(g_scale_paimon.x, g_scale_paimon.y, 0.0f));
g_traveler_matrix = glm::scale(g_traveler_matrix, INIT_SCALE_TRAVELER);
}
void draw_object(glm::mat4& object_g_model_matrix, GLuint& object_texture_id)
{
g_shader_program.set_model_matrix(object_g_model_matrix);
glBindTexture(GL_TEXTURE_2D, object_texture_id);
glDrawArrays(GL_TRIANGLES, 0, 6); // we are now drawing 2 triangles, so use 6, not 3
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
// Vertices
float vertices[] =
{
-0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, // triangle 1
-0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f // triangle 2
};
// Textures
float texture_coordinates[] =
{
0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // triangle 1
0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // triangle 2
};
glVertexAttribPointer(g_shader_program.get_position_attribute(), 2, GL_FLOAT, false,
0, vertices);
glEnableVertexAttribArray(g_shader_program.get_position_attribute());
glVertexAttribPointer(g_shader_program.get_tex_coordinate_attribute(), 2, GL_FLOAT,
false, 0, texture_coordinates);
glEnableVertexAttribArray(g_shader_program.get_tex_coordinate_attribute());
// Bind texture
draw_object(g_paimon_matrix, g_kano_texture_id);
draw_object(g_traveler_matrix, g_mahiru_texture_id);
// We disable two attribute arrays now
glDisableVertexAttribArray(g_shader_program.get_position_attribute());
glDisableVertexAttribArray(g_shader_program.get_tex_coordinate_attribute());
SDL_GL_SwapWindow(g_display_window);
}
void shutdown() { SDL_Quit(); }
int main(int argc, char* argv[])
{
initialise();
while (g_app_status == RUNNING)
{
process_input();
update();
render();
}
shutdown();
return 0;
}