]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
mouse controlled camera pitch/yaw
[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 , pitch_sensitivity(-0.0025f)
28 , yaw_sensitivity(-0.001f)
29 , cam()
30 , model()
31 , vtx_buf(0)
32 , mvp_handle(0)
33 , running(false) {
34         GLContext::EnableVSync();
35         program.LoadShader(
36                 GL_VERTEX_SHADER,
37                 "#version 330 core\n"
38                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
39                 "uniform mat4 MVP;\n"
40                 "void main() {\n"
41                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
42                         "gl_Position = MVP * v;\n"
43                 "}\n"
44         );
45         program.LoadShader(
46                 GL_FRAGMENT_SHADER,
47                 "#version 330 core\n"
48                 "out vec3 color;\n"
49                 "void main() {\n"
50                         "color = vec3(1, 1, 1);\n"
51                 "}\n"
52         );
53         program.Link();
54         if (!program.Linked()) {
55                 program.Log(std::cerr);
56                 throw std::runtime_error("link program");
57         }
58
59         GLuint VertexArrayID;
60         glGenVertexArrays(1, &VertexArrayID);
61         glBindVertexArray(VertexArrayID);
62
63         glGenBuffers(1, &vtx_buf);
64         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
65         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
66
67         model.Position(glm::vec3(0, 0, -4));
68         cam.Position(glm::vec3(0, 0, 4));
69
70         mvp_handle = program.UniformLocation("MVP");
71
72         glClearColor(0.0, 0.0, 0.0, 1.0);
73 }
74
75 Application::~Application() {
76
77 }
78
79
80 void Application::Run() {
81         running = true;
82         Uint32 last = SDL_GetTicks();
83         window.GrabMouse();
84         while (running) {
85                 Uint32 now = SDL_GetTicks();
86                 int delta = now - last;
87                 Loop(delta);
88                 last = now;
89         }
90 }
91
92 void Application::Loop(int dt) {
93         HandleEvents();
94         Render();
95 }
96
97
98 void Application::HandleEvents() {
99         SDL_Event event;
100         while (SDL_PollEvent(&event)) {
101                 switch (event.type) {
102                         case SDL_MOUSEMOTION:
103                                 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
104                                 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
105                                 std::cout << "x: " << event.motion.xrel << ", y: " << event.motion.yrel
106                                                 << ", pitch: " << cam.Pitch() << ", yaw: " << cam.Yaw() << std::endl;
107                                 break;
108                         case SDL_QUIT:
109                                 running = false;
110                                 break;
111                         case SDL_WINDOWEVENT:
112                                 switch (event.window.event) {
113                                         case SDL_WINDOWEVENT_RESIZED:
114                                                 cam.Viewport(event.window.data1, event.window.data2);
115                                                 break;
116                                         default:
117                                                 break;
118                                 }
119                                 break;
120                         default:
121                                 break;
122                 }
123         }
124 }
125
126 void Application::Render() {
127         glClear(GL_COLOR_BUFFER_BIT);
128
129         program.Use();
130
131         glm::mat4 mvp(cam.MakeMVP(model.Transform()));
132         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
133
134         glEnableVertexAttribArray(0);
135         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
136         glVertexAttribPointer(
137                 0,        // attribute 0 (for shader)
138                 3,        // size
139                 GL_FLOAT, // type
140                 GL_FALSE, // normalized
141                 0,        // stride
142                 nullptr   // offset
143         );
144         glDrawArrays(
145                 GL_TRIANGLES, // how
146                 0,            // start
147                 3             // len
148         );
149         glDisableVertexAttribArray(0);
150
151         window.Flip();
152 }
153
154 }