Performance optimization
This commit is contained in:
@@ -42,13 +42,15 @@ class World : public DataModel {
|
||||
private:
|
||||
VertexArray playerBaseGeometry;
|
||||
Texture playerBaseTexture;
|
||||
VertexArray skyboxGeometry;
|
||||
Texture skyboxTexture;
|
||||
public:
|
||||
|
||||
std::string name;
|
||||
World() : DataModel() {}
|
||||
//TODO: Store these more elegantly of course.
|
||||
VertexArray getPlayerBaseGeometry();
|
||||
VertexArray* getPlayerBaseGeometry();
|
||||
VertexArray* getSkyboxGeometry();
|
||||
GLuint getPlayerTextureID();
|
||||
GLuint getSkyboxTextureID();
|
||||
};
|
||||
|
@@ -9,6 +9,7 @@ class Entity {
|
||||
protected:
|
||||
Entity* parent;
|
||||
std::vector<Entity*> children;
|
||||
VertexArray* geometry = nullptr;
|
||||
public:
|
||||
|
||||
std::vector<Entity*> GetChildren();
|
||||
@@ -47,7 +48,6 @@ protected:
|
||||
public:
|
||||
uint32_t ticksAlive; //At 64tps it'd take 776 days to overflow.
|
||||
|
||||
VertexArray geometry;
|
||||
VertexArray boundingBox;
|
||||
[[nodiscard]] LinearAlgebra::Vector3 GetPos() const;
|
||||
void SetPos(const LinearAlgebra::Vector3& rhs);
|
||||
|
@@ -22,4 +22,7 @@ public:
|
||||
void update(float elapsed) override;
|
||||
void pre_render() override;
|
||||
void render() override;
|
||||
Player() {
|
||||
geometry = engine->world->getPlayerBaseGeometry();
|
||||
}
|
||||
};
|
@@ -10,4 +10,7 @@ public:
|
||||
|
||||
void render() override;
|
||||
|
||||
Skybox() {
|
||||
geometry = engine->world->getSkyboxGeometry();
|
||||
}
|
||||
};
|
@@ -19,7 +19,8 @@ public:
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
void scale(GLfloat multiplier);
|
||||
void rotate(LinearAlgebra::Vector3 angle);
|
||||
void rotate(const LinearAlgebra::Vector3& angle);
|
||||
static VertexArray rotate(VertexArray* vArray, const LinearAlgebra::Vector3& angle);
|
||||
void load(const std::string& filename);
|
||||
void drawWireframe();
|
||||
void draw();
|
||||
|
@@ -16,10 +16,10 @@ int DataModel::getMobyCount() const {
|
||||
}
|
||||
|
||||
|
||||
VertexArray World::getPlayerBaseGeometry() {
|
||||
VertexArray* World::getPlayerBaseGeometry() {
|
||||
if (playerBaseGeometry.vertices.empty())
|
||||
playerBaseGeometry.load("assets/models/cube.obj");
|
||||
return playerBaseGeometry;
|
||||
return &playerBaseGeometry;
|
||||
}
|
||||
|
||||
GLuint World::getPlayerTextureID() {
|
||||
@@ -28,6 +28,12 @@ GLuint World::getPlayerTextureID() {
|
||||
return playerBaseTexture.id;
|
||||
}
|
||||
|
||||
VertexArray* World::getSkyboxGeometry() {
|
||||
if (skyboxGeometry.vertices.empty())
|
||||
skyboxGeometry.load("assets/models/sphere.obj");
|
||||
return &skyboxGeometry;
|
||||
}
|
||||
|
||||
GLuint World::getSkyboxTextureID() {
|
||||
if (skyboxTexture.id == 0)
|
||||
skyboxTexture.load("assets/textures/skybox.png");
|
||||
|
@@ -3,10 +3,8 @@
|
||||
|
||||
//This is in the local space.
|
||||
VertexArray Collision::calculateBoundingBox(VertexArray* vArray, const LinearAlgebra::Vector3& angle) {
|
||||
VertexArray input = *vArray;
|
||||
input.rotate(angle);
|
||||
VertexArray input = VertexArray::rotate(vArray, angle);
|
||||
VertexArray output;
|
||||
|
||||
float minX = std::numeric_limits<float>::max();
|
||||
float minY = std::numeric_limits<float>::max();
|
||||
float minZ = std::numeric_limits<float>::max();
|
||||
|
@@ -1,18 +1,13 @@
|
||||
#include <types/player.h>
|
||||
#include <types/collision/collision.h>
|
||||
void Player::pre_render() {
|
||||
geometry = engine->world->getPlayerBaseGeometry();
|
||||
geometry.scale(0.75);
|
||||
boundingBox = Collision::calculateBoundingBox(&geometry, angle);
|
||||
//std::cout << "Occluded: " << occluded << std::endl;
|
||||
boundingBox = Collision::calculateBoundingBox(geometry, angle);
|
||||
hVelocity = 2;
|
||||
this->angle.y += 96*engine->frameDelta;
|
||||
velAngle = angle;
|
||||
hMove(LinearAlgebra::Vector3::Direction({0,velAngle.y,0}), hVelocity);
|
||||
vMove(vVelocity);
|
||||
//this->angle.x -= 96*engine->frameDelta;
|
||||
//angle.y = 45;
|
||||
this->angle.x -= 96*engine->frameDelta;
|
||||
hMove(LinearAlgebra::Vector3::Direction({0, velAngle.y, 0}), hVelocity);
|
||||
//vMove(vVelocity);
|
||||
this->angle.x += 96*engine->frameDelta;
|
||||
}
|
||||
|
||||
void Player::render() {
|
||||
@@ -23,8 +18,8 @@ void Player::render() {
|
||||
glRotatef(angle.y,0.0f, 1.0f, 0.0f);
|
||||
glRotatef(angle.z,0.0f, 0.0f, 1.0f);
|
||||
glBindTexture(GL_TEXTURE_2D, engine->world->getPlayerTextureID());
|
||||
geometry.draw();
|
||||
glBindTexture(GL_TEXTURE_2D,0);
|
||||
geometry->draw();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glPopMatrix();
|
||||
glPushMatrix();
|
||||
|
@@ -2,10 +2,9 @@
|
||||
#include <engine/engine.h>
|
||||
#include <types/camera.h>
|
||||
void Skybox::pre_render() {
|
||||
//PLACEHOLDER.
|
||||
|
||||
if (engine->frameCount == 1) {
|
||||
geometry.load("assets/models/sphere.obj");
|
||||
geometry.scale(75);
|
||||
geometry->scale(75);
|
||||
}
|
||||
Camera* camera;
|
||||
for (auto& e : engine->world->GetChildren())
|
||||
@@ -19,7 +18,7 @@ void Skybox::render() {
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x ,position.y, position.z);
|
||||
glBindTexture(GL_TEXTURE_2D, engine->world->getSkyboxTextureID());
|
||||
geometry.draw();
|
||||
geometry->draw();
|
||||
glBindTexture(GL_TEXTURE_2D,0);
|
||||
glPopMatrix();
|
||||
}
|
@@ -113,7 +113,7 @@ void VertexArray::drawWireframe() {
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
|
||||
void VertexArray::rotate(const LinearAlgebra::Vector3& angle) {
|
||||
if (vertices.empty())
|
||||
return;
|
||||
LinearAlgebra::Vector3 angleRad = LinearAlgebra::Vector3(Math::Radians(angle.x), Math::Radians(angle.y), Math::Radians(angle.z));
|
||||
@@ -122,14 +122,14 @@ void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
|
||||
LinearAlgebra::Vector3 vert;
|
||||
float length = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
|
||||
|
||||
vert.x = -vertex.x * std::sin(angleRad.y) +
|
||||
vertex.y * std::sin(angleRad.x) * std::cos(angleRad.y) +
|
||||
vertex.z * std::cos(angleRad.x) * std::cos(angleRad.y);
|
||||
|
||||
vert.z = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
|
||||
vertex.y * (std::cos(angleRad.x) * std::sin(angleRad.z) - std::sin(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z)) +
|
||||
vertex.z * (std::sin(angleRad.x) * std::sin(angleRad.z) + std::cos(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z));
|
||||
|
||||
vert.x = -vertex.x * std::sin(angleRad.y) +
|
||||
vertex.y * std::sin(angleRad.x) * std::cos(angleRad.y) +
|
||||
vertex.z * std::cos(angleRad.x) * std::cos(angleRad.y);
|
||||
|
||||
vert.y = vertex.x * std::cos(angleRad.y) * std::sin(angleRad.z) +
|
||||
vertex.y * (std::cos(angleRad.x) * std::cos(angleRad.z) + std::sin(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z)) +
|
||||
vertex.z * (std::sin(angleRad.x) * std::cos(angleRad.z) - std::cos(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z));
|
||||
@@ -143,3 +143,33 @@ void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
|
||||
vertex.z = vert.z;
|
||||
}
|
||||
}
|
||||
|
||||
VertexArray VertexArray::rotate(VertexArray* vArray, const LinearAlgebra::Vector3& angle) {
|
||||
VertexArray output;
|
||||
output.indices = vArray->indices;
|
||||
LinearAlgebra::Vector3 angleRad = LinearAlgebra::Vector3(Math::Radians(angle.x), Math::Radians(angle.y), Math::Radians(angle.z));
|
||||
|
||||
for (auto& vertex : vArray->vertices) {
|
||||
LinearAlgebra::Vector3 vert;
|
||||
float length = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
|
||||
|
||||
vert.z = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
|
||||
vertex.y * (std::cos(angleRad.x) * std::sin(angleRad.z) - std::sin(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z)) +
|
||||
vertex.z * (std::sin(angleRad.x) * std::sin(angleRad.z) + std::cos(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z));
|
||||
|
||||
vert.x = -vertex.x * std::sin(angleRad.y) +
|
||||
vertex.y * std::sin(angleRad.x) * std::cos(angleRad.y) +
|
||||
vertex.z * std::cos(angleRad.x) * std::cos(angleRad.y);
|
||||
|
||||
vert.y = vertex.x * std::cos(angleRad.y) * std::sin(angleRad.z) +
|
||||
vertex.y * (std::cos(angleRad.x) * std::cos(angleRad.z) + std::sin(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z)) +
|
||||
vertex.z * (std::sin(angleRad.x) * std::cos(angleRad.z) - std::cos(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z));
|
||||
|
||||
vert = vert.Normalize();
|
||||
vert.x *= length;
|
||||
vert.y *= length;
|
||||
vert.z *= length;
|
||||
output.vertices.push_back({vert.x, vert.y, vert.z});
|
||||
}
|
||||
return output;
|
||||
}
|
Reference in New Issue
Block a user