Skip to content

Commit

Permalink
added a auto camera control interface
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriousSamV committed Jan 31, 2017
1 parent 4428be5 commit c266514
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(exe06 "app_06.app")
set(exe07 "app_07.app")
set(exe08 "app_08.app")
set(exeUnderstaningCamera "app_understandingCamera.app")
set(exe09 "app_09.app")

find_package(PkgConfig)
pkg_check_modules(GTKMM gtkmm-3.0)
Expand All @@ -25,7 +26,8 @@ find_package(GLEW REQUIRED)
find_package(glfw3 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(glm REQUIRED)
find_package(Boost)
find_package(Boost REQUIRED)
find_package(tinyobjloader REQUIRED)

set(SOIL_INCLUDE_DIR "/usr/include/SOIL")
set(SOIL_LIBRARY "SOIL")
Expand All @@ -52,6 +54,7 @@ set(ALL_LIBS

link_directories(
${GTKMM_LIBRARY_DIRS}
${TINYOBJLOADER_LIBRARY_DIRS}
)

message(STATUS "INC DIR: \t" ${ALL_INCLUDE_DIR})
Expand Down Expand Up @@ -94,6 +97,9 @@ add_executable(${exeUnderstaningCamera} ./understanding_camera/main.cpp)
target_include_directories(${exeUnderstaningCamera} PUBLIC ${ALL_INCLUDE_DIR})
target_link_libraries(${exeUnderstaningCamera} PUBLIC ${ALL_LIBS})

add_executable(${exe09} ./09/main.cpp)
target_include_directories(${exe09} PUBLIC ${ALL_INCLUDE_DIR} ${TINYOBJLOADER_INCLUDE_DIRS})
target_link_libraries(${exe09} PUBLIC ${ALL_LIBS} ${TINYOBJLOADER_LIBRARIES})


# TESTS
Expand Down
104 changes: 100 additions & 4 deletions lib/camera.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __MYLIB_CAMERA_HPP__
#define __MYLIB_CAMERA_HPP__

#include "./mylib.hpp"

#include <iostream>
#include <vector>
#include <cmath>
Expand All @@ -24,7 +26,7 @@ enum Camera_Movement
const GLfloat YAW = -90.0f;
const GLfloat PITCH = 0.0f;
const GLfloat SPEED = 3.0f;
const GLfloat SENSITIVITY = 0.25f;
const GLfloat SENSITIVITY = 0.05f;
const GLfloat ZOOM = 45.0f;

// abstract Camera class
Expand Down Expand Up @@ -98,12 +100,13 @@ class Camera

// Processes input received from a mouse input system.
// Expects the offset value in both the x and y direction.
void processMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true)
virtual void processMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true)
{
xoffset *= this->mouseSensitivity;
yoffset *= this->mouseSensitivity;

this->yaw += xoffset;
//this->yaw += xoffset;
this->yaw = std::fmod((this->yaw + xoffset), (GLfloat)360.0f);
this->pitch += yoffset;

// Make sure that when pitch is out of bounds, screen doesn't get flipped
Expand All @@ -120,7 +123,7 @@ class Camera
}

// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
void processMouseScroll(GLfloat yoffset)
virtual void processMouseScroll(GLfloat yoffset)
{
if (this->zoom >= 1.0f && this->zoom <= 45.0f)
this->zoom -= yoffset;
Expand Down Expand Up @@ -176,6 +179,99 @@ class FPSCamera : public Camera
}
};

class CameraController
{
public:
Camera *camera;

double xpos{0}, ypos{0}, lastFrameTime{0}, deltaTime{0};
volatile GLfloat lastX{0}, lastY{0}, xoffset{0}, yoffset{0};

CameraController(
Camera *camera = nullptr,
mylib::Window *window = nullptr) : camera(std::move(camera)),
window(window)
{
std::cout << "construct" << std::endl;
if (window != nullptr)
{
// for keyboard
lastFrameTime = glfwGetTime();

// for mouse
glfwSetInputMode(window->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPos(window->getWindow(), window->width, window->height);
glfwGetCursorPos(window->getWindow(), &xpos, &ypos);
lastX = static_cast<GLfloat>(xpos);
lastY = static_cast<GLfloat>(ypos);
}
}

static void update(CameraController& cc, double time)
{
cc.updateKeyboard(time);
cc.updateMousePosition(time);
}

virtual void __attribute__((optimize("O0"))) updateKeyboard(double time)
{
if (window != nullptr)
{
this->deltaTime = time - lastFrameTime;
std::clog << "delta: " << deltaTime << "\tLFT: " << lastFrameTime
<< std::endl;
this->lastFrameTime = time;

// W - key press
if (glfwGetKey(window->getWindow(), GLFW_KEY_W) == GLFW_PRESS)
{
camera->processKeyboard(mylib::FORWARD, deltaTime);
}
// S - key press
if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS)
{
camera->processKeyboard(mylib::BACKWARD, deltaTime);
}
// A - key press
if (glfwGetKey(window->getWindow(), GLFW_KEY_A) == GLFW_PRESS)
{
camera->processKeyboard(mylib::LEFT, deltaTime);
}
// D - key press
if (glfwGetKey(window->getWindow(), GLFW_KEY_D) == GLFW_PRESS)
{
camera->processKeyboard(mylib::RIGHT, deltaTime);
}
}
}

virtual void __attribute__((optimize("O0"))) updateMousePosition(double time)
{
if (window != nullptr)
{
std::cout << "BEFORE\t\t" << this->xoffset << ", " << this->yoffset
<< "\tlast: (" << this->lastX << ", " << this->lastY << ")"
<< "\tpos: (" << this->xpos << ", " << this->ypos << ")" << std::endl;
glfwGetCursorPos(window->getWindow(), &this->xpos, &this->ypos);
this->xoffset = static_cast<GLfloat>(this->xpos - this->lastX);
// reversed y cord, botton to top
this->yoffset = static_cast<GLfloat>(this->lastY - this->ypos);

std::cout << this->xoffset << ", " << this->yoffset
<< "\tlast: (" << this->lastX << ", " << this->lastY << ")"
<< "\tpos: (" << this->xpos << ", " << this->ypos << ")" << std::endl;
this->lastX = static_cast<GLfloat>(this->xpos);
this->lastY = static_cast<GLfloat>(this->ypos);
camera->processMouseMovement(this->xoffset, this->yoffset);
}
}

protected:
mylib::Window *window{nullptr};

private:
};

} // namespace mylib

#endif
19 changes: 15 additions & 4 deletions understanding_camera/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* just a playground to understand my camera hehehe
*/

#define _NDEBUG

#include "../lib/mylib.hpp"
#include "../lib/camera.hpp"

Expand All @@ -23,6 +21,8 @@
#include <cstdio>
#include <cstdlib>

#undef _NDEBUG

GLfloat vertices_box[] = {
0.250000f, -0.250000f, -0.250000f, // 0,
0.250000f, -0.250000f, 0.250000f, // 1
Expand Down Expand Up @@ -96,8 +96,8 @@ class myAppUsingCameraClass : public mylib::App
lightColorLoc = glGetUniformLocation(lightingShader.program, "lightColor");

camera = mylib::FPSCamera(
glm::vec3(0.0f, 0.0f, 3.0f), // cameraPos
glm::vec3(0.0f, 1.0f, 0.0f)); // worldUp
glm::vec3(0.0f, 0.0f, 3.0f), // cameraPos
glm::vec3(0.0f, 1.0f, 0.0f)); // worldUp

// mouse setup
glfwSetInputMode(window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
Expand Down Expand Up @@ -126,6 +126,10 @@ class myAppUsingCameraClass : public mylib::App
lastY = ypos;
camera.processMouseMovement(xoffset, yoffset);

std::cout << xoffset << ", " << yoffset
<< "\tlast: (" << lastX << ", " << lastY << ")"
<< "\tpos: (" << xpos << ", " << ypos << ")" << std::endl;

// W - key press
if (glfwGetKey(window.getWindow(), GLFW_KEY_W) == GLFW_PRESS)
{
Expand Down Expand Up @@ -201,6 +205,7 @@ class myAppUsingCameraClass : public mylib::App
private:
};

/*
class myApp : public mylib::App
{
public:
Expand Down Expand Up @@ -268,6 +273,7 @@ class myApp : public mylib::App
void update(double time) override
{
std::cout << "time: " << time << std::endl;
deltaTime = time - lastFrameTime;
lastFrameTime = time;
cameraSpeed = 0.5f * deltaTime;
Expand All @@ -286,6 +292,10 @@ class myApp : public mylib::App
cameraFront.z = std::cos(glm::radians(pitch)) * std::sin(glm::radians(yaw));
cameraFront = glm::normalize(cameraFront);
std::cout << xoffset << ", " << yoffset
<< "\tlast: (" << lastX << ", " << lastY << ")"
<< "\tpos: (" << xpos << ", " << ypos << ")" << std::endl;
// W - key press
if (glfwGetKey(window.getWindow(), GLFW_KEY_W) == GLFW_PRESS)
{
Expand Down Expand Up @@ -380,6 +390,7 @@ class myApp : public mylib::App
private:
};
*/

int main(const int argc, const char *const argv[])
{
Expand Down

0 comments on commit c266514

Please sign in to comment.