code refactoring

master
Vectozavr 2021-10-31 16:01:31 +07:00
parent d2e34d6764
commit 59e8887eaf
3 changed files with 28 additions and 25 deletions

View File

@ -58,10 +58,10 @@ std::vector<std::shared_ptr<Triangle>> Camera::project(std::shared_ptr<Mesh> mes
for (auto &clipped : clippedTriangles) { for (auto &clipped : clippedTriangles) {
sf::Color color = clipped.color(); sf::Color color = clipped.color();
sf::Color ambientColor = sf::Color((sf::Uint8) (color.r * (0.3 * std::abs(dot) + 0.7)), sf::Color ambientColor = sf::Color(static_cast<sf::Uint8>(color.r * (0.3 * std::abs(dot) + 0.7)),
(sf::Uint8) (color.g * (0.3 * std::abs(dot) + 0.7)), static_cast<sf::Uint8>(color.g * (0.3 * std::abs(dot) + 0.7)),
(sf::Uint8) (color.b * (0.3 * std::abs(dot) + 0.7)), static_cast<sf::Uint8>(color.b * (0.3 * std::abs(dot) + 0.7)),
(sf::Uint8) color.a); static_cast<sf::Uint8>(color.a));
// Finally its time to project our clipped colored drawTriangle from 3D -> 2D // Finally its time to project our clipped colored drawTriangle from 3D -> 2D
// and transform it's coordinate to screen space (in pixels): // and transform it's coordinate to screen space (in pixels):

View File

@ -43,9 +43,9 @@ private:
/* /*
* Take into account when you rotate body, * Take into account when you rotate body,
* you change '_angle' only for this particular body, * you change '_angle' & '_angleLeftUpLookAt' only for this particular body,
* but not for attached objects! This way during rotation * but not for attached objects! This way during rotation
* '_angle' stays the same for all attached objects. * '_angle' & '_angleLeftUpLookAt' stays constant all attached objects.
*/ */
Vec3D _angle{0, 0, 0}; Vec3D _angle{0, 0, 0};
Vec3D _angleLeftUpLookAt{0, 0, 0}; Vec3D _angleLeftUpLookAt{0, 0, 0};

View File

@ -51,9 +51,12 @@ void Screen::clear() {
void Screen::drawTriangle(const Triangle &triangle) { void Screen::drawTriangle(const Triangle &triangle) {
sf::Vertex tris[3] = sf::Vertex tris[3] =
{ {
sf::Vertex(sf::Vector2f((float) triangle[0].x(), (float) triangle[0].y()), triangle.color()), sf::Vertex(sf::Vector2f(static_cast<float>(triangle[0].x()), static_cast<float>(triangle[0].y())),
sf::Vertex(sf::Vector2f((float) triangle[1].x(), (float) triangle[1].y()), triangle.color()), triangle.color()),
sf::Vertex(sf::Vector2f((float) triangle[2].x(), (float) triangle[2].y()), triangle.color()) sf::Vertex(sf::Vector2f(static_cast<float>(triangle[1].x()), static_cast<float>(triangle[1].y())),
triangle.color()),
sf::Vertex(sf::Vector2f(static_cast<float>(triangle[2].x()), static_cast<float>(triangle[2].y())),
triangle.color())
}; };
_window->pushGLStates(); _window->pushGLStates();
@ -80,10 +83,10 @@ void Screen::setMouseCursorVisible(bool visible) {
void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color) { void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color) {
sf::ConvexShape polygon; sf::ConvexShape polygon;
polygon.setPointCount(4); polygon.setPointCount(4);
polygon.setPoint(0, sf::Vector2f((float) p1.x(), (float) p1.y())); polygon.setPoint(0, sf::Vector2f(static_cast<float>(p1.x()), static_cast<float>(p1.y())));
polygon.setPoint(1, sf::Vector2f((float) p2.x(), (float) p2.y())); polygon.setPoint(1, sf::Vector2f(static_cast<float>(p2.x()), static_cast<float>(p2.y())));
polygon.setPoint(2, sf::Vector2f((float) p3.x(), (float) p3.y())); polygon.setPoint(2, sf::Vector2f(static_cast<float>(p3.x()), static_cast<float>(p3.y())));
polygon.setPoint(3, sf::Vector2f((float) p4.x(), (float) p4.y())); polygon.setPoint(3, sf::Vector2f(static_cast<float>(p4.x()), static_cast<float>(p4.y())));
polygon.setFillColor(color); polygon.setFillColor(color);
_window->pushGLStates(); _window->pushGLStates();
@ -99,7 +102,7 @@ void Screen::drawText(const std::string &string, const Vec2D &position, int size
text.setCharacterSize(size); text.setCharacterSize(size);
text.setFillColor(color); text.setFillColor(color);
text.setStyle(sf::Text::Italic); text.setStyle(sf::Text::Italic);
text.setPosition((float) position.x(), (float) position.y()); text.setPosition(static_cast<float>(position.x()), static_cast<float>(position.y()));
text.setString(string); text.setString(string);
@ -194,19 +197,19 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &c
dot[k] = MTriangle.norm().dot((Vec3D(MTriangle[k]) - cameraPosition).normalized()); dot[k] = MTriangle.norm().dot((Vec3D(MTriangle[k]) - cameraPosition).normalized());
sf::Color color = triangles[i].color(); sf::Color color = triangles[i].color();
ambientColor[k] = sf::Color((sf::Uint8) (color.r * (0.3 * std::abs(dot[k]) + 0.7)), ambientColor[k] = sf::Color(static_cast<sf::Uint8>(color.r * (0.3 * std::abs(dot[k]) + 0.7)),
(sf::Uint8) (color.g * (0.3 * std::abs(dot[k]) + 0.7)), static_cast<sf::Uint8>(color.g * (0.3 * std::abs(dot[k]) + 0.7)),
(sf::Uint8) (color.b * (0.3 * std::abs(dot[k]) + 0.7)), static_cast<sf::Uint8>(color.b * (0.3 * std::abs(dot[k]) + 0.7)),
(sf::Uint8) color.a); static_cast<sf::Uint8>(color.a));
geometry[stride + 7 * k + 0] = (GLfloat) MTriangle[k].x(); geometry[stride + 7 * k + 0] = static_cast<GLfloat>(MTriangle[k].x());
geometry[stride + 7 * k + 1] = (GLfloat) MTriangle[k].y(); geometry[stride + 7 * k + 1] = static_cast<GLfloat>(MTriangle[k].y());
geometry[stride + 7 * k + 2] = (GLfloat) MTriangle[k].z(); geometry[stride + 7 * k + 2] = static_cast<GLfloat>(MTriangle[k].z());
geometry[stride + 7 * k + 3] = (GLfloat) ambientColor[k].r / 255.0f; geometry[stride + 7 * k + 3] = static_cast<GLfloat>(ambientColor[k].r) / 255.0f;
geometry[stride + 7 * k + 4] = (GLfloat) ambientColor[k].g / 255.0f; geometry[stride + 7 * k + 4] = static_cast<GLfloat>(ambientColor[k].g) / 255.0f;
geometry[stride + 7 * k + 5] = (GLfloat) ambientColor[k].b / 255.0f; geometry[stride + 7 * k + 5] = static_cast<GLfloat>(ambientColor[k].b) / 255.0f;
geometry[stride + 7 * k + 6] = (GLfloat) ambientColor[k].a / 255.0f; geometry[stride + 7 * k + 6] = static_cast<GLfloat>(ambientColor[k].a) / 255.0f;
} }
} }
return geometry; return geometry;