【发布时间】:2011-11-09 09:57:51
【问题描述】:
我正在尝试为球体着色。我不知道从哪里开始。我计算了顶点,并使用 GL_TRIANGLE_FAN 将它们连接起来,我还绘制了每个顶点的法线。问题是我什至不知道如何开始做一些阴影/照明。我正在使用 OpeGL 3+。这是我的一些代码:
Sphere的顶点计算(我在网上找到并实现了):
void CreateUnitSphere(int dtheta,int dphi) //dtheta, dphi angle
{
GLdouble x,y,z;
GLdouble magnitude=0;
int no_vertice=-1;
int n;
int k;
int theta,phi;
const double PI = 3.1415926535897;
GLdouble DTOR = (PI/180);//degrees to radians
//setting the color to white
for (k=0; k<10296*3; k+=1)
{
sphere_vertices[k].color[0] = 1.0f;
sphere_vertices[k].color[1] = 1.0f;
sphere_vertices[k].color[2] = 1.0f;
}
for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
for (phi=0;phi<=360-dphi;phi+=dphi) {
x = cos(theta*DTOR) * cos(phi*DTOR);
y = cos(theta*DTOR) * sin(phi*DTOR);
z = sin(theta*DTOR);
//calculating Vertex 1
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
z = sin((theta+dtheta)*DTOR);
//calculating Vertex 2
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
z = sin((theta+dtheta)*DTOR);
//calculating Vertex 3
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) {
x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
z = sin(theta*DTOR);
//calculating Vertex 4
no_vertice+=1;
sphere_vertices[no_vertice].position[0] = x;
sphere_vertices[no_vertice].position[1] = y;
sphere_vertices[no_vertice].position[2] = z;
}
}
}
no_vertice = -1;
int no_index=10296;
//calculate normals and add them to the array of vertices
for (no_vertice=0; no_vertice<=10296; no_vertice+=1) {
no_index+=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));
//adding the new vector (the one divided by the magnitude
sphere_vertices[no_index].position[0] = (x/magnitude)/0.8;
sphere_vertices[no_index].position[1] = (y/magnitude)/0.8;
sphere_vertices[no_index].position[2] = (z/magnitude)/0.8;
///adding the vertex's normal (line drawing issue)
no_index+=1;
sphere_vertices[no_index].position[0] = sphere_vertices[no_vertice].position[0];
sphere_vertices[no_index].position[1] = sphere_vertices[no_vertice].position[1];
sphere_vertices[no_index].position[2] = sphere_vertices[no_vertice].position[2];
}
}
这是我的球体,没有“GL_TRIANGLE_FAN”,只有“GL_LINE_STRIP” 这就是我使用“glDrawArrays”的方式:
glDrawArrays(GL_LINE_STRIP, 0, 10296);
glDrawArrays(GL_LINES, 10297, 30888);
从 0 到 10296 是球体的顶点。 10297-30888 是球体的法线顶点。
这是我的顶点文件:
precision highp float;
in vec3 in_Position; //declare position
in vec3 in_Color;
// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 mvpmatrix;
out vec3 ex_Color;
void main(void) {
// Multiply the mvp matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
gl_Position = mvpmatrix * vec4(in_Position, 1.0);
ex_Color = in_Color;
}
和我的片段文件
#version 330
precision highp float;
in vec3 ex_Color;
out vec4 gl_FragColor;
void main(void) {
gl_FragColor = vec4(ex_Color,1.0);
}
现在我知道我需要将法线传递给顶点和片段着色器,但我该怎么做以及如何/在哪里实现光计算,线性插值? 谢谢
【问题讨论】:
-
你有没有考虑通过following a tutorial学习OpenGL?这比问诸如“我如何进行照明?”之类的大而复杂的问题要容易得多