aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-05-28 16:19:50 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-05-28 16:19:50 +0000
commit66450800a7ff01f67ae72a79570decbfbd42c0a8 (patch)
tree61d61646a22a2c5124c991df0077bba41ae52227
parent2295e1879d9c5ef869c9c5e3b1714fec0c67d799 (diff)
Fun with shaders
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@48 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--data/Makefile.am2
-rw-r--r--data/models/platform42
-rw-r--r--data/shaders/lighting.frag13
-rw-r--r--data/shaders/swirlytron.frag19
-rw-r--r--data/shaders/swirlytron.vert24
-rw-r--r--src/model.c3
-rw-r--r--src/render.c56
-rw-r--r--src/types.h29
8 files changed, 147 insertions, 41 deletions
diff --git a/data/Makefile.am b/data/Makefile.am
index 91cde53..cbeb059 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -3,7 +3,7 @@ models_DATA = models/floor models/lander models/randombox models/walle models/wa
models/ceiling models/tiledfloor
shadersdir = $(datadir)/thrust3d/shaders
-shaders_DATA = shaders/lighting.vert shaders/lighting.frag
+shaders_DATA = shaders/lighting.vert shaders/lighting.frag shaders/swirlytron.vert shaders/swirlytron.frag
texturesdir = $(datadir)/thrust3d/textures
textures_DATA = textures/floor1.png textures/tiledwall.png textures/radioactive.png textures/fuel.png
diff --git a/data/models/platform b/data/models/platform
index da92c34..7728c67 100644
--- a/data/models/platform
+++ b/data/models/platform
@@ -1,30 +1,30 @@
# Landing/refuelling platform
QUADS
- 1.00 1.00 0.05
- -1.00 1.00 0.05
- -1.00 -1.00 0.05
- 1.00 -1.00 0.05
+ 1.00 1.00 0.05 0.0 0.0
+ -1.00 1.00 0.05 1.0 0.0
+ -1.00 -1.00 0.05 1.0 1.0
+ 1.00 -1.00 0.05 0.0 1.0
#
- 1.00 1.00 0.00
- -1.00 1.00 0.00
- -1.00 1.00 0.05
- 1.00 1.00 0.05
+ 1.00 1.00 0.00 0.0 0.0
+ -1.00 1.00 0.00 1.0 0.0
+ -1.00 1.00 0.05 1.0 0.0
+ 1.00 1.00 0.05 0.0 0.0
#
- 1.00 -1.00 0.05
- -1.00 -1.00 0.05
- -1.00 -1.00 0.00
- 1.00 -1.00 0.00
+ 1.00 -1.00 0.05 0.0 1.0
+ -1.00 -1.00 0.05 1.0 1.0
+ -1.00 -1.00 0.00 1.0 1.0
+ 1.00 -1.00 0.00 0.0 1.0
#
- 1.00 1.00 0.05
- 1.00 -1.00 0.05
- 1.00 -1.00 0.00
- 1.00 1.00 0.00
+ 1.00 1.00 0.05 0.0 0.0
+ 1.00 -1.00 0.05 0.0 1.0
+ 1.00 -1.00 0.00 0.0 1.0
+ 1.00 1.00 0.00 0.0 0.0
#
- -1.00 1.00 0.00
- -1.00 -1.00 0.00
- -1.00 -1.00 0.05
- -1.00 1.00 0.05
+ -1.00 1.00 0.00 1.0 0.0
+ -1.00 -1.00 0.00 1.0 1.0
+ -1.00 -1.00 0.05 1.0 1.0
+ -1.00 1.00 0.05 1.0 0.0
#
-pulse 0.7 0.0 0.0
+swirly
diff --git a/data/shaders/lighting.frag b/data/shaders/lighting.frag
index 1008734..265f494 100644
--- a/data/shaders/lighting.frag
+++ b/data/shaders/lighting.frag
@@ -19,6 +19,7 @@ varying vec3 light2hvc;
uniform sampler2D texture;
uniform bool fill_light_enabled;
uniform bool texture_enabled;
+uniform bool texture_only;
varying vec3 col_ambi_diff;
varying vec3 col_emit;
@@ -55,10 +56,14 @@ void main() {
spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[2].specular.rgb * pow(ndothv, 80.0);
}
- gl_FragColor = vec4(min(emit.r + ambi.r + diff.r + spec.r, 1.0),
- min(emit.g + ambi.g + diff.g + spec.g, 1.0),
- min(emit.b + ambi.b + diff.b + spec.b, 1.0),
- 1.0);
+ if ( !texture_only ) {
+ gl_FragColor = vec4(min(emit.r + ambi.r + diff.r + spec.r, 1.0),
+ min(emit.g + ambi.g + diff.g + spec.g, 1.0),
+ min(emit.b + ambi.b + diff.b + spec.b, 1.0),
+ 1.0);
+ } else {
+ gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
+ }
if ( texture_enabled ) {
gl_FragColor *= texture2D(texture, gl_TexCoord[0].st);
diff --git a/data/shaders/swirlytron.frag b/data/shaders/swirlytron.frag
new file mode 100644
index 0000000..d152636
--- /dev/null
+++ b/data/shaders/swirlytron.frag
@@ -0,0 +1,19 @@
+/*
+ * swirlytron.vert
+ *
+ * Swirly stuff for rendering onto swirly things...
+ *
+ * (c) 2007-2008 Thomas White <taw27@cam.ac.uk>
+ *
+ * thrust3d - a silly game
+ *
+ */
+
+varying vec2 coords;
+
+void main() {
+
+ gl_FragColor = vec4(coords.x, 0.0, coords.y, 1.0);
+
+}
+
diff --git a/data/shaders/swirlytron.vert b/data/shaders/swirlytron.vert
new file mode 100644
index 0000000..0c6dc73
--- /dev/null
+++ b/data/shaders/swirlytron.vert
@@ -0,0 +1,24 @@
+/*
+ * swirlytron.vert
+ *
+ * Swirly stuff for rendering onto swirly things...
+ *
+ * (c) 2007-2008 Thomas White <taw27@cam.ac.uk>
+ *
+ * thrust3d - a silly game
+ *
+ */
+
+varying vec2 coords;
+
+void main() {
+
+ coords.x = (gl_Vertex.x + 1.0)/2.0;
+ coords.y = (gl_Vertex.y + 1.0)/2.0;
+
+ /* Coordinates */
+ gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
+ gl_Position = ftransform();
+
+}
+
diff --git a/src/model.c b/src/model.c
index 8e1a4ef..30bc289 100644
--- a/src/model.c
+++ b/src/model.c
@@ -415,6 +415,9 @@ static int model_load(ModelContext *ctx, const char *name, RenderContext *render
if ( sscanf(line, "shiny %f", &shininess) == 1 ) {
attribs = attribs | ATTRIB_SHINY;
}
+ if ( strncmp(line, "swirly", 6) == 0 ) {
+ attribs = attribs | ATTRIB_SWIRLY;
+ }
if ( strncmp(line, "texture", 7) == 0 ) {
if ( strlen(line) < 9 ) {
fprintf(stderr, "Invalid texture specification\n");
diff --git a/src/render.c b/src/render.c
index 3d92f31..eda88e2 100644
--- a/src/render.c
+++ b/src/render.c
@@ -88,6 +88,7 @@ static int render_validate_shader(GLhandleARB shader) {
static void render_load_shaders(RenderContext *r) {
+ /* Lighting */
r->lighting_vert = render_load_shader(DATADIR"/shaders/lighting.vert", GL_VERTEX_SHADER_ARB);
r->lighting_frag = render_load_shader(DATADIR"/shaders/lighting.frag", GL_FRAGMENT_SHADER_ARB);
r->lighting_program = glCreateProgramObjectARB();
@@ -96,6 +97,16 @@ static void render_load_shaders(RenderContext *r) {
glLinkProgramARB(r->lighting_program);
render_validate_shader(r->lighting_program);
+ /* Swirlyness */
+ r->swirly_vert = render_load_shader(DATADIR"/shaders/swirlytron.vert", GL_VERTEX_SHADER_ARB);
+ r->swirly_frag = render_load_shader(DATADIR"/shaders/swirlytron.frag", GL_FRAGMENT_SHADER_ARB);
+ r->swirly_program = glCreateProgramObjectARB();
+ glAttachObjectARB(r->swirly_program, r->swirly_vert);
+ glAttachObjectARB(r->swirly_program, r->swirly_frag);
+ glLinkProgramARB(r->swirly_program);
+ render_validate_shader(r->swirly_program);
+
+
}
static void render_delete_shaders(RenderContext *r) {
@@ -163,6 +174,18 @@ RenderContext *render_setup(int width, int height) {
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, r->fbodepth);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+ /* FBO for rendering swirlyness */
+ glGenFramebuffersEXT(1, &r->swirly_fbo);
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo);
+ /* Add a (texture) colour buffer to the FBO */
+ glGenTextures(1, &r->swirly_texture);
+ glBindTexture(GL_TEXTURE_2D, r->swirly_texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r->swirly_texture, 0);
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+
/* Create coordinates for a hemisphere to reuse later */
r->hemisphere_v = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat));
r->hemisphere_n = malloc(3*HEMI_NUM_VERTICES*sizeof(GLfloat));
@@ -275,15 +298,17 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC
glRotatef(rad2deg(instance->yaw), 0.0, 0.0, -1.0); /* Minus sign defines +yaw as "right" */
/* Texture */
- if ( p->texture != NULL ) {
+ if ( p->attribs & ATTRIB_SWIRLY ) {
+ glBindTexture(GL_TEXTURE_2D, r->swirly_texture);
+ glEnable(GL_TEXTURE_2D);
+ glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_only"), 1);
+ } else if ( p->texture != NULL ) {
Texture *texture;
texture = texture_lookup(r, p->texture);
if ( texture != NULL ) {
glBindTexture(GL_TEXTURE_2D, texture->texname);
glEnable(GL_TEXTURE_2D);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_enabled"), 1);
-
} else {
glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_enabled"), 0);
}
@@ -307,7 +332,11 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC
glDisable(GL_TEXTURE_2D);
glPopMatrix();
-
+
+ if ( p->attribs & ATTRIB_SWIRLY ) {
+ glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_only"), 0);
+ }
+
} else {
glPushMatrix();
@@ -343,7 +372,7 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC
glPopMatrix();
}
-
+
}
return 0;
@@ -575,6 +604,22 @@ void render_draw(Game *game, Uint32 t) {
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
render_draw_stuff(game, t);
#endif
+
+ /* Render some swirlyness */
+ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo);
+ glClear(GL_COLOR_BUFFER_BIT);
+ glViewport(0, 0, 256, 256);
+ glUseProgramObjectARB(r->swirly_program);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ glBegin(GL_QUADS);
+ glVertex2f(-1.0, -1.0);
+ glVertex2f(+1.0, -1.0);
+ glVertex2f(+1.0, +1.0);
+ glVertex2f(-1.0, +1.0);
+ glEnd();
/* Second pass: Main view */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
@@ -593,6 +638,7 @@ void render_draw(Game *game, Uint32 t) {
sqrtf(2.0)*cosf(game->lander->yaw)*sinf(game->view_angle), sqrtf(2.0)*cosf(game->view_angle));
glUseProgramObjectARB(r->lighting_program);
glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0);
+ glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture_only"), 0);
glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture_enabled"), 1);
glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "fill_light_enabled"), 0);
render_setup_lighting(game);
diff --git a/src/types.h b/src/types.h
index 6d772f5..ebf1f82 100644
--- a/src/types.h
+++ b/src/types.h
@@ -34,6 +34,7 @@ typedef enum {
ATTRIB_PULSE = 1<<1, /* Pulsating colour */
ATTRIB_RADIUS = 1<<2, /* Radius is set */
ATTRIB_SHINY = 1<<3, /* Primitive is shiny */
+ ATTRIB_SWIRLY = 1<<4, /* Primitive is texture with swirlyness */
} PrimitiveAttrib;
typedef enum {
@@ -114,20 +115,28 @@ typedef struct {
GLhandleARB lighting_vert;
GLhandleARB lighting_frag;
GLhandleARB lighting_program;
-
+
/* Textures */
Texture textures[MAX_TEXTURES];
unsigned int num_textures;
+
+ GLuint fbo;
+ GLuint fbotex;
+ GLuint fbodepth;
- GLuint fbo;
- GLuint fbotex;
- GLuint fbodepth;
- GLfloat aspect;
- int width;
- int height;
-
- GLfloat *hemisphere_v;
- GLfloat *hemisphere_n;
+ /* Swirlyness stuff */
+ GLhandleARB swirly_vert;
+ GLhandleARB swirly_frag;
+ GLhandleARB swirly_program;
+ GLuint swirly_fbo;
+ GLuint swirly_texture;
+
+ GLfloat aspect;
+ int width;
+ int height;
+
+ GLfloat *hemisphere_v;
+ GLfloat *hemisphere_n;
} RenderContext;