ld33_umonster

diff src/shadow.cc @ 0:4a6683050e29

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Aug 2015 07:15:00 +0300
parents
children 93ff21458a16
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/shadow.cc	Sat Aug 22 07:15:00 2015 +0300
     1.3 @@ -0,0 +1,141 @@
     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 +bool shadow_pass;
    1.10 +
    1.11 +static int tex_sz, prev_vp[4];
    1.12 +static unsigned int fbo, depth_tex, rb_color;
    1.13 +static Matrix4x4 shadow_mat;
    1.14 +
    1.15 +bool init_shadow(int sz)
    1.16 +{
    1.17 +	if(!glcaps.fbo || !glcaps.shadow) {
    1.18 +		return false;
    1.19 +	}
    1.20 +
    1.21 +	tex_sz = sz;
    1.22 +	printf("initializing shadow buffer (%dx%d)\n", tex_sz, tex_sz);
    1.23 +
    1.24 +	glGenFramebuffers(1, &fbo);
    1.25 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.26 +
    1.27 +	glGenTextures(1, &depth_tex);
    1.28 +	glBindTexture(GL_TEXTURE_2D, depth_tex);
    1.29 +	float border[] = {1, 1, 1, 1};
    1.30 +	glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
    1.31 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    1.32 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    1.33 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.34 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.35 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
    1.36 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, tex_sz, tex_sz, 0,
    1.37 +			GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
    1.38 +	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 0);
    1.39 +
    1.40 +	assert(glGetError() == GL_NO_ERROR);
    1.41 +
    1.42 +	glDrawBuffer(GL_FALSE);
    1.43 +	glReadBuffer(GL_FALSE);
    1.44 +
    1.45 +	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    1.46 +		fprintf(stderr, "incomplete framebuffer\n");
    1.47 +		return false;
    1.48 +	}
    1.49 +
    1.50 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
    1.51 +	glDrawBuffer(GL_BACK);
    1.52 +	glReadBuffer(GL_BACK);
    1.53 +	assert(glGetError() == GL_NO_ERROR);
    1.54 +
    1.55 +	return true;
    1.56 +}
    1.57 +
    1.58 +void destroy_shadow()
    1.59 +{
    1.60 +	glDeleteTextures(1, &depth_tex);
    1.61 +	glDeleteRenderbuffers(1, &rb_color);
    1.62 +	glDeleteFramebuffers(1, &fbo);
    1.63 +}
    1.64 +
    1.65 +void begin_shadow_pass(const Vector3 &lpos, const Vector3 &ltarg, float lfov)
    1.66 +{
    1.67 +	shadow_pass = true;
    1.68 +
    1.69 +	glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
    1.70 +	glDisable(GL_LIGHTING);
    1.71 +	glColorMask(0, 0, 0, 0);
    1.72 +	glDepthMask(1);
    1.73 +
    1.74 +	Matrix4x4 viewmat;
    1.75 +	glGetFloatv(GL_MODELVIEW_MATRIX, viewmat[0]);
    1.76 +	viewmat.transpose();
    1.77 +
    1.78 +	Matrix4x4 lt_viewmat, lt_projmat;
    1.79 +	lt_projmat.set_perspective(DEG_TO_RAD(lfov) * 2.0, 1.0, 8.0, 50.0);
    1.80 +	lt_viewmat.set_lookat(lpos, ltarg, Vector3(0, 1, 0));
    1.81 +	shadow_mat = lt_projmat * lt_viewmat * viewmat.inverse();
    1.82 +
    1.83 +	glMatrixMode(GL_PROJECTION);
    1.84 +	glPushMatrix();
    1.85 +	glLoadTransposeMatrixf(lt_projmat[0]);
    1.86 +
    1.87 +	glMatrixMode(GL_MODELVIEW);
    1.88 +	glPushMatrix();
    1.89 +	glLoadTransposeMatrixf(lt_viewmat[0]);
    1.90 +
    1.91 +	glGetIntegerv(GL_VIEWPORT, prev_vp);
    1.92 +	glViewport(0, 0, tex_sz, tex_sz);
    1.93 +
    1.94 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.95 +
    1.96 +	glPolygonOffset(2, 1);
    1.97 +	glEnable(GL_POLYGON_OFFSET_FILL);
    1.98 +
    1.99 +	glClear(GL_DEPTH_BUFFER_BIT);
   1.100 +	glUseProgram(0);
   1.101 +}
   1.102 +
   1.103 +
   1.104 +void end_shadow_pass()
   1.105 +{
   1.106 +	shadow_pass = false;
   1.107 +
   1.108 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
   1.109 +
   1.110 +	glViewport(prev_vp[0], prev_vp[1], prev_vp[2], prev_vp[3]);
   1.111 +
   1.112 +	glMatrixMode(GL_PROJECTION);
   1.113 +	glPopMatrix();
   1.114 +	glMatrixMode(GL_MODELVIEW);
   1.115 +	glPopMatrix();
   1.116 +
   1.117 +	glPopAttrib();
   1.118 +}
   1.119 +
   1.120 +Matrix4x4 get_shadow_matrix()
   1.121 +{
   1.122 +	return shadow_mat;
   1.123 +
   1.124 +	/*
   1.125 +	glMatrixMode(GL_MODELVIEW);
   1.126 +	glPushMatrix();
   1.127 +	glLoadIdentity();
   1.128 +	gluPerspective(lfov * 2.0, 1.0, 0.5, 50.0);
   1.129 +	gluLookAt(lpos.x, lpos.y, lpos.z, ltarg.x, ltarg.y, ltarg.z, 0, 1, 0);
   1.130 +
   1.131 +	float mat[16];
   1.132 +	glGetFloatv(GL_MODELVIEW_MATRIX, mat);
   1.133 +
   1.134 +	Matrix4x4 res;
   1.135 +	memcpy(res[0], mat, sizeof mat);
   1.136 +	res.transpose();
   1.137 +	return res;
   1.138 +	*/
   1.139 +}
   1.140 +
   1.141 +unsigned int get_shadow_tex()
   1.142 +{
   1.143 +	return depth_tex;
   1.144 +}