summaryrefslogtreecommitdiff
path: root/shadow_map.c
blob: 647b9a4a4f79a944071a1520f0939fb458f92b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright © 2009 Eric Anholt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <err.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <getopt.h>

#include "glass.h"

extern int no_multi_draw_arrays;
extern GLUvec4 eye_world;
static GLUmat4 world_to_light_eye;
GLUmat4 world_to_light_ndc, world_to_shadow_texcoords;
GLuint shadow_tex = 0, shadow_fbo;

static void
set_world_to_shadow_transform(void)
{
	GLUmat4 temp;

	/* Setup the viewport translation to map light NDC to texcoords.
	 */
	memcpy(&temp, &gluIdentityMatrix, sizeof(gluIdentityMatrix));
	temp.col[0].values[0] = (1.0 - 0.0) / 2.0;
	temp.col[3].values[0] = temp.col[0].values[0] + 0.0;
	temp.col[1].values[1] = (1.0 - 0.0) / 2.0;
	temp.col[3].values[1] = temp.col[1].values[1] + 0.0;
	temp.col[2].values[2] = (1.0 - 0.0) / 2.0;
	temp.col[3].values[2] = (1.0 - 0.0) / 2.0;

	gluMult4m_4m(&world_to_shadow_texcoords, &temp, &world_to_light_ndc);
}

static void
calculate_light_projection(void)
{
	GLUvec4 light_up;
	GLUmat4 temp;
	GLUvec4 light_to_rings;

	/* Wrong.  So wrong. */
	light_up.values[0] = 1.0;
	light_up.values[1] = 0.0;
	light_up.values[2] = 0.0;
	light_up.values[3] = 1.0;

	/*
	print_GLUvec4("light_world", &light_world);
	print_GLUvec4("ring_center", &ring_bounding_sphere_center_world);
	print_GLUvec4("light_up   ", &light_up);
	*/

	/* Our shadow mapping can't handle lights inside the frustum of the
	 * shadow casters.
	 */
	gluSub4v_4v(&light_to_rings,
		    &ring_bounding_sphere_center_world, &light_world);

	/* Transform for looking from they eye's point of view at the pile of
	 * rings.
	 */
	gluLookAt4v(&world_to_light_eye,
		    &light_world,
		    &ring_bounding_sphere_center_world,
		    &light_up);
	make_sphere_frustum(&temp, &light_to_rings,
			    ring_bounding_sphere_radius);
	/*print_GLUmat4("frustum", &temp);*/
	gluMult4m_4m(&world_to_light_ndc, &temp, &world_to_light_eye);
}

/* The ground plane is located in world coordinates, so no need to do any
 * object_to_world transformation.
 */
static void
install_shadow_transform(int instance)
{
	GLUmat4 mvp;

	gluMult4m_4m(&mvp, &world_to_light_ndc, &ring_obj_to_world[instance]);

	glUniformMatrix4fv(shadow_uniforms[SHADOW_UNIFORM_MVP].location, 1, 0,
			   (float *)&mvp);
}

void
generate_rings_shadowmap(void)
{
	int instance;
	int shadow_width = 1024, shadow_height = 1024;
	GLboolean first_time = GL_FALSE;

	if (shadow_prog == 0)
		return;

	glUseProgram(shadow_prog);

	calculate_light_projection();

	if (shadow_tex == 0) {
		glGenTextures(1, &shadow_tex);
		glBindTexture(GL_TEXTURE_2D, shadow_tex);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_COMPARE_MODE,
				GL_COMPARE_R_TO_TEXTURE);
		glTexParameteri(GL_TEXTURE_2D,
				GL_TEXTURE_COMPARE_FUNC,
				GL_LEQUAL);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
			     shadow_width, shadow_height, 0,
			     GL_DEPTH_COMPONENT, GL_FLOAT, NULL);

		glGenFramebuffersEXT(1, &shadow_fbo);
		glBindFramebuffer(GL_FRAMEBUFFER_EXT, shadow_fbo);
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
					  GL_DEPTH_ATTACHMENT_EXT,
					  GL_TEXTURE_2D,
					  shadow_tex,
					  0);
		glDrawBuffer(GL_NONE);
		glReadBuffer(GL_NONE);

		first_time = GL_TRUE;
	}

	glBindFramebuffer(GL_FRAMEBUFFER_EXT, shadow_fbo);

	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
	/* Move the faces away from the light, so that if they're compared
	 * for being lit, they'll tend to pass.
	 */
	glPolygonOffset(2, 2);
	glEnable(GL_POLYGON_OFFSET_FILL);

	if (first_time) {
		GLenum status;

		status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
		if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
			fprintf(stderr, "shadow map FBO incomplete: 0x%08x\n",
				status);
	}

	glViewport(0, 0, shadow_width, shadow_height);

	/* Clear to the material color. */
	glClear(GL_DEPTH_BUFFER_BIT);

	/* Draw the rings in black. */
	glBindVertexArray(ring.shadow_array_obj);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ring.vbo);
	for (instance = 0; instance < NUM_RINGS; instance++) {
		install_shadow_transform(instance);
		do_ring_drawelements();
	}

	glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	glDisable(GL_POLYGON_OFFSET_FILL);

	set_world_to_shadow_transform();
}