Projection matrix fix

This commit is contained in:
Andrew Golovashevich 2025-03-20 17:49:04 +03:00
parent 3d152d77f8
commit 280abd4a38
4 changed files with 73 additions and 17 deletions

View File

@ -64,10 +64,19 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
return 0; return 0;
#else #else
auto x = Utilities::Matrix4d::projection_orto(-2, 2, -1, 1, 0, 2); auto x = Utilities::Matrix4d::projection_central(-1, 1, -1, 1, 1, 2);
Utilities::Vec4<double> a[]{
{1, 1, -1, 1},
{1, 1, -2, 1},
{2, 2, -2, 1},
};
Utilities::Vec4<double> b[]{
x * a[0],
x * a[1],
x * a[2],
};
RendererApi::PointF<3> a{1, 1, 6};
RendererApi::PointF<3> b = x * a;
return 0; return 0;

View File

@ -56,9 +56,11 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
SpriteData get_sprite_data() override { SpriteData get_sprite_data() override {
this->sync.lock(); this->sync.lock();
double radius = (((this->w < this->h) ? this->w : this->h) * 7 / 16.0); double radius = (((this->w < this->h) ? this->w : this->h) * 7 / 16.0);
auto projection = this->is_projection_enabled ? Utilities::Matrix4d::projection_central(-2, 2, -2, 2, this->scale - 2, this->scale + 2) * Utilities::Matrix4d::shift(0, 0, this->scale) : Utilities::Matrix4d::projection_orto(-2, 2, -2, 2, -2, 2); auto projection = this->is_projection_enabled
? Utilities::Matrix4d::projection_central(-2, 2, -2, 2, 1, 9) * Utilities::Matrix4d::scale(1, 1, -1) * Utilities::Matrix4d::shift(0, 0, 7)
: Utilities::Matrix4d::projection_orto(-2, 2, -2, 2, -6, 2);
SpriteData cached{ SpriteData cached{
Utilities::Matrix4d::shift(this->w / 2, this->h / 2, 2) * Utilities::Matrix4d::scale(radius) * projection * Utilities::Matrix4d::rotate_x(this->x_angle) * Utilities::Matrix4d::rotate_y(this->y_angle), Utilities::Matrix4d::shift(this->w / 2, this->h / 2, 0) * Utilities::Matrix4d::scale(radius) * projection * Utilities::Matrix4d::rotate_x(this->x_angle) * Utilities::Matrix4d::rotate_y(this->y_angle),
}; };
this->sync.unlock(); this->sync.unlock();
return cached; return cached;

View File

@ -35,14 +35,21 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
shader_p0{shader_p0}, shader_p1{shader_p1}, shader_p2{shader_p2}, shader{shader} {} shader_p0{shader_p0}, shader_p1{shader_p1}, shader_p2{shader_p2}, shader{shader} {}
void draw(BGTU::ComputerGraphicsLabWork::RendererApi::VirtualVoxelPainter *frame, SpriteData const *data) const override { void draw(BGTU::ComputerGraphicsLabWork::RendererApi::VirtualVoxelPainter *frame, SpriteData const *data) const override {
auto ta = data->transform * this->triangle_p0; auto ta4 = data->transform * Utilities::Vec4{this->triangle_p0.x, this->triangle_p0.y, -this->triangle_p0.z, 1.0};
auto tb = data->transform * this->triangle_p1; auto tb4 = data->transform * Utilities::Vec4{this->triangle_p1.x, this->triangle_p1.y, -this->triangle_p1.z, 1.0};
auto tc = data->transform * this->triangle_p2; auto tc4 = data->transform * Utilities::Vec4{this->triangle_p2.x, this->triangle_p2.y, -this->triangle_p2.z, 1.0};
RendererApi::PointF<3> ta{ta4[0] / ta4[3], ta4[1] / ta4[3], ta4[2] / ta4[3]};
RendererApi::PointF<3> tb{tb4[0] / tb4[3], tb4[1] / tb4[3], tb4[2] / tb4[3]};
RendererApi::PointF<3> tc{tc4[0] / tc4[3], tc4[1] / tc4[3], tc4[2] / tc4[3]};
Utilities::Shapes::triangle_barycentric_interpolator<RendererApi::PointF<3>> interpolator{ Utilities::Shapes::triangle_barycentric_interpolator<RendererApi::PointF<3>> interpolator{
{ta.x, ta.y}, {this->shader_p0.x, this->shader_p0.y, ta.z}, {ta.x, ta.y},
{tb.x, tb.y}, {this->shader_p1.x, this->shader_p1.y, tb.z}, {this->shader_p0.x, this->shader_p0.y, ta.z},
{tc.x, tc.y}, {this->shader_p2.x, this->shader_p2.y, tc.z}, {tb.x, tb.y},
{this->shader_p1.x, this->shader_p1.y, tb.z},
{tc.x, tc.y},
{this->shader_p2.x, this->shader_p2.y, tc.z},
}; };
Utilities::Shapes::iterate_triangle_fill( Utilities::Shapes::iterate_triangle_fill(

View File

@ -159,6 +159,27 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
using Matrix3f = Matrix3<float>; using Matrix3f = Matrix3<float>;
using Matrix3d = Matrix3<double>; using Matrix3d = Matrix3<double>;
template<class elem_t>
class Matrix4;
template<class elem_t>
class Vec4 {
private:
elem_t data[4];
template<class>
friend
class Matrix4;
public:
Vec4(elem_t a, elem_t b, elem_t c, elem_t d) : data{a, b, c, d} {}
elem_t operator[](std::size_t i) const {
return this->data[i];
};
};
template<class elem_t> template<class elem_t>
class Matrix4 { class Matrix4 {
private: private:
@ -309,18 +330,35 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
}; };
} }
Vec4<elem_t> operator*(Vec4<elem_t> other) const {
return Vec4<elem_t>{
this->data[0][0] * other.data[0] + this->data[0][1] * other.data[1] + this->data[0][2] * other.data[2] + this->data[0][3] * other.data[3],
this->data[1][0] * other.data[0] + this->data[1][1] * other.data[1] + this->data[1][2] * other.data[2] + this->data[1][3] * other.data[3],
this->data[2][0] * other.data[0] + this->data[2][1] * other.data[1] + this->data[2][2] * other.data[2] + this->data[2][3] * other.data[3],
this->data[3][0] * other.data[0] + this->data[3][1] * other.data[1] + this->data[3][2] * other.data[2] + this->data[3][3] * other.data[3]
};
}
Matrix4<elem_t> transposed() const {
return Matrix4<elem_t>{
this->data[0][0], this->data[1][0], this->data[2][0], this->data[3][0],
this->data[0][1], this->data[1][1], this->data[2][1], this->data[3][1],
this->data[0][2], this->data[1][2], this->data[2][2], this->data[3][2],
this->data[0][3], this->data[1][3], this->data[2][3], this->data[3][3],
};
}
static Matrix4<elem_t> projection_orto(elem_t l, elem_t r, elem_t b, elem_t t, elem_t n, elem_t f) { static Matrix4<elem_t> projection_orto(elem_t l, elem_t r, elem_t b, elem_t t, elem_t n, elem_t f) {
return Matrix4<elem_t>{static_cast<elem_t>(2.0 / (r - l)), 0, 0, static_cast<elem_t>(-(r + l) * (r - l)), return Matrix4<elem_t>{static_cast<elem_t>(2.0 / (r - l)), 0, 0, static_cast<elem_t>(-(r + l) / (r - l)),
0, static_cast<elem_t>(2.0 / (t - b)), 0, static_cast<elem_t>(-(t + b) * (t - b)), 0, static_cast<elem_t>(2.0 / (t - b)), 0, static_cast<elem_t>(-(t + b) / (t - b)),
0, 0, static_cast<elem_t>(2.0 / (f - n)), static_cast<elem_t>(-(f + n) * (f - n)), 0, 0, static_cast<elem_t>(2.0 / (f - n)), static_cast<elem_t>(-(f + n) / (f - n)),
0, 0, 0, 1}; 0, 0, 0, 1};
} }
static Matrix4<elem_t> projection_central(elem_t l, elem_t r, elem_t b, elem_t t, elem_t n, elem_t f) { static Matrix4<elem_t> projection_central(elem_t l, elem_t r, elem_t b, elem_t t, elem_t n, elem_t f) {
return Matrix4<elem_t>{static_cast<elem_t>(2.0 * n / (r - l)), 0, static_cast<elem_t>((r + l) * (r - l)), 0, return Matrix4<elem_t>{static_cast<elem_t>(2.0 * n / (r - l)), 0, static_cast<elem_t>((r + l) / (r - l)), 0,
0, static_cast<elem_t>(2.0 * n / (t - b)), static_cast<elem_t>((t + b) * (t - b)), 0, 0, static_cast<elem_t>(2.0 * n / (t - b)), static_cast<elem_t>((t + b) / (t - b)), 0,
0, 0, static_cast<elem_t>(-(f + n) * (f - n)), static_cast<elem_t>(-2.0 * n * f * (f - n)), 0, 0, static_cast<elem_t>(-(f + n) / (f - n)), static_cast<elem_t>(-2.0 * n * f / (f - n)),
0, 0, -1, 0}; 0, 0, -1, 0};
} }
}; };