glm initial remove pass.

This commit is contained in:
Relintai 2023-12-19 22:33:04 +01:00
parent 50e8606b59
commit b58b00ed06
14 changed files with 260 additions and 231 deletions

View File

@ -135,8 +135,8 @@ GameScene::GameScene() {
sprite = new Sprite(); sprite = new Sprite();
sprite->mesh_instance->material = material; sprite->mesh_instance->material = material;
sprite->position.x = 0; //sprite->position.x = 0;
sprite->position.y = 0; //sprite->position.y = 0;
//sprite->region_x = 7.0 * (1.0 / 16.0); //sprite->region_x = 7.0 * (1.0 / 16.0);
//sprite->region_y = 7.0 * (1.0 / 16.0); //sprite->region_y = 7.0 * (1.0 / 16.0);
//sprite->region_width = 1.0 / 16.0; //sprite->region_width = 1.0 / 16.0;
@ -164,15 +164,15 @@ GameScene::GameScene() {
*/ */
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera->width = 2; //camera->width = 2;
camera->height = 2; //camera->height = 2;
camera->position.x = 0; //camera->position.x = 0;
camera->position.y = 0; //camera->position.y = 0;
//camera->position.z = -2; //camera->position.z = -2;
mesh = memnew(Mesh(2)); mesh = memnew(Mesh(2));
cmaterial = memnew(ColoredMaterial()); cmaterial = memnew(ColoredMaterial());
cmaterial->color = glm::vec4(1, 1, 0, 1); //cmaterial->color = glm::vec4(1, 1, 0, 1);
mesh->clear(); mesh->clear();

View File

@ -1,12 +1,11 @@
#include "camera.h" #include "camera.h"
#include "../../libs/glm/gtc/matrix_transform.hpp" #include "math_funcs.h"
void Camera::bind() { void Camera::bind() {
make_current(); make_current();
model_view_matrix = glm::mat4(1); model_view_matrix = Transform();
} }
void Camera::make_current() { void Camera::make_current() {
@ -14,13 +13,13 @@ void Camera::make_current() {
} }
Camera::Camera() { Camera::Camera() {
width = 2; screen_aspect_ratio = 1; //p_viewport_size.width / (float)p_viewport_size.height,
height = 2;
fov = glm::radians(45.0);
position = glm::vec3(0, 0, 0); znear = 0.05;
rotation = glm::vec3(0, 0, 0); zfar = 100;
scale = glm::vec3(1, 1, 1); size = 1.0;
vaspect = false;
} }
Camera::~Camera() { Camera::~Camera() {
} }
@ -30,21 +29,16 @@ Camera* Camera::current_camera = NULL;
void OrthographicCamera::bind() { void OrthographicCamera::bind() {
Camera::bind(); Camera::bind();
projection_matrix = glm::ortho<float>(-(width / 2.0), projection_matrix.set_orthogonal(
width / 2.0, size,
-(height / 2.0), screen_aspect_ratio,
height / 2.0); znear,
zfar,
projection_matrix = glm::translate(projection_matrix, -position); vaspect);
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);
} }
OrthographicCamera::OrthographicCamera() : Camera() { OrthographicCamera::OrthographicCamera() :
Camera() {
} }
OrthographicCamera::~OrthographicCamera() { OrthographicCamera::~OrthographicCamera() {
} }
@ -52,22 +46,18 @@ OrthographicCamera::~OrthographicCamera() {
void PerspectiveCamera::bind() { void PerspectiveCamera::bind() {
Camera::bind(); Camera::bind();
projection_matrix = glm::perspectiveFov<float>(fov, width, height, near, far); projection_matrix.set_perspective(
fov,
projection_matrix = glm::translate(projection_matrix, -position); screen_aspect_ratio,
znear,
projection_matrix = glm::rotate(projection_matrix, rotation.x, glm::vec3(1, 0, 0)); zfar,
projection_matrix = glm::rotate(projection_matrix, rotation.y, glm::vec3(0, 1, 0)); vaspect);
projection_matrix = glm::rotate(projection_matrix, rotation.z, glm::vec3(0, 0, 1));
projection_matrix = glm::scale(projection_matrix, scale);
} }
PerspectiveCamera::PerspectiveCamera() : Camera() { PerspectiveCamera::PerspectiveCamera() :
near = 0.1; Camera() {
far = 10; fov = 70;
} }
PerspectiveCamera::~PerspectiveCamera() { PerspectiveCamera::~PerspectiveCamera() {
} }

View File

@ -3,8 +3,9 @@
#include "3rd_glad.h" #include "3rd_glad.h"
#include "../../libs/glm/vec3.hpp" #include "projection.h"
#include "../../libs/glm/matrix.hpp" #include "transform.h"
#include "vector3.h"
class Camera { class Camera {
public: public:
@ -15,18 +16,17 @@ public:
Camera(); Camera();
virtual ~Camera(); virtual ~Camera();
float width; float size;
float height; float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height,
float fov; float znear;
float zfar;
glm::vec3 position; bool vaspect;
glm::vec3 rotation;
glm::vec3 scale;
static Camera *current_camera; static Camera *current_camera;
glm::mat4x4 projection_matrix; Transform camera_transform;
glm::mat4x4 model_view_matrix; Transform model_view_matrix;
Projection projection_matrix;
}; };
class OrthographicCamera : public Camera { class OrthographicCamera : public Camera {
@ -39,8 +39,7 @@ public:
class PerspectiveCamera : public Camera { class PerspectiveCamera : public Camera {
public: public:
float near; float fov;
float far;
void bind(); void bind();
@ -48,4 +47,6 @@ public:
~PerspectiveCamera(); ~PerspectiveCamera();
}; };
//frustum
#endif // CAMERA_H #endif // CAMERA_H

View File

@ -2,12 +2,9 @@
#define COLOR_MATERIAL_H #define COLOR_MATERIAL_H
#include "material.h" #include "material.h"
#include "glm/vec4.hpp"
#include "camera.h" #include "camera.h"
#include "./glm/gtc/type_ptr.hpp"
class ColorMaterial : public Material { class ColorMaterial : public Material {
public: public:
@ -16,8 +13,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(Camera::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(Camera::current_camera->model_view_matrix));
} }
void setup_uniforms() { void setup_uniforms() {

View File

@ -3,8 +3,7 @@
#include "material.h" #include "material.h"
#include "../../libs/glm/vec4.hpp" #include "color.h"
#include "../../libs/glm/gtc/type_ptr.hpp"
#include "camera.h" #include "camera.h"
@ -15,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(Camera::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(Camera::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);
} }
@ -59,14 +58,13 @@ public:
} }
ColoredMaterial() : Material() { ColoredMaterial() : Material() {
color = glm::vec4(1, 1, 1, 1);
} }
GLint projection_matrix_location; GLint projection_matrix_location;
GLint model_view_matrix_location; GLint model_view_matrix_location;
GLint tri_color_uniform_location; GLint tri_color_uniform_location;
glm::vec4 color; Color color;
}; };

View File

@ -1,7 +1,10 @@
#ifndef MATERIAL_H #ifndef MATERIAL_H
#define MATERIAL_H #define MATERIAL_H
#include "projection.h"
#include "shader.h" #include "shader.h"
#include "transform.h"
#include "transform_2d.h"
class Material { class Material {
public: public:
@ -24,7 +27,69 @@ 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 #endif // MATERIAL_H

View File

@ -2,26 +2,14 @@
#include "camera.h" #include "camera.h"
#include "../../libs/glm/vec3.hpp"
#include "../../libs/glm/matrix.hpp"
#include "../../libs/glm/gtc/matrix_transform.hpp"
void MeshInstance::render() { void MeshInstance::render() {
if (!mesh) { if (!mesh) {
return; 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, 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);
Camera::current_camera->model_view_matrix *= transform;
if (material) { if (material) {
material->bind(); material->bind();
@ -43,10 +31,6 @@ void MeshInstance::render() {
MeshInstance::MeshInstance() { MeshInstance::MeshInstance() {
material = NULL; material = NULL;
mesh = NULL; mesh = NULL;
position = glm::vec3(0, 0, 0);
rotation = glm::vec3(0, 0, 0);
scale = glm::vec3(1, 1, 1);
} }
MeshInstance::~MeshInstance() { MeshInstance::~MeshInstance() {
children.clear(); children.clear();

View File

@ -6,7 +6,7 @@
#include "material.h" #include "material.h"
#include "mesh.h" #include "mesh.h"
#include "../../libs/glm/vec3.hpp" #include "transform.h"
class MeshInstance { class MeshInstance {
public: public:
@ -18,9 +18,7 @@ public:
Material *material; Material *material;
Mesh *mesh; Mesh *mesh;
glm::vec3 position; Transform transform;
glm::vec3 rotation;
glm::vec3 scale;
std::vector<MeshInstance *> children; std::vector<MeshInstance *> children;
}; };

View File

@ -2,9 +2,6 @@
Object2D::Object2D() { Object2D::Object2D() {
position = glm::vec2(0, 0);
rotation = 0;
scale = glm::vec2(1, 1);
} }
Object2D::~Object2D() { Object2D::~Object2D() {

View File

@ -1,16 +1,14 @@
#ifndef OBJECT_2D_H #ifndef OBJECT_2D_H
#define OBJECT_2D_H #define OBJECT_2D_H
#include "../../libs/glm/vec2.hpp" #include "transform_2d.h"
class Object2D { class Object2D {
public: public:
Object2D(); Object2D();
virtual ~Object2D(); virtual ~Object2D();
glm::vec2 position; Transform2D transform;
float rotation;
glm::vec2 scale;
}; };

View File

@ -56,5 +56,4 @@ protected:
std::unordered_map<int, Shader *> shaders; std::unordered_map<int, Shader *> shaders;
}; };
#endif // SHADER_H #endif // SHADER_H

View File

@ -1,6 +1,7 @@
#include "sprite.h" #include "sprite.h"
void Sprite::render() { void Sprite::render() {
/*
mesh_instance->position.x = position.x; mesh_instance->position.x = position.x;
mesh_instance->position.y = position.y; mesh_instance->position.y = position.y;
@ -8,6 +9,7 @@ void Sprite::render() {
mesh_instance->scale.x = scale.x; mesh_instance->scale.x = scale.x;
mesh_instance->scale.y = scale.y; mesh_instance->scale.y = scale.y;
*/
mesh_instance->render(); mesh_instance->render();
} }

View File

@ -4,9 +4,6 @@
#include "material.h" #include "material.h"
#include "texture.h" #include "texture.h"
#include "../../libs/glm/vec4.hpp"
#include "../../libs/glm/gtc/type_ptr.hpp"
#include "camera.h" #include "camera.h"
class TextureMaterial : public Material { class TextureMaterial : public Material {
@ -16,8 +13,9 @@ public:
} }
void bind_uniforms() { void bind_uniforms() {
glUniformMatrix4fv(projection_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->projection_matrix)); set_uniform(projection_matrix_location, Camera::current_camera->projection_matrix);
glUniformMatrix4fv(model_view_matrix_location, 1, GL_FALSE, glm::value_ptr(Camera::current_camera->model_view_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) { if (texture) {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -28,6 +26,7 @@ public:
void setup_uniforms() { void setup_uniforms() {
projection_matrix_location = get_uniform("u_proj_matrix"); 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"); model_view_matrix_location = get_uniform("u_model_view_matrix");
texture_location = get_uniform("u_texture"); texture_location = get_uniform("u_texture");
@ -44,6 +43,7 @@ public:
const GLchar **get_vertex_shader_source() { const GLchar **get_vertex_shader_source() {
static const GLchar *vertex_shader_source[] = { static const GLchar *vertex_shader_source[] = {
"uniform mat4 u_proj_matrix;\n" "uniform mat4 u_proj_matrix;\n"
"uniform mat4 u_camera_matrix;\n"
"uniform mat4 u_model_view_matrix;\n" "uniform mat4 u_model_view_matrix;\n"
"\n" "\n"
"attribute vec4 a_position;\n" "attribute vec4 a_position;\n"
@ -53,7 +53,7 @@ public:
"\n" "\n"
"void main() {\n" "void main() {\n"
" v_uv = a_uv;\n" " v_uv = a_uv;\n"
" gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" " gl_Position = u_proj_matrix * camera_matrix_location * u_model_view_matrix * a_position;\n"
"}" "}"
}; };
@ -76,12 +76,18 @@ public:
return fragment_shader_source; return fragment_shader_source;
} }
TextureMaterial() : Material() { TextureMaterial() :
Material() {
projection_matrix_location = 0;
camera_matrix_location = 0;
model_view_matrix_location = 0;
texture_location = 0; texture_location = 0;
texture = NULL; texture = NULL;
} }
GLint projection_matrix_location; GLint projection_matrix_location;
GLint camera_matrix_location;
GLint model_view_matrix_location; GLint model_view_matrix_location;
GLint texture_location; GLint texture_location;
@ -89,6 +95,4 @@ public:
Texture *texture; Texture *texture;
}; };
#endif // COLORED_MATERIAL_H #endif // COLORED_MATERIAL_H

View File

@ -1,9 +1,5 @@
#include "tile_map.h" #include "tile_map.h"
#include "../../libs/glm/gtc/matrix_transform.hpp"
#include "../../libs/glm/matrix.hpp"
#include "../../libs/glm/vec3.hpp"
#include "camera.h" #include "camera.h"
void TileMap::build_mesh() { void TileMap::build_mesh() {
@ -105,13 +101,13 @@ void TileMap::render() {
if (!mesh) if (!mesh)
return; 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) if (material)
material->bind(); material->bind();