如何创建廉价的阴影在OpenGL? [英] How Do I Create Cheap Shadows In OpenGL?
问题描述
我有两个型号,A和B,以及一盏灯,L。我想模型一个施展型号B的影子,我不想打扰阴影卷或适当的阴影的时刻,只是一个简单的圆形阴影就足够了。其效果是,模型A被视为一个球体阴影投射的目的。
I have two models, A and B, and one light, L. I would like model A to cast a shadow on model B. I don't want to bother with shadow volumes or proper shadows for the moment, just a simple circle shadow will suffice. The effect is that model A is treated as a sphere for shadow casting purposes.
下面是我想象的算法:
对于B型每一个三角形,绘制三角形。项目一圆到沿线由L到A三角形,增加了圆圈的大小取决于如何远离三角形。确保圆夹在三角形的边界(使用模板缓存,在某种程度上,我想)。
For each triangle in model B, draw the triangle. Project a circle onto the triangle along the line from L to A, increasing the size of the circle depending on how far away the triangle is. Ensure the circle is clipped to the triangle's boundaries (using the stencil buffer in some way, I imagine).
我正与OpenGL和普通的C。
I'm working with OpenGL and plain C.
在一些参考文献,我可以读任何指针?或者implmentation想法?
Any pointers on some reference documentation I can read? Or implmentation ideas?
推荐答案
我觉得它实际上是更容易实现正确的阴影,因为OpenGL的可以做的工作给你。
I think it is actually easier to implement correct shadows because OpenGL can do the work for you.
我找到了工作影子code,有很多在这里的文档:<一href="http://www.opengl.org/resources/$c$c/samples/mjktips/TexShadowReflectLight.html">http://www.opengl.org/resources/$c$c/samples/mjktips/TexShadowReflectLight.html
I found a working shadow code with lots of documentation here: http://www.opengl.org/resources/code/samples/mjktips/TexShadowReflectLight.html
在code以上呈现对象两次:第一次通常然后用特殊的矩阵。它做了很多不相关的东西,如鼠标和反射控制。因此,这里有一个有趣的部分。
The code above renders the object twice: first normally then with a special matrix. It does a lot of unrelated things such as control with mouse and reflections. So here are the interesting parts.
这计算的影子矩阵:
/* 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];
}
我不知道pretend完全理解这一点。 lightpos是光源的位置。接地面的前3个坐标在地面表面的法线矢量。第四个是偏移量(多远从0,0,0是)。
I do not pretend to understand this completely. lightpos is the position of the light source. The first 3 coordinates of groundplane are the normal vector of the ground surface. The fourth is the offset (how far is it from 0,0,0).
和这部分实际上呈现的阴影:
And this part actually renders the shadow:
glPushMatrix();
/* Project the shadow. */
glMultMatrixf((GLfloat *) floorShadow);
drawDinosaur();
glPopMatrix();
有一些事情你需要过glEnable / glDisable第一次为此工作所以看的链接。
There are some things you need to glEnable/glDisable first for this to work so look at the link.
这篇关于如何创建廉价的阴影在OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!