diff --git a/compile_linux.sh b/compile_linux.sh index d63d335..d50a1a4 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -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/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/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/camera.cpp -o sfw/application/camera.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/mesh_utils.cpp -o sfw/application/mesh_utils.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/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/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/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 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/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/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/camera_2d.o sfw/application/mesh_instance_2d.o \ sfw/application/mesh_utils.o \ diff --git a/game_scene.cpp b/game_scene.cpp index ded7bbe..99a2c44 100644 --- a/game_scene.cpp +++ b/game_scene.cpp @@ -200,11 +200,11 @@ GameScene::GameScene() { MeshUtils::create_cone(mesh); mesh->upload(); - mi = memnew(MeshInstance()); + mi = memnew(MeshInstance3D()); mi->material = color_material; mi->mesh = mesh; - mi2 = memnew(MeshInstance()); + mi2 = memnew(MeshInstance3D()); mi2->material = color_material; mi2->mesh = mesh; mi2->transform.origin.x = 1; diff --git a/game_scene.h b/game_scene.h index ae32fcb..d5b906e 100644 --- a/game_scene.h +++ b/game_scene.h @@ -4,8 +4,8 @@ #include "scene.h" #include "tile_map.h" -#include "mesh_instance.h" -#include "camera.h" +#include "mesh_instance_3d.h" +#include "camera_3d.h" #include "camera_2d.h" #include "color_material.h" #include "colored_material.h" @@ -37,10 +37,10 @@ public: TileMap *tile_map; Sprite *sprite; - Camera *camera; + Camera3D *camera; Mesh *mesh; - MeshInstance *mi; - MeshInstance *mi2; + MeshInstance3D *mi; + MeshInstance3D *mi2; ColorMaterial *color_material; //ColoredMaterial *cmaterial; diff --git a/sfw/application/camera_2d.h b/sfw/application/camera_2d.h index d65f64f..0f4b931 100644 --- a/sfw/application/camera_2d.h +++ b/sfw/application/camera_2d.h @@ -3,16 +3,21 @@ #include "3rd_glad.h" +#include "object_2d.h" + #include "transform.h" #include "transform_2d.h" #include "vector2.h" -class Camera2D { +class Camera2D : Object2D { public: virtual void bind(); void make_current(); + //void push_transform(const Transform2D &transform); + //void pop_transform(); + Camera2D(); virtual ~Camera2D(); diff --git a/sfw/application/camera.cpp b/sfw/application/camera_3d.cpp similarity index 65% rename from sfw/application/camera.cpp rename to sfw/application/camera_3d.cpp index ff14ac0..4ef7a50 100644 --- a/sfw/application/camera.cpp +++ b/sfw/application/camera_3d.cpp @@ -1,18 +1,18 @@ -#include "camera.h" +#include "camera_3d.h" #include "math_funcs.h" -void Camera::bind() { +void Camera3D::bind() { make_current(); model_view_matrix = Transform(); } -void Camera::make_current() { +void Camera3D::make_current() { current_camera = this; } -Camera::Camera() { +Camera3D::Camera3D() { screen_aspect_ratio = 1; //p_viewport_size.width / (float)p_viewport_size.height, znear = 0.05; @@ -21,13 +21,13 @@ Camera::Camera() { vaspect = false; } -Camera::~Camera() { +Camera3D::~Camera3D() { } -Camera *Camera::current_camera = NULL; +Camera3D *Camera3D::current_camera = NULL; void OrthographicCamera::bind() { - Camera::bind(); + Camera3D::bind(); projection_matrix.set_orthogonal( size, screen_aspect_ratio, @@ -37,13 +37,13 @@ void OrthographicCamera::bind() { } OrthographicCamera::OrthographicCamera() : - Camera() { + Camera3D() { } OrthographicCamera::~OrthographicCamera() { } void PerspectiveCamera::bind() { - Camera::bind(); + Camera3D::bind(); projection_matrix.set_perspective( fov, @@ -54,9 +54,20 @@ void PerspectiveCamera::bind() { } PerspectiveCamera::PerspectiveCamera() : - Camera() { + Camera3D() { fov = 70; } PerspectiveCamera::~PerspectiveCamera() { } + +void FrustumCamera::bind() { + Camera3D::bind(); +} + +FrustumCamera::FrustumCamera() : + Camera3D() { +} + +FrustumCamera::~FrustumCamera() { +} diff --git a/sfw/application/camera.h b/sfw/application/camera_3d.h similarity index 63% rename from sfw/application/camera.h rename to sfw/application/camera_3d.h index 4f351c8..1a7f51e 100644 --- a/sfw/application/camera.h +++ b/sfw/application/camera_3d.h @@ -1,5 +1,5 @@ -#ifndef CAMERA_H -#define CAMERA_H +#ifndef CAMERA_3D_H +#define CAMERA_3D_H #include "3rd_glad.h" @@ -7,14 +7,14 @@ #include "transform.h" #include "vector3.h" -class Camera { +class Camera3D { public: virtual void bind(); void make_current(); - Camera(); - virtual ~Camera(); + Camera3D(); + virtual ~Camera3D(); float size; float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height, @@ -22,14 +22,14 @@ public: float zfar; bool vaspect; - static Camera *current_camera; + static Camera3D *current_camera; Transform camera_transform; Transform model_view_matrix; Projection projection_matrix; }; -class OrthographicCamera : public Camera { +class OrthographicCamera : public Camera3D { public: void bind(); @@ -37,7 +37,7 @@ public: ~OrthographicCamera(); }; -class PerspectiveCamera : public Camera { +class PerspectiveCamera : public Camera3D { public: float fov; @@ -47,6 +47,14 @@ public: ~PerspectiveCamera(); }; -//frustum +class FrustumCamera : public Camera3D { +public: + //todo + void bind(); + + FrustumCamera(); + ~FrustumCamera(); +}; + #endif // CAMERA_H diff --git a/sfw/application/color_material.h b/sfw/application/color_material.h index e368891..4c19f6f 100644 --- a/sfw/application/color_material.h +++ b/sfw/application/color_material.h @@ -3,7 +3,7 @@ #include "material.h" -#include "camera.h" +#include "camera_3d.h" class ColorMaterial : public Material { public: @@ -12,9 +12,9 @@ public: } void bind_uniforms() { - set_uniform(projection_matrix_location, Camera::current_camera->projection_matrix); - set_uniform(camera_matrix_location, Camera::current_camera->camera_transform); - set_uniform(model_view_matrix_location, Camera::current_camera->model_view_matrix); + set_uniform(projection_matrix_location, Camera3D::current_camera->projection_matrix); + set_uniform(camera_matrix_location, Camera3D::current_camera->camera_transform); + set_uniform(model_view_matrix_location, Camera3D::current_camera->model_view_matrix); } void setup_uniforms() { diff --git a/sfw/application/colored_material.h b/sfw/application/colored_material.h index e4e3311..4fee84d 100644 --- a/sfw/application/colored_material.h +++ b/sfw/application/colored_material.h @@ -5,7 +5,7 @@ #include "color.h" -#include "camera.h" +#include "camera_3d.h" class ColoredMaterial : public Material { public: @@ -14,8 +14,8 @@ public: } void bind_uniforms() { - //glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix)); - //glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_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(Camera3D::current_camera->model_view_matrix)); glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); } diff --git a/sfw/application/mesh_instance.cpp b/sfw/application/mesh_instance.cpp deleted file mode 100644 index 2fcaa97..0000000 --- a/sfw/application/mesh_instance.cpp +++ /dev/null @@ -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(); -} diff --git a/sfw/application/mesh_instance.h b/sfw/application/mesh_instance.h deleted file mode 100644 index 000741a..0000000 --- a/sfw/application/mesh_instance.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MESH_INSTACE_H -#define MESH_INSTACE_H - -#include - -#include "material.h" -#include "mesh.h" - -#include "transform.h" - -class MeshInstance { -public: - void render(); - - MeshInstance(); - ~MeshInstance(); - - Material *material; - Mesh *mesh; - - Transform transform; - - std::vector children; -}; - -#endif // MESH_INSTACE_H diff --git a/sfw/application/mesh_instance_3d.cpp b/sfw/application/mesh_instance_3d.cpp new file mode 100644 index 0000000..4b02c03 --- /dev/null +++ b/sfw/application/mesh_instance_3d.cpp @@ -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(); +} diff --git a/sfw/application/mesh_instance_3d.h b/sfw/application/mesh_instance_3d.h new file mode 100644 index 0000000..5307266 --- /dev/null +++ b/sfw/application/mesh_instance_3d.h @@ -0,0 +1,26 @@ +#ifndef MESH_INSTACE_3D_H +#define MESH_INSTACE_3D_H + +#include + +#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 children; +}; + +#endif // MESH_INSTACE_H diff --git a/sfw/application/object_3d.cpp b/sfw/application/object_3d.cpp new file mode 100644 index 0000000..502e7de --- /dev/null +++ b/sfw/application/object_3d.cpp @@ -0,0 +1,8 @@ +#include "object_3d.h" + + +Object3D::Object3D() { +} + +Object3D::~Object3D() { +} diff --git a/sfw/application/object_3d.h b/sfw/application/object_3d.h new file mode 100644 index 0000000..0f8ec56 --- /dev/null +++ b/sfw/application/object_3d.h @@ -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 diff --git a/sfw/application/opengl.h b/sfw/application/opengl.h deleted file mode 100644 index b391339..0000000 --- a/sfw/application/opengl.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef OPENGL_H -#define OPENGL_H - -#if defined(__unix__) && !defined(__ANDROID__) -#define GL_GLEXT_PROTOTYPES -#include -#endif // __unix__ - -#if __ANDROID__ -#define GL_GLEXT_PROTOTYPES -#include -#endif - -#if defined(_WIN64) || defined(_WIN32) -#include "3rd_glad.h" -#endif // defined - -#endif // OPENGL_H diff --git a/sfw/application/texture_material.h b/sfw/application/texture_material.h index e154b29..c1b11e1 100644 --- a/sfw/application/texture_material.h +++ b/sfw/application/texture_material.h @@ -4,7 +4,7 @@ #include "material.h" #include "texture.h" -#include "camera.h" +#include "camera_3d.h" class TextureMaterial : public Material { public: @@ -13,9 +13,9 @@ public: } void bind_uniforms() { - set_uniform(projection_matrix_location, Camera::current_camera->projection_matrix); - set_uniform(camera_matrix_location, Camera::current_camera->camera_transform); - set_uniform(model_view_matrix_location, Camera::current_camera->model_view_matrix); + set_uniform(projection_matrix_location, Camera3D::current_camera->projection_matrix); + set_uniform(camera_matrix_location, Camera3D::current_camera->camera_transform); + set_uniform(model_view_matrix_location, Camera3D::current_camera->model_view_matrix); if (texture) { glActiveTexture(GL_TEXTURE0); diff --git a/sfw/application/transparent_texture_material.h b/sfw/application/transparent_texture_material.h index 3690555..e25a9f9 100644 --- a/sfw/application/transparent_texture_material.h +++ b/sfw/application/transparent_texture_material.h @@ -7,7 +7,7 @@ #include "./libs/glm/vec4.hpp" #include "./libs/glm/gtc/type_ptr.hpp" -#include "camera.h" +#include "camera_3d.h" class TransparentTextureMaterial : public Material { public: @@ -16,8 +16,8 @@ public: } void bind_uniforms() { - glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix)); - glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_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(Camera3D::current_camera->model_view_matrix)); if (texture) { glActiveTexture(GL_TEXTURE0);