diff options
author | vimene <vincent.menegaux@gmail.com> | 2024-12-31 03:40:14 +0100 |
---|---|---|
committer | vimene <vincent.menegaux@gmail.com> | 2024-12-31 03:40:14 +0100 |
commit | cc6fb8c33637566a7b116d46440e6063f016deea (patch) | |
tree | 5cc83f84525507994add57df7dd974e6611d675a /src/fb | |
parent | 6b765a85cf81bf4b7162e4c9280dd4054581c611 (diff) | |
download | engine-cc6fb8c33637566a7b116d46440e6063f016deea.tar.gz |
various improvements
- added quaternions and rewrote all rotations to use them
- added transforms to put all object transforms in a single place
- added Wavefront .obj file parser
- removed frame buffer's abstract class
- improved vectors, matrices, triangles, vertices and vertices data by putting all code in header file
- added vector's operations
- changed from NULL to nullptr
- miscellaneous improvements
Diffstat (limited to 'src/fb')
-rw-r--r-- | src/fb/chfb.cpp | 25 | ||||
-rw-r--r-- | src/fb/chfb.h | 26 | ||||
-rw-r--r-- | src/fb/fb.h | 20 | ||||
-rw-r--r-- | src/fb/pixfb.cpp | 34 | ||||
-rw-r--r-- | src/fb/pixfb.h | 26 |
5 files changed, 55 insertions, 76 deletions
diff --git a/src/fb/chfb.cpp b/src/fb/chfb.cpp index 029d619..cfd7635 100644 --- a/src/fb/chfb.cpp +++ b/src/fb/chfb.cpp @@ -4,6 +4,7 @@ #include "o3d/vertex_data.h" using namespace engine::fb; +using engine::math::Vector3, engine::o3d::VertexData; CharacterFrameBuffer::CharacterFrameBuffer(unsigned int w, unsigned int h) { resize(w, h); @@ -16,29 +17,15 @@ void CharacterFrameBuffer::resize(unsigned int w, unsigned int h) { clear(); } -unsigned int CharacterFrameBuffer::width() const { - return w; -} - -unsigned int CharacterFrameBuffer::height() const { - return h; -} - -const char* CharacterFrameBuffer::chars() const { - return chars_vector.data(); -} - void CharacterFrameBuffer::clear() { std::fill(chars_vector.begin(), chars_vector.end(), ' '); } -void CharacterFrameBuffer::draw_point(int x, int y, engine::math::Vector3 loc, const engine::o3d::VertexData& vd) { +void CharacterFrameBuffer::draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const engine::math::Vector3& normal) { + (void) x; + (void) y; (void) loc; (void) vd; - chars_vector[x + y * w] = face_char(0); -} - -char CharacterFrameBuffer::face_char(int face_ind) const { - int n = 1 + face_ind / 2; - return (n < 10 ? '0' : 'A' - 10) + n; + (void) normal; + chars_vector[x + y * w] = 'A'; } diff --git a/src/fb/chfb.h b/src/fb/chfb.h index 280f6d8..7c786fc 100644 --- a/src/fb/chfb.h +++ b/src/fb/chfb.h @@ -2,25 +2,35 @@ #define FB_CHFB_H #include <vector> -#include "fb/fb.h" +#include "math/vector.h" +#include "o3d/vertex_data.h" namespace engine::fb { -class CharacterFrameBuffer : public FrameBuffer { +using engine::math::Vector3, engine::o3d::VertexData; + +class CharacterFrameBuffer { public: CharacterFrameBuffer(unsigned int w, unsigned int h); void resize(unsigned int w, unsigned int h); - unsigned int width() const; - unsigned int height() const; - const char* chars() const; void clear(); - void draw_point(int x, int y, engine::math::Vector3 loc, const engine::o3d::VertexData& vd); + void draw_point(int x, int y, const engine::math::Vector3& loc, const engine::o3d::VertexData& vd, const engine::math::Vector3& normal); + + constexpr unsigned int width() const & { + return w; + } + + constexpr unsigned int height() const & { + return h; + } + + constexpr const char* chars() const & { + return chars_vector.data(); + } private: unsigned int w, h; std::vector<char> chars_vector; - - char face_char(int face_inf) const; }; } diff --git a/src/fb/fb.h b/src/fb/fb.h deleted file mode 100644 index 04b13ee..0000000 --- a/src/fb/fb.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef FB_FB_H -#define FB_FB_H - -#include "math/vector.h" -#include "o3d/vertex_data.h" - -namespace engine::fb { - -class FrameBuffer { - public: - virtual void resize(unsigned int w, unsigned int h) = 0; - virtual unsigned int width() const = 0; - virtual unsigned int height() const = 0; - virtual void clear() = 0; - virtual void draw_point(int x, int y, engine::math::Vector3 loc, const engine::o3d::VertexData& vd) = 0; -}; - -} - -#endif // FB_FB_H diff --git a/src/fb/pixfb.cpp b/src/fb/pixfb.cpp index 10f2d02..a8d6bee 100644 --- a/src/fb/pixfb.cpp +++ b/src/fb/pixfb.cpp @@ -2,9 +2,11 @@ #include <cstdint> #include <algorithm> #include "math/vector.h" +#include "math/quat.h" #include "o3d/vertex_data.h" using namespace engine::fb; +using engine::math::Vector3, engine::o3d::VertexData; PixelFrameBuffer::PixelFrameBuffer(unsigned int w, unsigned int h) { resize(w, h); @@ -17,30 +19,20 @@ void PixelFrameBuffer::resize(unsigned int w, unsigned int h) { clear(); } -unsigned int PixelFrameBuffer::width() const { - return w; -} - -unsigned int PixelFrameBuffer::height() const { - return h; -} - -const uint32_t* PixelFrameBuffer::pixels() const { - return pixels_vector.data(); -} - void PixelFrameBuffer::clear() { std::fill(pixels_vector.begin(), pixels_vector.end(), 0x000000FF); } -void PixelFrameBuffer::draw_point(int x, int y, engine::math::Vector3 loc, const engine::o3d::VertexData& vd) { - (void) loc; - 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; -} +extern engine::math::Quaternion camera_quat; -uint32_t PixelFrameBuffer::face_color(int face_ind) const { - int n = face_ind / 2; - return ((n % 2 ? 0xFF000000 : 0x55000000) >> ((n % 6) / 2 * 8)) | 0xFF; +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); + pixels_vector[x + y * w] = c << 24 | c << 16 | c << 8 | 0xff; } diff --git a/src/fb/pixfb.h b/src/fb/pixfb.h index 84bce2c..81a7ac3 100644 --- a/src/fb/pixfb.h +++ b/src/fb/pixfb.h @@ -3,26 +3,36 @@ #include <vector> #include <cstdint> -#include "fb/fb.h" +#include "math/vector.h" +#include "o3d/vertex_data.h" namespace engine::fb { -class PixelFrameBuffer : public FrameBuffer { +using engine::math::Vector3, engine::o3d::VertexData; + +class PixelFrameBuffer { public: PixelFrameBuffer(unsigned int w, unsigned int h); void resize(unsigned int w, unsigned int h); - unsigned int width() const; - unsigned int height() const; - const uint32_t* pixels() const; void clear(); - void draw_point(int x, int y, engine::math::Vector3 loc, const engine::o3d::VertexData& vd); + void draw_point(int x, int y, const Vector3& loc, const VertexData& vd, const Vector3& normal); + + constexpr unsigned int width() const & { + return w; + } + + constexpr unsigned int height() const & { + return h; + } + + constexpr const uint32_t* pixels() const & { + return pixels_vector.data(); + } private: unsigned int w, h; std::vector<uint32_t> pixels_vector; int face_ind; - - uint32_t face_color(int face_ind) const; }; } |