5 #include <glm/gtc/matrix_transform.hpp>
11 using namespace blank;
14 constexpr GLfloat vtx_coords[] = {
21 int main(int argc, char *argv[]) {
27 GLContext ctx = window.CreateContext();
29 GLContext::EnableVSync();
32 Shader vtx_shader(GL_VERTEX_SHADER);
35 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
38 "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
39 "gl_Position = MVP * v;\n"
44 if (!vtx_shader.Compiled()) {
45 cerr << "vertex shader compile error" << endl;
50 Shader frag_shader(GL_FRAGMENT_SHADER);
55 "color = vec3(1, 1, 1);\n"
58 frag_shader.Compile();
60 if (!frag_shader.Compiled()) {
61 cerr << "fragment shader compile error" << endl;
62 frag_shader.Log(cerr);
68 program.Attach(vtx_shader);
69 program.Attach(frag_shader);
72 if (!program.Linked()) {
73 cerr << "program link error" << endl;
80 glGenVertexArrays(1, &VertexArrayID);
81 glBindVertexArray(VertexArrayID);
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);
90 glm::mat4 projection = glm::perspective(
91 45.0f, // FOV in degrees
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
101 glm::mat4 model(1.0f); // identity: no transformation
102 glm::mat4 mvp = projection * view * model;
104 GLuint mvp_id = program.UniformLocation("MVP");
107 glClearColor(0.0, 0.0, 0.0, 1.0);
111 Uint32 last = SDL_GetTicks();
113 Uint32 now = SDL_GetTicks();
114 int delta = now - last;
117 while (SDL_PollEvent(&event)) {
118 switch (event.type) {
127 glClear(GL_COLOR_BUFFER_BIT);
131 glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp[0][0]);
133 glEnableVertexAttribArray(0);
134 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
135 glVertexAttribPointer(
136 0, // attribute 0 (for shader)
139 GL_FALSE, // normalized
148 glDisableVertexAttribArray(0);