diff options
Diffstat (limited to 'src/render.c')
-rw-r--r-- | src/render.c | 151 |
1 files changed, 82 insertions, 69 deletions
diff --git a/src/render.c b/src/render.c index 4c51496..d8ca276 100644 --- a/src/render.c +++ b/src/render.c @@ -158,36 +158,42 @@ RenderContext *render_setup(int width, int height) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - /* Create a small FBO for rendering reflections with */ - glGenFramebuffersEXT(1, &r->fbo); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->fbo); - /* Add a (texture) colour buffer to the FBO */ - glGenTextures(1, &r->fbotex); - glBindTexture(GL_TEXTURE_2D, r->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, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r->fbotex, 0); - /* Add a depth buffer to the FBO */ - glGenRenderbuffersEXT(1, &r->fbodepth); - glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, r->fbodepth); - glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, 128, 128); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, r->fbodepth); - 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, 128, 128, 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); - + if ( GLEW_EXT_framebuffer_object ) { + + /* Create a small FBO for rendering reflections with */ + glGenFramebuffersEXT(1, &r->fbo); + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->fbo); + /* Add a (texture) colour buffer to the FBO */ + glGenTextures(1, &r->fbotex); + glBindTexture(GL_TEXTURE_2D, r->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, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, r->fbotex, 0); + /* Add a depth buffer to the FBO */ + glGenRenderbuffersEXT(1, &r->fbodepth); + glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, r->fbodepth); + glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, 128, 128); + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, r->fbodepth); + 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, 128, 128, 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); + + } else { + fprintf(stderr, "EXT_framebuffer_object not supported\n"); + } + /* 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)); @@ -359,12 +365,14 @@ static int render_model_instance_draw(ModelInstance *instance, Uint32 t, RenderC glNormalPointer(GL_FLOAT, 0, r->hemisphere_n); glTexCoordPointer(2, GL_FLOAT, 0, r->hemisphere_t); - glBindTexture(GL_TEXTURE_2D, r->fbotex); - glEnable(GL_TEXTURE_2D); - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + if ( GLEW_EXT_framebuffer_object ) { + glBindTexture(GL_TEXTURE_2D, r->fbotex); + glEnable(GL_TEXTURE_2D); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_enabled"), 1); + glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_emits"), 1); + } - glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_enabled"), 1); - glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_emits"), 1); glDrawArrays(GL_QUADS, 0, HEMI_NUM_VERTICES); *nvert += HEMI_NUM_VERTICES; glUniform1iARB(glGetUniformLocationARB(r->lighting_program, "texture_emits"), 0); @@ -589,43 +597,48 @@ void render_draw(Game *game, Uint32 t) { RenderContext *r; r = game->render; + + if ( GLEW_EXT_framebuffer_object ) { + + /* First pass: Looking upwards */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->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, 80.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); + GLfloat amb[] = { 0.1, 0.1, 0.1, 1.0 }; + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); + render_draw_stuff(game, t); + + /* Render some swirlyness */ + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo); + glClear(GL_COLOR_BUFFER_BIT); + glViewport(0, 0, 128, 128); + glUseProgramObjectARB(r->swirly_program); + glUniform1fARB(glGetUniformLocationARB(game->render->swirly_program, "time"), t); + 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(); + + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); - /* First pass: Looking upwards */ - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->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, 80.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); - GLfloat amb[] = { 0.1, 0.1, 0.1, 1.0 }; - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb); - render_draw_stuff(game, t); - - /* Render some swirlyness */ - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo); - glClear(GL_COLOR_BUFFER_BIT); - glViewport(0, 0, 128, 128); - glUseProgramObjectARB(r->swirly_program); - glUniform1fARB(glGetUniformLocationARB(game->render->swirly_program, "time"), t); - 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, r->width, r->height); glMatrixMode(GL_PROJECTION); |