Skip to content

Commit

Permalink
couple of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntRanch committed Jan 8, 2025
1 parent ebcdfc0 commit a3816bf
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 19 deletions.
14 changes: 12 additions & 2 deletions include/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ enum Camera_Movement {
RIGHT
};

enum Camera_Type {
CAMERA_ORTHOGRAPHIC,
CAMERA_PERSPECTIVE,
};

// Default camera values
const float YAW = 0.0f;
const float PITCH = 0.0f;
Expand All @@ -33,6 +38,8 @@ class Object;
class Camera
{
public:
Camera_Type type = CAMERA_PERSPECTIVE;

// camera Attributes
glm::vec3 Front;
glm::vec3 Up;
Expand All @@ -46,11 +53,14 @@ class Camera
float MouseSensitivity;
float FOV;

float AspectRatio;
float OrthographicWidth;

// constructor with vectors
Camera(glm::vec3 up = glm::vec3(0.0f, 0.0f, 1.0f), float yaw = YAW, float pitch = PITCH);
Camera(float aspectRatio, glm::vec3 up = glm::vec3(0.0f, 0.0f, 1.0f), float yaw = YAW, float pitch = PITCH);

// constructor with scalar values
Camera(float upX = 0.0f, float upY = 0.0f, float upZ = 1.0f, float yaw = YAW, float pitch = PITCH);
Camera(float aspectRatio, float upX = 0.0f, float upY = 0.0f, float upZ = 1.0f, float yaw = YAW, float pitch = PITCH);

// returns the view matrix calculated using Euler Angles and the LookAt Matrix
glm::mat4 GetViewMatrix();
Expand Down
5 changes: 5 additions & 0 deletions include/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ struct Networking_Object {

struct Networking_Camera {
int cameraID;

bool isOrthographic = false;
float aspectRatio;
float orthographicWidth;

float pitch;
float yaw;
glm::vec3 up;
Expand Down
8 changes: 6 additions & 2 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@
#include "fmt/format.h"
#include "object.hpp"

Camera::Camera(glm::vec3 up, float yaw, float pitch) : Front(glm::vec3(0.0f, 1.0f, 0.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), FOV(FIELDOFVIEW)
Camera::Camera(float aspectRatio, glm::vec3 up, float yaw, float pitch) : Front(glm::vec3(0.0f, 1.0f, 0.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), FOV(FIELDOFVIEW)
{
HighestCameraID++;
SetCameraID(HighestCameraID);

AspectRatio = aspectRatio;

WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}

// constructor with scalar values
Camera::Camera(float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 1.0f, 0.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), FOV(FIELDOFVIEW)
Camera::Camera(float aspectRatio, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 1.0f, 0.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), FOV(FIELDOFVIEW)
{
HighestCameraID++;
SetCameraID(HighestCameraID);

AspectRatio = aspectRatio;

WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
Expand Down
54 changes: 42 additions & 12 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,23 @@ std::array<TextureImageAndMemory, 1> Renderer::LoadTexturesFromMesh(Mesh &mesh,
{
if (!mesh.diffuseMapPath.empty()) {
std::filesystem::path path;
// map_Kd /home/toni/.../brown_mud_dry_diff_4k.jpg
if (mesh.diffuseMapPath.has_root_path())
{
path = mesh.diffuseMapPath;
}
else
{

if (!mesh.diffuseMapPath.has_root_path()) {
// https://stackoverflow.com/a/73927710
auto rel = std::filesystem::relative(mesh.diffuseMapPath, "textures");
// map_Kd textures/brown_mud_dry_diff_4k.jpg
auto rel = std::filesystem::relative(mesh.diffuseMapPath, "resources");
// map_Kd resources/brown_mud_dry_diff_4k.jpg
if (!rel.empty() && rel.native()[0] != '.')
path = mesh.diffuseMapPath;
// map_Kd brown_mud_dry_diff_4k.jpg
else
path = "textures" / mesh.diffuseMapPath;
path = "resources" / mesh.diffuseMapPath;
}

std::string absoluteSourcePath = std::filesystem::absolute(path).string();
std::string absoluteResourcesPath = std::filesystem::absolute("resources").string();

UTILASSERT(absoluteSourcePath.substr(0, absoluteResourcesPath.length()).compare(absoluteResourcesPath) == 0);

TextureBufferAndMemory textureBufferAndMemory = LoadTextureFromFile(path);
VkFormat textureFormat = getBestFormatFromChannels(textureBufferAndMemory.channels);

Expand Down Expand Up @@ -2178,7 +2178,12 @@ void Renderer::Start() {

if (m_PrimaryCamera) {
viewMatrix = m_PrimaryCamera->GetViewMatrix();
projectionMatrix = glm::perspective(glm::radians(m_PrimaryCamera->FOV), m_Settings.RenderWidth / (float) m_Settings.RenderHeight, m_Settings.CameraNear, CAMERA_FAR);

if (m_PrimaryCamera->type == CAMERA_PERSPECTIVE) {
projectionMatrix = glm::perspective(glm::radians(m_PrimaryCamera->FOV), m_PrimaryCamera->AspectRatio, m_Settings.CameraNear, CAMERA_FAR);
} else {
projectionMatrix = glm::ortho(0.0f, m_PrimaryCamera->OrthographicWidth, 0.0f, m_PrimaryCamera->OrthographicWidth*m_PrimaryCamera->AspectRatio);
}

// invert Y axis, glm was meant for OpenGL which inverts the Y axis.
projectionMatrix[1][1] *= -1;
Expand Down Expand Up @@ -3399,9 +3404,17 @@ void Engine::ProcessNetworkEvents(std::vector<Networking_Event> *networkingEvent
case NETWORKING_NEW_CAMERA:
cameraPacket = event.camera.value();

camera = new Camera(cameraPacket.up, cameraPacket.yaw, cameraPacket.pitch);
camera = new Camera(cameraPacket.aspectRatio, cameraPacket.up, cameraPacket.yaw, cameraPacket.pitch);

camera->SetCameraID(cameraPacket.cameraID);

if (cameraPacket.isOrthographic) {
camera->type = CAMERA_ORTHOGRAPHIC;

camera->OrthographicWidth = cameraPacket.orthographicWidth;
}

camera->AspectRatio = cameraPacket.aspectRatio;

camera->Pitch = cameraPacket.pitch;
camera->Yaw = cameraPacket.yaw;
Expand Down Expand Up @@ -3643,6 +3656,11 @@ void Engine::DeserializeNetworkingObject(std::vector<std::byte> &serializedObjec
void Engine::DeserializeNetworkingCamera(std::vector<std::byte> &serializedCameraPacket, Networking_Camera &dest) {
Deserialize(serializedCameraPacket, dest.cameraID);

Deserialize(serializedCameraPacket, dest.isOrthographic);

Deserialize(serializedCameraPacket, dest.aspectRatio);
Deserialize(serializedCameraPacket, dest.orthographicWidth);

Deserialize(serializedCameraPacket, dest.pitch);
Deserialize(serializedCameraPacket, dest.yaw);

Expand Down Expand Up @@ -3733,6 +3751,9 @@ Networking_Camera Engine::AddCameraToStatePacket(Camera *cam, Networking_StatePa
Networking_Camera cameraPacket{};

cameraPacket.cameraID = cam->GetCameraID();
cameraPacket.isOrthographic = cam->type == CAMERA_ORTHOGRAPHIC;
cameraPacket.aspectRatio = cam->AspectRatio;
cameraPacket.orthographicWidth = cam->OrthographicWidth;
cameraPacket.pitch = cam->Pitch;
cameraPacket.yaw = cam->Yaw;
cameraPacket.up = cam->Up;
Expand All @@ -3749,10 +3770,14 @@ void Engine::AddCameraToStatePacketIfChanged(Camera *cam, Networking_StatePacket
bool anythingChanged = lastPacketCameraEquivalent == m_LastPacket.cameras.end();

if (!anythingChanged && (
(cam->type == CAMERA_ORTHOGRAPHIC) != lastPacketCameraEquivalent->isOrthographic ||
cam->AspectRatio != lastPacketCameraEquivalent->aspectRatio ||
cam->OrthographicWidth != lastPacketCameraEquivalent->orthographicWidth ||
cam->Pitch != lastPacketCameraEquivalent->pitch ||
cam->Yaw != lastPacketCameraEquivalent->yaw ||
cam->Up != lastPacketCameraEquivalent->up ||
cam->FOV != lastPacketCameraEquivalent->fov ||
/* TODO: This constantly syncs the camera if there's more than one player connected because isMainCamera could change across connections. */
isMainCamera != lastPacketCameraEquivalent->isMainCamera))
anythingChanged = true;

Expand Down Expand Up @@ -3879,6 +3904,11 @@ void Engine::SerializeNetworkingObject(Networking_Object &objectPacket, std::vec
void Engine::SerializeNetworkingCamera(Networking_Camera &cameraPacket, std::vector<std::byte> &dest) {
Serialize(cameraPacket.cameraID, dest);

Serialize(cameraPacket.isOrthographic, dest);

Serialize(cameraPacket.aspectRatio, dest);
Serialize(cameraPacket.orthographicWidth, dest);

Serialize(cameraPacket.pitch, dest);
Serialize(cameraPacket.yaw, dest);

Expand Down
2 changes: 1 addition & 1 deletion src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene)
if (diffuseTextureCount >= 1) {
aiString path;
material->GetTexture(aiTextureType_DIFFUSE, 0, &path);
diffuseMap = string(path.C_Str());
diffuseMap = string(std::filesystem::absolute(path.C_Str()));
}

aiColor4D aiDiffuseColor;
Expand Down
11 changes: 9 additions & 2 deletions src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,22 @@ void Object::ProcessNode(aiNode *node, const aiScene *scene, int &sourceID, Obje
if (scene->mCameras[i]->mName == node->mName) {
aiCamera *sceneCam = scene->mCameras[i];

UTILASSERT((sceneCam->mOrthographicWidth > 0 || sceneCam->mHorizontalFOV > 0) && !(sceneCam->mOrthographicWidth > 0 && sceneCam->mHorizontalFOV > 0));

aiVector3D direction = aiVector3D(-node->mTransformation.a3, -node->mTransformation.b3, -node->mTransformation.c3);
direction.Normalize();

float pitch = glm::degrees(std::atan2(direction.x, direction.y));
float yaw = glm::degrees(std::asin(direction.z));

Camera *cam = new Camera(glm::vec3(sceneCam->mUp.x, sceneCam->mUp.y, sceneCam->mUp.z), yaw, pitch);
Camera *cam = new Camera(sceneCam->mAspect, glm::vec3(sceneCam->mUp.x, sceneCam->mUp.y, sceneCam->mUp.z), yaw, pitch);

cam->FOV = glm::degrees(sceneCam->mHorizontalFOV);
if (sceneCam->mHorizontalFOV > 0) {
cam->FOV = glm::degrees(sceneCam->mHorizontalFOV);
} else {
cam->type = CAMERA_ORTHOGRAPHIC;
cam->OrthographicWidth = sceneCam->mOrthographicWidth;
}

obj->SetCameraAttachment(cam);

Expand Down

0 comments on commit a3816bf

Please sign in to comment.