9 constexpr GLfloat vtx_coords[] = {
19 Application::Application()
24 , ctx(window.CreateContext())
27 , move_velocity(0.001f)
28 , pitch_sensitivity(-0.0025f)
29 , yaw_sensitivity(-0.001f)
41 GLContext::EnableVSync();
42 GLContext::EnableBackfaceCulling();
46 "layout(location = 0) in vec3 vertexPosition_modelspace;\n"
49 "vec4 v = vec4(vertexPosition_modelspace, 1);\n"
50 "gl_Position = MVP * v;\n"
58 "color = vec3(1, 1, 1);\n"
62 if (!program.Linked()) {
63 program.Log(std::cerr);
64 throw std::runtime_error("link program");
68 glGenVertexArrays(1, &VertexArrayID);
69 glBindVertexArray(VertexArrayID);
71 glGenBuffers(1, &vtx_buf);
72 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
75 model.Position(glm::vec3(0, 0, -4));
76 cam.Position(glm::vec3(0, 0, 4));
78 mvp_handle = program.UniformLocation("MVP");
80 glClearColor(0.0, 0.0, 0.0, 1.0);
83 Application::~Application() {
88 void Application::Run() {
90 Uint32 last = SDL_GetTicks();
93 Uint32 now = SDL_GetTicks();
94 int delta = now - last;
100 void Application::Loop(int dt) {
107 void Application::HandleEvents() {
109 while (SDL_PollEvent(&event)) {
110 switch (event.type) {
113 switch (event.key.keysym.sym) {
115 front = event.key.state == SDL_PRESSED;
118 back = event.key.state == SDL_PRESSED;
121 left = event.key.state == SDL_PRESSED;
124 right = event.key.state == SDL_PRESSED;
127 up = event.key.state == SDL_PRESSED;
130 down = event.key.state == SDL_PRESSED;
134 case SDL_MOUSEMOTION:
135 cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
136 cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
141 case SDL_WINDOWEVENT:
142 switch (event.window.event) {
143 case SDL_WINDOWEVENT_RESIZED:
144 cam.Viewport(event.window.data1, event.window.data2);
156 void Application::Update(int dt) {
158 if (right && !left) {
159 vel.x = move_velocity;
160 } else if (left && !right) {
161 vel.x = -move_velocity;
164 vel.y = move_velocity;
165 } else if (down && !up) {
166 vel.y = -move_velocity;
168 if (back && !front) {
169 vel.z = move_velocity;
170 } else if (front && !back) {
171 vel.z = -move_velocity;
179 void Application::Render() {
180 glClear(GL_COLOR_BUFFER_BIT);
184 glm::mat4 mvp(cam.MakeMVP(model.Transform()));
185 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
187 glEnableVertexAttribArray(0);
188 glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
189 glVertexAttribPointer(
190 0, // attribute 0 (for shader)
193 GL_FALSE, // normalized
202 glDisableVertexAttribArray(0);