我正在尝试通过挂钩 OpenGl 调用来破解和修改旧版 opengl 固定管道游戏的多个渲染功能,而我当前的任务是实现着色器照明。我已经创建了一个适当的着色器程序,可以正确照亮大部分对象,但该游戏的地形是在没有提供正常数据的情况下绘制的。
游戏中调用:
void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer);
and
void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid * indices);`
定义和绘制地形,因此我将这些函数都挂钩,并且我希望在指针处循环遍历给定的顶点数组,并在每次 DrawElements 调用或 VertexPointer 调用时计算每个表面的法线,但我有很难想出一种方法来做到这一点 - 具体来说,如何读取、迭代和理解指针处的数据。在这种情况下,通常的参数为glVertexPointer
电话是size = 3, type = GL_float, stride = 16, pointer = some pointer
。挂钩 glVertexPointer,我不知道如何迭代指针并获取网格的所有顶点,考虑到我不知道所有顶点的总数,也不了解数据在指针处的结构给定步幅 - 以及类似地我应该如何构造普通数组
尝试计算索引数组中每个指定索引的绘制元素中的法线是否是一个更好的主意?
根据您的顶点数组构建过程,索引将是构建法线的唯一相关信息。
如果您在顶点数组中添加法线字段,并对解析索引数组的所有法线计算求和,则定义一个顶点的法线平均值很简单。
您必须将每个法线总和除以索引中的重复次数,计算可以保存在顶点索引后面的临时数组中的数量(每次将法线添加到顶点时都会增加)
更清楚地说:
Vertex[vertexCount]: {Pos,Normal}
normalCount[vertexCount]: int count
Indices[indecesCount]: int vertexIndex
每个顶点可能有 6 个法线,因此添加法线数组的临时数组来对每个顶点的法线进行平均:
NormalTemp[vertexCount][6] {x,y,z}
比解析你的索引数组(如果它是三角形):
for i=0 to indicesCount step 3
for each triangle top (t from 0 to 2)
NormalTemp[indices[i + t]][normalCount[indices[i+t]]+1] = normal calculation with cross product of vectors ending with other summits or this triangle
normalCount[indices[i+t]]++
比你必须将总和除以计数
for i=0 to vertexCount step 1
for j=0 to NormalCount[i] step 1
sum += NormalTemp[i][j]
normal[i] = sum / normacount[i]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)