【发布时间】:2011-11-13 03:03:26
【问题描述】:
我正在使用四边形绘制一个球体。我绘制了一个额外的顶点,只是为了将四边形分成 2 个三角形。所以它是这样的:
1 ----> 2
| |
| |
4 ----> 3
但是在 3 之后我再次绘制 1。所以想象一下从 3-->1 增加一行。
我现在正在尝试计算每个顶点的法线。 这是我的代码:
//calculate normals
for (no_vertice=0; no_vertice<12887; no_vertice+=1)
{
//getting the sphere's vertices
x=sphere_vertices[no_vertice].position[0];
y=sphere_vertices[no_vertice].position[1];
z=sphere_vertices[no_vertice].position[2];
//normalising vector "norm(Vertex - Center)"
magnitude = sqrt((x*x) + (y*y) + (z*z));
sphere_vertices[no_vertice].normal[0] = (x/magnitude);
sphere_vertices[no_vertice].normal[1] = (y/magnitude);
sphere_vertices[no_vertice].normal[2] = (z/magnitude);
printf("Normal at vertice %d = X:%f, Y:%f, Z:%f. \n", no_vertice, sphere_vertices[no_vertice].normal[0], sphere_vertices[no_vertice].normal[1], sphere_vertices[no_vertice].normal[2]);
}
我正在计算每个顶点的幅度,然后将该顶点的每个分量除以幅度,这样我就得到了一个单位向量。问题是我得到了很多零向量。即 x=0, y=0, z=0... 的顶点 当我将法线传递给顶点着色器时,
//my vertex structure
struct Vertex {
GLdouble position[3];
GLfloat color[3];
GLdouble normal[3];
};
....
..
.
/* Enable attribute index 2 as being used */
glEnableVertexAttribArray ( 2 );
glVertexAttribPointer ( ( GLuint ) 2, 3, GL_FLOAT, GL_FALSE, sizeof ( struct Vertex ), ( const GLvoid* )
offsetof(struct Vertex, normal) );
...
..
.
//pass the normal to vertex shader
glBindAttribLocation(shaderprogram, 2, "in_Normal");
然后进行光照计算,我会得到所有奇怪的效果。
我做错了什么吗?
最令人困惑的部分是我被要求这样做:
“对于球体,计算表面法线方向并增加线框 用表示每个顶点法线方向的短线绘制球体 现在应该看起来像一只刺猬。”
“注意:表面法线是与表面贴片成直角的单位向量,假设它是平面的。”
那么它基本上是顶点的法线,还是我必须绘制的四边形曲面? 我很困惑,因为它说,
"计算曲面法线方向"
然后
"用短线表示每个顶点的法线方向"
那么应该在哪里画线???在顶点?还是在四边形的中间?谢谢
编辑:顶点计算
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
//calculating Vertex 1
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//calculating Vertex 2
x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
z = sin((theta+dtheta)*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//calculating Vertex 3
x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
z = sin((theta+dtheta)*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
//adding Vertex_1 again to divide the Quad into 2 triangles
//calculating Vertex 1
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
if (theta > -90 && theta < 90) {
//calculating Vertex 4
x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
z = sin(theta*DTOR);
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
}
}
}
【问题讨论】:
-
是的。我很难理解它。这就是为什么我在这里发帖,为了澄清一下
-
当然,您的讲师已经谈到了曲面法线是什么。这个家庭作业不是你第一次听说这个词。
-
如果这一系列问题(以及明显拒绝遵循任何像样的OpenGL教程)是全班的代表,那么讲师需要被带走并枪毙:o