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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264
#endif
#include "main/shadowhandler.h"
#include "math/vector.h"
#include "math/matrix.h"
#include "packer/file.h"
#include "exception.h"
#include "demolib_prefs.h"
#define SHADOWLENGTH 10.0f
#if DEMOLIB_MAINLOOP
ShadowHandler::ShadowHandler(MainLoop *ml, const char *title, const char *elem, Hashtable *attr) :
ObjHandler(ml, title, elem, attr)
{
if (!this->pure_indices)
throw new FatalException(elem, "Needs a presorted object!");
if (this->vertices_per_face != 3)
throw new FatalException(elem, "Can only work with triangles!");
int i;
for (i = 0; i < faces.num_elems(); i += 3) {
struct extravertexinfo e;
/* calculate the plane for the triangle */
const struct vertex &v1 = vertices[faces[i]];
const struct vertex &v2 = vertices[faces[i + 1]];
const struct vertex &v3 = vertices[faces[i + 2]];
e.a = v1.y*(v2.z-v3.z) + v2.y*(v3.z-v1.z) + v3.y*(v1.z-v2.z);
e.b = v1.z*(v2.x-v3.x) + v2.z*(v3.x-v1.x) + v3.z*(v1.x-v2.x);
e.c = v1.x*(v2.y-v3.y) + v2.x*(v3.y-v1.y) + v3.x*(v1.y-v2.y);
e.d = -(v1.x*(v2.y*v3.z - v3.y*v2.z) +
v2.x*(v3.y*v1.z - v1.y*v3.z) +
v3.x*(v1.y*v2.z - v2.y*v1.z));
e.neighbours[0] = -1;
e.neighbours[1] = -1;
e.neighbours[2] = -1;
e.visible = false;
evi.add_end(e);
}
/*
* now find the neighbours
* optimization: use a vertex->face cache, which we make first :-)
*/
struct vfc {
int face[6];
};
vfc *vfcache = new vfc[vertices.num_elems()];
for (i = 0; i < vertices.num_elems(); i++) {
for (int j = 0; j < 6; j++) {
vfcache[i].face[j] = -1;
}
}
for (i = 0; i < faces.num_elems(); i++) {
int v = faces[i];
bool found = true;
for (int j = 0; j < 6; j++) {
if (vfcache[v].face[j] == -1) {
vfcache[v].face[j] = i/3;
found = true;
break;
}
}
if (!found) {
throw new FatalException("Vertex->face cache overload");
}
}
for (i = 0; i < faces.num_elems(); i += 3) {
for (int ea = 0; ea < 3; ea++) {
if (evi[i/3].neighbours[ea] != -1) continue;
for (int k = 0; k < 6; k++) {
int j = vfcache[faces[i + ea]].face[k];
if (j >= i/3) continue;
if (j == -1) break;
bool found = false;
for (int eb = 0; eb < 3; eb++) {
const int va1 = faces[i + ea];
const int va2 = faces[i + (ea+1)%3];
const int vb1 = faces[j*3 + eb];
const int vb2 = faces[j*3 + (eb+1)%3];
if ((va1 == vb1 &&
va2 == vb2) ||
(va1 == vb2 &&
va2 == vb1)) {
evi[i/3].neighbours[ea] = j;
evi[j].neighbours[eb] = i/3;
found = true;
break;
}
}
if (found) break;
}
}
}
delete[] vfcache;
this->shadowvol = new GLfloat[evi.num_elems() * 16];
}
ShadowHandler::~ShadowHandler()
{
delete[] this->shadowvol;
this->shadowvol = NULL;
}
void ShadowHandler::start_effect()
{
ObjHandler::start_effect();
}
void ShadowHandler::draw_scene(float progress)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/* translate */
const float x = this->get_val("xpos", progress);
const float y = this->get_val("ypos", progress);
const float z = this->get_val("zpos", progress);
const float xr = this->get_val("xrot", progress);
const float yr = this->get_val("yrot", progress);
const float zr = this->get_val("zrot", progress);
glTranslatef(x,y,z);
if (this->inverse_rotorder) {
glRotatef(zr, 0.0f, 0.0f, 1.0f);
glRotatef(yr, 0.0f, 1.0f, 0.0f);
glRotatef(xr, 1.0f, 0.0f, 0.0f);
} else {
glRotatef(xr, 1.0f, 0.0f, 0.0f);
glRotatef(yr, 0.0f, 1.0f, 0.0f);
glRotatef(zr, 0.0f, 0.0f, 1.0f);
}
const float scale = this->get_val("scale", progress);
glScalef(scale, scale, scale);
/* get the lightsource position from x/y/z */
const float lx = this->get_val("user1", progress);
const float ly = this->get_val("user2", progress);
const float lz = this->get_val("user3", progress);
const Vector lvec(lx, ly, lz);
/* get the modelview matrix */
float mv[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mv);
Matrix mvm(mv);
/* find the lightsource position in object space */
const Vector olvec = mvm.transpose() * lvec;
/* determine which faces are visible by the light */
int i;
const float olx = olvec.x;
const float oly = olvec.y;
const float olz = olvec.z;
for (i = 0; i < evi.num_elems(); i++) {
evi[i].visible = (evi[i].a * olx +
evi[i].b * oly +
evi[i].c * olz +
evi[i].d > 0);
}
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_ENABLE_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_FOG);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_STENCIL_TEST);
glColor4f(0.0f, 1.0f, 0.0f, 0.0f);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFFL);
glStencilMask(0xFFFFFFFFL);
/* generate the shadow volume */
int svi = this->do_shadow_pass(olx, oly, olz, shadowvol);
glVertexPointer(4, GL_FLOAT, 0, shadowvol);
if (this->has_compiled_vertex_array) {
(*this->glLockArraysEXT)(0, svi);
}
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
/* pass 1 - increase stencil value in the shadow */
glFrontFace(GL_CCW);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glDrawArrays(GL_QUADS, 0, svi/4);
/* pass 2 - decrease stencil value in the shadow */
glFrontFace(GL_CW);
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glDrawArrays(GL_QUADS, 0, svi/4);
/* pass 3 - remove the light-culled objects from the shadow volume */
glFrontFace(GL_CCW);
glBegin(GL_TRIANGLES);
for (int i = 0, ii = 0; i < evi.num_elems(); i++, ii += 3) {
if (evi[i].visible) continue;
const struct vertex * const v1 = &this->vertices[faces[ii]];
const struct vertex * const v2 = &this->vertices[faces[ii + 1]];
const struct vertex * const v3 = &this->vertices[faces[ii + 2]];
glVertex3fv((GLfloat *)v1);
glVertex3fv((GLfloat *)v2);
glVertex3fv((GLfloat *)v3);
}
glEnd();
if (this->has_compiled_vertex_array) {
(*this->glUnlockArraysEXT)();
}
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPopAttrib();
glPopMatrix();
}
void ShadowHandler::end_effect()
{
ObjHandler::end_effect();
}
int ShadowHandler::do_shadow_pass(const float lx, const float ly, const float lz, GLfloat *shadowvol)
{
int svi = 0;
const float lxm = lx * SHADOWLENGTH;
const float lym = ly * SHADOWLENGTH;
const float lzm = lz * SHADOWLENGTH;
for (int i = 0, ii = 0; i < evi.num_elems(); i++, ii += 3) {
if (!evi[i].visible) continue;
for (int j = 0; j < 3; j++) {
const int ni = evi[i].neighbours[j];
if (ni != -1 && evi[ni].visible) continue;
const struct vertex * const v1 = &this->vertices[faces[ii + j]];
const struct vertex * const v2 = &this->vertices[faces[ii + (j+1)%3]];
/* calculate the two vertices in distance */
shadowvol[svi++] = v1->x;
shadowvol[svi++] = v1->y;
shadowvol[svi++] = v1->z;
shadowvol[svi++] = 1.0f;
/* equivalent to v1->x + (v1->x - lx) * SHADOWLENGTH etc. */
shadowvol[svi++] = v1->x * (SHADOWLENGTH + 1.0f) - lxm;
shadowvol[svi++] = v1->y * (SHADOWLENGTH + 1.0f) - lym;
shadowvol[svi++] = v1->z * (SHADOWLENGTH + 1.0f) - lzm;
shadowvol[svi++] = 1.0f;
/* equivalent to v2->x + (v2->x - lx) * SHADOWLENGTH etc. */
shadowvol[svi++] = v2->x * (SHADOWLENGTH + 1.0f) - lxm;
shadowvol[svi++] = v2->y * (SHADOWLENGTH + 1.0f) - lym;
shadowvol[svi++] = v2->z * (SHADOWLENGTH + 1.0f) - lzm;
shadowvol[svi++] = 1.0f;
shadowvol[svi++] = v2->x;
shadowvol[svi++] = v2->y;
shadowvol[svi++] = v2->z;
shadowvol[svi++] = 1.0f;
}
}
return svi;
}
#endif /* DEMOLIB_MAINLOOP */
|