From eed6d1aa59d9ac10d3fbfd67c4659fd72be40621 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 19 Dec 2023 15:57:36 +0100 Subject: [PATCH] Initial test rendering setup. --- compile_linux.sh | 5 +++ game_application.h | 4 --- game_scene.cpp | 53 +++++++++++++++++++++++++++--- game_scene.h | 40 ++++++++++++---------- main.cpp | 6 ++-- sfw/application/application.cpp | 1 - sfw/application/camera.h | 2 +- sfw/application/colored_material.h | 44 ++++++++++++++----------- sfw/application/material.h | 1 - sfw/application/mesh.h | 2 +- sfw/application/mesh_instance.cpp | 6 ++-- sfw/application/mesh_instance.h | 3 +- sfw/application/shader.h | 2 +- sfw/application/texture.h | 1 - 14 files changed, 113 insertions(+), 57 deletions(-) diff --git a/compile_linux.sh b/compile_linux.sh index cb1fef4..62e2ff6 100755 --- a/compile_linux.sh +++ b/compile_linux.sh @@ -44,6 +44,10 @@ g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/scene.cpp - g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/window.cpp -o sfw/application/window.o +g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/shader.cpp -o sfw/application/shader.o +g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/material.cpp -o sfw/application/material.o +g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c sfw/application/mesh.cpp -o sfw/application/mesh.o + g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c game_scene.cpp -o game_scene.o g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.o @@ -54,6 +58,7 @@ g++ -Wall -lm -ldl -lpthread -lX11 -D_REENTRANT -g sfw/aabb.o sfw/basis.o sfw/c sfw/ustring.o sfw/vector2.o sfw/vector2i.o sfw/vector3.o sfw/vector3i.o sfw/vector4.o sfw/vector4i.o \ sfw/pool_vector.o sfw/pool_allocator.o sfw/mutex.o sfw/stime.o \ sfw/application/application.o sfw/application/scene.o sfw/application/window.o \ + sfw/application/shader.o sfw/application/material.o sfw/application/mesh.o \ game_scene.o main.o \ -o game diff --git a/game_application.h b/game_application.h index 9114a57..5e7fff4 100644 --- a/game_application.h +++ b/game_application.h @@ -4,15 +4,11 @@ #include "application.h" #include "game_scene.h" -#include "window.h" class GameApplication : public Application { public: GameApplication() : Application() { scene = new GameScene(); - - AppWindow *w = memnew(AppWindow()); - w->create(100, 0); } ~GameApplication() { diff --git a/game_scene.cpp b/game_scene.cpp index 92e4e25..5369405 100644 --- a/game_scene.cpp +++ b/game_scene.cpp @@ -3,6 +3,7 @@ #include "application.h" #include "3rd_glad.h" +#include "memory.h" //#include "camera.h" //#include "sprite.h" @@ -97,7 +98,7 @@ void GameScene::update(float delta) { } void GameScene::render() { - glClearColor(1.0f, 0.0f, 0.0f, 1.0f); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); //camera->bind(); @@ -105,14 +106,17 @@ void GameScene::render() { //tile_map->render(); //sprite->render(); + + material->bind(); + mesh->render(); } GameScene::GameScene() { /* left = false; - right = false; - up = false; - down = false; + right = false; + up = false; + down = false; camera = new OrthographicCamera(); camera->width = 16; @@ -165,6 +169,44 @@ GameScene::GameScene() { tile_map->build_mesh(); */ + + mesh = memnew(Mesh(2)); + material = memnew(ColoredMaterial()); + + mesh->clear(); + + //float width = 1; + //float height = 1; + + //float region_x = 0; + //float region_y = 0; + //float region_width = 1; + //float region_height = 1; + + //float w2 = width / 2.0; + //float h2 = height / 2.0; + + //mesh->add_uv(region_x, region_y); + //mesh->add_vertex2(-w2, h2); + + //mesh->add_uv(region_x + region_width, region_y + region_height); + //mesh->add_vertex2(w2, -h2); + + //mesh->add_uv(region_x, region_y + region_height); + //mesh->add_vertex2(-w2, -h2); + + //mesh->add_uv(region_x + region_width, region_y); + //mesh->add_vertex2(w2, h2); + + + mesh->add_vertex2(0, 0.5); + mesh->add_vertex2(-0.5, -0.5); + mesh->add_vertex2(0.5, -0.5); + + mesh->add_triangle(0, 1, 2); + //mesh->add_triangle(0, 1, 3); + + mesh->upload(); } GameScene::~GameScene() { @@ -175,4 +217,7 @@ GameScene::~GameScene() { delete tile_map; delete sprite; */ + + memdelete(mesh); + memdelete(material); } diff --git a/game_scene.h b/game_scene.h index e356a91..e08cca6 100644 --- a/game_scene.h +++ b/game_scene.h @@ -6,32 +6,38 @@ /* #include "camera.h" #include "mesh_instance.h" +#include "sprite.h" #include "texture.h" #include "texture_material.h" #include "tile_map.h" -#include "sprite.h" */ +#include "colored_material.h" +#include "mesh.h" + class GameScene : public Scene { public: - virtual void event(); - virtual void update(float delta); - virtual void render(); + virtual void event(); + virtual void update(float delta); + virtual void render(); - GameScene(); - ~GameScene(); -/* - bool left; - bool right; - bool up; - bool down; + GameScene(); + ~GameScene(); + /* + bool left; + bool right; + bool up; + bool down; - Camera *camera; - Texture *texture; - TextureMaterial *material; - TileMap *tile_map; - Sprite *sprite; - */ + Camera *camera; + Texture *texture; + TextureMaterial *material; + TileMap *tile_map; + Sprite *sprite; + */ + + Mesh *mesh; + ColoredMaterial *material; }; #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 1b23964..28aff15 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include "window.h" #include "game_application.h" +#include "window.h" Application *application = NULL; @@ -14,6 +15,9 @@ void handle_frame() { } int main(int argc, char **argv) { + AppWindow *w = memnew(AppWindow()); + w->create(100, 0); + application = new GameApplication(); #ifdef __EMSCRIPTEN__ @@ -24,8 +28,6 @@ int main(int argc, char **argv) { // application->main_loop(); //} - AppWindow *w = AppWindow::get_singleton(); - while (application->running) { if (w->frame_begin()) { //w->resize(); diff --git a/sfw/application/application.cpp b/sfw/application/application.cpp index b265d6e..86dfbbe 100644 --- a/sfw/application/application.cpp +++ b/sfw/application/application.cpp @@ -1,6 +1,5 @@ #include "application.h" -#include "opengl.h" #include #include "window.h" diff --git a/sfw/application/camera.h b/sfw/application/camera.h index 3c25872..269d99f 100644 --- a/sfw/application/camera.h +++ b/sfw/application/camera.h @@ -1,7 +1,7 @@ #ifndef CAMERA_H #define CAMERA_H -#include "opengl.h" +#include "3rd_glad.h" #include "./libs/glm/vec3.hpp" #include "./libs/glm/matrix.hpp" diff --git a/sfw/application/colored_material.h b/sfw/application/colored_material.h index 6ea0a4b..7a3d28b 100644 --- a/sfw/application/colored_material.h +++ b/sfw/application/colored_material.h @@ -3,10 +3,10 @@ #include "material.h" -#include "./libs/glm/vec4.hpp" -#include "./libs/glm/gtc/type_ptr.hpp" +#include "../../libs/glm/vec4.hpp" +#include "../../libs/glm/gtc/type_ptr.hpp" -#include "camera.h" +//#include "camera.h" class ColoredMaterial : public Material { public: @@ -15,42 +15,48 @@ 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); + //glUniform4f(tri_color_uniform_location, color.r, color.g, color.b, color.a); } void setup_uniforms() { - projection_matrix_location = get_uniform("u_proj_matrix"); - model_view_matrix_location = get_uniform("u_model_view_matrix"); + // projection_matrix_location = get_uniform("u_proj_matrix"); + // model_view_matrix_location = get_uniform("u_model_view_matrix"); - tri_color_uniform_location = get_uniform("fragment_color"); + //tri_color_uniform_location = get_uniform("fragment_color"); } + // "uniform mat4 u_proj_matrix;\n" + // "uniform mat4 u_model_view_matrix;\n" + // "\n" + //"attribute vec4 a_position;\n" + // "\n" + // "void main() {\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_model_view_matrix;\n" - "\n" "attribute vec4 a_position;\n" - "\n" "void main() {\n" - " gl_Position = u_proj_matrix * u_model_view_matrix * a_position;\n" + " gl_Position = a_position;\n" "}" }; return vertex_shader_source; } + //"precision mediump float;\n" + //"\n" + //"uniform vec4 fragment_color;\n" + //"\n" const GLchar **get_fragment_shader_source() { static const GLchar *fragment_shader_source[] = { - "precision mediump float;\n" - "\n" - "uniform vec4 fragment_color;\n" - "\n" + "void main() {\n" - " gl_FragColor = fragment_color;\n" + " gl_FragColor = vec4(1, 0, 0, 1);\n" "}" }; diff --git a/sfw/application/material.h b/sfw/application/material.h index 04c1780..0d0da93 100644 --- a/sfw/application/material.h +++ b/sfw/application/material.h @@ -1,7 +1,6 @@ #ifndef MATERIAL_H #define MATERIAL_H -#include "opengl.h" #include "shader.h" class Material { diff --git a/sfw/application/mesh.h b/sfw/application/mesh.h index a3bbfbf..6f6e75c 100644 --- a/sfw/application/mesh.h +++ b/sfw/application/mesh.h @@ -4,7 +4,7 @@ #include #include -#include "opengl.h" +#include "3rd_glad.h" class Mesh { public: diff --git a/sfw/application/mesh_instance.cpp b/sfw/application/mesh_instance.cpp index c8bdf70..e9bacd6 100644 --- a/sfw/application/mesh_instance.cpp +++ b/sfw/application/mesh_instance.cpp @@ -2,9 +2,9 @@ #include "camera.h" -#include "./libs/glm/vec3.hpp" -#include "./libs/glm/matrix.hpp" -#include "./libs/glm/gtc/matrix_transform.hpp" +#include "../../libs/glm/vec3.hpp" +#include "../../libs/glm/matrix.hpp" +#include "../../libs/glm/gtc/matrix_transform.hpp" void MeshInstance::render() { diff --git a/sfw/application/mesh_instance.h b/sfw/application/mesh_instance.h index a9e5741..ae21a41 100644 --- a/sfw/application/mesh_instance.h +++ b/sfw/application/mesh_instance.h @@ -3,11 +3,10 @@ #include -#include "opengl.h" #include "material.h" #include "mesh.h" -#include "./libs/glm/vec3.hpp" +#include "../../libs/glm/vec3.hpp" class MeshInstance { public: diff --git a/sfw/application/shader.h b/sfw/application/shader.h index a41ca8f..ce924f4 100644 --- a/sfw/application/shader.h +++ b/sfw/application/shader.h @@ -1,7 +1,7 @@ #ifndef SHADER_H #define SHADER_H -#include "opengl.h" +#include "3rd_glad.h" #include class Shader { diff --git a/sfw/application/texture.h b/sfw/application/texture.h index 35572f1..594f319 100644 --- a/sfw/application/texture.h +++ b/sfw/application/texture.h @@ -1,7 +1,6 @@ #ifndef TEXTURE_H #define TEXTURE_H -#include "opengl.h" #include class Texture {