]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
lousy implementation of camera movement
[blank.git] / src / app.cpp
1 #include "app.hpp"
2
3 #include <iostream>
4 #include <stdexcept>
5
6
7 namespace {
8
9 constexpr GLfloat vtx_coords[] = {
10         -1.0f, -1.0f, 0.0f,
11          1.0f, -1.0f, 0.0f,
12          0.0f,  1.0f, 0.0f,
13 };
14
15 }
16
17 namespace blank {
18
19 Application::Application()
20 : init_sdl()
21 , init_img()
22 , init_gl()
23 , window()
24 , ctx(window.CreateContext())
25 , init_glew()
26 , program()
27 , move_velocity(0.001f)
28 , pitch_sensitivity(-0.0025f)
29 , yaw_sensitivity(-0.001f)
30 , cam()
31 , model()
32 , vtx_buf(0)
33 , mvp_handle(0)
34 , running(false)
35 , front(false)
36 , back(false)
37 , left(false)
38 , right(false)
39 , up(false)
40 , down(false) {
41         GLContext::EnableVSync();
42         program.LoadShader(
43                 GL_VERTEX_SHADER,
44                 "#version 330 core\n"
45                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
46                 "uniform mat4 MVP;\n"
47                 "void main() {\n"
48                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
49                         "gl_Position = MVP * v;\n"
50                 "}\n"
51         );
52         program.LoadShader(
53                 GL_FRAGMENT_SHADER,
54                 "#version 330 core\n"
55                 "out vec3 color;\n"
56                 "void main() {\n"
57                         "color = vec3(1, 1, 1);\n"
58                 "}\n"
59         );
60         program.Link();
61         if (!program.Linked()) {
62                 program.Log(std::cerr);
63                 throw std::runtime_error("link program");
64         }
65
66         GLuint VertexArrayID;
67         glGenVertexArrays(1, &VertexArrayID);
68         glBindVertexArray(VertexArrayID);
69
70         glGenBuffers(1, &vtx_buf);
71         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
72         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
73
74         model.Position(glm::vec3(0, 0, -4));
75         cam.Position(glm::vec3(0, 0, 4));
76
77         mvp_handle = program.UniformLocation("MVP");
78
79         glClearColor(0.0, 0.0, 0.0, 1.0);
80 }
81
82 Application::~Application() {
83
84 }
85
86
87 void Application::Run() {
88         running = true;
89         Uint32 last = SDL_GetTicks();
90         window.GrabMouse();
91         while (running) {
92                 Uint32 now = SDL_GetTicks();
93                 int delta = now - last;
94                 Loop(delta);
95                 last = now;
96         }
97 }
98
99 void Application::Loop(int dt) {
100         HandleEvents();
101         Update(dt);
102         Render();
103 }
104
105
106 void Application::HandleEvents() {
107         SDL_Event event;
108         while (SDL_PollEvent(&event)) {
109                 switch (event.type) {
110                         case SDL_KEYDOWN:
111                         case SDL_KEYUP:
112                                 switch (event.key.keysym.sym) {
113                                         case SDLK_w:
114                                                 front = event.key.state == SDL_PRESSED;
115                                                 break;
116                                         case SDLK_s:
117                                                 back = event.key.state == SDL_PRESSED;
118                                                 break;
119                                         case SDLK_a:
120                                                 left = event.key.state == SDL_PRESSED;
121                                                 break;
122                                         case SDLK_d:
123                                                 right = event.key.state == SDL_PRESSED;
124                                                 break;
125                                         case SDLK_q:
126                                                 up = event.key.state == SDL_PRESSED;
127                                                 break;
128                                         case SDLK_e:
129                                                 down = event.key.state == SDL_PRESSED;
130                                                 break;
131                                 }
132                                 break;
133                         case SDL_MOUSEMOTION:
134                                 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
135                                 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
136                                 break;
137                         case SDL_QUIT:
138                                 running = false;
139                                 break;
140                         case SDL_WINDOWEVENT:
141                                 switch (event.window.event) {
142                                         case SDL_WINDOWEVENT_RESIZED:
143                                                 cam.Viewport(event.window.data1, event.window.data2);
144                                                 break;
145                                         default:
146                                                 break;
147                                 }
148                                 break;
149                         default:
150                                 break;
151                 }
152         }
153 }
154
155 void Application::Update(int dt) {
156         glm::vec3 vel;
157         if (right && !left) {
158                 vel.x = move_velocity;
159         } else if (left && !right) {
160                 vel.x = -move_velocity;
161         }
162         if (up && !down) {
163                 vel.y = move_velocity;
164         } else if (down && !up) {
165                 vel.y = -move_velocity;
166         }
167         if (back && !front) {
168                 vel.z = move_velocity;
169         } else if (front && !back) {
170                 vel.z = -move_velocity;
171         }
172         cam.Velocity(vel);
173
174         cam.Update(dt);
175         model.Update(dt);
176 }
177
178 void Application::Render() {
179         glClear(GL_COLOR_BUFFER_BIT);
180
181         program.Use();
182
183         glm::mat4 mvp(cam.MakeMVP(model.Transform()));
184         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
185
186         glEnableVertexAttribArray(0);
187         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
188         glVertexAttribPointer(
189                 0,        // attribute 0 (for shader)
190                 3,        // size
191                 GL_FLOAT, // type
192                 GL_FALSE, // normalized
193                 0,        // stride
194                 nullptr   // offset
195         );
196         glDrawArrays(
197                 GL_TRIANGLES, // how
198                 0,            // start
199                 3             // len
200         );
201         glDisableVertexAttribArray(0);
202
203         window.Flip();
204 }
205
206 }