commit 14ce37ac0c869a05c8c4c8704a0d82fb3710b305
parent 4fee14c39475347ebcf15409f87cd440896b3fbd
Author: William Casarin <jb55@jb55.com>
Date: Sat, 9 Oct 2021 19:37:38 -0700
prepare rogue for grid rendering
Diffstat:
15 files changed, 121 insertions(+), 144 deletions(-)
diff --git a/.envrc b/.envrc
@@ -1,2 +1,2 @@
use nix
-export CC=tcc
+export CC=gcc
diff --git a/etc/shaders/rogue/grid.v.glsl b/etc/shaders/rogue/grid.v.glsl
@@ -0,0 +1,20 @@
+
+#include profile
+
+in vec3 position;
+in vec3 normal;
+
+#include rogue/uniforms.glsl
+
+uniform int is_white;
+
+out shader_data {
+#include shadervars.glsl
+} data_out;
+
+void main()
+{
+ vec3 color = is_white == 1 ? vec3(0.9, 0.9, 0.9) : vec3(0.3, 0.3, 0.3);
+#include standard_vtxos.glsl
+ gl_Position = mvp * v4_pos;
+}
diff --git a/etc/shaders/rogue/main.f.glsl b/etc/shaders/rogue/main.f.glsl
@@ -12,7 +12,7 @@ in shader_data {
void main() {
vec4 v4_pos = vec4(vertex.position, 1.0);
- vec3 color = v4_pos;
+ vec3 color = v4_pos.xyz;
frag_color = vec4(color, 1.0);
}
diff --git a/etc/shaders/rogue/uniforms.glsl b/etc/shaders/rogue/uniforms.glsl
@@ -3,4 +3,4 @@ precision highp int;
uniform mat4 mvp;
uniform mat4 model_view;
-
+uniform mat4 depth_mvp;
diff --git a/etc/shaders/rogue/vertex-color.glsl b/etc/shaders/rogue/vertex-color.glsl
@@ -0,0 +1,18 @@
+
+#include profile
+
+in vec3 position;
+in vec3 normal;
+in vec3 color;
+
+out shader_data {
+#include shadervars.glsl
+} data_out;
+
+#include rogue/uniforms.glsl
+
+void main()
+{
+#include standard_vtxos.glsl
+ gl_Position = mvp * v4_pos;
+}
diff --git a/etc/shaders/vertex-color.glsl b/etc/shaders/vertex-color.glsl
@@ -10,7 +10,6 @@ out shader_data {
#include uniforms.glsl
-
void main()
{
#include standard_vtxos.glsl
diff --git a/main.c b/main.c
@@ -35,6 +35,7 @@ int main(void)
int seed = 1589065328;
int width = 640;
int height = 480;
+ int ok;
/* int seed = 42; */
printf("seed %d\n", seed);
@@ -47,13 +48,15 @@ int main(void)
engine.seed = seed;
init_engine(&engine, width, height);
- init_rogue_game(&rogue_game);
+ if (!init_rogue_game(&engine, &rogue_game)) {
+ printf("failed to init game!\n");
+ return 1;
+ }
//init_test_game(&engine, &game);
//reset_scene(&engine);
//default_scene(&game);
//pbr_scene(&game);
- //chess_scene(&engine, NULL, &fragment);
check_gl();
double last = hires_time_in_seconds();
diff --git a/rogue/rogue.c b/rogue/rogue.c
@@ -6,13 +6,13 @@
#include "mat4.h"
#include "lens.h"
-enum rogue_shader_structs {
+enum shader_structs {
COMMON_VARS
};
#define CV_LENS(field, typ) { #field, offsetof(struct common_vars, field), DATA_ID_##typ }
-static struct lens common_vars[] = {
- //CV_LENS(depth_mvp, MAT4),
+static struct lens common_lenses[] = {
+ CV_LENS(depth_mvp, MAT4),
CV_LENS(mvp, MAT4),
CV_LENS(model_view, MAT4),
//CV_LENS(normal_matrix, MAT4),
@@ -21,6 +21,10 @@ static struct lens common_vars[] = {
};
#undef CV_LENS
+static struct structure_binding uniform_bindings[] = {
+ [COMMON_VARS] = { common_lenses, ARRAY_SIZE(common_lenses) },
+};
+
static void init_ortho(struct rogue_game *game)
{
float left = 0.0;
@@ -33,26 +37,65 @@ static void init_ortho(struct rogue_game *game)
mat4_ortho(left, right, bottom, top, near, far, game->ortho);
}
-void init_rogue_game(struct rogue_game *game)
+static int compile_shaders(struct gpu *gpu)
+{
+ struct shader fragment, vertex;
+
+ if (!make_shader(GL_FRAGMENT_SHADER, SHADER("rogue/main.f.glsl"), &fragment))
+ return 0;
+
+ if (!make_shader(GL_VERTEX_SHADER, SHADER("rogue/grid.v.glsl"), &vertex))
+ return 0;
+
+ return make_program("grid", &vertex, &fragment,
+ &gpu->programs[GRID_PROGRAM],
+ uniform_bindings,
+ ARRAY_SIZE(uniform_bindings));
+}
+
+static int init_grid(struct grid *grid)
+{
+ make_grid_geom(&grid->geom, 1.0);
+ return 1;
+}
+
+static void render_grid(struct grid *grid, struct gpu *gpu,
+ struct common_vars *cvars)
+{
+ struct gpu_program *program;
+
+ void *structures[] = {
+ [COMMON_VARS] = cvars
+ };
+
+ program = &gpu->programs[GRID_PROGRAM];
+
+ glUseProgram(program->handle); check_gl();
+ bind_uniforms(program, structures, 1); check_gl();
+ bind_geometry(&grid->geom, program); check_gl();
+ render_geometry(&grid->geom, program); check_gl();
+}
+
+int init_rogue_game(struct engine *engine, struct rogue_game *game)
{
int cell_size = 1.0;
init_ortho(game);
-
- init_id(&game->grid.geom_id);
- make_grid_geom(&game->grid.geom_id, cell_size);
+ if (!compile_shaders(&engine->gpu))
+ return 0;
+ return init_grid(&game->grid);
}
static void render(struct engine *engine, struct rogue_game *game, struct render_config *config) {
float gtmp[3];
- u32 num_entities;
+ struct geometry *geom;
struct gpu_program *program = NULL;
struct common_vars *cvars = &game->common_vars;
glEnable(GL_DEPTH_TEST);
- glClearColor( 0.5, 0.5, 0.5, 1.0 ); //clear background screen to black
+ glClearColor( 0.25, 0.25, 0.25, 1.0 ); //clear background screen to black
/* glClearColor( 0.5294f * adjust, 0.8078f * adjust, 0.9216f * adjust, 1.0f ); //clear background screen to black */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
@@ -67,9 +110,6 @@ static void render(struct engine *engine, struct rogue_game *game, struct render
mat4_id(cvars->model_view);
mat4 *projection = config->projection;
- struct entity *entities =
- get_all_entities(&num_entities, NULL);
-
/*
struct node *camera_node = get_node(&config->camera);
assert(camera_node);
@@ -86,6 +126,8 @@ static void render(struct engine *engine, struct rogue_game *game, struct render
// no camera for now
mat4_copy(projection, view_proj);
+ render_grid(&game->grid, &engine->gpu, cvars);
+
if (config->is_depth_pass) {
glDisable(GL_CULL_FACE);
//mat4_multiply(bias_matrix, view_proj, config->depth_vp);
@@ -94,56 +136,6 @@ static void render(struct engine *engine, struct rogue_game *game, struct render
glCullFace(GL_BACK);
}
- for (u32 i = 0; i < num_entities; ++i) {
- struct entity *entity = &entities[i];
- struct model *model = get_model(&entity->model_id);
- assert(model);
- struct node *node = get_node(&entity->node_id);
- assert(node);
-
- if (entity->flags & ENT_INVISIBLE)
- continue;
-
- if (config->is_depth_pass && !(entity->flags & ENT_CASTS_SHADOWS))
- continue;
-
- program = &engine->gpu.programs[model->shader];
-
- glUseProgram(program->handle);
- check_gl();
-
- mat4_multiply(view_proj, node->mat, cvars->mvp);
- mat4_copy(node->mat, cvars->model_view);
- //mat4_multiply(config->depth_vp, cvars->model_view, cvars->depth_mvp);
-
- void *shader_data[] = {
- [COMMON_VARS] = cvars
- };
-
- bind_uniforms(program, shader_data, ARRAY_SIZE(shader_data));
- check_gl();
-
- /*
- recalc_normals(program->uniforms[UNIFORM_NORMAL_MATRIX].location,
- model_view, normal_matrix);
-
- check_gl();
- */
-
- struct geometry *geo = get_geometry(&model->geom_id);
- /* debug("geo node %s\n", node->label); */
- assert(geo);
- render_geometry(geo, program);
- check_gl();
- }
-
- if (engine->gpu.wireframe) {
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
- }
- else {
- glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
- }
-
if (!config->is_depth_pass) {
//mat4_inverse((float*)camera, view);
mat4_remove_translations(view);
@@ -154,20 +146,6 @@ static void render(struct engine *engine, struct rogue_game *game, struct render
//if (config->draw_ui)
// render_ui(&game->ui, view);
-
- //player
- // y tho
-
- // terrain
-
- /* glUniformMatrix4fv(res->uniforms.mvp, 1, 0, mvp); */
- /* glUniformMatrix4fv(res->uniforms.model_view, 1, 0, id); */
- /* glUniformMatrix4fv(res->uniforms.world, 1, 0, id); */
- /* glUniformMatrix4fv(res->uniforms.normal_matrix, 1, 0, id); */
- /* recalc_normals(res->uniforms.normal_matrix, model_view, normal_matrix); */
- /* render_geom(res, geom, GL_TRIANGLES); */
- /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */
- /* render_geom(res, geom, GL_TRIANGLES); */
}
void rogue_frame(struct engine *engine, struct rogue_game *game)
diff --git a/rogue/rogue.h b/rogue/rogue.h
@@ -18,9 +18,13 @@ struct common_vars {
float *camera_position;
};
+enum shader_programs {
+ GRID_PROGRAM,
+};
+
struct grid
{
- struct geometry_id geom_id;
+ struct geometry geom;
};
struct rogue_game
@@ -36,5 +40,5 @@ struct rogue_game
float ortho[MAT4_ELEMS];
};
-void init_rogue_game(struct rogue_game *game);
+int init_rogue_game(struct engine *engine, struct rogue_game *game);
void rogue_frame(struct engine *engine, struct rogue_game *game);
diff --git a/src/chess.c b/src/chess.c
@@ -50,12 +50,10 @@ static void setup_pieces(struct node_id *chessboard, float cell_size)
pawn_model = new_model(&pawn_model_id, "pawn");
pawn_model->geom_id = pirate_officer->geom_id;
- pawn_model->shader = CHESS_PIECE_PROGRAM;
pawn_model->name = "pawn";
rook_model = new_model(&rook_model_id, "rook");
rook_model->geom_id = tower_model->geom_id;
- rook_model->shader = CHESS_PIECE_PROGRAM;
rook_model->name = "rook";
// pawns
@@ -109,44 +107,6 @@ static void setup_pieces(struct node_id *chessboard, float cell_size)
}
-static struct structure_binding *get_chess_piece_bindings(int *count)
-{
- static struct structure_binding bindings[2];
-
- bindings[0].lenses = get_common_lenses();
- bindings[0].num_lenses = get_common_lenses_length();
- assert(bindings[0].num_lenses > 1);
-
- bindings[1].lenses = chess_piece_lenses;
- bindings[1].num_lenses = ARRAY_SIZE(chess_piece_lenses);
-
- *count = 2;
-
- return bindings;
-}
-
-static struct gpu_program *make_chess_program(struct shader *fragment,
- struct gpu_program *program)
-{
- struct shader chess_piece_vertex;
- struct structure_binding *bindings;
-
- int num_bindings;
- int ok;
-
- ok = make_shader(GL_VERTEX_SHADER, SHADER("chess-piece.v.glsl"), &chess_piece_vertex);
- rtassert(ok, "chess-piece vertex shader");
- check_gl();
-
- bindings = get_chess_piece_bindings(&num_bindings);
-
- ok = make_program("chess-piece", &chess_piece_vertex, fragment, program,
- bindings, num_bindings);
- rtassert(ok, "chess-piece program");
- check_gl();
-
- return program;
-}
/* needs some fragment shader, pro
@@ -158,7 +118,7 @@ static struct gpu_program *make_chess_program(struct shader *fragment,
rtassert(fragment, "failed to get default fragment shader");
*/
-struct entity_id make_chessboard(struct gpu *gpu, struct shader *fragment)
+struct entity_id make_chessboard(struct gpu *gpu)
{
static const float cell_size = 10.0;
@@ -166,9 +126,9 @@ struct entity_id make_chessboard(struct gpu *gpu, struct shader *fragment)
struct geometry_id chessboard_geom_id;
struct model_id chessboard_model_id;
struct model *chessboard_model;
- struct gpu_program *program;
struct entity *chessboard;
struct node *node;
+ struct geometry *geom;
init_id(&chessboard_geom_id);
init_id(&chessboard_model_id);
@@ -176,10 +136,11 @@ struct entity_id make_chessboard(struct gpu *gpu, struct shader *fragment)
chessboard = new_entity(&chessboard_ent_id);
node = get_node(&chessboard->node_id);
+ geom = new_geometry(&chessboard_geom_id);
assert(node);
// setup chessboard
- make_grid_geom(&chessboard_geom_id, cell_size);
+ make_grid_geom(geom, cell_size);
chessboard_model = new_model(&chessboard_model_id, "chessboard");
chessboard_model->geom_id = chessboard_geom_id;
@@ -187,23 +148,22 @@ struct entity_id make_chessboard(struct gpu *gpu, struct shader *fragment)
chessboard->model_id = chessboard_model_id;
chessboard->flags &= ~ENT_CASTS_SHADOWS;
- program = new_gpu_program(gpu, "chess-piece");
- make_chess_program(fragment, program);
-
setup_pieces(&chessboard->node_id, cell_size);
return chessboard_ent_id;
}
-struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam, struct shader *fragment)
+struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam)
{
- struct entity_id chessboard = make_chessboard(gpu, fragment);
+ struct entity_id chessboard = make_chessboard(gpu);
// setup camera
- struct spherical *coords = &orbcam->coords;
- coords->radius = 72.0;
- coords->inclination = 0.5;
- coords->azimuth = -7.86;
+ if (orbcam) {
+ struct spherical *coords = &orbcam->coords;
+ coords->radius = 72.0;
+ coords->inclination = 0.5;
+ coords->azimuth = -7.86;
+ }
return chessboard;
}
diff --git a/src/chess.h b/src/chess.h
@@ -2,9 +2,8 @@
#ifndef POLYADVENT_CHESS_H
#define POLYADVENT_CHESS_H
-#include "test_game.h"
#include "engine.h"
-struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam, struct shader *fragment);
+struct entity_id chess_scene(struct gpu *gpu, struct orbit *orbcam);
#endif /* POLYADVENT_CHESS_H */
diff --git a/src/grid.c b/src/grid.c
@@ -77,7 +77,7 @@ static void make_grid(float *verts, float vert_capacity,
}
}
-void make_grid_geom(struct geometry_id *id, float cell_size)
+void make_grid_geom(struct geometry *geom, float cell_size)
{
struct make_geometry mkgeom;
init_make_geometry(&mkgeom);
@@ -101,8 +101,6 @@ void make_grid_geom(struct geometry_id *id, float cell_size)
mkgeom.num_verts = ARRAY_SIZE(verts)/3;
mkgeom.num_uv_components = 0;
- init_id(id);
- struct geometry *geom = new_geometry(id);
make_buffer_geometry(&mkgeom, geom);
check_gl();
}
diff --git a/src/grid.h b/src/grid.h
@@ -3,4 +3,4 @@
#include "geometry.h"
-void make_grid_geom(struct geometry_id *id, float cell_size);
+void make_grid_geom(struct geometry *geom, float cell_size);
diff --git a/src/model.c b/src/model.c
@@ -13,7 +13,6 @@ static struct resource_manager dyn_modelman;
struct model *init_model(struct model *model) {
init_id(&model->geom_id);
- model->shader = 0;
model->texture = 0;
model->nposes = 0;
for (u16 i = 0; i < ARRAY_SIZE(model->poses); i++) {
diff --git a/src/model.h b/src/model.h
@@ -20,7 +20,6 @@ struct model_id {
struct model {
/* geometry_id geom_id; */
struct geometry_id geom_id;
- int shader;
struct pose poses[MAX_POSES]; // TODO: separate animated_model buffer?
int nposes;
u32 texture;