9 constexpr GLfloat vtx_coords[] = {
19 Application::Application()
23 , ctx(window.CreateContext())
30 GLContext::EnableVSync();
34 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
37 "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
38 "gl_Position = MVP * v;\n"
46 "color = vec3(1, 1, 1);\n"
50 if (!program.Linked()) {
51 program.Log(std::cerr);
52 throw std::runtime_error("link program");
56 glGenVertexArrays(1, &VertexArrayID);
57 glBindVertexArray(VertexArrayID);
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);
63 glm::mat4 projection = glm::perspective(
64 45.0f, // FOV in degrees
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
74 glm::mat4 model(1.0f); // identity: no transformation
75 mvp = projection * view * model;
77 mvp_handle = program.UniformLocation("MVP");
79 glClearColor(0.0, 0.0, 0.0, 1.0);
82 Application::~Application() {
87 void Application::Run() {
89 Uint32 last = SDL_GetTicks();
91 Uint32 now = SDL_GetTicks();
92 int delta = now - last;
98 void Application::Loop(int dt) {
104 void Application::HandleEvents() {
106 while (SDL_PollEvent(&event)) {
107 switch (event.type) {
117 void Application::Render() {
118 glClear(GL_COLOR_BUFFER_BIT);
122 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
124 glEnableVertexAttribArray(0);
125 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
126 glVertexAttribPointer(
127 0, // attribute 0 (for shader)
130 GL_FALSE, // normalized
139 glDisableVertexAttribArray(0);