]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
moved some stuff around
[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, -1.0f,
11          1.0f, -1.0f, -1.0f,
12          0.0f,  1.0f, -1.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 , vtx_buf(0)
27 , mvp()
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         glm::mat4 projection = glm::perspective(
64                 45.0f, // FOV in degrees
65                 1.0f,  // aspect ratio
66                 0.1f,  // near clip
67                 100.0f // far clip
68         );
69         glm::mat4 view = glm::lookAt(
70                 glm::vec3(0, 0,  0),  // observer
71                 glm::vec3(0, 0, -1), // target
72                 glm::vec3(0, 1,  0) // up
73         );
74         glm::mat4 model(1.0f); // identity: no transformation
75         mvp = projection * view * model;
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         while (running) {
91                 Uint32 now = SDL_GetTicks();
92                 int delta = now - last;
93                 Loop(delta);
94                 last = now;
95         }
96 }
97
98 void Application::Loop(int dt) {
99         HandleEvents();
100         Render();
101 }
102
103
104 void Application::HandleEvents() {
105         SDL_Event event;
106         while (SDL_PollEvent(&event)) {
107                 switch (event.type) {
108                         case SDL_QUIT:
109                                 running = false;
110                                 break;
111                         default:
112                                 break;
113                 }
114         }
115 }
116
117 void Application::Render() {
118         glClear(GL_COLOR_BUFFER_BIT);
119
120         program.Use();
121
122         glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
123
124         glEnableVertexAttribArray(0);
125         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
126         glVertexAttribPointer(
127                 0,        // attribute 0 (for shader)
128                 3,        // size
129                 GL_FLOAT, // type
130                 GL_FALSE, // normalized
131                 0,        // stride
132                 nullptr   // offset
133         );
134         glDrawArrays(
135                 GL_TRIANGLES, // how
136                 0,            // start
137                 3             // len
138         );
139         glDisableVertexAttribArray(0);
140
141         window.Flip();
142 }
143
144 }