Some refactoring.

This commit is contained in:
Relintai 2023-12-20 14:33:24 +01:00
parent 9af2c28ac7
commit 6f7a64e7a4
17 changed files with 160 additions and 128 deletions

View File

@ -43,19 +43,21 @@ ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/appl
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/scene.cpp -o sfw/application/scene.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/scene.cpp -o sfw/application/scene.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/window.cpp -o sfw/application/window.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/window.cpp -o sfw/application/window.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/shader.cpp -o sfw/application/shader.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/shader.cpp -o sfw/application/shader.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/material.cpp -o sfw/application/material.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/material.cpp -o sfw/application/material.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh.cpp -o sfw/application/mesh.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh.cpp -o sfw/application/mesh.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/camera.cpp -o sfw/application/camera.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_utils.cpp -o sfw/application/mesh_utils.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_instance.cpp -o sfw/application/mesh_instance.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/texture.cpp -o sfw/application/texture.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/camera_3d.cpp -o sfw/application/camera_3d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/object_3d.cpp -o sfw/application/object_3d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_instance_3d.cpp -o sfw/application/mesh_instance_3d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/camera_2d.cpp -o sfw/application/camera_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/camera_2d.cpp -o sfw/application/camera_2d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_instance_2d.cpp -o sfw/application/mesh_instance_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_instance_2d.cpp -o sfw/application/mesh_instance_2d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/object_2d.cpp -o sfw/application/object_2d.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/object_2d.cpp -o sfw/application/object_2d.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/sprite.cpp -o sfw/application/sprite.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/sprite.cpp -o sfw/application/sprite.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/tile_map.cpp -o sfw/application/tile_map.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/tile_map.cpp -o sfw/application/tile_map.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/texture.cpp -o sfw/application/texture.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh_utils.cpp -o sfw/application/mesh_utils.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c game_scene.cpp -o game_scene.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c game_scene.cpp -o game_scene.o
ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.o ccache g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.o
@ -68,7 +70,8 @@ ccache g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/aabb.o sfw/basis.
sfw/pool_vector.o sfw/pool_allocator.o sfw/mutex.o sfw/stime.o \ sfw/pool_vector.o sfw/pool_allocator.o sfw/mutex.o sfw/stime.o \
sfw/application/application.o sfw/application/scene.o sfw/application/window.o \ sfw/application/application.o sfw/application/scene.o sfw/application/window.o \
sfw/application/shader.o sfw/application/material.o sfw/application/mesh.o \ sfw/application/shader.o sfw/application/material.o sfw/application/mesh.o \
sfw/application/camera.o sfw/application/mesh_instance.o sfw/application/object_2d.o \ sfw/application/camera_3d.o sfw/application/object_3d.o sfw/application/mesh_instance_3d.o \
sfw/application/object_2d.o \
sfw/application/sprite.o sfw/application/tile_map.o sfw/application/texture.o \ sfw/application/sprite.o sfw/application/tile_map.o sfw/application/texture.o \
sfw/application/camera_2d.o sfw/application/mesh_instance_2d.o \ sfw/application/camera_2d.o sfw/application/mesh_instance_2d.o \
sfw/application/mesh_utils.o \ sfw/application/mesh_utils.o \

View File

@ -200,11 +200,11 @@ GameScene::GameScene() {
MeshUtils::create_cone(mesh); MeshUtils::create_cone(mesh);
mesh->upload(); mesh->upload();
mi = memnew(MeshInstance()); mi = memnew(MeshInstance3D());
mi->material = color_material; mi->material = color_material;
mi->mesh = mesh; mi->mesh = mesh;
mi2 = memnew(MeshInstance()); mi2 = memnew(MeshInstance3D());
mi2->material = color_material; mi2->material = color_material;
mi2->mesh = mesh; mi2->mesh = mesh;
mi2->transform.origin.x = 1; mi2->transform.origin.x = 1;

View File

@ -4,8 +4,8 @@
#include "scene.h" #include "scene.h"
#include "tile_map.h" #include "tile_map.h"
#include "mesh_instance.h" #include "mesh_instance_3d.h"
#include "camera.h" #include "camera_3d.h"
#include "camera_2d.h" #include "camera_2d.h"
#include "color_material.h" #include "color_material.h"
#include "colored_material.h" #include "colored_material.h"
@ -37,10 +37,10 @@ public:
TileMap *tile_map; TileMap *tile_map;
Sprite *sprite; Sprite *sprite;
Camera *camera; Camera3D *camera;
Mesh *mesh; Mesh *mesh;
MeshInstance *mi; MeshInstance3D *mi;
MeshInstance *mi2; MeshInstance3D *mi2;
ColorMaterial *color_material; ColorMaterial *color_material;
//ColoredMaterial *cmaterial; //ColoredMaterial *cmaterial;

View File

@ -3,16 +3,21 @@
#include "3rd_glad.h" #include "3rd_glad.h"
#include "object_2d.h"
#include "transform.h" #include "transform.h"
#include "transform_2d.h" #include "transform_2d.h"
#include "vector2.h" #include "vector2.h"
class Camera2D { class Camera2D : Object2D {
public: public:
virtual void bind(); virtual void bind();
void make_current(); void make_current();
//void push_transform(const Transform2D &transform);
//void pop_transform();
Camera2D(); Camera2D();
virtual ~Camera2D(); virtual ~Camera2D();

View File

@ -1,18 +1,18 @@
#include "camera.h" #include "camera_3d.h"
#include "math_funcs.h" #include "math_funcs.h"
void Camera::bind() { void Camera3D::bind() {
make_current(); make_current();
model_view_matrix = Transform(); model_view_matrix = Transform();
} }
void Camera::make_current() { void Camera3D::make_current() {
current_camera = this; current_camera = this;
} }
Camera::Camera() { Camera3D::Camera3D() {
screen_aspect_ratio = 1; //p_viewport_size.width / (float)p_viewport_size.height, screen_aspect_ratio = 1; //p_viewport_size.width / (float)p_viewport_size.height,
znear = 0.05; znear = 0.05;
@ -21,13 +21,13 @@ Camera::Camera() {
vaspect = false; vaspect = false;
} }
Camera::~Camera() { Camera3D::~Camera3D() {
} }
Camera *Camera::current_camera = NULL; Camera3D *Camera3D::current_camera = NULL;
void OrthographicCamera::bind() { void OrthographicCamera::bind() {
Camera::bind(); Camera3D::bind();
projection_matrix.set_orthogonal( projection_matrix.set_orthogonal(
size, size,
screen_aspect_ratio, screen_aspect_ratio,
@ -37,13 +37,13 @@ void OrthographicCamera::bind() {
} }
OrthographicCamera::OrthographicCamera() : OrthographicCamera::OrthographicCamera() :
Camera() { Camera3D() {
} }
OrthographicCamera::~OrthographicCamera() { OrthographicCamera::~OrthographicCamera() {
} }
void PerspectiveCamera::bind() { void PerspectiveCamera::bind() {
Camera::bind(); Camera3D::bind();
projection_matrix.set_perspective( projection_matrix.set_perspective(
fov, fov,
@ -54,9 +54,20 @@ void PerspectiveCamera::bind() {
} }
PerspectiveCamera::PerspectiveCamera() : PerspectiveCamera::PerspectiveCamera() :
Camera() { Camera3D() {
fov = 70; fov = 70;
} }
PerspectiveCamera::~PerspectiveCamera() { PerspectiveCamera::~PerspectiveCamera() {
} }
void FrustumCamera::bind() {
Camera3D::bind();
}
FrustumCamera::FrustumCamera() :
Camera3D() {
}
FrustumCamera::~FrustumCamera() {
}

View File

@ -1,5 +1,5 @@
#ifndef CAMERA_H #ifndef CAMERA_3D_H
#define CAMERA_H #define CAMERA_3D_H
#include "3rd_glad.h" #include "3rd_glad.h"
@ -7,14 +7,14 @@
#include "transform.h" #include "transform.h"
#include "vector3.h" #include "vector3.h"
class Camera { class Camera3D {
public: public:
virtual void bind(); virtual void bind();
void make_current(); void make_current();
Camera(); Camera3D();
virtual ~Camera(); virtual ~Camera3D();
float size; float size;
float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height, float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height,
@ -22,14 +22,14 @@ public:
float zfar; float zfar;
bool vaspect; bool vaspect;
static Camera *current_camera; static Camera3D *current_camera;
Transform camera_transform; Transform camera_transform;
Transform model_view_matrix; Transform model_view_matrix;
Projection projection_matrix; Projection projection_matrix;
}; };
class OrthographicCamera : public Camera { class OrthographicCamera : public Camera3D {
public: public:
void bind(); void bind();
@ -37,7 +37,7 @@ public:
~OrthographicCamera(); ~OrthographicCamera();
}; };
class PerspectiveCamera : public Camera { class PerspectiveCamera : public Camera3D {
public: public:
float fov; float fov;
@ -47,6 +47,14 @@ public:
~PerspectiveCamera(); ~PerspectiveCamera();
}; };
//frustum class FrustumCamera : public Camera3D {
public:
//todo
void bind();
FrustumCamera();
~FrustumCamera();
};
#endif // CAMERA_H #endif // CAMERA_H

View File

@ -3,7 +3,7 @@
#include "material.h" #include "material.h"
#include "camera.h" #include "camera_3d.h"
class ColorMaterial : public Material { class ColorMaterial : public Material {
public: public:
@ -12,9 +12,9 @@ public:
} }
void bind_uniforms() { void bind_uniforms() {
set_uniform(projection_matrix_location, Camera::current_camera->projection_matrix); set_uniform(projection_matrix_location, Camera3D::current_camera->projection_matrix);
set_uniform(camera_matrix_location, Camera::current_camera->camera_transform); set_uniform(camera_matrix_location, Camera3D::current_camera->camera_transform);
set_uniform(model_view_matrix_location, Camera::current_camera->model_view_matrix); set_uniform(model_view_matrix_location, Camera3D::current_camera->model_view_matrix);
} }
void setup_uniforms() { void setup_uniforms() {

View File

@ -5,7 +5,7 @@
#include "color.h" #include "color.h"
#include "camera.h" #include "camera_3d.h"
class ColoredMaterial : public Material { class ColoredMaterial : public Material {
public: public:
@ -14,8 +14,8 @@ public:
} }
void bind_uniforms() { void bind_uniforms() {
//glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix)); //glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera3D::current_camera->projection_matrix));
//glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix)); //glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera3D::current_camera->model_view_matrix));
glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a);
} }

View File

@ -1,37 +0,0 @@
#include "mesh_instance.h"
#include "camera.h"
void MeshInstance::render() {
if (!mesh) {
return;
}
Transform mat_orig = Camera::current_camera->model_view_matrix;
Camera::current_camera->model_view_matrix *= transform;
if (material) {
material->bind();
}
mesh->render();
for (uint32_t i = 0; i < children.size(); ++i) {
MeshInstance * c = children[i];
if (c) {
c->render();
}
}
Camera::current_camera->model_view_matrix = mat_orig;
}
MeshInstance::MeshInstance() {
material = NULL;
mesh = NULL;
}
MeshInstance::~MeshInstance() {
children.clear();
}

View File

@ -1,26 +0,0 @@
#ifndef MESH_INSTACE_H
#define MESH_INSTACE_H
#include <vector>
#include "material.h"
#include "mesh.h"
#include "transform.h"
class MeshInstance {
public:
void render();
MeshInstance();
~MeshInstance();
Material *material;
Mesh *mesh;
Transform transform;
std::vector<MeshInstance *> children;
};
#endif // MESH_INSTACE_H

View File

@ -0,0 +1,37 @@
#include "mesh_instance_3d.h"
#include "camera_3d.h"
void MeshInstance3D::render() {
if (!mesh) {
return;
}
Transform mat_orig = Camera3D::current_camera->model_view_matrix;
Camera3D::current_camera->model_view_matrix *= transform;
if (material) {
material->bind();
}
mesh->render();
for (uint32_t i = 0; i < children.size(); ++i) {
MeshInstance3D * c = children[i];
if (c) {
c->render();
}
}
Camera3D::current_camera->model_view_matrix = mat_orig;
}
MeshInstance3D::MeshInstance3D() {
material = NULL;
mesh = NULL;
}
MeshInstance3D::~MeshInstance3D() {
children.clear();
}

View File

@ -0,0 +1,26 @@
#ifndef MESH_INSTACE_3D_H
#define MESH_INSTACE_3D_H
#include <vector>
#include "object_3d.h"
#include "material.h"
#include "mesh.h"
#include "transform.h"
class MeshInstance3D : public Object3D {
public:
void render();
MeshInstance3D();
~MeshInstance3D();
Material *material;
Mesh *mesh;
std::vector<MeshInstance3D *> children;
};
#endif // MESH_INSTACE_H

View File

@ -0,0 +1,8 @@
#include "object_3d.h"
Object3D::Object3D() {
}
Object3D::~Object3D() {
}

View File

@ -0,0 +1,15 @@
#ifndef OBJECT_3D_H
#define OBJECT_3D_H
#include "transform.h"
class Object3D {
public:
Object3D();
virtual ~Object3D();
Transform transform;
};
#endif // OBJECT_3D_h

View File

@ -1,18 +0,0 @@
#ifndef OPENGL_H
#define OPENGL_H
#if defined(__unix__) && !defined(__ANDROID__)
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#endif // __unix__
#if __ANDROID__
#define GL_GLEXT_PROTOTYPES
#include <GLES2/gl2.h>
#endif
#if defined(_WIN64) || defined(_WIN32)
#include "3rd_glad.h"
#endif // defined
#endif // OPENGL_H

View File

@ -4,7 +4,7 @@
#include "material.h" #include "material.h"
#include "texture.h" #include "texture.h"
#include "camera.h" #include "camera_3d.h"
class TextureMaterial : public Material { class TextureMaterial : public Material {
public: public:
@ -13,9 +13,9 @@ public:
} }
void bind_uniforms() { void bind_uniforms() {
set_uniform(projection_matrix_location, Camera::current_camera->projection_matrix); set_uniform(projection_matrix_location, Camera3D::current_camera->projection_matrix);
set_uniform(camera_matrix_location, Camera::current_camera->camera_transform); set_uniform(camera_matrix_location, Camera3D::current_camera->camera_transform);
set_uniform(model_view_matrix_location, Camera::current_camera->model_view_matrix); set_uniform(model_view_matrix_location, Camera3D::current_camera->model_view_matrix);
if (texture) { if (texture) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);

View File

@ -7,7 +7,7 @@
#include "./libs/glm/vec4.hpp" #include "./libs/glm/vec4.hpp"
#include "./libs/glm/gtc/type_ptr.hpp" #include "./libs/glm/gtc/type_ptr.hpp"
#include "camera.h" #include "camera_3d.h"
class TransparentTextureMaterial : public Material { class TransparentTextureMaterial : public Material {
public: public:
@ -16,8 +16,8 @@ public:
} }
void bind_uniforms() { void bind_uniforms() {
glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix)); glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera3D::current_camera->projection_matrix));
glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix)); glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera3D::current_camera->model_view_matrix));
if (texture) { if (texture) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);