Projection matrix fix
This commit is contained in:
parent
3d152d77f8
commit
280abd4a38
programs/lab5/src
utilities/include/bgtu/computer_graphics_lab_work/utilities
@ -64,10 +64,19 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
|
||||
|
||||
return 0;
|
||||
#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;
|
||||
|
@ -56,9 +56,11 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
|
||||
SpriteData get_sprite_data() override {
|
||||
this->sync.lock();
|
||||
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{
|
||||
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();
|
||||
return cached;
|
||||
|
@ -35,14 +35,21 @@ namespace BGTU::ComputerGraphicsLabWork::Impl {
|
||||
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 {
|
||||
auto ta = data->transform * this->triangle_p0;
|
||||
auto tb = data->transform * this->triangle_p1;
|
||||
auto tc = data->transform * this->triangle_p2;
|
||||
auto ta4 = data->transform * Utilities::Vec4{this->triangle_p0.x, this->triangle_p0.y, -this->triangle_p0.z, 1.0};
|
||||
auto tb4 = data->transform * Utilities::Vec4{this->triangle_p1.x, this->triangle_p1.y, -this->triangle_p1.z, 1.0};
|
||||
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{
|
||||
{ta.x, ta.y}, {this->shader_p0.x, this->shader_p0.y, ta.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},
|
||||
{ta.x, ta.y},
|
||||
{this->shader_p0.x, this->shader_p0.y, ta.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(
|
||||
|
@ -159,6 +159,27 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
|
||||
using Matrix3f = Matrix3<float>;
|
||||
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>
|
||||
class Matrix4 {
|
||||
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) {
|
||||
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, 0, static_cast<elem_t>(2.0 / (f - n)), static_cast<elem_t>(-(f + n) * (f - n)),
|
||||
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, 0, static_cast<elem_t>(2.0 / (f - n)), static_cast<elem_t>(-(f + n) / (f - n)),
|
||||
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) {
|
||||
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, 0, static_cast<elem_t>(-(f + n) * (f - n)), static_cast<elem_t>(-2.0 * n * f * (f - n)),
|
||||
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, 0, static_cast<elem_t>(-(f + n) / (f - n)), static_cast<elem_t>(-2.0 * n * f / (f - n)),
|
||||
0, 0, -1, 0};
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user