-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
98 lines (72 loc) · 3.29 KB
/
Menu.cpp
File metadata and controls
98 lines (72 loc) · 3.29 KB
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
#define LOG(argument) std::cout << argument << '\n'
#define GL_SILENCE_DEPRECATION
#define STB_IMAGE_IMPLEMENTATION
#ifdef _WINDOWS
#include <GL/glew.h>
#endif
#define GL_GLEXT_PROTOTYPES 1
#include <SDL.h>
#include <SDL_opengl.h>
#include "include/glm/mat4x4.hpp"
#include "include/glm/gtc/matrix_transform.hpp"
#include "include/ShaderProgram.h"
#include "Menu.h"
glm::vec3 scale_vector(10,10,1);
Menu::Menu()
{
// ––––– PHYSICS ––––– //
m_position = glm::vec3(0.0f);
// ––––– TRANSLATION ––––– //
m_movement = glm::vec3(0.0f);
m_model_matrix = glm::mat4(1.0f);
}
void Menu::draw_sprite_from_texture_atlas(ShaderProgram* program, GLuint texture_id, int index)
{
// Step 1: Calculate the UV location of the indexed frame
float u_coord = (float)(index % m_animation_cols) / (float)m_animation_cols;
float v_coord = (float)(index / m_animation_cols) / (float)m_animation_rows;
// Step 2: Calculate its UV size
float width = 1.0f / (float)m_animation_cols;
float height = 1.0f / (float)m_animation_rows;
// Step 3: Just as we have done before, match the texture coordinates to the vertices
float tex_coords[] =
{
u_coord, v_coord + height, u_coord + width, v_coord + height, u_coord + width, v_coord,
u_coord, v_coord + height, u_coord + width, v_coord, u_coord, v_coord
};
float vertices[] =
{
-0.5, -0.5, 0.5, -0.5, 0.5, 0.5,
-0.5, -0.5, 0.5, 0.5, -0.5, 0.5
};
// Step 4: And render
glBindTexture(GL_TEXTURE_2D, texture_id);
glVertexAttribPointer(program->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}
void Menu::render(ShaderProgram* program)
{
program->set_model_matrix(m_model_matrix);
// m_model_matrix = glm::scale(m_model_matrix, scale_vector);
if (m_animation_indices != NULL)
{
draw_sprite_from_texture_atlas(program, m_texture_id, m_animation_indices[m_animation_index]);
return;
}
float vertices[] = { -5.5, -5.5, 5.5, -5.5, 5.5, 5.5, -5.5, -5.5, 5.5, 5.5, -5.5, 5.5 };
// float vertices[] = { -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5 };
float tex_coords[] = { 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
glBindTexture(GL_TEXTURE_2D, m_texture_id);
glVertexAttribPointer(program->get_position_attribute(), 2, GL_FLOAT, false, 0, vertices);
glEnableVertexAttribArray(program->get_position_attribute());
glVertexAttribPointer(program->get_tex_coordinate_attribute(), 2, GL_FLOAT, false, 0, tex_coords);
glEnableVertexAttribArray(program->get_tex_coordinate_attribute());
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(program->get_position_attribute());
glDisableVertexAttribArray(program->get_tex_coordinate_attribute());
}