球体着色OpenGL

时间:2011-11-09 01:30:34

标签: opengl glsl

我试图遮蔽一个球体。我不知道从哪里开始。我计算了顶点,并使用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];

    }

}

Here is my Sphere without the "GL_TRIANGLE_FAN", JUST "GL_LINE_STRIP"

这是我的Sphere没有“GL_TRIANGLE_FAN”,只是“GL_LINE_STRIP” 这就是我使用“glDrawArrays”的方式:

glDrawArrays(GL_LINE_STRIP, 0, 10296);
glDrawArrays(GL_LINES, 10297, 30888);

0-10296是球体的顶点。 从10297到30888是球体的法线顶点。

这是我的Vertex文件:

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);

}

现在我知道我需要将法线传递给顶点和片段着色器,但是我该怎么做以及如何/在哪里实现光计算,线性插值? 感谢

1 个答案:

答案 0 :(得分:1)

基本上你需要计算顶点着色器中的光照并将顶点颜色传递给片段着色器,如果你想要每顶点光照或将法线和光线方向作为变化变量传递并计算每像素的一切灯光。

这里的主要技巧是当你将法线传递给片段着色器时,它会在每个片段的顶点之间进行插值,结果阴影非常平滑但也很慢。

Here is a very nice article to start with.