X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp.cpp;h=051a5f0f0975944af4059cc5c06496b8f97a128a;hb=49c81f76b80e0de99ca57db49510eb5e3385e1d1;hp=d54e8df56d1d0e875da4e599cca3ae67ea4b67cb;hpb=b314df303ffedbd6d2e81872908d12fc9712801a;p=blank.git diff --git a/src/app.cpp b/src/app.cpp index d54e8df..051a5f0 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -7,9 +7,9 @@ namespace { constexpr GLfloat vtx_coords[] = { - -1.0f, -1.0f, -1.0f, - 1.0f, -1.0f, -1.0f, - 0.0f, 1.0f, -1.0f, + -1.0f, -1.0f, 0.0f, + 1.0f, -1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, }; } @@ -18,15 +18,26 @@ namespace blank { Application::Application() : init_sdl() +, init_img() , init_gl() , window() , ctx(window.CreateContext()) , init_glew() , program() +, move_velocity(0.001f) +, pitch_sensitivity(-0.0025f) +, yaw_sensitivity(-0.001f) +, cam() +, model() , vtx_buf(0) -, mvp() , mvp_handle(0) -, running(false) { +, running(false) +, front(false) +, back(false) +, left(false) +, right(false) +, up(false) +, down(false) { GLContext::EnableVSync(); program.LoadShader( GL_VERTEX_SHADER, @@ -60,19 +71,8 @@ Application::Application() glBindBuffer(GL_ARRAY_BUFFER, vtx_buf); glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW); - glm::mat4 projection = glm::perspective( - 45.0f, // FOV in degrees - 1.0f, // aspect ratio - 0.1f, // near clip - 100.0f // far clip - ); - glm::mat4 view = glm::lookAt( - glm::vec3(0, 0, 0), // observer - glm::vec3(0, 0, -1), // target - glm::vec3(0, 1, 0) // up - ); - glm::mat4 model(1.0f); // identity: no transformation - mvp = projection * view * model; + model.Position(glm::vec3(0, 0, -4)); + cam.Position(glm::vec3(0, 0, 4)); mvp_handle = program.UniformLocation("MVP"); @@ -87,6 +87,7 @@ Application::~Application() { void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); + window.GrabMouse(); while (running) { Uint32 now = SDL_GetTicks(); int delta = now - last; @@ -97,6 +98,7 @@ void Application::Run() { void Application::Loop(int dt) { HandleEvents(); + Update(dt); Render(); } @@ -105,20 +107,80 @@ void Application::HandleEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + switch (event.key.keysym.sym) { + case SDLK_w: + front = event.key.state == SDL_PRESSED; + break; + case SDLK_s: + back = event.key.state == SDL_PRESSED; + break; + case SDLK_a: + left = event.key.state == SDL_PRESSED; + break; + case SDLK_d: + right = event.key.state == SDL_PRESSED; + break; + case SDLK_q: + up = event.key.state == SDL_PRESSED; + break; + case SDLK_e: + down = event.key.state == SDL_PRESSED; + break; + } + break; + case SDL_MOUSEMOTION: + cam.RotateYaw(event.motion.xrel * yaw_sensitivity); + cam.RotatePitch(event.motion.yrel * pitch_sensitivity); + break; case SDL_QUIT: running = false; break; + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_RESIZED: + cam.Viewport(event.window.data1, event.window.data2); + break; + default: + break; + } + break; default: break; } } } +void Application::Update(int dt) { + glm::vec3 vel; + if (right && !left) { + vel.x = move_velocity; + } else if (left && !right) { + vel.x = -move_velocity; + } + if (up && !down) { + vel.y = move_velocity; + } else if (down && !up) { + vel.y = -move_velocity; + } + if (back && !front) { + vel.z = move_velocity; + } else if (front && !back) { + vel.z = -move_velocity; + } + cam.Velocity(vel); + + cam.Update(dt); + model.Update(dt); +} + void Application::Render() { glClear(GL_COLOR_BUFFER_BIT); program.Use(); + glm::mat4 mvp(cam.MakeMVP(model.Transform())); glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); glEnableVertexAttribArray(0);