aboutsummaryrefslogtreecommitdiff
path: root/src/fb
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2025-01-02 13:25:14 +0100
committervimene <vincent.menegaux@gmail.com>2025-01-02 13:25:14 +0100
commit26b4b82a7be2c029491f3009b66b3cbf8db5d93c (patch)
treeddc247a0600b5933185677212f158ebea5a87530 /src/fb
parent9fdb5881d46f5d80626f961f9c9f133cc25dab70 (diff)
downloadengine-26b4b82a7be2c029491f3009b66b3cbf8db5d93c.tar.gz
various improvements
- cleaned up the computation of the camera's matrix - changed VertexData to being a struct which transmit data between the "vertex shader" and the "fragment shader" - started working on keyboard and mouse controls - added fov (field of view) - changed quaternion to euler angles conversion, from zyx to zxy - fixed computations of z coordinates in triangle rendering - improved naming in the triangle rasterizer
Diffstat (limited to 'src/fb')
-rw-r--r--src/fb/chfb.cpp10
-rw-r--r--src/fb/pixfb.cpp26
2 files changed, 25 insertions, 11 deletions
diff --git a/src/fb/chfb.cpp b/src/fb/chfb.cpp
index 71259aa..8068d8f 100644
--- a/src/fb/chfb.cpp
+++ b/src/fb/chfb.cpp
@@ -4,9 +4,13 @@
#include "math/vector.h"
#include "math/quat.h"
#include "o3d/vertex_data.h"
+#include "o3d/camera.h"
using namespace engine::fb;
-using engine::math::Vector3, engine::math::Quaternion, engine::o3d::VertexData;
+using
+ engine::math::Vector3,
+ engine::o3d::VertexData,
+ engine::o3d::Camera;
CharacterFrameBuffer::CharacterFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
@@ -25,12 +29,12 @@ void CharacterFrameBuffer::clear() {
// taken from https://stackoverflow.com/a/74186686
char brightness_chars[] = " `.-':_,^=;><+!rc*/z?sLTv)J7(|Fi{C}fI31tlu[neoZ5Yxjya]2ESwqkP6h9d4VpOGbUAKXHm8RD#$Bg0MNWQ%&@";
-extern Quaternion camera_quat;
+extern Camera* camera;
void CharacterFrameBuffer::draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal) {
(void) loc;
(void) vd;
- auto v = normal.rot(camera_quat.conjugate());
+ auto v = normal.rot(camera->transform.rot.conjugate());
float light = .1f + (v.z < 0.f ? 0.f : v.z) * .9f;
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/pixfb.cpp b/src/fb/pixfb.cpp
index a8d6bee..d49566e 100644
--- a/src/fb/pixfb.cpp
+++ b/src/fb/pixfb.cpp
@@ -4,9 +4,13 @@
#include "math/vector.h"
#include "math/quat.h"
#include "o3d/vertex_data.h"
+#include "o3d/camera.h"
using namespace engine::fb;
-using engine::math::Vector3, engine::o3d::VertexData;
+using
+ engine::math::Vector3,
+ engine::o3d::VertexData,
+ engine::o3d::Camera;
PixelFrameBuffer::PixelFrameBuffer(unsigned int w, unsigned int h) {
resize(w, h);
@@ -23,16 +27,22 @@ void PixelFrameBuffer::clear() {
std::fill(pixels_vector.begin(), pixels_vector.end(), 0x000000FF);
}
-extern engine::math::Quaternion camera_quat;
+extern Camera* camera;
void PixelFrameBuffer::draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal) {
(void) loc;
(void) vd;
- // int ir = ((int) (((float) ((int) (vd.tx * 10.f))) * 25.5f));
- // int ig = ((int) (((float) ((int) (vd.ty * 10.f))) * 25.5f));
- // pixels_vector[x + y * w] = (ir << 24) | (ig << 16) | 0xFFFF;
- auto v = normal.rot(camera_quat.conjugate());
- float light = .1f + (v.z < 0.f ? 0.f : v.z) * .9f;
- std::uint32_t c = (int) (light * 255.f);
+ auto u = vd.world_loc - camera->transform.loc;
+ float u_len_sq = u.length_squared();
+ u = u.normalize();
+ float attenuation = Vector3(0.f, 0.f, -1.f).rot(camera->transform.rot).dot(u);
+ if (attenuation < .7f) attenuation = 0.f;
+ else if (attenuation > .8f) attenuation = 1.f;
+ else attenuation = (attenuation - .7f) / .1f;
+ float light = -normal.dot(u) / u_len_sq;
+ if (light < 0.f) light = 0.f;
+ float final_light = .05f + light * attenuation * .8f * .95f;
+ if (final_light > 1.f) final_light = 1.f;
+ std::uint32_t c = (int) (final_light * 255.f);
pixels_vector[x + y * w] = c << 24 | c << 16 | c << 8 | 0xff;
}