14

我有两个模型,A 和 B,一个灯,L。我希望模型 A 在模型 B 上投射阴影。我暂时不想打扰阴影体积或适当的阴影,只是一个简单的圆形阴影就足够了。其效果是模型 A 被视为用于阴影投射的球体。

以下是我对算法的设想:

对于模型 B 中的每个三角形,画出三角形。沿着从 L 到 A 的线在三角形上投影一个圆,根据三角形的距离增加圆的大小。确保圆圈被剪裁到三角形的边界(我想以某种方式使用模板缓冲区)。

我正在使用 OpenGL 和普通 C。

关于我可以阅读的一些参考文档的任何指针?还是实施想法?

4

2 回答 2

8

我认为实现正确的阴影实际上更容易,因为 OpenGL 可以为您完成这项工作。

我在这里找到了一个包含大量文档的工作影子代码:http ://www.opengl.org/resources/code/samples/mjktips/TexShadowReflectLight.html

上面的代码两次渲染对象:第一次是正常的,然后是一个特殊的矩阵。它做了很多不相关的事情,比如用鼠标控制和反射。所以这里是有趣的部分。

计算阴影矩阵:

/* Create a matrix that will project the desired shadow. */
void
shadowMatrix(GLfloat shadowMat[4][4],
  GLfloat groundplane[4],
  GLfloat lightpos[4])
{
  GLfloat dot;

  /* Find dot product between light position vector and ground plane normal. */
  dot = groundplane[X] * lightpos[X] +
    groundplane[Y] * lightpos[Y] +
    groundplane[Z] * lightpos[Z] +
    groundplane[W] * lightpos[W];

  shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
  shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
  shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
  shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];

  shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
  shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
  shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
  shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];

  shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
  shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
  shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
  shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];

  shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
  shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
  shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
  shadowMat[3][3] = dot - lightpos[W] * groundplane[W];

}

我并不假装完全理解这一点。lightpos 是光源的位置。地平面的前 3 个坐标是地表的法向量。第四个是偏移量(距离 0,0,0 多远)。

而这部分实际上渲染了阴影:

glPushMatrix();
/* Project the shadow. */
glMultMatrixf((GLfloat *) floorShadow);
drawDinosaur();
glPopMatrix();

您需要先执行 glEnable/glDisable 一些操作才能使其正常工作,因此请查看链接。

于 2009-07-11T23:49:53.373 回答
-1

本文似乎涵盖了您的要求,使用 OpenGL 和硬件加速来创建详细的阴影贴图。

如果我试图实现这一点,我会很想使用光线投射。对于 B 中的每个三角形,创建一个从三角形到灯光的向量。如果它沿途撞到任何东西,它就在阴影中。除非您使用不错的加速结构和快速的三角形命中测试,否则这会有点慢。我喜欢包围体层次结构;许多程序也将它们用于碰撞检测。

于 2009-07-12T12:20:59.833 回答