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->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();

View File

@ -1,12 +1,11 @@
#include "camera.h"
#include "../../libs/glm/gtc/matrix_transform.hpp"
#include "math_funcs.h"
void Camera::bind() {
make_current();
model_view_matrix = glm::mat4(1);
model_view_matrix = Transform();
}
void Camera::make_current() {
@ -14,13 +13,13 @@ void Camera::make_current() {
}
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() {
}
@ -30,21 +29,16 @@ Camera* Camera::current_camera = NULL;
void OrthographicCamera::bind() {
Camera::bind();
projection_matrix = glm::ortho<float>(-(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() {
}
@ -52,22 +46,18 @@ OrthographicCamera::~OrthographicCamera() {
void PerspectiveCamera::bind() {
Camera::bind();
projection_matrix = glm::perspectiveFov<float>(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() {
}

View File

@ -3,8 +3,9 @@
#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:
@ -15,18 +16,17 @@ public:
Camera();
virtual ~Camera();
float width;
float height;
float fov;
glm::vec3 position;
glm::vec3 rotation;
glm::vec3 scale;
float size;
float screen_aspect_ratio; //p_viewport_size.width / (float)p_viewport_size.height,
float znear;
float zfar;
bool vaspect;
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 {
@ -39,8 +39,7 @@ public:
class PerspectiveCamera : public Camera {
public:
float near;
float far;
float fov;
void bind();
@ -48,4 +47,6 @@ public:
~PerspectiveCamera();
};
//frustum
#endif // CAMERA_H

View File

@ -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() {

View File

@ -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;
};

View File

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

View File

@ -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();

View File

@ -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<MeshInstance *> children;
};

View File

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

View File

@ -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;
};

View File

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

View File

@ -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();
}

View File

@ -4,9 +4,6 @@
#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 {
@ -16,8 +13,9 @@ 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));
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);
@ -28,6 +26,7 @@ public:
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");
@ -44,6 +43,7 @@ public:
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"
@ -53,7 +53,7 @@ public:
"\n"
"void main() {\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;
}
TextureMaterial() : Material() {
TextureMaterial() :
Material() {
projection_matrix_location = 0;
camera_matrix_location = 0;
model_view_matrix_location = 0;
texture_location = 0;
texture = NULL;
}
GLint projection_matrix_location;
GLint camera_matrix_location;
GLint model_view_matrix_location;
GLint texture_location;
@ -89,6 +95,4 @@ public:
Texture *texture;
};
#endif // COLORED_MATERIAL_H

View File

@ -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();