tavli
diff src/shadow.cc @ 18:986c0b76513f
shadows, not completed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 29 Jun 2015 01:29:36 +0300 |
parents | |
children | 37dead56f01e |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/shadow.cc Mon Jun 29 01:29:36 2015 +0300 1.3 @@ -0,0 +1,108 @@ 1.4 +#include <assert.h> 1.5 +#include "opengl.h" 1.6 +#include "shadow.h" 1.7 +#include "vmath/vmath.h" 1.8 + 1.9 +static int tex_sz, prev_vp[4]; 1.10 +static unsigned int fbo, depth_tex, rb_color; 1.11 +static Matrix4x4 shadow_mat; 1.12 + 1.13 +bool init_shadow(int sz) 1.14 +{ 1.15 + if(!glcaps.fbo) { 1.16 + return true; 1.17 + } 1.18 + 1.19 + tex_sz = sz; 1.20 + 1.21 + glGenFramebuffers(1, &fbo); 1.22 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.23 + 1.24 + glGenTextures(1, &depth_tex); 1.25 + glBindTexture(GL_TEXTURE_2D, depth_tex); 1.26 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 1.27 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 1.28 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.29 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.30 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); 1.31 + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, tex_sz, tex_sz, 0, 1.32 + GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); 1.33 + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 0); 1.34 + 1.35 + assert(glGetError() == GL_NO_ERROR); 1.36 + 1.37 + glDrawBuffer(GL_FALSE); 1.38 + glReadBuffer(GL_FALSE); 1.39 + 1.40 + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { 1.41 + fprintf(stderr, "incomplete framebuffer\n"); 1.42 + return false; 1.43 + } 1.44 + 1.45 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.46 + glDrawBuffer(GL_BACK); 1.47 + glReadBuffer(GL_BACK); 1.48 + assert(glGetError() == GL_NO_ERROR); 1.49 + 1.50 + return true; 1.51 +} 1.52 + 1.53 +void destroy_shadow() 1.54 +{ 1.55 + glDeleteTextures(1, &depth_tex); 1.56 + glDeleteRenderbuffers(1, &rb_color); 1.57 + glDeleteFramebuffers(1, &fbo); 1.58 +} 1.59 + 1.60 +void begin_shadow_pass(const Vector3 &lpos, const Vector3 <arg, float lfov) 1.61 +{ 1.62 + Matrix4x4 viewmat, projmat; 1.63 + 1.64 + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); 1.65 + glDisable(GL_LIGHTING); 1.66 + glColorMask(0, 0, 0, 0); 1.67 + glDepthMask(1); 1.68 + 1.69 + projmat.set_perspective(DEG_TO_RAD(lfov) * 2.0, 1.0, 0.5, 500.0); 1.70 + viewmat.set_lookat(lpos, ltarg, Vector3(0, 1, 0)); 1.71 + shadow_mat = viewmat * projmat; 1.72 + 1.73 + glMatrixMode(GL_PROJECTION); 1.74 + glPushMatrix(); 1.75 + glLoadTransposeMatrixf(projmat[0]); 1.76 + glMatrixMode(GL_MODELVIEW); 1.77 + glPushMatrix(); 1.78 + glLoadTransposeMatrixf(viewmat[0]); 1.79 + 1.80 + glGetIntegerv(GL_VIEWPORT, prev_vp); 1.81 + glViewport(0, 0, tex_sz, tex_sz); 1.82 + 1.83 + glCullFace(GL_FRONT); 1.84 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.85 +} 1.86 + 1.87 + 1.88 +void end_shadow_pass() 1.89 +{ 1.90 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.91 + glCullFace(GL_BACK); 1.92 + 1.93 + glViewport(prev_vp[0], prev_vp[1], prev_vp[2], prev_vp[3]); 1.94 + 1.95 + glMatrixMode(GL_PROJECTION); 1.96 + glPopMatrix(); 1.97 + glMatrixMode(GL_MODELVIEW); 1.98 + glPopMatrix(); 1.99 + 1.100 + glPopAttrib(); 1.101 +} 1.102 + 1.103 +Matrix4x4 get_shadow_matrix() 1.104 +{ 1.105 + return shadow_mat; 1.106 +} 1.107 + 1.108 +unsigned int get_shadow_tex() 1.109 +{ 1.110 + return depth_tex; 1.111 +}