Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3D rendered models #50

Draft
wants to merge 3 commits into
base: vss
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ SET(renderer_SRCS
src/renderer/ResourceId.h
src/renderer/common.h
src/renderer/common.cpp
src/renderer/vectormath.cpp
src/renderer/vectormath.h
src/renderer/compositor/AbstractCompositor.h
src/renderer/compositor/CompositorException.cpp
src/renderer/compositor/CompositorException.h
Expand All @@ -19,6 +21,7 @@ SET(renderer_SRCS
src/renderer/compositor/sdl_ext/SDL_extensions.cpp
src/renderer/compositor/sdl_ext/SDL_extensions.h
src/renderer/visualbackend/AbstractVisualBackend.h
src/renderer/visualbackend/AbstractVisualBackend.cpp
src/renderer/visualbackend/VisualBackendContext.cpp
src/renderer/visualbackend/VisualBackendContext.h
src/renderer/visualbackend/dummy/DummyVisualBackend.cpp
Expand Down
19 changes: 19 additions & 0 deletions lib/renderer/src/renderer/vectormath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "vectormath.h"

using namespace renderer::vectormath;

Transform Transform::concat(const Transform& other)
{
return {
.position = position + rotation.transform(other.position * scale),
.scale = scale * other.scale,
.rotation = rotation * other.rotation
};
}

Vector3 Quaternion::transform(const Vector3& v) const
{
Vector3 u {x, y, z};
Vector3 uv = u.cross(v);
return v + ((uv * w) + u.cross(uv)) * 2.0f;
}
111 changes: 111 additions & 0 deletions lib/renderer/src/renderer/vectormath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef MATH_H
#define MATH_H

#include <math.h>

namespace renderer::vectormath {
struct Vector3 {
float x;
float y;
float z;

inline Vector3 operator+(const Vector3& other) const {
return {
.x = x + other.x,
.y = y + other.y,
.z = z + other.z,
};
}

inline Vector3 operator-(const Vector3& other) const {
return {
.x = x - other.x,
.y = y - other.y,
.z = z - other.z,
};
}

inline Vector3 operator*(float value) const {
return {
.x = value * x,
.y = value * y,
.z = value * z,
};
}


inline Vector3 operator/(float value) const {
Vector3 r = {
.x = x / value,
.y = y / value,
.z = z / value,
};
return r;
}

inline Vector3 cross(const Vector3& other) const {
return {
.x = (y * other.z) - (z * other.y),
.y = (z * other.x) - (x * other.z),
.z = (x * other.y) - (y * other.x),
};
}

inline float dot(const Vector3& other) const {
return x * other.x + y * other.y + z * other.z;
}
};

struct Quaternion {
float x;
float y;
float z;
float w;

inline friend Quaternion operator*(const Quaternion& q1, const Quaternion& q2){
return {
.x = (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y) + (q1.w * q2.x),
.y = (-q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x) + (q1.w * q2.y),
.z = (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w) + (q1.w * q2.z),
.w = (-q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z) + (q1.w * q2.w),
};
}

inline Quaternion operator*(float value) const {
return {
x * value,
y * value,
z * value,
w * value,
};
}

inline Quaternion operator/(float value) const {
return {
x / value,
y / value,
z / value,
w / value,
};
}

inline float length() const {
return std::sqrt(x * x + y * y + z * z + w * w);
}

inline Quaternion normalized() const {
return *this / length();
}

Vector3 transform(const Vector3& v) const;
};

struct Transform {
Vector3 position;
float scale;
Quaternion rotation;

Transform concat(const Transform& other);
};
}
#endif // MATH_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "AbstractVisualBackend.h"
using namespace renderer::visualbackend;


32 changes: 16 additions & 16 deletions lib/renderer/src/renderer/visualbackend/AbstractVisualBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../common.h"
#include "../ResourceId.h"
#include "../vectormath.h"

namespace renderer::visualbackend {
struct MapDescription {
Expand All @@ -25,30 +26,20 @@ namespace renderer::visualbackend {
float aspect;
float near_plane;
float far_plane;
};

struct Vector3 {
float x;
float y;
float z;
};
};

struct Quaternion {
float x;
float y;
float z;
float w;
struct ModelHandle {
uint64_t handle;
};

struct Transform {
Vector3 position;
Quaternion rotation;
struct ModelInstanceHandle {
uint64_t handle;
};

class AbstractVisualBackend {
public:
virtual void camera_set_projection(const CameraProjection& camera_projection) = 0;
virtual void camera_set_transform(const Transform& transform) = 0;
virtual void camera_set_transform(const vectormath::Transform& transform) = 0;

// Creates HeightMap from description
virtual void map_create(const MapDescription& map_description) = 0;
Expand All @@ -67,6 +58,15 @@ namespace renderer::visualbackend {
// Call this on screen resolution change
virtual void set_screen_resolution(int32_t width, int32_t height) = 0;

// TODO: replace void* with Model*
virtual ModelHandle model_create(const char* name, void* model) = 0;
virtual void model_destroy(ModelHandle model_handle) = 0;

virtual ModelInstanceHandle model_instance_create(ModelHandle model_handle, uint8_t color_id) = 0;
virtual void model_instance_destroy(ModelInstanceHandle model_instance_handle) = 0;
virtual void model_instance_set_transform(ModelInstanceHandle model_instance_handle, const vectormath::Transform& transform) = 0;
virtual void model_instance_set_visible(ModelInstanceHandle model_instance_handle, bool visible) = 0;

// Renders the scene into the viewport with size viewport_width*viewport_height and with camera position *camera_pos_XXX*
// TODO: need to discuss and refactor this function signature
virtual void render(const Rect& viewport) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
using namespace renderer::visualbackend;

std::unique_ptr<VisualBackendContext> VisualBackendContext::_instance(nullptr);
bool VisualBackendContext::_renderer_enabled(true);
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ namespace renderer::visualbackend {
_renderer = std::move(renderer);
}

static bool renderer_enabled() {
return _renderer_enabled;
}

private:
std::unique_ptr<AbstractVisualBackend> _renderer;
static std::unique_ptr<VisualBackendContext> _instance;
static bool _renderer_enabled;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void DummyVisualBackend::camera_set_projection(const CameraProjection &camera_pr

}

void DummyVisualBackend::camera_set_transform(const Transform &transform)
void DummyVisualBackend::camera_set_transform(const vectormath::Transform &transform)
{

}
Expand All @@ -45,3 +45,38 @@ void DummyVisualBackend::map_update_palette(uint32_t *palette, int32_t palette_s

}

void DummyVisualBackend::set_screen_resolution(int32_t width, int32_t height)
{

}

ModelHandle DummyVisualBackend::model_create(const char* name, void* model)
{

}

void DummyVisualBackend::model_destroy(ModelHandle model_handle)
{

}

ModelInstanceHandle DummyVisualBackend::model_instance_create(ModelHandle model_handle, uint8_t color_id)
{

}

void DummyVisualBackend::model_instance_destroy(ModelInstanceHandle model_instance_handle)
{

}

void DummyVisualBackend::model_instance_set_transform(ModelInstanceHandle model_instance_handle, const vectormath::Transform& transform)
{

}

void DummyVisualBackend::destroy()
{

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ namespace renderer::visualbackend::dummy {

void camera_set_projection(const CameraProjection &camera_projection) override;

void camera_set_transform(const Transform &transform) override;
void camera_set_transform(const vectormath::Transform &transform) override;

void map_update_palette(uint32_t *palette, int32_t palette_size) override;

void set_screen_resolution(int32_t width, int32_t height);
ModelHandle model_create(const char* name, void* model);
void model_destroy(ModelHandle model_handle);
ModelInstanceHandle model_instance_create(ModelHandle model_handle, uint8_t color_id);
void model_instance_destroy(ModelInstanceHandle model_instance_handle);
void model_instance_set_transform(ModelInstanceHandle model_instance_handle, const vectormath::Transform& transform);
void destroy();
};

}
Expand Down
Loading