From 0f5e2c1f67d6f9b7d6ee3dd55b8c7cd903881bd6 Mon Sep 17 00:00:00 2001 From: taw27 Date: Fri, 16 May 2008 16:54:55 +0000 Subject: Reshuffle lots of things Basic environment mapping stuff Remove OBJ file stuff git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@19 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- src/render.c | 170 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 117 insertions(+), 53 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index 23fde64..b765e63 100644 --- a/src/render.c +++ b/src/render.c @@ -79,19 +79,17 @@ static void render_delete_shaders(RenderContext *ctx) { } /* OpenGL initial setup */ -RenderContext *render_setup(width, height) { +RenderContext *render_setup(int width, int height) { RenderContext *ctx; ctx = malloc(sizeof(RenderContext)); if ( ctx == NULL ) return NULL; - glClearColor(0.0, 0.0, 0.0, 1.0); - glViewport(0, 0, width, height); + ctx->width = width; + ctx->height = height; - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(50.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0); /* Depth buffer 10cm to 100m */ + glClearColor(0.0, 0.0, 0.0, 1.0); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); @@ -104,18 +102,28 @@ 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); + /* Create a small FBO for rendering reflections with */ + 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, 256, 256, 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, 256, 256); + 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); ctx->num_textures = 0; - ctx->frames = 0; - ctx->t_fps = SDL_GetTicks(); - ctx->fps = 0; - return ctx; } @@ -206,38 +214,15 @@ static void render_draw_line(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLf } -void render_draw(Game *game) { - - int i; - Uint32 t; - - t = SDL_GetTicks(); +static void render_draw_stuff(Game *game, Uint32 t) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(game->lander->x-(game->view_dist*sinf(game->lander->yaw)*cosf(game->view_angle)), - game->lander->y-(game->view_dist*cosf(game->lander->yaw)*cosf(game->view_angle)), - game->lander->z+(game->view_dist*sinf(game->view_angle)), - 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)); + int i; - GLfloat pos[] = {-1.0, -0.8, 1.3, 0.0}; - GLfloat ambient[4]; - GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0}; - GLfloat specular[] = {0.8, 0.8, 0.8, 1.0}; - ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0; - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); - glLightfv(GL_LIGHT0, GL_POSITION, pos); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); - glLightfv(GL_LIGHT0, GL_SPECULAR, specular); - glEnable(GL_LIGHT0); + glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); - /* Draw the objects */ - //glUniform1iARB(glGetUniformLocationARB(game->render->lighting_program, "texture"), 0); - //glUseProgramObjectARB(game->render->lighting_program); for ( i=0; inum_rooms; i++ ) { Room *room; @@ -261,21 +246,100 @@ void render_draw(Game *game) { } } - /* Finally, the lander */ + + glPopClientAttrib(); + +} + +static void render_setup_lighting(Game *game) { + + GLfloat pos[] = {-1.0, -0.8, 1.3, 0.0}; + GLfloat ambient[4]; + GLfloat diffuse[] = {0.8, 0.8, 0.8, 1.0}; + GLfloat specular[] = {0.8, 0.8, 0.8, 1.0}; + + glEnable(GL_LIGHTING); + + ambient[0] = 0.3; ambient[1] = 0.3; ambient[2] = 0.3; ambient[3] = 1.0; + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient); + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular); + glEnable(GL_LIGHT0); + +} + +void render_draw(Game *game, Uint32 t) { + + /* First pass: Looking upwards */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, game->render->fbo); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, 256, 256); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(70.0, 1.0, 0.1, 100.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(game->lander->x, game->lander->y, game->lander->z, + game->lander->x, game->lander->y, game->lander->z+10.0, + sqrtf(2.0)*sinf(game->lander->yaw), sqrtf(2.0)*cosf(game->lander->yaw), 0.0); + render_setup_lighting(game); + render_draw_stuff(game, t); + + /* Second pass: Main view */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, game->render->width, game->render->height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, (GLfloat)game->render->width/(GLfloat)game->render->height, 0.1, 100.0); /* Depth buffer 10cm to 100m */ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(game->lander->x-(game->view_dist*sinf(game->lander->yaw)*cosf(game->view_angle)), + game->lander->y-(game->view_dist*cosf(game->lander->yaw)*cosf(game->view_angle)), + game->lander->z+(game->view_dist*sinf(game->view_angle)), + 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)); + render_setup_lighting(game); + render_draw_stuff(game, t); + + /* Finally, draw the lander */ + glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnable(GL_TEXTURE_GEN_S); + glEnable(GL_TEXTURE_GEN_T); + glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); + glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); render_model_instance_draw(game->lander, t, game->render); + glPopClientAttrib(); 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); + glDisable(GL_TEXTURE_GEN_S); + glDisable(GL_TEXTURE_GEN_T); - SDL_GL_SwapBuffers(); + glClear(GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glBindTexture(GL_TEXTURE_2D, game->render->fbotex); + glEnable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + glColor3f(1.0, 1.0, 1.0); + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); + glVertex2f(-1.0, -1.0); + glTexCoord2f(1.0, 0.0); + glVertex2f(-0.5, -1.0); + glTexCoord2f(1.0, 1.0); + glVertex2f(-0.5, -0.5); + glTexCoord2f(0.0, 1.0); + glVertex2f(-1.0, -0.5); + glEnd(); + glDisable(GL_TEXTURE_2D); - /* Calculate FPS every half a second */ - game->render->frames++; - if ( t - game->render->t_fps > 500 ) { - game->render->fps = (500*game->render->frames) / (t - game->render->t_fps); - game->render->t_fps = t; - game->render->frames = 0; - game->frame_delay_fiddled = 0; - } + SDL_GL_SwapBuffers(); } -- cgit v1.2.3