Automation/src/util/render.cpp

173 lines
4.5 KiB
C++

#include "util/render.h"
void Render::rectangle(Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
auto screen_end = m_matrix * m_view_matrix * Vector3<float>(end, 1);
m_pge->DrawRect(
screen_start.x(),
screen_start.y(),
screen_end.x() - screen_start.x(),
screen_end.y() - screen_start.y(),
olc::Pixel(color.r(), color.g(), color.b())
);
}
#include <iostream>
void Render::rectangle_sz(Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
// auto screen_size = m_matrix * Vector3<float>(size, 1);
m_pge->DrawRect(
screen_start.x(),
screen_start.y(),
size.x(),
size.y(),
olc::Pixel(color.r(), color.g(), color.b())
);
}
void Render::fill_rectangle(Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
auto screen_end = m_matrix * m_view_matrix * Vector3<float>(end, 1);
m_pge->FillRect(
screen_start.x(),
screen_start.y(),
screen_end.x() - screen_start.x() + 1,
screen_end.y() - screen_start.y() + 1,
olc::Pixel(color.r(), color.g(), color.b())
);
}
void Render::fill_rectangle_sz(Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
// auto screen_size = m_matrix * Vector3<float>(size, 1);
m_pge->FillRect(
screen_start.x(),
screen_start.y(),
size.x() + 1,
size.y() + 1,
olc::Pixel(color.r(), color.g(), color.b())
);
}
void Render::draw_lines(std::vector<Vector2<float>> points, Color color) {
auto prev_cache = m_matrix * m_view_matrix * Vector3<float>(points[0], 1);
for(size_t i = 1; i < points.size(); i++) {
auto start = prev_cache;
auto end = m_matrix * m_view_matrix * Vector3<float>(points[i], 1);
m_pge->DrawLine(
start.x(),
start.y(),
end.x(),
end.y(),
olc::Pixel(color.r(), color.g(), color.b())
);
prev_cache = end;
}
}
void Render::set_camera_pos(Vector2<int> camera_pos) {
m_camera_pos = camera_pos;
m_view_matrix = translation_matrix(camera_pos);
}
Vector2<int> Render::camera_pos() {
return m_camera_pos;
}
void Render::set_matrix_mode(MatrixMode mode) {
m_matrix_mode = mode;
}
void Render::load_identity() {
switch(m_matrix_mode) {
case MODEL:
m_matrix = Matrix3x3<float>(1);
break;
case PROJECTION:
m_view_matrix = Matrix3x3<float>(1);
break;
}
}
void Render::push_matrix() {
switch(m_matrix_mode) {
case MODEL:
m_matrix_stack.push(m_matrix);
break;
case PROJECTION:
m_view_stack.push(m_view_matrix);
break;
}
}
void Render::pop_matrix() {
switch(m_matrix_mode) {
case MODEL:
m_matrix = m_matrix_stack.top();
m_matrix_stack.pop();
break;
case PROJECTION:
m_view_matrix = m_view_stack.top();
m_view_stack.pop();
break;
}
}
void Render::translate(Vector2<float> trans) {
switch(m_matrix_mode) {
case MODEL:
m_matrix = translation_matrix(trans) * m_matrix;
break;
case PROJECTION:
m_view_matrix = translation_matrix(trans) * m_view_matrix;
break;
}
}
void Render::rotate(float theta) {
switch(m_matrix_mode) {
case MODEL:
m_matrix = rotation_matrix(theta) * m_matrix;
break;
case PROJECTION:
m_view_matrix = rotation_matrix(theta) * m_view_matrix;
break;
}
}
void Render::scale(Vector2<float> scale) {
switch(m_matrix_mode) {
case MODEL:
m_matrix = scale_matrix(scale) * m_matrix;
break;
case PROJECTION:
m_view_matrix = scale_matrix(scale) * m_view_matrix;
}
}
Matrix3x3<float> Render::translation_matrix(Vector2<float> trans) {
return Matrix3x3<float>(
{1., 0., trans[0]},
{0., 1., trans[1]},
{0., 0., 1.}
);
}
Matrix3x3<float> Render::rotation_matrix(float theta) {
return Matrix3x3<float>(
{cos(theta), -sin(theta), 0},
{sin(theta), cos(theta), 0},
{0., 0., 1.}
);
}
Matrix3x3<float> Render::scale_matrix(Vector2<float> scale) {
return Matrix3x3<float>(
{scale[0], 0., 0.},
{0., scale[1], 0.},
{0., 0., 1.}
);
}