Skip to content

Commit

Permalink
Simplified shader system to only reference one file
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 4, 2011
1 parent 2d90d84 commit 530c23b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 48 deletions.
18 changes: 2 additions & 16 deletions include/sc_shading.h
Expand Up @@ -2,17 +2,7 @@
Example usage:
sc_shader_t *sh = sc_new_shader();
if (!sc_shader_attach_from_file(sh, "simple.vert", SC_VERTEX_SHADER) ||
!sc_shader_attach_from_file(sh, "simple.frag", SC_FRAGMENT_SHADER) ||
!sc_shader_finalize(sh))
sc_error_make_critical();
The above code can also be written in abbreviated form with the
help of a builtin factory function which will automatically load
*.vert files as vertex shaders and *.frag as fragment shaders.
sc_shader_t *sh = sc_shader_from_file("simple");
sc_shader_t *sh = sc_shader_from_file("simple.shader");
Binding of shaders:
Expand Down Expand Up @@ -48,15 +38,11 @@ typedef struct _sc_shader sc_shader_t;
sc_shader_t *sc_new_shader(void);

/* simplified way to load shaders from files */
sc_shader_t *sc_shader_from_file(const char *basename);
sc_shader_t *sc_shader_from_file(const char *filename);

/* frees a shader */
void sc_free_shader(sc_shader_t *shader);

/* attaches a new shader module */
int sc_shader_attach_from_file(sc_shader_t *shader, const char *filename,
int type);

/* links all modules in the shader */
int sc_shader_finalize(sc_shader_t *shader);

Expand Down
24 changes: 24 additions & 0 deletions resources/shaders/loading.shader
@@ -0,0 +1,24 @@
varying vec3 N, L;
varying vec2 coord;
uniform sampler2D sc_texture;

#ifdef SC_VERTEX_SHADER
void
main(void)
{
coord = sc_texcoord;
gl_Position = sc_mvp_matrix * vec4(sc_vertex, 1.0);
N = sc_normal_matrix * sc_normal;
L = vec3(0.3, 3.0, 10.0);
}
#endif

#ifdef SC_FRAGMENT_SHADER
void
main(void)
{
float intensity = max(0.0, dot(normalize(N), normalize(L)));
gl_FragColor = texture2D(sc_texture, coord);
gl_FragColor.rgb *= intensity;
}
#endif
14 changes: 14 additions & 0 deletions resources/shaders/simple.frag → resources/shaders/simple.shader
Expand Up @@ -6,6 +6,19 @@ varying vec3 coord;
varying float vertex_light;
uniform sampler2DArray sc_texture;

#ifdef SC_VERTEX_SHADER
void
main(void)
{
coord = sc_texcoord3;
vertex_light = sc_vertex_light;
gl_Position = sc_mvp_matrix * vec4(sc_vertex, 1.0);
normal = sc_normal_matrix * sc_normal;
half_vec = normalize(sc_vertex + sc_sun_direction) / 2.0;
}
#endif

#ifdef SC_FRAGMENT_SHADER
void
main(void)
{
Expand All @@ -22,3 +35,4 @@ main(void)
color = color * clamp(darkness + (ambient + diffuse) * vertex_light, 0.0, 1.0);
gl_FragColor = clamp(color, 0.0, 1.0);
}
#endif
16 changes: 0 additions & 16 deletions resources/shaders/simple.vert

This file was deleted.

4 changes: 2 additions & 2 deletions src/game.c
Expand Up @@ -83,11 +83,11 @@ init_game(void)
sc_vec3_set(&cam->position, 0.0f, 30.0f, 100.0f);
sc_camera_look_at(cam, 0.0f, 0.0f, 0.0f);
loading_texture = sc_texture_from_resource("loading.png", 0);
loading_shader = sc_shader_from_file("loading");
loading_shader = sc_shader_from_file("loading.shader");

/* this has to happen in the main thread before anything else */
sc_init_blocks();
shader = sc_shader_from_file("simple");
shader = sc_shader_from_file("simple.shader");

load_thread = sc_new_thread(init_game_in_thread, &done);

Expand Down
25 changes: 11 additions & 14 deletions src/shading.c
Expand Up @@ -22,6 +22,7 @@ struct _sc_shader {
};

static const sc_shader_t *current_shader;
static int attach_from_file(sc_shader_t *, const char *, int);

static GLenum
convert_shader_type(int type)
Expand All @@ -45,28 +46,24 @@ sc_new_shader(void)
}

sc_shader_t *
sc_shader_from_file(const char *basename)
{
#define ATTACH(Suffix, Type) do { \
size_t bn_len = strlen(basename); \
size_t s_len = strlen(Suffix); \
char *buf = sc_xmalloc(bn_len + s_len + 1); \
strcpy(buf, basename); \
strcpy(buf + bn_len, Suffix); \
if (!sc_shader_attach_from_file(rv, buf, Type)) { \
sc_shader_from_file(const char *filename)
{
#define ATTACH(Type) do { \
if (!attach_from_file(rv, filename, Type)) { \
if (sc_get_errno() != SC_ENOENT) \
goto error; \
sc_clear_error(); \
} \
} while (0)

sc_shader_t *rv = sc_new_shader();
ATTACH(".vert", SC_VERTEX_SHADER);
ATTACH(".frag", SC_FRAGMENT_SHADER);
ATTACH(SC_VERTEX_SHADER);
ATTACH(SC_FRAGMENT_SHADER);
if (sc_shader_finalize(rv))
return rv;

error:
sc_free_shader(rv);
sc_error_make_critical();
return NULL;
}
Expand Down Expand Up @@ -131,6 +128,7 @@ read_shader_source(sc_strbuf_t *strbuf, sc_shader_t *shader,
source_found:
if (type) {
char *common_file = sc_path_to_resource("shaders", "common.shader");
sc_strbuf_appendf(strbuf, "#version 120\n");
switch (type) {
case SC_VERTEX_SHADER:
sc_strbuf_appendf(strbuf, "#define SC_VERTEX_SHADER\n");
Expand Down Expand Up @@ -182,9 +180,8 @@ set_shading_error(const sc_shader_t *shader, const char *info_log,
msg, source_info, info_log);
}

int
sc_shader_attach_from_file(sc_shader_t *shader, const char *filename,
int type)
static int
attach_from_file(sc_shader_t *shader, const char *filename, int type)
{
int success;
char *path;
Expand Down

0 comments on commit 530c23b

Please sign in to comment.