]> git.localhorst.tv Git - blank.git/blob - src/app.cpp
begun extracting model 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, 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 , cam()
28 , model()
29 , vtx_buf(0)
30 , mvp_handle(0)
31 , running(false) {
32         GLContext::EnableVSync();
33         program.LoadShader(
34                 GL_VERTEX_SHADER,
35                 "#version 330 core\n"
36                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
37                 "uniform mat4 MVP;\n"
38                 "void main() {\n"
39                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
40                         "gl_Position = MVP * v;\n"
41                 "}\n"
42         );
43         program.LoadShader(
44                 GL_FRAGMENT_SHADER,
45                 "#version 330 core\n"
46                 "out vec3 color;\n"
47                 "void main() {\n"
48                         "color = vec3(1, 1, 1);\n"
49                 "}\n"
50         );
51         program.Link();
52         if (!program.Linked()) {
53                 program.Log(std::cerr);
54                 throw std::runtime_error("link program");
55         }
56
57         GLuint VertexArrayID;
58         glGenVertexArrays(1, &VertexArrayID);
59         glBindVertexArray(VertexArrayID);
60
61         glGenBuffers(1, &vtx_buf);
62         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
63         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
64
65         model.Position(glm::vec3(0, 0, -4));
66
67         mvp_handle = program.UniformLocation("MVP");
68
69         glClearColor(0.0, 0.0, 0.0, 1.0);
70 }
71
72 Application::~Application() {
73
74 }
75
76
77 void Application::Run() {
78         running = true;
79         Uint32 last = SDL_GetTicks();
80         while (running) {
81                 Uint32 now = SDL_GetTicks();
82                 int delta = now - last;
83                 Loop(delta);
84                 last = now;
85         }
86 }
87
88 void Application::Loop(int dt) {
89         HandleEvents();
90         Render();
91 }
92
93
94 void Application::HandleEvents() {
95         SDL_Event event;
96         while (SDL_PollEvent(&event)) {
97                 switch (event.type) {
98                         case SDL_QUIT:
99                                 running = false;
100                                 break;
101                         case SDL_WINDOWEVENT:
102                                 switch (event.window.event) {
103                                         case SDL_WINDOWEVENT_RESIZED:
104                                                 cam.Viewport(event.window.data1, event.window.data2);
105                                                 break;
106                                         default:
107                                                 break;
108                                 }
109                                 break;
110                         default:
111                                 break;
112                 }
113         }
114 }
115
116 void Application::Render() {
117         glClear(GL_COLOR_BUFFER_BIT);
118
119         program.Use();
120
121         glm::mat4 mvp(cam.MakeMVP(model.Transform()));
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 }