]> git.localhorst.tv Git - blank.git/blob - src/main.cpp
33071f1ef3510f6d088feea27273245097d78493
[blank.git] / src / main.cpp
1 #include <iostream>
2 #include <SDL.h>
3 #include <GL/glew.h>
4 #include <glm/glm.hpp>
5 #include <glm/gtc/matrix_transform.hpp>
6
7 #include "init.hpp"
8 #include "shader.hpp"
9
10 using namespace std;
11 using namespace blank;
12
13
14 constexpr GLfloat vtx_coords[] = {
15         -1.0f, -1.0f, -1.0f,
16          1.0f, -1.0f, -1.0f,
17          0.0f,  1.0f, -1.0f,
18 };
19
20
21 int main(int argc, char *argv[]) {
22
23         InitSDL init_sdl;
24         InitGL init_gl;
25         Window window;
26
27         GLContext ctx = window.CreateContext();
28         InitGLEW init_glew;
29         GLContext::EnableVSync();
30
31
32         Shader vtx_shader(GL_VERTEX_SHADER);
33         vtx_shader.Source(
34                 "#version 330 core\n"
35                 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
36                 "uniform mat4 MVP;\n"
37                 "void main() {\n"
38                         "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
39                         "gl_Position = MVP * v;\n"
40                 "}\n"
41         );
42         vtx_shader.Compile();
43
44         if (!vtx_shader.Compiled()) {
45                 cerr << "vertex shader compile error" << endl;
46                 vtx_shader.Log(cerr);
47                 return 4;
48         }
49
50         Shader frag_shader(GL_FRAGMENT_SHADER);
51         frag_shader.Source(
52                 "#version 330 core\n"
53                 "out vec3 color;\n"
54                 "void main() {\n"
55                         "color = vec3(1, 1, 1);\n"
56                 "}\n"
57         );
58         frag_shader.Compile();
59
60         if (!frag_shader.Compiled()) {
61                 cerr << "fragment shader compile error" << endl;
62                 frag_shader.Log(cerr);
63                 return 4;
64         }
65
66
67         Program program;
68         program.Attach(vtx_shader);
69         program.Attach(frag_shader);
70         program.Link();
71
72         if (!program.Linked()) {
73                 cerr << "program link error" << endl;
74                 program.Log(cerr);
75                 return 4;
76         }
77
78
79         GLuint VertexArrayID;
80         glGenVertexArrays(1, &VertexArrayID);
81         glBindVertexArray(VertexArrayID);
82
83
84         GLuint vtx_buf;
85         glGenBuffers(1, &vtx_buf);
86         glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
87         glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
88
89
90         glm::mat4 projection = glm::perspective(
91                 45.0f, // FOV in degrees
92                 1.0f,  // aspect ratio
93                 0.1f,  // near clip
94                 100.0f // far clip
95         );
96         glm::mat4 view = glm::lookAt(
97                 glm::vec3(0, 0,  0),  // observer
98                 glm::vec3(0, 0, -1), // target
99                 glm::vec3(0, 1,  0) // up
100         );
101         glm::mat4 model(1.0f); // identity: no transformation
102         glm::mat4 mvp = projection * view * model;
103
104         GLuint mvp_id = program.UniformLocation("MVP");
105
106
107         glClearColor(0.0, 0.0, 0.0, 1.0);
108
109
110         bool running = true;
111         Uint32 last = SDL_GetTicks();
112         while (running) {
113                 Uint32 now = SDL_GetTicks();
114                 int delta = now - last;
115
116                 SDL_Event event;
117                 while (SDL_PollEvent(&event)) {
118                         switch (event.type) {
119                                 case SDL_QUIT:
120                                         running = false;
121                                         break;
122                                 default:
123                                         break;
124                         }
125                 }
126
127                 glClear(GL_COLOR_BUFFER_BIT);
128
129                 program.Use();
130
131                 glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
132
133                 glEnableVertexAttribArray(0);
134                 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
135                 glVertexAttribPointer(
136                         0,        // attribute 0 (for shader)
137                         3,        // size
138                         GL_FLOAT, // type
139                         GL_FALSE, // normalized
140                         0,        // stride
141                         nullptr   // offset
142                 );
143                 glDrawArrays(
144                         GL_TRIANGLES, // how
145                         0,            // start
146                         3             // len
147                 );
148                 glDisableVertexAttribArray(0);
149
150                 window.Flip();
151
152                 last = now;
153         }
154
155         return 0;
156
157 }