]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
extracted projection and viewport to camera class
[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, -5.0f,
11          1.0f, -1.0f, -5.0f,
12          0.0f,  1.0f, -5.0f,
13 };
14
15 }
16
17 namespace blank {
18
19 Application::Application()
20 : init_sdl()
21 , init_gl()
22 , window()
23 , ctx(window.CreateContext())
24 , init_glew()
25 , program()
26 , cam()
27 , vtx_buf(0)
28 , mvp_handle(0)
29 , running(false) {
30         GLContext::EnableVSync();
31         program.LoadShader(
32                 GL_VERTEX_SHADER,
33                 "#version 330 core\n"
34                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
35                 "uniform mat4 MVP;\n"
36                 "void main() {\n"
37                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
38                         "gl_Position = MVP * v;\n"
39                 "}\n"
40         );
41         program.LoadShader(
42                 GL_FRAGMENT_SHADER,
43                 "#version 330 core\n"
44                 "out vec3 color;\n"
45                 "void main() {\n"
46                         "color = vec3(1, 1, 1);\n"
47                 "}\n"
48         );
49         program.Link();
50         if (!program.Linked()) {
51                 program.Log(std::cerr);
52                 throw std::runtime_error("link program");
53         }
54
55         GLuint VertexArrayID;
56         glGenVertexArrays(1, &VertexArrayID);
57         glBindVertexArray(VertexArrayID);
58
59         glGenBuffers(1, &vtx_buf);
60         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
61         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
62
63         mvp_handle = program.UniformLocation("MVP");
64
65         glClearColor(0.0, 0.0, 0.0, 1.0);
66 }
67
68 Application::~Application() {
69
70 }
71
72
73 void Application::Run() {
74         running = true;
75         Uint32 last = SDL_GetTicks();
76         while (running) {
77                 Uint32 now = SDL_GetTicks();
78                 int delta = now - last;
79                 Loop(delta);
80                 last = now;
81         }
82 }
83
84 void Application::Loop(int dt) {
85         HandleEvents();
86         Render();
87 }
88
89
90 void Application::HandleEvents() {
91         SDL_Event event;
92         while (SDL_PollEvent(&event)) {
93                 switch (event.type) {
94                         case SDL_QUIT:
95                                 running = false;
96                                 break;
97                         case SDL_WINDOWEVENT:
98                                 switch (event.window.event) {
99                                         case SDL_WINDOWEVENT_RESIZED:
100                                                 cam.Viewport(event.window.data1, event.window.data2);
101                                                 break;
102                                         default:
103                                                 break;
104                                 }
105                                 break;
106                         default:
107                                 break;
108                 }
109         }
110 }
111
112 void Application::Render() {
113         glClear(GL_COLOR_BUFFER_BIT);
114
115         program.Use();
116
117         glm::mat4 model(1.0f); // identity: no transformation
118         glm::mat4 mvp(cam.MakeMVP(model));
119         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
120
121         glEnableVertexAttribArray(0);
122         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
123         glVertexAttribPointer(
124                 0,        // attribute 0 (for shader)
125                 3,        // size
126                 GL_FLOAT, // type
127                 GL_FALSE, // normalized
128                 0,        // stride
129                 nullptr   // offset
130         );
131         glDrawArrays(
132                 GL_TRIANGLES, // how
133                 0,            // start
134                 3             // len
135         );
136         glDisableVertexAttribArray(0);
137
138         window.Flip();
139 }
140
141 }