Screen refactor. To accelerate engine the lighting turned off

master
Vectozavr 2021-11-05 04:05:06 +07:00
parent 6f45c9e45b
commit 5f05a7cb23
8 changed files with 29 additions and 40 deletions

View File

@ -10,10 +10,10 @@ using namespace std;
int main() { int main() {
Shooter game; Shooter game;
game.create(1280, 720, ShooterConsts::PROJECT_NAME); game.create(1280, 720, ShooterConsts::PROJECT_NAME, false);
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, true); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false);
//game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
return 0; return 0;

View File

@ -61,7 +61,7 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name,
for (auto &it : *world) { for (auto &it : *world) {
if (it.second->isVisible()) { if (it.second->isVisible()) {
GLfloat *model = it.second->glModel(); GLfloat *model = it.second->glModel();
GLfloat *geometry = Screen::glMeshToGLfloatArray(it.second, camera->position()); GLfloat *geometry = Screen::glMeshToGLfloatArray(it.second);
screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size()); screen->glDrawMesh(geometry, view, model, 3 * it.second->triangles().size());
delete[] geometry; delete[] geometry;
delete[] model; delete[] model;

View File

@ -18,8 +18,8 @@ void Screen::open(int screenWidth, int screenHeight, const std::string &name, bo
_background = background; _background = background;
sf::ContextSettings settings; sf::ContextSettings settings;
settings.depthBits = 24; settings.depthBits = 12;
settings.antialiasingLevel = 8; settings.antialiasingLevel = 1;
_window->create(sf::VideoMode(screenWidth, screenHeight), name, style, settings); _window->create(sf::VideoMode(screenWidth, screenHeight), name, style, settings);
_window->setVerticalSyncEnabled(verticalSync); _window->setVerticalSyncEnabled(verticalSync);
@ -124,6 +124,11 @@ void Screen::drawText(const sf::Text &text) {
// OpenGL functions // OpenGL functions
void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count) { void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count) {
if (!sf::Shader::isAvailable())
{
Log::log("Shaders are not available!");
}
glEnable(GL_CULL_FACE); // enable culling face glEnable(GL_CULL_FACE); // enable culling face
glCullFace(GL_BACK); // cull faces from back glCullFace(GL_BACK); // cull faces from back
glFrontFace(GL_CCW); // vertex order (counter clock wise) glFrontFace(GL_CCW); // vertex order (counter clock wise)
@ -164,32 +169,29 @@ void Screen::glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t
glLoadIdentity(); glLoadIdentity();
glLoadMatrixf(view); glLoadMatrixf(view);
//glMultMatrixf(model); glMultMatrixf(model);
// Draw the mesh // Draw the mesh
glDrawArrays(GL_TRIANGLES, 0, count); glDrawArrays(GL_TRIANGLES, 0, count);
sf::Shader::bind(NULL);
} }
GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &cameraPosition) { GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh) {
std::vector<Triangle> const &triangles = mesh->triangles(); std::vector<Triangle> const &triangles = mesh->triangles();
auto *geometry = new GLfloat[7 * 3 * triangles.size()]; auto *geometry = new GLfloat[7 * 3 * triangles.size()];
auto model = mesh->model();
for (size_t i = 0; i < triangles.size(); i++) { for (size_t i = 0; i < triangles.size(); i++) {
int stride = 21 * i; int stride = 21 * i;
Triangle MTriangle = triangles[i] * model; Triangle triangle = triangles[i];
Vec3D norm = MTriangle.norm();
for (int k = 0; k < 3; k++) { for (int k = 0; k < 3; k++) {
float dot = 0.5;
auto& tris = MTriangle[k]; sf::Color color = triangle.color();
float dot = norm.dot((Vec3D(tris) - cameraPosition).normalized());
sf::Color color = MTriangle.color();
GLfloat ambientColor[4] = { GLfloat ambientColor[4] = {
color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, color.r * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
color.g * (0.3f * std::fabs(dot) + 0.7f) / 255.0f, color.g * (0.3f * std::fabs(dot) + 0.7f) / 255.0f,
@ -197,9 +199,9 @@ GLfloat *Screen::glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &c
color.a / 255.0f color.a / 255.0f
}; };
geometry[stride + 7 * k + 0] = static_cast<GLfloat>(tris.x()); geometry[stride + 7 * k + 0] = static_cast<GLfloat>(triangle[k].x());
geometry[stride + 7 * k + 1] = static_cast<GLfloat>(tris.y()); geometry[stride + 7 * k + 1] = static_cast<GLfloat>(triangle[k].y());
geometry[stride + 7 * k + 2] = static_cast<GLfloat>(tris.z()); geometry[stride + 7 * k + 2] = static_cast<GLfloat>(triangle[k].z());
geometry[stride + 7 * k + 3] = ambientColor[0]; geometry[stride + 7 * k + 3] = ambientColor[0];
geometry[stride + 7 * k + 4] = ambientColor[1]; geometry[stride + 7 * k + 4] = ambientColor[1];

View File

@ -62,7 +62,7 @@ public:
// OpenGL functions // OpenGL functions
void glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count); void glDrawMesh(GLfloat *geometry, GLfloat *view, GLfloat *model, size_t count);
static GLfloat *glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh, const Vec3D &cameraPosition); static GLfloat *glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh);
[[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; } [[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; }
}; };

View File

@ -5,6 +5,7 @@
#ifndef SHOOTER_TIMELINE_H #ifndef SHOOTER_TIMELINE_H
#define SHOOTER_TIMELINE_H #define SHOOTER_TIMELINE_H
#include <memory> #include <memory>
#include "Animation.h" #include "Animation.h"
class AnimationListTag final { class AnimationListTag final {

View File

@ -3,10 +3,8 @@
// //
#include <algorithm> #include <algorithm>
#include <execution>
#include "HitBox.h" #include "HitBox.h"
#include "../Consts.h"
HitBox::HitBox(const Mesh &mesh) { HitBox::HitBox(const Mesh &mesh) {
_hitBox.reserve(mesh.triangles().size() * 3); _hitBox.reserve(mesh.triangles().size() * 3);
@ -23,7 +21,7 @@ void HitBox::_addIfUnique(Vec3D &&point) {
auto check = [&point](const auto& p) { return p == point; }; auto check = [&point](const auto& p) { return p == point; };
if (std::find_if(std::execution::par, _hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend()) { if (std::find_if(_hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend()) {
_hitBox.push_back(point); _hitBox.push_back(point);
} }
} }
@ -41,7 +39,7 @@ HitBox HitBox::Box(const Mesh &mesh) {
for(const auto& t : mesh.triangles()) { for(const auto& t : mesh.triangles()) {
for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) {
Vec3D point = Vec3D(t[i]); auto point = Vec3D(t[i]);
if(point.x() > maxX) { if(point.x() > maxX) {
maxX = point.x(); maxX = point.x();
} }

View File

@ -15,6 +15,7 @@ private:
public: public:
HitBox() = default; HitBox() = default;
HitBox(const HitBox &hitBox) = default;
explicit HitBox(const Mesh &mesh); explicit HitBox(const Mesh &mesh);

View File

@ -15,6 +15,10 @@ RigidBody::RigidBody(ObjectNameTag nameTag, const std::string &filename, const V
_hitBox(*this) { _hitBox(*this) {
} }
RigidBody::RigidBody(const Mesh &mesh) : Mesh(mesh), _hitBox(mesh) {
}
Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) { Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) {
Vec3D maxPoint{0, 0, 0}; Vec3D maxPoint{0, 0, 0};
@ -22,20 +26,6 @@ Vec3D RigidBody::_findFurthestPoint(const Vec3D &direction) {
Vec3D transformedDirection = (view() * direction).normalized(); Vec3D transformedDirection = (view() * direction).normalized();
/*
for (auto &tri : triangles()) {
for (int i = 0; i < 3; i++) {
Vec3D point = Vec3D(tri[i]);
double distance = point.dot(transformedDirection);
if (distance > maxDistance) {
maxDistance = distance;
maxPoint = point;
}
}
}
*/
for(auto & it : _hitBox) { for(auto & it : _hitBox) {
double distance = it.dot(transformedDirection); double distance = it.dot(transformedDirection);
@ -360,9 +350,6 @@ void RigidBody::setAcceleration(const Vec3D &acceleration) {
_acceleration = acceleration; _acceleration = acceleration;
} }
RigidBody::RigidBody(const Mesh &mesh) : Mesh(mesh), _hitBox(mesh) {
}
void RigidBody::setSimpleHitBox(bool b) { void RigidBody::setSimpleHitBox(bool b) {
_simpleHitBox = b; _simpleHitBox = b;
if (_simpleHitBox) { if (_simpleHitBox) {