diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-16 00:13:29 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-16 00:13:29 +0000 |
commit | 17a153e21767fcc7a67186dd7ef8cdd0a7e26ed2 (patch) | |
tree | dbadcfb1a615f355f030add6c4df45563dd5d38f /src | |
parent | 789361fb972673d5c1ecca77c4ffb5242ecd493f (diff) |
Postprocessing stuff
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@16 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'src')
-rw-r--r-- | src/render.c | 176 | ||||
-rw-r--r-- | src/types.h | 9 |
2 files changed, 86 insertions, 99 deletions
diff --git a/src/render.c b/src/render.c index ed28be2..ac79fc5 100644 --- a/src/render.c +++ b/src/render.c @@ -65,6 +65,13 @@ static void render_load_shaders(RenderContext *ctx) { glAttachObjectARB(ctx->lighting_program, ctx->lighting_vert); glAttachObjectARB(ctx->lighting_program, ctx->lighting_frag); glLinkProgramARB(ctx->lighting_program); + + ctx->postprocess_frag = render_load_shader(DATADIR"/shaders/postprocess.frag", GL_FRAGMENT_SHADER_ARB); + ctx->postprocess_program = glCreateProgramObjectARB(); + glAttachObjectARB(ctx->postprocess_program, ctx->postprocess_frag); + glLinkProgramARB(ctx->postprocess_program); + + glUniform1iARB(glGetUniformLocationARB(ctx->postprocess_program, "texture"), 0); } @@ -76,6 +83,10 @@ static void render_delete_shaders(RenderContext *ctx) { glDeleteObjectARB(ctx->lighting_frag); glDeleteObjectARB(ctx->lighting_program); + glDetachObjectARB(ctx->postprocess_program, ctx->postprocess_frag); + glDeleteObjectARB(ctx->postprocess_frag); + glDeleteObjectARB(ctx->postprocess_program); + } /* OpenGL initial setup */ @@ -87,11 +98,9 @@ RenderContext *render_setup(width, height) { if ( ctx == NULL ) return NULL; glClearColor(0.0, 0.0, 0.0, 1.0); - glViewport(0, 0, width, height); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(50.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0); /* Depth buffer 10cm to 100m */ + ctx->aspect = (GLfloat)width/(GLfloat)height; + ctx->width = width; + ctx->height = height; glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); @@ -104,9 +113,23 @@ RenderContext *render_setup(width, height) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); + /* Generate FBO */ + glGenFramebuffersEXT(1, &ctx->fbo); + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, ctx->fbo); + /* Add a (texture) colour buffer to the FBO */ + glGenTextures(1, &ctx->fbotex); + glBindTexture(GL_TEXTURE_2D, ctx->fbotex); + 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, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, ctx->fbotex, 0); + /* Add a depth buffer to the FBO */ + glGenRenderbuffersEXT(1, &ctx->fbodepth); + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ctx->fbodepth); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, 1024, 1024); + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, ctx->fbodepth); + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, ctx->fbodepth); + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); render_load_shaders(ctx); @@ -192,82 +215,6 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC } -#if 0 -static void render_configure_light(int *ilightp, Light light, Room *room, Game *game) { - - GLfloat x, y, z; - int ilight; - - GLfloat pos[4]; - GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; - GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; - - ilight = *ilightp; - - x = room->rx - game->cur_room_x; - y = room->ry - game->cur_room_y; - z = room->rz - game->cur_room_z; - glPushMatrix(); - glTranslatef(10.0*x, 10.0*y, 10.0*z); - - pos[0] = light.x; - pos[1] = light.y; - pos[2] = light.z; - pos[3] = 1.0; - - if ( ilight < GL_MAX_LIGHTS ) { - glLightfv(GL_LIGHT0+ilight, GL_POSITION, pos); - glLightfv(GL_LIGHT0+ilight, GL_DIFFUSE, diffuse); - glLightfv(GL_LIGHT0+ilight, GL_SPECULAR, specular); - glLightf(GL_LIGHT0+ilight, GL_LINEAR_ATTENUATION, 0.01); - glEnable(GL_LIGHT0+ilight); - ilight++; - } - - glPopMatrix(); - - *ilightp = ilight; - -} - -static void render_setup_lighting(Game *game, Room *room_current) { - - int i, ilight; - GLfloat ambient[4]; - - /* Start with a blackout */ - for ( ilight=0; ilight<GL_MAX_LIGHTS; ilight++ ) { - glDisable(GL_LIGHT0+ilight); - } - ilight = 0; - - /* Lights within this room */ - for ( i=0; i<room_current->num_lights; i++ ) { - render_configure_light(&ilight, room_current->lights[i], room_current, game); - } - - /* Lights in rooms connected to this one */ - for ( i=0; i<room_current->num_connected; i++ ) { - - Room *room; - int j; - - room = game_find_room(game, room_current->connected[i].rx, room_current->connected[i].ry, room_current->connected[i].rz); - if ( room == NULL ) continue; /* Shouldn't happen... */ - - for ( j=0; j<room->num_lights; j++ ) { - render_configure_light(&ilight, room->lights[j], room, game); - } - - } - - ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0; - //ambient[0] = 1.0; ambient[1] = 1.0; ambient[2] = 1.0; ambient[3] = 1.0; - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); - -} -#endif - static void render_draw_line(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2) { GLfloat red[] = {1.0, 0.0, 0.0, 1.0}; @@ -289,7 +236,9 @@ void render_draw(Game *game) { t = SDL_GetTicks(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, game->render->aspect, 0.1, 100.0); /* Depth buffer 10cm to 100m */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -299,13 +248,6 @@ void render_draw(Game *game) { game->lander->x, game->lander->y, game->lander->z, sqrtf(2.0)*sinf(game->lander->yaw)*sinf(game->view_angle), sqrtf(2.0)*cosf(game->lander->yaw)*sinf(game->view_angle), sqrtf(2.0)*cosf(game->view_angle)); - //gluLookAt(game->lander->x-(1.5*sinf(game->lander->yaw)), - // game->lander->y-(1.5*cosf(game->lander->yaw)), 1.5+game->lander->z, - // game->lander->x, game->lander->y, game->lander->z, - // sqrt(2.0)*sinf(game->lander->yaw), sqrtf(2.0)*cosf(game->lander->yaw), sqrtf(2.0)); - //gluLookAt(0.0, -250.0, -495.0+250.0, - // 0.0, 0.0, -495.0, - // 0.0, sqrtf(2.0), sqrtf(2.0)); GLfloat pos[] = {-1.0, -0.8, 1.3, 0.0}; GLfloat ambient[4]; @@ -318,9 +260,17 @@ void render_draw(Game *game) { glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glEnable(GL_LIGHT0); - /* Draw the objects */ - //glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0); - //glUseProgramObjectARB(game->render->lighting_program); + /* Render to FBO */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, game->render->fbo); + glViewport(0, 0, 1024, 1024); + glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0); +// glUseProgramObjectARB(game->render->lighting_program); + glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); for ( i=0; i<game->num_rooms; i++ ) { Room *room; @@ -329,8 +279,6 @@ void render_draw(Game *game) { room = game->rooms[i]; if ( room == NULL ) return; - //render_setup_lighting(game, room); - for ( j=0; j<room->num_objects; j++ ) { GLfloat x, y, z; if ( room->objects[j] == NULL ) continue; @@ -347,12 +295,42 @@ void render_draw(Game *game) { /* Finally, the lander */ render_model_instance_draw(game->lander, t, game->render); render_draw_line(game->lander->x, game->lander->y, game->lander->z, game->lander->x, game->lander->y, game->lander->z-200.0); - //glUseProgramObjectARB(0); + glPopClientAttrib(); +// glUseProgramObjectARB(0); + /* Final render to screen */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + glViewport(0, 0, game->render->width, game->render->height); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -20.0); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-game->render->aspect*3.0, game->render->aspect*3.0, -3.0, 3.0, 0.001, 21.0); + glDisable(GL_LIGHTING); + glBindTexture(GL_TEXTURE_2D, game->render->fbotex); + glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glEnable(GL_TEXTURE_2D); + glColor3f(1.0, 1.0, 1.0); +// glUseProgramObjectARB(game->render->postprocess_program); + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); + glVertex3f(-3.0*game->render->aspect, -3.0*game->render->aspect, 0.0); + glTexCoord2f(1.0, 0.0); + glVertex3f(+3.0*game->render->aspect, -3.0*game->render->aspect, 0.0); + glTexCoord2f(1.0, 1.0); + glVertex3f(+3.0*game->render->aspect, +3.0*game->render->aspect, 0.0); + glTexCoord2f(0.0, 1.0); + glVertex3f(-3.0*game->render->aspect, +3.0*game->render->aspect, 0.0); + glEnd(); +// glUseProgramObjectARB(0); + glDisable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); SDL_GL_SwapBuffers(); + /* Calculate FPS (every half a second) */ game->render->frames++; - /* Calculate FPS every half a second */ if ( t - game->render->t_fps > 500 ) { game->render->fps = (500*game->render->frames) / (t - game->render->t_fps); game->render->t_fps = t; diff --git a/src/types.h b/src/types.h index 5c1ba53..d072331 100644 --- a/src/types.h +++ b/src/types.h @@ -101,6 +101,8 @@ typedef struct { GLhandleARB lighting_vert; GLhandleARB lighting_frag; GLhandleARB lighting_program; + GLhandleARB postprocess_frag; + GLhandleARB postprocess_program; /* Textures */ Texture textures[MAX_TEXTURES]; @@ -110,6 +112,13 @@ typedef struct { Uint32 t_fps; int fps; + GLuint fbo; + GLuint fbotex; + GLuint fbodepth; + GLfloat aspect; + int width; + int height; + } RenderContext; typedef struct { |