aboutsummaryrefslogtreecommitdiff
path: root/src/fb
diff options
context:
space:
mode:
Diffstat (limited to 'src/fb')
-rw-r--r--src/fb/chfb.cpp13
-rw-r--r--src/fb/chfb.hpp4
-rw-r--r--src/fb/fb.hpp21
-rw-r--r--src/fb/pixfb.cpp37
-rw-r--r--src/fb/pixfb.hpp4
5 files changed, 33 insertions, 46 deletions
diff --git a/src/fb/chfb.cpp b/src/fb/chfb.cpp
index 7341015..e7717eb 100644
--- a/src/fb/chfb.cpp
+++ b/src/fb/chfb.cpp
@@ -3,15 +3,10 @@
#include <cstdint>
#include "math/vector.hpp"
#include "math/quat.hpp"
-#include "o3d/vertex_data.hpp"
#include "o3d/camera.hpp"
using namespace engine::fb;
-using
- engine::math::Vector2,
- engine::math::Vector3,
- engine::o3d::VertexData,
- engine::o3d::Camera;
+using engine::math::Vector4;
CharacterFrameBuffer::CharacterFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
@@ -30,11 +25,9 @@ void CharacterFrameBuffer::clear() {
// taken from https://stackoverflow.com/a/74186686
char brightness_chars[] = " `.-':_,^=;><+!rc*/z?sLTv)J7(|Fi{C}fI31tlu[neoZ5Yxjya]2ESwqkP6h9d4VpOGbUAKXHm8RD#$Bg0MNWQ%&@";
-extern Camera* camera;
-void CharacterFrameBuffer::draw_point(int x, int y, const Vector3& /* loc */, const Vector3& normal, const Vector2& /* uv */, const VertexData& /* vd */) {
- auto v = camera->transform.rot.conjugate().rot(normal).normalize();
- float light = .1f + (v.z < 0.f ? 0.f : v.z) * .9f;
+void CharacterFrameBuffer::draw_point(int x, int y, const Vector4& point) {
+ float light = .2126f * point.x + .7152f * point.y + .0722f * point.z;
std::uint32_t c = (int) (light * static_cast<float>(sizeof(brightness_chars) - 1));
chars_vector[x + y * w] = brightness_chars[c];
}
diff --git a/src/fb/chfb.hpp b/src/fb/chfb.hpp
index 1748abf..b076f7d 100644
--- a/src/fb/chfb.hpp
+++ b/src/fb/chfb.hpp
@@ -3,7 +3,6 @@
#include <vector>
#include "math/vector.hpp"
-#include "o3d/vertex_data.hpp"
namespace engine::fb {
@@ -12,8 +11,7 @@ class CharacterFrameBuffer {
CharacterFrameBuffer(unsigned int w, unsigned int h);
void resize(unsigned int w, unsigned int h);
void clear();
- void draw_point(int x, int y, const engine::math::Vector3& loc,
- const engine::math::Vector3& normal, const engine::math::Vector2& uv, const engine::o3d::VertexData& vd);
+ void draw_point(int x, int y, const engine::math::Vector4& point);
constexpr unsigned int width() const & {
return w;
diff --git a/src/fb/fb.hpp b/src/fb/fb.hpp
new file mode 100644
index 0000000..25b3a51
--- /dev/null
+++ b/src/fb/fb.hpp
@@ -0,0 +1,21 @@
+#ifndef FB_FB_HPP
+#define FB_FB_HPP
+
+#include <concepts>
+#include "math/vector.hpp"
+
+namespace engine::fb {
+
+template<typename T>
+concept FrameBufferConcept =
+ requires (T fb, int x, int y, unsigned w, unsigned h, engine::math::Vector4 point) {
+ { fb.resize(w, h) } -> std::same_as<void >;
+ { fb.clear() } -> std::same_as<void >;
+ { fb.draw_point(x, y, point) } -> std::same_as<void >;
+ { fb.width() } -> std::same_as<unsigned>;
+ { fb.height() } -> std::same_as<unsigned>;
+ };
+
+}
+
+#endif // FB_FB_HPP
diff --git a/src/fb/pixfb.cpp b/src/fb/pixfb.cpp
index c629adb..c86859b 100644
--- a/src/fb/pixfb.cpp
+++ b/src/fb/pixfb.cpp
@@ -2,16 +2,9 @@
#include <cstdint>
#include <algorithm>
#include "math/vector.hpp"
-#include "math/quat.hpp"
-#include "o3d/vertex_data.hpp"
-#include "o3d/camera.hpp"
using namespace engine::fb;
-using
- engine::math::Vector2,
- engine::math::Vector3,
- engine::o3d::VertexData,
- engine::o3d::Camera;
+using engine::math::Vector4;
PixelFrameBuffer::PixelFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
@@ -28,26 +21,10 @@ void PixelFrameBuffer::clear() {
std::fill(pixels_vector.begin(), pixels_vector.end(), 0xff000000);
}
-extern Camera* camera;
-
-void PixelFrameBuffer::draw_point(int x, int y, const Vector3& /* loc */, const Vector3& normal, const Vector2& /* uv */, const VertexData& vd) {
- auto u = vd.world_loc - camera->transform.loc;
- float u_len_sq = u.length_squared();
- u = u.normalize();
- float attenuation = camera->transform.rot.rot({ 0.f, 0.f, -1.f }).dot(u);
- if (attenuation < .7f) attenuation = 0.f;
- else if (attenuation > .8f) attenuation = 1.f;
- else attenuation = (attenuation - .7f) / .1f;
- float light = -normal.normalize().dot(u) / u_len_sq;
- if (light < 0.f) light = 0.f;
- float final_light = .003f + light * attenuation * .8f * .997f;
- if (final_light > 1.f) final_light = 1.f;
- // gamma correction
- // TODO: improve, we shouldn't do it here
- if (final_light <= .0031308f)
- final_light = final_light * 12.92f;
- else
- final_light = 1.055f * pow(final_light, 1.f / 2.4f) - .055f;
- std::uint32_t c = (int) (final_light * 255.f);
- pixels_vector[x + y * w] = 0xff000000 | c << 16 | c << 8 | c;
+void PixelFrameBuffer::draw_point(int x, int y, const Vector4& point) {
+ pixels_vector[x + y * w] =
+ std::clamp(static_cast<std::uint32_t>(point.w * 256), static_cast<std::uint32_t>(0), static_cast<std::uint32_t>(255)) << (3 * 8)
+ | std::clamp(static_cast<std::uint32_t>(point.x * 256), static_cast<std::uint32_t>(0), static_cast<std::uint32_t>(255)) << (2 * 8)
+ | std::clamp(static_cast<std::uint32_t>(point.y * 256), static_cast<std::uint32_t>(0), static_cast<std::uint32_t>(255)) << (1 * 8)
+ | std::clamp(static_cast<std::uint32_t>(point.z * 256), static_cast<std::uint32_t>(0), static_cast<std::uint32_t>(255)) << (0 * 8);
}
diff --git a/src/fb/pixfb.hpp b/src/fb/pixfb.hpp
index 954f6e1..d341b10 100644
--- a/src/fb/pixfb.hpp
+++ b/src/fb/pixfb.hpp
@@ -4,7 +4,6 @@
#include <vector>
#include <cstdint>
#include "math/vector.hpp"
-#include "o3d/vertex_data.hpp"
namespace engine::fb {
@@ -13,8 +12,7 @@ class PixelFrameBuffer {
PixelFrameBuffer(unsigned int w, unsigned int h);
void resize(unsigned int w, unsigned int h);
void clear();
- void draw_point(int x, int y, const engine::math::Vector3& loc,
- const engine::math::Vector3& normal, const engine::math::Vector2& uv, const engine::o3d::VertexData& vd);
+ void draw_point(int x, int y, const engine::math::Vector4& point);
constexpr unsigned int width() const & {
return w;