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,73 +1,63 @@
#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() {
current_camera = this; current_camera = this;
} }
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() {
} }
Camera* Camera::current_camera = NULL; 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() {
} }
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,49 +3,50 @@
#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:
virtual void bind(); virtual void bind();
void make_current(); void make_current();
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;
bool vaspect;
glm::vec3 position; static Camera *current_camera;
glm::vec3 rotation;
glm::vec3 scale;
static Camera* current_camera; Transform camera_transform;
Transform model_view_matrix;
glm::mat4x4 projection_matrix; Projection projection_matrix;
glm::mat4x4 model_view_matrix;
}; };
class OrthographicCamera : public Camera { class OrthographicCamera : public Camera {
public: public:
void bind(); void bind();
OrthographicCamera(); OrthographicCamera();
~OrthographicCamera(); ~OrthographicCamera();
}; };
class PerspectiveCamera : public Camera { class PerspectiveCamera : public Camera {
public: public:
float near; float fov;
float far;
void bind(); void bind();
PerspectiveCamera(); PerspectiveCamera();
~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,30 +1,95 @@
#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:
void bind(); void bind();
virtual void unbind(); virtual void unbind();
virtual int get_material_id() = 0; virtual int get_material_id() = 0;
virtual void bind_uniforms(); virtual void bind_uniforms();
virtual void setup_uniforms(); virtual void setup_uniforms();
virtual void setup_state(); virtual void setup_state();
virtual const GLchar **get_vertex_shader_source() = 0; virtual const GLchar **get_vertex_shader_source() = 0;
virtual const GLchar **get_fragment_shader_source() = 0; virtual const GLchar **get_fragment_shader_source() = 0;
GLint get_uniform(const char* name); GLint get_uniform(const char *name);
Material(); Material();
virtual ~Material(); virtual ~Material();
protected: 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

@ -6,55 +6,54 @@
class Shader { class Shader {
public: public:
enum VertexAttributes { enum VertexAttributes {
ATTRIBUTE_POSITION = 0, ATTRIBUTE_POSITION = 0,
ATTRIBUTE_NORMAL, ATTRIBUTE_NORMAL,
ATTRIBUTE_COLOR, ATTRIBUTE_COLOR,
ATTRIBUTE_UV, ATTRIBUTE_UV,
}; };
bool bind(); bool bind();
void unbind(); void unbind();
void compile(); void compile();
void destroy(); void destroy();
const GLchar **get_vertex_shader_source(); const GLchar **get_vertex_shader_source();
void set_vertex_shader_source(const GLchar **source); void set_vertex_shader_source(const GLchar **source);
const GLchar **get_fragment_shader_source(); const GLchar **get_fragment_shader_source();
void set_fragment_shader_source(const GLchar **source); void set_fragment_shader_source(const GLchar **source);
void print_shader_errors(const GLuint p_program, const char *name); void print_shader_errors(const GLuint p_program, const char *name);
void print_program_errors(const GLuint p_program); void print_program_errors(const GLuint p_program);
Shader(); Shader();
~Shader(); ~Shader();
GLuint vertex_shader; GLuint vertex_shader;
GLuint fragment_shader; GLuint fragment_shader;
GLuint program; GLuint program;
static Shader * current_shader; static Shader *current_shader;
protected: protected:
const GLchar **vertex_shader_source; const GLchar **vertex_shader_source;
const GLchar **fragment_shader_source; const GLchar **fragment_shader_source;
}; };
class ShaderCache { class ShaderCache {
public: public:
static ShaderCache *get_singleton(); static ShaderCache *get_singleton();
Shader *get_shader(const int id); Shader *get_shader(const int id);
void add_shader(const int id, Shader *shader); void add_shader(const int id, Shader *shader);
ShaderCache(); ShaderCache();
~ShaderCache(); ~ShaderCache();
protected: 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,91 +4,95 @@
#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 {
public: public:
int get_material_id() { int get_material_id() {
return 3; return 3;
} }
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);
glBindTexture(GL_TEXTURE_2D, texture->texture); glBindTexture(GL_TEXTURE_2D, texture->texture);
glUniform1i(texture_location, 0); glUniform1i(texture_location, 0);
} }
} }
void setup_uniforms() { void setup_uniforms() {
projection_matrix_location = get_uniform("u_proj_matrix"); projection_matrix_location = get_uniform("u_proj_matrix");
model_view_matrix_location = get_uniform("u_model_view_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() { void unbind() {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
} }
void setup_state() { void setup_state() {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
} }
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_model_view_matrix;\n" "uniform mat4 u_camera_matrix;\n"
"\n" "uniform mat4 u_model_view_matrix;\n"
"attribute vec4 a_position;\n" "\n"
"attribute vec2 a_uv;\n" "attribute vec4 a_position;\n"
"\n" "attribute vec2 a_uv;\n"
"varying vec2 v_uv;\n" "\n"
"\n" "varying vec2 v_uv;\n"
"void main() {\n" "\n"
" v_uv = a_uv;\n" "void main() {\n"
" gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\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() { const GLchar **get_fragment_shader_source() {
static const GLchar *fragment_shader_source[] = { static const GLchar *fragment_shader_source[] = {
"precision mediump float;\n" "precision mediump float;\n"
"\n" "\n"
"uniform sampler2D u_texture;\n" "uniform sampler2D u_texture;\n"
"\n" "\n"
"varying vec2 v_uv;\n" "varying vec2 v_uv;\n"
"\n" "\n"
"void main() {\n" "void main() {\n"
" gl_FragColor = texture2D(u_texture, v_uv);\n" " gl_FragColor = texture2D(u_texture, v_uv);\n"
"}" "}"
}; };
return fragment_shader_source; return fragment_shader_source;
} }
TextureMaterial() : Material() { TextureMaterial() :
texture_location = 0; Material() {
texture = NULL; projection_matrix_location = 0;
} camera_matrix_location = 0;
model_view_matrix_location = 0;
GLint projection_matrix_location; texture_location = 0;
GLint model_view_matrix_location; 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 #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();