Skip to content

Commit

Permalink
Added camera
Browse files Browse the repository at this point in the history
  • Loading branch information
denis authored and denis committed Jul 30, 2024
1 parent 7c93454 commit 8cfc7dc
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 11 deletions.
1 change: 1 addition & 0 deletions Engine/include/SceneData/SceneData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Prisma {
virtual void start() = 0;
virtual void update() = 0;
virtual void finish() = 0;
virtual std::shared_ptr<CallbackHandler> callbacks() = 0;
};

struct Scene {
Expand Down
1 change: 1 addition & 0 deletions Engine/include/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Prisma {
Pipeline pipeline();
std::shared_ptr<Prisma::Scene> getScene(std::string scene, Prisma::SceneLoader::SceneParameters sceneParameters);
void setUserEngine(std::shared_ptr<Prisma::UserData> userData);
std::shared_ptr<Prisma::UserData> getUserEngine();
void setGuiData(std::shared_ptr<Prisma::SceneHandler> guiData);
void debug(bool debug);

Expand Down
12 changes: 8 additions & 4 deletions Engine/src/GlobalData/PrismaFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

struct PrivatePrisma {
std::shared_ptr<Prisma::CallbackHandler> callback;
bool initCallback = false;;
};

std::shared_ptr<Prisma::PrismaFunc> Prisma::PrismaFunc::instance = nullptr;
Expand All @@ -31,7 +32,7 @@ namespace Prisma {
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
privatePrisma->callback->mouseClick(button,action,xpos,ypos);
privatePrisma->callback->mouseClick(button, action, xpos, ypos);
}
}

Expand Down Expand Up @@ -76,9 +77,12 @@ void Prisma::PrismaFunc::clear()
void Prisma::PrismaFunc::setCallback(std::shared_ptr<CallbackHandler> callbackHandler)
{
privatePrisma->callback = callbackHandler;
glfwSetFramebufferSizeCallback(m_window, Prisma::framebufferSizeCallback);
glfwSetCursorPosCallback(m_window, Prisma::mouseCallback);
glfwSetMouseButtonCallback(m_window, Prisma::mouseButtonCallback);
if (!privatePrisma->initCallback) {
glfwSetFramebufferSizeCallback(m_window, Prisma::framebufferSizeCallback);
glfwSetCursorPosCallback(m_window, Prisma::mouseCallback);
glfwSetMouseButtonCallback(m_window, Prisma::mouseButtonCallback);
privatePrisma->initCallback = true;
}
}

void Prisma::PrismaFunc::closeWindow()
Expand Down
5 changes: 5 additions & 0 deletions Engine/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ void Prisma::Engine::debug(bool debug)
data->debug = debug;
}

std::shared_ptr<Prisma::UserData> Prisma::Engine::getUserEngine()
{
return data->userData;
}

std::shared_ptr<Prisma::Scene> Prisma::Engine::getScene(std::string scene, Prisma::SceneLoader::SceneParameters sceneParameters)
{
SceneLoader sceneLoader;
Expand Down
2 changes: 1 addition & 1 deletion GUI/include/ImGuiCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Prisma {
float m_yaw = -90.0f;
float m_pitch = 0.0f;
float m_velocity = 1.0;
bool firstMouse = true;
bool m_firstMouse = true;
float m_lastX;
float m_lastY;
bool m_lock = false;
Expand Down
4 changes: 2 additions & 2 deletions GUI/src/ImGuiCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ void Prisma::ImGuiCamera::mouseCallback()
float xpos = static_cast<float>(x);
float ypos = static_cast<float>(y);

if (firstMouse)
if (m_firstMouse)
{
m_lastX = xpos;
m_lastY = ypos;
firstMouse = false;
m_firstMouse = false;
}

float xoffset = xpos - m_lastX;
Expand Down
6 changes: 6 additions & 0 deletions GUI/src/ImguiDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ void Prisma::ImguiDebug::drawGui()
{
m_run = !m_run;
Prisma::Engine::getInstance().debug(!m_run);
if (m_run) {
Prisma::Engine::getInstance().setCallback(Prisma::Engine::getInstance().getUserEngine()->callbacks());
}
else {
Prisma::Engine::getInstance().setCallback(m_imguiCamera.callback());
}
}
ImGui::End();
if (!m_run) {
Expand Down
31 changes: 31 additions & 0 deletions UserEngine/include/UserEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,35 @@ class UserEngine : public Prisma::UserData {

virtual void finish();

virtual std::shared_ptr<Prisma::CallbackHandler> callbacks();
private:

void updateCamera();

void updateKeyboard();

void createCamera();

std::shared_ptr<Prisma::CallbackHandler> m_handler;

glm::vec3 m_position = glm::vec3(0.0f);

glm::vec3 m_front = glm::vec3(0.0f, 0.0f, -1.0f);

glm::vec3 m_up = glm::vec3(0.0f, 1.0f, 0.0f);

float m_yaw = -90.0f;

float m_pitch = 0.0f;

float m_velocity = 1.0;

bool m_firstMouse = true;

float m_lastX;

float m_lastY;

std::shared_ptr<Prisma::Scene> m_root;

};
66 changes: 62 additions & 4 deletions UserEngine/src/UserEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

void UserEngine::start()
{
auto root = Prisma::Engine::getInstance().getScene("../../../Resources/DefaultScene/default.gltf", { true });
m_root = Prisma::Engine::getInstance().getScene("../../../Resources/DefaultScene/default.gltf", { true });
Prisma::Texture texture;
texture.loadEquirectangular("../../../Resources/Skybox/equirectangular.hdr");
texture.data({ 4096,4096,3 });
Prisma::PipelineSkybox::getInstance().texture(texture, true);

Prisma::NodeHelper nodeHelper;

auto animatedMesh = std::dynamic_pointer_cast<Prisma::AnimatedMesh>(nodeHelper.find(root->root, "MutantMesh")->children()[0]);
auto animatedMesh = std::dynamic_pointer_cast<Prisma::AnimatedMesh>(nodeHelper.find(m_root->root, "MutantMesh")->children()[0]);

if (animatedMesh) {
auto animation = std::make_shared<Prisma::Animation>("../../../Resources/DefaultScene/animations/animation.gltf", animatedMesh);
Expand All @@ -31,7 +31,7 @@ void UserEngine::start()
animatedMesh->animator(animator);
}

nodeHelper.nodeIterator(root->root, [](auto mesh, auto parent) {
nodeHelper.nodeIterator(m_root->root, [](auto mesh, auto parent) {
auto currentMesh = std::dynamic_pointer_cast<Prisma::Mesh>(mesh);
if (currentMesh) {
auto physicsComponent = std::make_shared<Prisma::PhysicsMeshComponent>();
Expand All @@ -41,13 +41,71 @@ void UserEngine::start()
});

Prisma::Physics::getInstance().physicsWorld()->dynamicsWorld->setGravity(btVector3(0.0, -10.0, 0.0));
m_handler = std::make_shared<Prisma::CallbackHandler>();
createCamera();
}

void UserEngine::update()
{

updateCamera();
}

void UserEngine::finish()
{
}

std::shared_ptr<Prisma::CallbackHandler> UserEngine::callbacks()
{
return m_handler;
}

void UserEngine::updateCamera()
{
m_root->camera->position(m_position);
m_root->camera->center(m_position + m_front);
m_root->camera->up(m_up);
}

void UserEngine::updateKeyboard()
{
}

void UserEngine::createCamera()
{
m_handler->mouse = [this](float x, float y) {
float xpos = static_cast<float>(x);
float ypos = static_cast<float>(y);

if (m_firstMouse)
{
m_lastX = xpos;
m_lastY = ypos;
m_firstMouse = false;
}

float xoffset = xpos - m_lastX;
float yoffset = m_lastY - ypos; // reversed since y-coordinates go from bottom to top
m_lastX = xpos;
m_lastY = ypos;

float sensitivity = 0.1f; // change this value to your liking
xoffset *= sensitivity;
yoffset *= sensitivity;

m_yaw += xoffset;
m_pitch += yoffset;

// make sure that when pitch is out of bounds, screen doesn't get flipped
if (m_pitch > 89.0f)
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = -89.0f;

glm::vec3 front;
front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
front.y = sin(glm::radians(m_pitch));
front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(front);
};
m_handler->mouseClick = [](int button, int action, double x, double y) {};
}

0 comments on commit 8cfc7dc

Please sign in to comment.