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,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<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() {
}
void PerspectiveCamera::bind() {
Camera::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,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

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,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

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

@ -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<int, Shader *> shaders;
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,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

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