在opengl-es 1.x中有没有简单的方法来添加阴影?还是只在2.0中使用?
发布于 2011-12-29 21:52:42
在平面上投影阴影有一种简单的方法(不是很有效,但很简单)。
这个函数不是我的,我忘了是我找到的。它所做的是创建一个矩阵投影,将您绘制的所有内容映射到单个平面上。
static inline void glShadowProjection(float * l, float * e, float * n)
{
float d, c;
float mat[16];
// These are c and d (corresponding to the tutorial)
d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2] - d;
// Create the matrix. OpenGL uses column by column
// ordering
mat[0] = l[0]*n[0]+c;
mat[4] = n[1]*l[0];
mat[8] = n[2]*l[0];
mat[12] = -l[0]*c-l[0]*d;
mat[1] = n[0]*l[1];
mat[5] = l[1]*n[1]+c;
mat[9] = n[2]*l[1];
mat[13] = -l[1]*c-l[1]*d;
mat[2] = n[0]*l[2];
mat[6] = n[1]*l[2];
mat[10] = l[2]*n[2]+c;
mat[14] = -l[2]*c-l[2]*d;
mat[3] = n[0];
mat[7] = n[1];
mat[11] = n[2];
mat[15] = -d;
// Finally multiply the matrices together *plonk*
glMultMatrixf(mat);
}像这样使用它:
绘制您的对象。
glDrawArrays(GL_TRIANGLES, 0, machadoNumVerts); // Machado给它提供一个光源位置,一个投影阴影的平面和法线。
float lightPosition[] = {383.0, 461.0, 500.0, 0.0}
float n[] = { 0.0, 0.0, -1.0 }; // Normal vector for the plane
float e[] = { 0.0, 0.0, beltOrigin+1 }; // Point of the plane
glShadowProjection(lightPosition,e,n); 好的,应用阴影矩阵。
将绘图颜色更改为适合的颜色。
glColor4f(0.3, 0.3, 0.3, 0.9);再次绘制您的对象。
glDrawArrays(GL_TRIANGLES, 0, machadoNumVerts); // Machado这就是为什么这是效率不高的原因,对象越复杂,仅仅为了阴影而浪费的无用三角形就越多。
还要记住,您对未阴影对象所做的每一次操作都需要在应用阴影矩阵之后完成。
对于更复杂的东西,主题有点宽泛,很大程度上取决于你的场景和复杂性。
发布于 2011-12-29 19:23:49
投影纹理贴图阴影,就像他们在没有着色器的OpenGL-1.2中做的那样。查找1999至2002年间编写的较旧的阴影贴图教程。
https://stackoverflow.com/questions/8667108
复制相似问题