Initial test rendering setup.

This commit is contained in:
Relintai 2023-12-19 15:57:36 +01:00
parent 3a6f715f35
commit eed6d1aa59
14 changed files with 113 additions and 57 deletions

View File

@ -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/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 game_scene.cpp -o game_scene.o
g++ -Wall -D_REENTRANT -g -Isfw -Isfw/application -c main.cpp -o main.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/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/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/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 \ game_scene.o main.o \
-o game -o game

View File

@ -4,15 +4,11 @@
#include "application.h" #include "application.h"
#include "game_scene.h" #include "game_scene.h"
#include "window.h"
class GameApplication : public Application { class GameApplication : public Application {
public: public:
GameApplication() : Application() { GameApplication() : Application() {
scene = new GameScene(); scene = new GameScene();
AppWindow *w = memnew(AppWindow());
w->create(100, 0);
} }
~GameApplication() { ~GameApplication() {

View File

@ -3,6 +3,7 @@
#include "application.h" #include "application.h"
#include "3rd_glad.h" #include "3rd_glad.h"
#include "memory.h"
//#include "camera.h" //#include "camera.h"
//#include "sprite.h" //#include "sprite.h"
@ -97,7 +98,7 @@ void GameScene::update(float delta) {
} }
void GameScene::render() { 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); glClear(GL_COLOR_BUFFER_BIT);
//camera->bind(); //camera->bind();
@ -105,14 +106,17 @@ void GameScene::render() {
//tile_map->render(); //tile_map->render();
//sprite->render(); //sprite->render();
material->bind();
mesh->render();
} }
GameScene::GameScene() { GameScene::GameScene() {
/* /*
left = false; left = false;
right = false; right = false;
up = false; up = false;
down = false; down = false;
camera = new OrthographicCamera(); camera = new OrthographicCamera();
camera->width = 16; camera->width = 16;
@ -165,6 +169,44 @@ GameScene::GameScene() {
tile_map->build_mesh(); 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() { GameScene::~GameScene() {
@ -175,4 +217,7 @@ GameScene::~GameScene() {
delete tile_map; delete tile_map;
delete sprite; delete sprite;
*/ */
memdelete(mesh);
memdelete(material);
} }

View File

@ -6,32 +6,38 @@
/* /*
#include "camera.h" #include "camera.h"
#include "mesh_instance.h" #include "mesh_instance.h"
#include "sprite.h"
#include "texture.h" #include "texture.h"
#include "texture_material.h" #include "texture_material.h"
#include "tile_map.h" #include "tile_map.h"
#include "sprite.h"
*/ */
#include "colored_material.h"
#include "mesh.h"
class GameScene : public Scene { class GameScene : public Scene {
public: public:
virtual void event(); virtual void event();
virtual void update(float delta); virtual void update(float delta);
virtual void render(); virtual void render();
GameScene(); GameScene();
~GameScene(); ~GameScene();
/* /*
bool left; bool left;
bool right; bool right;
bool up; bool up;
bool down; bool down;
Camera *camera; Camera *camera;
Texture *texture; Texture *texture;
TextureMaterial *material; TextureMaterial *material;
TileMap *tile_map; TileMap *tile_map;
Sprite *sprite; Sprite *sprite;
*/ */
Mesh *mesh;
ColoredMaterial *material;
}; };
#endif #endif

View File

@ -6,6 +6,7 @@
#include "window.h" #include "window.h"
#include "game_application.h" #include "game_application.h"
#include "window.h"
Application *application = NULL; Application *application = NULL;
@ -14,6 +15,9 @@ void handle_frame() {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
AppWindow *w = memnew(AppWindow());
w->create(100, 0);
application = new GameApplication(); application = new GameApplication();
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
@ -24,8 +28,6 @@ int main(int argc, char **argv) {
// application->main_loop(); // application->main_loop();
//} //}
AppWindow *w = AppWindow::get_singleton();
while (application->running) { while (application->running) {
if (w->frame_begin()) { if (w->frame_begin()) {
//w->resize(); //w->resize();

View File

@ -1,6 +1,5 @@
#include "application.h" #include "application.h"
#include "opengl.h"
#include <chrono> #include <chrono>
#include "window.h" #include "window.h"

View File

@ -1,7 +1,7 @@
#ifndef CAMERA_H #ifndef CAMERA_H
#define CAMERA_H #define CAMERA_H
#include "opengl.h" #include "3rd_glad.h"
#include "./libs/glm/vec3.hpp" #include "./libs/glm/vec3.hpp"
#include "./libs/glm/matrix.hpp" #include "./libs/glm/matrix.hpp"

View File

@ -3,10 +3,10 @@
#include "material.h" #include "material.h"
#include "./libs/glm/vec4.hpp" #include "../../libs/glm/vec4.hpp"
#include "./libs/glm/gtc/type_ptr.hpp" #include "../../libs/glm/gtc/type_ptr.hpp"
#include "camera.h" //#include "camera.h"
class ColoredMaterial : public Material { class ColoredMaterial : public Material {
public: public:
@ -15,42 +15,48 @@ 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);
} }
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"); // 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() { 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_model_view_matrix;\n"
"\n"
"attribute vec4 a_position;\n" "attribute vec4 a_position;\n"
"\n"
"void main() {\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; return vertex_shader_source;
} }
//"precision mediump float;\n"
//"\n"
//"uniform vec4 fragment_color;\n"
//"\n"
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"
"\n"
"uniform vec4 fragment_color;\n"
"\n"
"void main() {\n" "void main() {\n"
" gl_FragColor = fragment_color;\n" " gl_FragColor = vec4(1, 0, 0, 1);\n"
"}" "}"
}; };

View File

@ -1,7 +1,6 @@
#ifndef MATERIAL_H #ifndef MATERIAL_H
#define MATERIAL_H #define MATERIAL_H
#include "opengl.h"
#include "shader.h" #include "shader.h"
class Material { class Material {

View File

@ -4,7 +4,7 @@
#include <vector> #include <vector>
#include <inttypes.h> #include <inttypes.h>
#include "opengl.h" #include "3rd_glad.h"
class Mesh { class Mesh {
public: public:

View File

@ -2,9 +2,9 @@
#include "camera.h" #include "camera.h"
#include "./libs/glm/vec3.hpp" #include "../../libs/glm/vec3.hpp"
#include "./libs/glm/matrix.hpp" #include "../../libs/glm/matrix.hpp"
#include "./libs/glm/gtc/matrix_transform.hpp" #include "../../libs/glm/gtc/matrix_transform.hpp"
void MeshInstance::render() { void MeshInstance::render() {

View File

@ -3,11 +3,10 @@
#include <vector> #include <vector>
#include "opengl.h"
#include "material.h" #include "material.h"
#include "mesh.h" #include "mesh.h"
#include "./libs/glm/vec3.hpp" #include "../../libs/glm/vec3.hpp"
class MeshInstance { class MeshInstance {
public: public:

View File

@ -1,7 +1,7 @@
#ifndef SHADER_H #ifndef SHADER_H
#define SHADER_H #define SHADER_H
#include "opengl.h" #include "3rd_glad.h"
#include <unordered_map> #include <unordered_map>
class Shader { class Shader {

View File

@ -1,7 +1,6 @@
#ifndef TEXTURE_H #ifndef TEXTURE_H
#define TEXTURE_H #define TEXTURE_H
#include "opengl.h"
#include <SDL.h> #include <SDL.h>
class Texture { class Texture {