ld33_umonster
view src/shadow.cc @ 2:35349df5392d
wtf?
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 22 Aug 2015 23:55:21 +0300 |
parents | |
children | 93ff21458a16 |
line source
1 #include <assert.h>
2 #include "opengl.h"
3 #include "shadow.h"
4 #include "vmath/vmath.h"
6 bool shadow_pass;
8 static int tex_sz, prev_vp[4];
9 static unsigned int fbo, depth_tex, rb_color;
10 static Matrix4x4 shadow_mat;
12 bool init_shadow(int sz)
13 {
14 if(!glcaps.fbo || !glcaps.shadow) {
15 return false;
16 }
18 tex_sz = sz;
19 printf("initializing shadow buffer (%dx%d)\n", tex_sz, tex_sz);
21 glGenFramebuffers(1, &fbo);
22 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
24 glGenTextures(1, &depth_tex);
25 glBindTexture(GL_TEXTURE_2D, depth_tex);
26 float border[] = {1, 1, 1, 1};
27 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border);
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
32 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
33 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, tex_sz, tex_sz, 0,
34 GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
35 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_tex, 0);
37 assert(glGetError() == GL_NO_ERROR);
39 glDrawBuffer(GL_FALSE);
40 glReadBuffer(GL_FALSE);
42 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
43 fprintf(stderr, "incomplete framebuffer\n");
44 return false;
45 }
47 glBindFramebuffer(GL_FRAMEBUFFER, 0);
48 glDrawBuffer(GL_BACK);
49 glReadBuffer(GL_BACK);
50 assert(glGetError() == GL_NO_ERROR);
52 return true;
53 }
55 void destroy_shadow()
56 {
57 glDeleteTextures(1, &depth_tex);
58 glDeleteRenderbuffers(1, &rb_color);
59 glDeleteFramebuffers(1, &fbo);
60 }
62 void begin_shadow_pass(const Vector3 &lpos, const Vector3 <arg, float lfov)
63 {
64 shadow_pass = true;
66 glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
67 glDisable(GL_LIGHTING);
68 glColorMask(0, 0, 0, 0);
69 glDepthMask(1);
71 Matrix4x4 viewmat;
72 glGetFloatv(GL_MODELVIEW_MATRIX, viewmat[0]);
73 viewmat.transpose();
75 Matrix4x4 lt_viewmat, lt_projmat;
76 lt_projmat.set_perspective(DEG_TO_RAD(lfov) * 2.0, 1.0, 8.0, 50.0);
77 lt_viewmat.set_lookat(lpos, ltarg, Vector3(0, 1, 0));
78 shadow_mat = lt_projmat * lt_viewmat * viewmat.inverse();
80 glMatrixMode(GL_PROJECTION);
81 glPushMatrix();
82 glLoadTransposeMatrixf(lt_projmat[0]);
84 glMatrixMode(GL_MODELVIEW);
85 glPushMatrix();
86 glLoadTransposeMatrixf(lt_viewmat[0]);
88 glGetIntegerv(GL_VIEWPORT, prev_vp);
89 glViewport(0, 0, tex_sz, tex_sz);
91 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
93 glPolygonOffset(2, 1);
94 glEnable(GL_POLYGON_OFFSET_FILL);
96 glClear(GL_DEPTH_BUFFER_BIT);
97 glUseProgram(0);
98 }
101 void end_shadow_pass()
102 {
103 shadow_pass = false;
105 glBindFramebuffer(GL_FRAMEBUFFER, 0);
107 glViewport(prev_vp[0], prev_vp[1], prev_vp[2], prev_vp[3]);
109 glMatrixMode(GL_PROJECTION);
110 glPopMatrix();
111 glMatrixMode(GL_MODELVIEW);
112 glPopMatrix();
114 glPopAttrib();
115 }
117 Matrix4x4 get_shadow_matrix()
118 {
119 return shadow_mat;
121 /*
122 glMatrixMode(GL_MODELVIEW);
123 glPushMatrix();
124 glLoadIdentity();
125 gluPerspective(lfov * 2.0, 1.0, 0.5, 50.0);
126 gluLookAt(lpos.x, lpos.y, lpos.z, ltarg.x, ltarg.y, ltarg.z, 0, 1, 0);
128 float mat[16];
129 glGetFloatv(GL_MODELVIEW_MATRIX, mat);
131 Matrix4x4 res;
132 memcpy(res[0], mat, sizeof mat);
133 res.transpose();
134 return res;
135 */
136 }
138 unsigned int get_shadow_tex()
139 {
140 return depth_tex;
141 }