diff --git a/game_scene.cpp b/game_scene.cpp index 9151679..9abf290 100644 --- a/game_scene.cpp +++ b/game_scene.cpp @@ -135,8 +135,8 @@ GameScene::GameScene() { sprite = new Sprite(); sprite->mesh_instance->material = material; - sprite->position.x = 0; - sprite->position.y = 0; + //sprite->position.x = 0; + //sprite->position.y = 0; //sprite->region_x = 7.0 * (1.0 / 16.0); //sprite->region_y = 7.0 * (1.0 / 16.0); //sprite->region_width = 1.0 / 16.0; @@ -164,15 +164,15 @@ GameScene::GameScene() { */ camera = new OrthographicCamera(); - camera->width = 2; - camera->height = 2; - camera->position.x = 0; - camera->position.y = 0; + //camera->width = 2; + //camera->height = 2; + //camera->position.x = 0; + //camera->position.y = 0; //camera->position.z = -2; mesh = memnew(Mesh(2)); cmaterial = memnew(ColoredMaterial()); - cmaterial->color = glm::vec4(1, 1, 0, 1); + //cmaterial->color = glm::vec4(1, 1, 0, 1); mesh->clear(); diff --git a/sfw/application/camera.cpp b/sfw/application/camera.cpp index bf7b9c0..73bf3ce 100644 --- a/sfw/application/camera.cpp +++ b/sfw/application/camera.cpp @@ -1,73 +1,63 @@ #include "camera.h" -#include "../../libs/glm/gtc/matrix_transform.hpp" - +#include "math_funcs.h" void Camera::bind() { - make_current(); + make_current(); - model_view_matrix = glm::mat4(1); + model_view_matrix = Transform(); } void Camera::make_current() { - current_camera = this; + current_camera = this; } Camera::Camera() { - width = 2; - height = 2; - fov = glm::radians(45.0); + screen_aspect_ratio = 1; //p_viewport_size.width / (float)p_viewport_size.height, - position = glm::vec3(0, 0, 0); - rotation = glm::vec3(0, 0, 0); - scale = glm::vec3(1, 1, 1); + znear = 0.05; + zfar = 100; + size = 1.0; + + vaspect = false; } Camera::~Camera() { } -Camera* Camera::current_camera = NULL; +Camera *Camera::current_camera = NULL; void OrthographicCamera::bind() { - Camera::bind(); + Camera::bind(); - projection_matrix = glm::ortho(-(width / 2.0), - width / 2.0, - -(height / 2.0), - height / 2.0); - - projection_matrix = glm::translate(projection_matrix, -position); - - projection_matrix = glm::rotate(projection_matrix, rotation.x, glm::vec3(1, 0, 0)); - projection_matrix = glm::rotate(projection_matrix, rotation.y, glm::vec3(0, 1, 0)); - projection_matrix = glm::rotate(projection_matrix, rotation.z, glm::vec3(0, 0, 1)); - - projection_matrix = glm::scale(projection_matrix, scale); + projection_matrix.set_orthogonal( + size, + screen_aspect_ratio, + znear, + zfar, + vaspect); } -OrthographicCamera::OrthographicCamera() : Camera() { +OrthographicCamera::OrthographicCamera() : + Camera() { } OrthographicCamera::~OrthographicCamera() { } void PerspectiveCamera::bind() { - Camera::bind(); + Camera::bind(); - projection_matrix = glm::perspectiveFov(fov, width, height, near, far); - - projection_matrix = glm::translate(projection_matrix, -position); - - projection_matrix = glm::rotate(projection_matrix, rotation.x, glm::vec3(1, 0, 0)); - projection_matrix = glm::rotate(projection_matrix, rotation.y, glm::vec3(0, 1, 0)); - projection_matrix = glm::rotate(projection_matrix, rotation.z, glm::vec3(0, 0, 1)); - - projection_matrix = glm::scale(projection_matrix, scale); + projection_matrix.set_perspective( + fov, + screen_aspect_ratio, + znear, + zfar, + vaspect); } -PerspectiveCamera::PerspectiveCamera() : Camera() { - near = 0.1; - far = 10; +PerspectiveCamera::PerspectiveCamera() : + Camera() { + fov = 70; } PerspectiveCamera::~PerspectiveCamera() { } - diff --git a/sfw/application/camera.h b/sfw/application/camera.h index 5501ff1..4f351c8 100644 --- a/sfw/application/camera.h +++ b/sfw/application/camera.h @@ -3,49 +3,50 @@ #include "3rd_glad.h" -#include "../../libs/glm/vec3.hpp" -#include "../../libs/glm/matrix.hpp" +#include "projection.h" +#include "transform.h" +#include "vector3.h" class Camera { public: - virtual void bind(); + virtual void bind(); - void make_current(); + void make_current(); - Camera(); - virtual ~Camera(); + Camera(); + virtual ~Camera(); - float width; - float height; - float fov; + float size; + float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height, + float znear; + float zfar; + bool vaspect; - glm::vec3 position; - glm::vec3 rotation; - glm::vec3 scale; + static Camera *current_camera; - static Camera* current_camera; - - glm::mat4x4 projection_matrix; - glm::mat4x4 model_view_matrix; + Transform camera_transform; + Transform model_view_matrix; + Projection projection_matrix; }; class OrthographicCamera : public Camera { public: - void bind(); + void bind(); - OrthographicCamera(); - ~OrthographicCamera(); + OrthographicCamera(); + ~OrthographicCamera(); }; class PerspectiveCamera : public Camera { public: - float near; - float far; + float fov; - void bind(); + void bind(); - PerspectiveCamera(); - ~PerspectiveCamera(); + PerspectiveCamera(); + ~PerspectiveCamera(); }; +//frustum + #endif // CAMERA_H diff --git a/sfw/application/color_material.h b/sfw/application/color_material.h index 1ee7888..717eefd 100644 --- a/sfw/application/color_material.h +++ b/sfw/application/color_material.h @@ -2,12 +2,9 @@ #define COLOR_MATERIAL_H #include "material.h" -#include "glm/vec4.hpp" #include "camera.h" -#include "./glm/gtc/type_ptr.hpp" - class ColorMaterial : public Material { public: @@ -16,8 +13,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(Camera::current_camera->projection_matrix)); + //glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix)); } void setup_uniforms() { diff --git a/sfw/application/colored_material.h b/sfw/application/colored_material.h index 41837e4..e4e3311 100644 --- a/sfw/application/colored_material.h +++ b/sfw/application/colored_material.h @@ -3,8 +3,7 @@ #include "material.h" -#include "../../libs/glm/vec4.hpp" -#include "../../libs/glm/gtc/type_ptr.hpp" +#include "color.h" #include "camera.h" @@ -15,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(Camera::current_camera->projection_matrix)); + //glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_matrix)); glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); } @@ -59,14 +58,13 @@ public: } ColoredMaterial() : Material() { - color = glm::vec4(1, 1, 1, 1); } GLint projection_matrix_location; GLint model_view_matrix_location; GLint tri_color_uniform_location; - glm::vec4 color; + Color color; }; diff --git a/sfw/application/material.h b/sfw/application/material.h index 0d0da93..c73cce9 100644 --- a/sfw/application/material.h +++ b/sfw/application/material.h @@ -1,30 +1,95 @@ #ifndef MATERIAL_H #define MATERIAL_H +#include "projection.h" #include "shader.h" +#include "transform.h" +#include "transform_2d.h" class Material { public: - void bind(); + void bind(); - virtual void unbind(); - virtual int get_material_id() = 0; - virtual void bind_uniforms(); - virtual void setup_uniforms(); - virtual void setup_state(); - virtual const GLchar **get_vertex_shader_source() = 0; - virtual const GLchar **get_fragment_shader_source() = 0; + virtual void unbind(); + virtual int get_material_id() = 0; + virtual void bind_uniforms(); + virtual void setup_uniforms(); + virtual void setup_state(); + virtual const GLchar **get_vertex_shader_source() = 0; + virtual const GLchar **get_fragment_shader_source() = 0; - GLint get_uniform(const char* name); + GLint get_uniform(const char *name); - Material(); - virtual ~Material(); + Material(); + virtual ~Material(); protected: - static Material *current_material; + static Material *current_material; - Shader * shader; + Shader *shader; + +protected: + _FORCE_INLINE_ void set_uniform(GLint p_uniform, const Transform &p_transform) { + const Transform &tr = p_transform; + + GLfloat matrix[16] = { /* build a 16x16 matrix */ + tr.basis.rows[0][0], + tr.basis.rows[1][0], + tr.basis.rows[2][0], + 0, + tr.basis.rows[0][1], + tr.basis.rows[1][1], + tr.basis.rows[2][1], + 0, + tr.basis.rows[0][2], + tr.basis.rows[1][2], + tr.basis.rows[2][2], + 0, + tr.origin.x, + tr.origin.y, + tr.origin.z, + 1 + }; + + glUniformMatrix4fv(p_uniform, 1, false, matrix); + } + + _FORCE_INLINE_ void set_uniform(GLint p_uniform, const Transform2D &p_transform) { + const Transform2D &tr = p_transform; + + GLfloat matrix[16] = { /* build a 16x16 matrix */ + tr.columns[0][0], + tr.columns[0][1], + 0, + 0, + tr.columns[1][0], + tr.columns[1][1], + 0, + 0, + 0, + 0, + 1, + 0, + tr.columns[2][0], + tr.columns[2][1], + 0, + 1 + }; + + glUniformMatrix4fv(p_uniform, 1, false, matrix); + } + + _FORCE_INLINE_ void set_uniform(GLint p_uniform, const Projection &p_matrix) { + GLfloat matrix[16]; + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + matrix[i * 4 + j] = p_matrix.matrix[i][j]; + } + } + + glUniformMatrix4fv(p_uniform, 1, false, matrix); + } }; - #endif // MATERIAL_H diff --git a/sfw/application/mesh_instance.cpp b/sfw/application/mesh_instance.cpp index e9bacd6..2fcaa97 100644 --- a/sfw/application/mesh_instance.cpp +++ b/sfw/application/mesh_instance.cpp @@ -2,26 +2,14 @@ #include "camera.h" -#include "../../libs/glm/vec3.hpp" -#include "../../libs/glm/matrix.hpp" -#include "../../libs/glm/gtc/matrix_transform.hpp" - - void MeshInstance::render() { if (!mesh) { return; } - glm::mat4 mat_orig = Camera::current_camera->model_view_matrix; - - Camera::current_camera->model_view_matrix = glm::translate(Camera::current_camera->model_view_matrix, position); - - Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation.x, glm::vec3(1, 0, 0)); - Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation.y, glm::vec3(0, 1, 0)); - Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation.z, glm::vec3(0, 0, 1)); - - Camera::current_camera->model_view_matrix = glm::scale(Camera::current_camera->model_view_matrix, scale); + Transform mat_orig = Camera::current_camera->model_view_matrix; + Camera::current_camera->model_view_matrix *= transform; if (material) { material->bind(); @@ -43,10 +31,6 @@ void MeshInstance::render() { MeshInstance::MeshInstance() { material = NULL; mesh = NULL; - - position = glm::vec3(0, 0, 0); - rotation = glm::vec3(0, 0, 0); - scale = glm::vec3(1, 1, 1); } MeshInstance::~MeshInstance() { children.clear(); diff --git a/sfw/application/mesh_instance.h b/sfw/application/mesh_instance.h index ae21a41..000741a 100644 --- a/sfw/application/mesh_instance.h +++ b/sfw/application/mesh_instance.h @@ -6,7 +6,7 @@ #include "material.h" #include "mesh.h" -#include "../../libs/glm/vec3.hpp" +#include "transform.h" class MeshInstance { public: @@ -18,9 +18,7 @@ public: Material *material; Mesh *mesh; - glm::vec3 position; - glm::vec3 rotation; - glm::vec3 scale; + Transform transform; std::vector children; }; diff --git a/sfw/application/object_2d.cpp b/sfw/application/object_2d.cpp index f1e5dcf..21570bf 100644 --- a/sfw/application/object_2d.cpp +++ b/sfw/application/object_2d.cpp @@ -2,9 +2,6 @@ Object2D::Object2D() { - position = glm::vec2(0, 0); - rotation = 0; - scale = glm::vec2(1, 1); } Object2D::~Object2D() { diff --git a/sfw/application/object_2d.h b/sfw/application/object_2d.h index 2370ef8..ecdaa2c 100644 --- a/sfw/application/object_2d.h +++ b/sfw/application/object_2d.h @@ -1,16 +1,14 @@ #ifndef OBJECT_2D_H #define OBJECT_2D_H -#include "../../libs/glm/vec2.hpp" +#include "transform_2d.h" class Object2D { public: Object2D(); virtual ~Object2D(); - glm::vec2 position; - float rotation; - glm::vec2 scale; + Transform2D transform; }; diff --git a/sfw/application/shader.h b/sfw/application/shader.h index ce924f4..99c3a25 100644 --- a/sfw/application/shader.h +++ b/sfw/application/shader.h @@ -6,55 +6,54 @@ class Shader { public: - enum VertexAttributes { - ATTRIBUTE_POSITION = 0, - ATTRIBUTE_NORMAL, - ATTRIBUTE_COLOR, - ATTRIBUTE_UV, - }; + enum VertexAttributes { + ATTRIBUTE_POSITION = 0, + ATTRIBUTE_NORMAL, + ATTRIBUTE_COLOR, + ATTRIBUTE_UV, + }; - bool bind(); - void unbind(); + bool bind(); + void unbind(); - void compile(); - void destroy(); + void compile(); + void destroy(); - const GLchar **get_vertex_shader_source(); - void set_vertex_shader_source(const GLchar **source); + const GLchar **get_vertex_shader_source(); + void set_vertex_shader_source(const GLchar **source); - const GLchar **get_fragment_shader_source(); - void set_fragment_shader_source(const GLchar **source); + const GLchar **get_fragment_shader_source(); + void set_fragment_shader_source(const GLchar **source); - void print_shader_errors(const GLuint p_program, const char *name); - void print_program_errors(const GLuint p_program); + void print_shader_errors(const GLuint p_program, const char *name); + void print_program_errors(const GLuint p_program); - Shader(); - ~Shader(); + Shader(); + ~Shader(); - GLuint vertex_shader; - GLuint fragment_shader; - GLuint program; + GLuint vertex_shader; + GLuint fragment_shader; + GLuint program; - static Shader * current_shader; + static Shader *current_shader; protected: - const GLchar **vertex_shader_source; - const GLchar **fragment_shader_source; + const GLchar **vertex_shader_source; + const GLchar **fragment_shader_source; }; class ShaderCache { public: - static ShaderCache *get_singleton(); + static ShaderCache *get_singleton(); - Shader *get_shader(const int id); - void add_shader(const int id, Shader *shader); + Shader *get_shader(const int id); + void add_shader(const int id, Shader *shader); - ShaderCache(); - ~ShaderCache(); + ShaderCache(); + ~ShaderCache(); protected: - std::unordered_map shaders; + std::unordered_map shaders; }; - #endif // SHADER_H diff --git a/sfw/application/sprite.cpp b/sfw/application/sprite.cpp index 00882d1..e9641ff 100644 --- a/sfw/application/sprite.cpp +++ b/sfw/application/sprite.cpp @@ -1,6 +1,7 @@ #include "sprite.h" void Sprite::render() { + /* mesh_instance->position.x = position.x; mesh_instance->position.y = position.y; @@ -8,6 +9,7 @@ void Sprite::render() { mesh_instance->scale.x = scale.x; mesh_instance->scale.y = scale.y; + */ mesh_instance->render(); } diff --git a/sfw/application/texture_material.h b/sfw/application/texture_material.h index 29d6c9d..3afd94f 100644 --- a/sfw/application/texture_material.h +++ b/sfw/application/texture_material.h @@ -4,91 +4,95 @@ #include "material.h" #include "texture.h" -#include "../../libs/glm/vec4.hpp" -#include "../../libs/glm/gtc/type_ptr.hpp" - #include "camera.h" class TextureMaterial : public Material { public: - int get_material_id() { - return 3; - } + int get_material_id() { + return 3; + } - 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)); + 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); - if (texture) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture->texture); - glUniform1i(texture_location, 0); - } - } + if (texture) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->texture); + glUniform1i(texture_location, 0); + } + } - void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); + void setup_uniforms() { + projection_matrix_location = get_uniform("u_proj_matrix"); + camera_matrix_location = get_uniform("u_camera_matrix"); + model_view_matrix_location = get_uniform("u_model_view_matrix"); - texture_location = get_uniform("u_texture"); - } + texture_location = get_uniform("u_texture"); + } - void unbind() { - glDisable(GL_TEXTURE_2D); - } + void unbind() { + glDisable(GL_TEXTURE_2D); + } - void setup_state() { - glEnable(GL_TEXTURE_2D); - } + void setup_state() { + glEnable(GL_TEXTURE_2D); + } - const GLchar **get_vertex_shader_source() { - static const GLchar *vertex_shader_source[] = { - "uniform mat4 u_proj_matrix;\n" - "uniform mat4 u_model_view_matrix;\n" - "\n" - "attribute vec4 a_position;\n" - "attribute vec2 a_uv;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " v_uv = a_uv;\n" - " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" - "}" - }; + const GLchar **get_vertex_shader_source() { + static const GLchar *vertex_shader_source[] = { + "uniform mat4 u_proj_matrix;\n" + "uniform mat4 u_camera_matrix;\n" + "uniform mat4 u_model_view_matrix;\n" + "\n" + "attribute vec4 a_position;\n" + "attribute vec2 a_uv;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " v_uv = a_uv;\n" + " gl_Position = u_proj_matrix * camera_matrix_location * u_model_view_matrix * a_position;\n" + "}" + }; - return vertex_shader_source; - } + return vertex_shader_source; + } - const GLchar **get_fragment_shader_source() { - static const GLchar *fragment_shader_source[] = { - "precision mediump float;\n" - "\n" - "uniform sampler2D u_texture;\n" - "\n" - "varying vec2 v_uv;\n" - "\n" - "void main() {\n" - " gl_FragColor = texture2D(u_texture, v_uv);\n" - "}" - }; + const GLchar **get_fragment_shader_source() { + static const GLchar *fragment_shader_source[] = { + "precision mediump float;\n" + "\n" + "uniform sampler2D u_texture;\n" + "\n" + "varying vec2 v_uv;\n" + "\n" + "void main() {\n" + " gl_FragColor = texture2D(u_texture, v_uv);\n" + "}" + }; - return fragment_shader_source; - } + return fragment_shader_source; + } - TextureMaterial() : Material() { - texture_location = 0; - texture = NULL; - } + TextureMaterial() : + Material() { + projection_matrix_location = 0; + camera_matrix_location = 0; + model_view_matrix_location = 0; - GLint projection_matrix_location; - GLint model_view_matrix_location; + texture_location = 0; + texture = NULL; + } - GLint texture_location; + GLint projection_matrix_location; + GLint camera_matrix_location; + GLint model_view_matrix_location; - Texture *texture; + GLint texture_location; + + Texture *texture; }; - - #endif // COLORED_MATERIAL_H diff --git a/sfw/application/tile_map.cpp b/sfw/application/tile_map.cpp index c8a03f1..05b0646 100644 --- a/sfw/application/tile_map.cpp +++ b/sfw/application/tile_map.cpp @@ -1,9 +1,5 @@ #include "tile_map.h" -#include "../../libs/glm/gtc/matrix_transform.hpp" -#include "../../libs/glm/matrix.hpp" -#include "../../libs/glm/vec3.hpp" - #include "camera.h" void TileMap::build_mesh() { @@ -105,13 +101,13 @@ void TileMap::render() { if (!mesh) return; - glm::mat4 mat_orig = Camera::current_camera->model_view_matrix; + Transform mat_orig = Camera::current_camera->model_view_matrix; - Camera::current_camera->model_view_matrix = glm::translate(Camera::current_camera->model_view_matrix, glm::vec3(position.x, position.y, 0)); + //Camera::current_camera->model_view_matrix = glm::translate(Camera::current_camera->model_view_matrix, glm::vec3(position.x, position.y, 0)); - Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation, glm::vec3(0, 0, 1)); + //Camera::current_camera->model_view_matrix = glm::rotate(Camera::current_camera->model_view_matrix, rotation, glm::vec3(0, 0, 1)); - Camera::current_camera->model_view_matrix = glm::scale(Camera::current_camera->model_view_matrix, glm::vec3(scale.x, scale.y, 0)); + //Camera::current_camera->model_view_matrix = glm::scale(Camera::current_camera->model_view_matrix, glm::vec3(scale.x, scale.y, 0)); if (material) material->bind();