一片云雾

写博客挺浪费时间的……
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用OpenGL绘制一颗围棋子

Posted on 2011-11-08 16:54  一片云雾  阅读(1121)  评论(0编辑  收藏  举报


/***************************************************************************//**
* 函数名称:    DrawChess
* 功能描述:    绘制一个围棋子。
* 参 数:    a    >> 底部半径;
* 参 数:    b    >> 底部距离圆心距离;
* 参 数:    c    >> 侧面半径;
* 参 数:    n    >> 分割粒度;
* 返回值:    
* 其它说明:    
* 修改日期        修改人            修改内容
* ------------------------------------------------------------------------------
* 2011-08-28    Cloud         创建
*******************************************************************************/
void DrawChess(double a, double b, double c, int n)
{
    const double PI = 3.14159265358979323846;
    double fRange1 = PI - atan(a / (b + c));            //侧面弧度区间
    double R = sqrt(a * a + (b + c) * (b + c)) + c;        //大圆顶半径
    double fRange2 = atan(a / (b + c));                    //顶部弧度区间
    double vPos[3], vNormal[3];                            //顶点位置和法线方向
 
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            ;//底面
#define FILL1(n1, n2) \
            {\
            vPos[0] = (-a * (n1) / n) * cos((n2) * (2.0 * PI) / n);\
            vPos[1] = b;\
            vPos[2] = (-a * (n1) / n) * sin((n2) * (2.0 * PI) / n);\
            vNormal[0] = 0;\
            vNormal[1] = -1.0;\
            vNormal[2] = 0;    \
            }\
 
#define DRAW_TRIANGLE(x) \
            {\
            glBegin(GL_TRIANGLE_STRIP);\
            FILL##x(i, j);            glNormal3dv(vNormal); glVertex3dv(vPos);\
            FILL##x(i + 1, j);        glNormal3dv(vNormal); glVertex3dv(vPos);\
            FILL##x(i, j + 1);        glNormal3dv(vNormal); glVertex3dv(vPos);\
            FILL##x(i + 1, j + 1);    glNormal3dv(vNormal); glVertex3dv(vPos);\
            glEnd();\
            }\
 
            DRAW_TRIANGLE(1);
 
            //侧面
#define FILL2(n1, n2) \
            {\
            vPos[0] = (-a - c * sin((n1) * fRange1 / n)) * cos((n2) * (2.0 * PI) / n);\
            vPos[1] = b + c - c * cos((n1) * fRange1 / n);\
            vPos[2] = (-a - c * sin((n1) * fRange1 / n)) * sin((n2) * (2.0 * PI) / n);\
            vNormal[0] = -sin((n1) * fRange1 / n) * cos((n2) * (2.0 * PI) / n);\
            vNormal[1] = -cos((n1) * fRange1 / n);\
            vNormal[2] = -sin((n1) * fRange1 / n) * sin((n2) * (2.0 * PI) / n);\
            }\
 
            DRAW_TRIANGLE(2);
 
            //顶部
#define FILL3(n1, n2) \
            {\
            vPos[0] = (-R * sin(fRange2 - fRange2 * (n1) / n)) * cos((n2) * (2.0 * PI) / n);\
            vPos[1] = R * cos(fRange2 - fRange2 * (n1) / n);\
            vPos[2] = (-R * sin(fRange2 - fRange2 * (n1) / n)) * sin((n2) * (2.0 * PI) / n);\
            vNormal[0] = -sin(fRange2 - fRange2 * (n1) / n) * cos((n2) * (2.0 * PI) / n);\
            vNormal[1] = cos(fRange2 - fRange2 * (n1) / n);\
            vNormal[2] = -sin(fRange2 - fRange2 * (n1) / n) * sin((n2) * (2.0 * PI) / n);\
            }\
 
            DRAW_TRIANGLE(3);
        }
    }
}

   

DrawChess(2, 1, 0.8, 50)的绘制效果如下:

DrawChess(2, 1, 0.8, 20)的网格图图效果如下(我把镜头拉近了因此显得大一些):