]> git.localhorst.tv Git - blank.git/blobdiff - src/app.cpp
mouse controlled camera pitch/yaw
[blank.git] / src / app.cpp
index 7ecbb744ac65ce5be365e8548fac7ef13788a275..ff5a3bef59f1a3081b2a5c93ceae49953670ee03 100644 (file)
@@ -7,9 +7,9 @@
 namespace {
 
 constexpr GLfloat vtx_coords[] = {
-       -1.0f, -1.0f, -5.0f,
-        1.0f, -1.0f, -5.0f,
-        0.0f,  1.0f, -5.0f,
+       -1.0f, -1.0f, 0.0f,
+        1.0f, -1.0f, 0.0f,
+        0.0f,  1.0f, 0.0f,
 };
 
 }
@@ -24,7 +24,10 @@ Application::Application()
 , ctx(window.CreateContext())
 , init_glew()
 , program()
+, pitch_sensitivity(-0.0025f)
+, yaw_sensitivity(-0.001f)
 , cam()
+, model()
 , vtx_buf(0)
 , mvp_handle(0)
 , running(false) {
@@ -61,6 +64,9 @@ Application::Application()
        glBindBuffer(GL_ARRAY_BUFFER, vtx_buf);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_coords), vtx_coords, GL_STATIC_DRAW);
 
+       model.Position(glm::vec3(0, 0, -4));
+       cam.Position(glm::vec3(0, 0, 4));
+
        mvp_handle = program.UniformLocation("MVP");
 
        glClearColor(0.0, 0.0, 0.0, 1.0);
@@ -74,6 +80,7 @@ Application::~Application() {
 void Application::Run() {
        running = true;
        Uint32 last = SDL_GetTicks();
+       window.GrabMouse();
        while (running) {
                Uint32 now = SDL_GetTicks();
                int delta = now - last;
@@ -92,6 +99,12 @@ void Application::HandleEvents() {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
                switch (event.type) {
+                       case SDL_MOUSEMOTION:
+                               cam.RotateYaw(event.motion.xrel * yaw_sensitivity);
+                               cam.RotatePitch(event.motion.yrel * pitch_sensitivity);
+                               std::cout << "x: " << event.motion.xrel << ", y: " << event.motion.yrel
+                                               << ", pitch: " << cam.Pitch() << ", yaw: " << cam.Yaw() << std::endl;
+                               break;
                        case SDL_QUIT:
                                running = false;
                                break;
@@ -115,8 +128,7 @@ void Application::Render() {
 
        program.Use();
 
-       glm::mat4 model(1.0f); // identity: no transformation
-       glm::mat4 mvp(cam.MakeMVP(model));
+       glm::mat4 mvp(cam.MakeMVP(model.Transform()));
        glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
 
        glEnableVertexAttribArray(0);