commit fabca1d3600c810f8bc8246c426b9147cacd8aed
parent 0a836ee9e2e9afcd5b5d9896c4f37899a85013c6
Author: William Casarin <jb55@jb55.com>
Date: Sat, 3 Oct 2020 17:26:25 -0700
uncommited stuff
Diffstat:
12 files changed, 119 insertions(+), 85 deletions(-)
diff --git a/etc/shaders/chess-piece.v.glsl b/etc/shaders/chess-piece.v.glsl
@@ -6,13 +6,15 @@ in vec3 normal;
#include uniforms.glsl
+uniform bool is_white;
+
out shader_data {
#include shadervars.glsl
} data_out;
void main()
{
- vec3 color = piece_color;
+ vec3 color = is_white ? 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/lighting.glsl b/etc/shaders/lighting.glsl
@@ -14,6 +14,20 @@
// return clouds;
// }
+
+vec3 hemispherical(vec3 color, vec3 normal) {
+ vec4 v4_normal = vec4(normal , 1);
+ vec4 trans_normal = normal_matrix * v4_normal;
+
+ vec3 l = light_dir;
+ vec3 n = normalize(trans_normal.xyz);
+
+ float costheta = dot(l,n);
+
+ float a = 0.5 + (0.5 * costheta);
+ return a * light_intensity * color + (1.0-a) * vec3(0.0, 0.0, 0.0) * color;
+}
+
const float PI = 3.14159265;
vec3 gamma_correct(vec3 color) {
diff --git a/etc/shaders/main.f.glsl b/etc/shaders/main.f.glsl
@@ -31,13 +31,14 @@ vec3 reflect_env(vec3 vert_pos) {
// return color;
// }
+
void main() {
vec3 V = camera_position - vertex.position;
vec4 v4_pos = vec4(vertex.position, 1.0);
vec4 v4_normal = vec4(vertex.normal, 1.0);
// vec3 color = standard_light(vertex.color, v4_pos, v4_normal);
- // vec3 color = reflect_env(vertex.position);
+ // vec3 color = reflect_env(vertex.color);
vec3 color = pbr(vertex.color, normalize(V), vertex.normal);
if (fog_on) {
diff --git a/etc/shaders/shadervars.glsl b/etc/shaders/shadervars.glsl
@@ -5,4 +5,3 @@ vec3 position;
// ivec4 joint_indices;
// vec3 weights;
vec4 shadow_coord;
-vec3 piece_color;
diff --git a/etc/shaders/standard_vtxos.glsl b/etc/shaders/standard_vtxos.glsl
@@ -4,6 +4,6 @@ vec4 v4_pos = vec4(position, 1.0);
data_out.normal = mat3(transpose(inverse(model))) * normal;
data_out.position = vec3(model * v4_pos);
-data_out.color_smooth = data_out.color = color + piece_color * 0.0000001;
+data_out.color_smooth = data_out.color = color;
data_out.shadow_coord = depth_mvp * v4_pos;
// TODO: move shadow coord calc from frag to here
diff --git a/etc/shaders/uniforms.glsl b/etc/shaders/uniforms.glsl
@@ -14,4 +14,3 @@ uniform vec3 sun_color;
uniform vec3 camera_position;
uniform vec3 light_dir;
-uniform vec3 piece_color;
diff --git a/src/geometry.c b/src/geometry.c
@@ -64,6 +64,7 @@ void render_geometry(struct geometry *geom, gpu_addr *vertex_attrs,
GL_UNSIGNED_INT, /* type */
(void*)0 /* element array buffer offset */
);
+ /* printf("render_geometry %s\n", program->name); */
check_gl();
}
else {
diff --git a/src/model.h b/src/model.h
@@ -17,16 +17,6 @@
typedef struct resource_id model_id;
-enum program_type {
- DEFAULT_PROGRAM,
- TERRAIN_PROGRAM,
- UI_PROGRAM,
- SKYBOX_PROGRAM,
- CHESS_PIECE_PROGRAM,
- NUM_PROGRAMS,
-};
-
-
struct model {
/* geometry_id geom_id; */
geometry_id geom_id;
diff --git a/src/render.c b/src/render.c
@@ -62,32 +62,6 @@ static const float bias_matrix[] = {
0.5, 0.5, 0.5, 1.0
};
-static void add_attribute(struct gpu_program *program,
- const char *name,
- enum vertex_attr attr)
-{
- int id = program->vertex_attrs[attr] =
- (gpu_addr)glGetAttribLocation(program->handle, name);
- /* assert(id != -1); */
- check_gl();
-}
-
-static void add_uniform(struct gpu_program *program,
- const char *name,
- enum uniform_id id)
-{
- struct uniform *var = &program->uniforms[id];
- var->name = name;
- var->id = id;
- var->location = glGetUniformLocation(program->handle, var->name);
- if (var->location == -1) {
- debug("warn: could not find uniform location: %s in program %s\n",
- var->name, program->name);
- }
- /* assert(var->location != -1); */
- check_gl();
-}
-
void init_gl(struct resources *resources, int width, int height) {
struct shader vertex, terrain_vertex, chess_piece_vertex, terrain_geom, fragment, fragment_smooth;
struct shader terrain_teval, terrain_tc;
@@ -125,9 +99,6 @@ void init_gl(struct resources *resources, int width, int height) {
5000,
resources->proj_persp);
- struct shader *terrain_shaders[] =
- { &terrain_vertex, &fragment };
-
struct gpu_program *programs = resources->programs;
struct gpu_program *program;
@@ -141,43 +112,12 @@ void init_gl(struct resources *resources, int width, int height) {
rtassert(ok, "vertex-color program");
check_gl();
+
program = &programs[CHESS_PIECE_PROGRAM];
ok = make_program("chess-piece", &chess_piece_vertex, &fragment, program);
rtassert(ok, "chess-piece program");
- add_uniform(program, "piece_color", UNIFORM_PIECE_COLOR);
-
- struct uniform *var;
- for (int i = 0; i < NUM_PROGRAMS; ++i) {
- struct gpu_program *program = &programs[i];
- if (program == NULL) {
- debug("program %d is NULL\n", i);
- continue;
- }
-
- if (program->name == NULL) {
- debug("program %d name is NULL, not initialized?\n", i);
- continue;
- }
-
- // Program variables
- add_uniform(program, "camera_position", UNIFORM_CAMERA_POSITION);
- add_uniform(program, "depth_mvp", UNIFORM_DEPTH_MVP);
- add_uniform(program, "light_intensity", UNIFORM_LIGHT_INTENSITY);
- add_uniform(program, "sky_intensity", UNIFORM_SKY_INTENSITY);
- add_uniform(program, "time", UNIFORM_TIME);
- add_uniform(program, "light_dir", UNIFORM_LIGHT_DIR);
- add_uniform(program, "sun_color", UNIFORM_SUN_COLOR);
- add_uniform(program, "fog_on", UNIFORM_FOG_ON);
- add_uniform(program, "model", UNIFORM_MODEL);
- add_uniform(program, "mvp", UNIFORM_MVP);
- add_uniform(program, "normal_matrix", UNIFORM_NORMAL_MATRIX);
-
- // Attributes
- add_attribute(program, "normal", va_normal);
- add_attribute(program, "position", va_position);
- add_attribute(program, "color", va_color);
- }
+ find_uniforms(programs);
}
@@ -304,10 +244,8 @@ void render (struct game *game, struct render_config *config) {
uniform_3f(program, UNIFORM_CAMERA_POSITION, &camera[M_X]);
if (model->shader == CHESS_PIECE_PROGRAM) {
- if (entity->flags & ENT_IS_WHITE)
- uniform_3f(program, UNIFORM_PIECE_COLOR, V3(0.9f, 0.9f, 0.9f));
- else
- uniform_3f(program, UNIFORM_PIECE_COLOR, V3(0.2f, 0.2f, 0.2f));
+ uniform_1i(program, UNIFORM_IS_WHITE,
+ (entity->flags & ENT_IS_WHITE) == ENT_IS_WHITE);
}
uniform_1i(program, UNIFORM_FOG_ON, res->fog_on);
diff --git a/src/shader.c b/src/shader.c
@@ -149,8 +149,80 @@ void init_gpu_program(struct gpu_program *program) {
memset(program, 0, sizeof(*program));
}
+void add_attribute(struct gpu_program *program, const char *name,
+ enum vertex_attr attr)
+{
+ program->vertex_attrs[attr] =
+ (gpu_addr)glGetAttribLocation(program->handle, name);
+ check_gl();
+}
+
+void add_uniform(struct gpu_program *program,
+ const char *name,
+ enum uniform_id id)
+{
+ struct uniform *var = &program->uniforms[id];
+ var->name = name;
+ var->id = id;
+ var->location = glGetUniformLocation(program->handle, var->name);
+ if (var->location == -1) {
+ debug("warn: could not find uniform location: %s in program %s\n",
+ var->name, program->name);
+ }
+ /* assert(var->location != -1); */
+ check_gl();
+}
+
+void find_program_uniforms(struct gpu_program *program,
+ struct gpu_program *programs)
+{
+ // Program variables
+ add_uniform(program, "camera_position", UNIFORM_CAMERA_POSITION);
+ add_uniform(program, "depth_mvp", UNIFORM_DEPTH_MVP);
+ add_uniform(program, "light_intensity", UNIFORM_LIGHT_INTENSITY);
+ add_uniform(program, "sky_intensity", UNIFORM_SKY_INTENSITY);
+ add_uniform(program, "time", UNIFORM_TIME);
+ add_uniform(program, "light_dir", UNIFORM_LIGHT_DIR);
+ add_uniform(program, "sun_color", UNIFORM_SUN_COLOR);
+ add_uniform(program, "fog_on", UNIFORM_FOG_ON);
+ add_uniform(program, "model", UNIFORM_MODEL);
+ add_uniform(program, "mvp", UNIFORM_MVP);
+ add_uniform(program, "normal_matrix", UNIFORM_NORMAL_MATRIX);
+
+ // Attributes
+ add_attribute(program, "normal", va_normal);
+ add_attribute(program, "position", va_position);
+ add_attribute(program, "color", va_color);
+
+ // chess stuff
+ if (program == &programs[CHESS_PIECE_PROGRAM]) {
+ add_uniform(program, "is_white",
+ UNIFORM_IS_WHITE);
+ }
+}
+
+void find_uniforms(struct gpu_program *programs)
+{
+ for (int i = 0; i < NUM_PROGRAMS; ++i) {
+ struct gpu_program *program = &programs[i];
+ if (program == NULL) {
+ debug("program %d is NULL\n", i);
+ continue;
+ }
+
+ if (program->name == NULL) {
+ debug("program %d name is NULL, not initialized?\n", i);
+ continue;
+ }
+
+ find_program_uniforms(program, programs);
+ }
+
+}
+
#ifdef DEBUG
-int reload_program(struct gpu_program *program) {
+int reload_program(struct gpu_program *program,
+ struct gpu_program *programs) {
int ok;
int n_shaders = program->n_shaders;
@@ -210,6 +282,8 @@ int reload_program(struct gpu_program *program) {
}
*program = new_program;
+
+ find_program_uniforms(program, programs);
return 1;
}
#endif
diff --git a/src/shader.h b/src/shader.h
@@ -21,6 +21,16 @@ struct shader {
time_t load_mtime;
};
+enum program_type {
+ DEFAULT_PROGRAM,
+ TERRAIN_PROGRAM,
+ UI_PROGRAM,
+ SKYBOX_PROGRAM,
+ CHESS_PIECE_PROGRAM,
+ NUM_PROGRAMS,
+};
+
+
enum uniform_id {
UNIFORM_AMBIENT_STR,
UNIFORM_CAMERA_POSITION,
@@ -38,7 +48,7 @@ enum uniform_id {
UNIFORM_SUN_COLOR,
UNIFORM_TIME,
UNIFORM_VIEW_PROJ,
- UNIFORM_PIECE_COLOR,
+ UNIFORM_IS_WHITE,
MAX_UNIFORMS
};
@@ -57,11 +67,17 @@ struct gpu_program {
const char *name;
};
+void add_uniform(struct gpu_program *program, const char *name, enum uniform_id id);
+void add_attribute(struct gpu_program *program, const char *name, enum vertex_attr attr);
+
#define NO_GEOM_SHADER NULL
-int reload_program(struct gpu_program *program);
+int reload_program(struct gpu_program *program, struct gpu_program *programs);
int make_shader(GLenum type, const char *filename, struct shader *shader);
+void find_uniforms(struct gpu_program *programs);
+void find_program_uniforms(struct gpu_program *program,
+ struct gpu_program *programs);
void init_gpu_program(struct gpu_program *program);
int make_program_from_shaders(const char *name,
struct shader **shaders,
diff --git a/src/update.c b/src/update.c
@@ -157,7 +157,7 @@ static int try_reload_shaders(struct resources *res) {
int ret;
for (int i = 0; i < NUM_PROGRAMS; ++i) {
struct gpu_program *program = &res->programs[i];
- ret = reload_program(program);
+ ret = reload_program(program, res->programs);
if (ret == 2) {}
else if (ret == 1)