游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2677|回复: 4

求助:绘制球体网格问题

[复制链接]

4

主题

13

帖子

13

积分

新手上路

Rank: 1

积分
13
发表于 2008-10-8 16:41:00 | 显示全部楼层 |阅读模式
我用如下代码创建定点坐标并进行绘制:
/****************************************************************************
函数名:MakeSphere
功  能:通过数学计算建立一个半径为一单位的球体
*****************************************************************************/
BOOL CEarthModel::MakeSphere(int nRings , int nSegments)
{
        //设置顶点数目和索引数目
    DWORD dwVertices = ( nRings + 1 ) * ( nSegments + 1 ) ;
    DWORD dwIndices = 2 * nRings * (nSegments + 1) ;

    //申请顶点缓存
    if(FAILED(m_pD3DDevice->CreateVertexBuffer(
                             dwVertices * sizeof(SPHEREVERTEX),
                             D3DUSAGE_WRITEONLY,
                                                         SPHEREVERTEX_FVF,
                             D3DPOOL_MANAGED,
                                                         &SphereVertexsBuffer,
                                                         0)))
       return E_FAIL;

    //申请索引缓存
    if(FAILED(m_pD3DDevice->CreateIndexBuffer(
                             //dwIndices * 2 ,
                                                         dwIndices * sizeof(DWORD),
                             D3DUSAGE_WRITEONLY,
                                                         D3DFMT_INDEX16,
                             D3DPOOL_MANAGED,
                                                         &SphereIndexBuffer,
                                                         0)))
       return E_FAIL;

     //锁定顶点缓存
     SPHEREVERTEX * pVertex ;
     SphereVertexsBuffer->Lock(0, 0, (VOID**)&pVertex, 0);

     //锁定索引缓存
     DWORD* pIndices;
     if(FAILED(SphereIndexBuffer->Lock(0, 0, (VOID**)&pIndices, 0)))
        return E_FAIL;

     //设定两个方向的角度增量
     FLOAT fDeltaRingAngle = (D3DX_PI / nRings);
     FLOAT fDeltaSegAngle = (2.0f * D3DX_PI / nSegments);

     DWORD wVerticeIndex = 0 ;
     //循环产生球体各环的值
     for(int ring = 0; ring < nRings + 1 ; ring++)
         {
                 FLOAT r0 = sinf(ring * fDeltaRingAngle);
         FLOAT y0 = cosf(ring * fDeltaRingAngle);

         //生成当前纬线圈的各点
         for(int seg = 0; seg < nSegments + 1 ; seg++)
                 {
                         FLOAT x0 = r0 * sinf(seg * fDeltaSegAngle);
             FLOAT z0 = r0 * cosf(seg * fDeltaSegAngle);

             //记录此次产生的点的信息
             pVertex->p = D3DXVECTOR3(x0, y0, z0); // normalized
             pVertex->n = pVertex->p;
             pVertex->color = 0xffffffff;
//             pVertex->tu = 1.0f - ((FLOAT) seg / (FLOAT) nSegments);
             pVertex->tu = (FLOAT)seg / (FLOAT)nSegments;
                         pVertex->tv = (FLOAT) ring / (FLOAT) nRings;

             pVertex++;

             //除最后的环外,产生其他环的索引信息
             if ( ring != nRings )
                         {
                                 *pIndices = wVerticeIndex;
                 pIndices++;
                 *pIndices = wVerticeIndex + (DWORD)(nSegments + 1);
                 pIndices++;
                 wVerticeIndex++;
                         }
                 }// end for seg

         } // end for ring

     SphereVertexsBuffer->Unlock();

     SphereIndexBuffer->Unlock();

     return S_OK ;
}

并使用如下代码进行绘制:
           DWORD iIndexStart = 0;
        DWORD iVertexStart = 0;
        for (i=0; i<iBallRing; i++)
        {
                m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,
                                                   iVertexStart,
                                                                               0,
                                                                               //(iBallRing+1)*(iBallSegment+1),
                                                                               2*(iBallSegment+1),
                                                                                       iIndexStart,
                                                                               2*(iBallSegment+1));
               
                iIndexStart += (2 * (iBallSegment+1));
                iVertexStart += (iBallSegment+1);

        }

这样得到的是很乱的图形,而不是我想要的球体网格。
反复试了很多绘制参数都不对
请的大家指教,谢谢!

2

主题

15

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2008-10-8 17:02:00 | 显示全部楼层

Re:求助:绘制球体网格问题

你是要画球体的线的网格吧?那怎么在绘制时会用到D3DPT_TRIANGLESTRIP,这是画三角面的。
应该用LineList

4

主题

13

帖子

13

积分

新手上路

Rank: 1

积分
13
 楼主| 发表于 2008-10-8 17:42:00 | 显示全部楼层

Re:求助:绘制球体网格问题

我是想画一个由三角形网格组成的球体
然后再进行纹理贴图

34

主题

657

帖子

672

积分

高级会员

Rank: 4

积分
672
发表于 2008-10-9 16:57:00 | 显示全部楼层

Re:求助:绘制球体网格问题

这个例子有建立球体的代码。
http://www.paulsprojects.net/opengl/sh/sh.html
纹理贴图网络上也有相关文章,搜索sphere texture mapping,我以前有找到过。

4

主题

13

帖子

13

积分

新手上路

Rank: 1

积分
13
 楼主| 发表于 2008-10-10 09:55:00 | 显示全部楼层

Re:求助:绘制球体网格问题

To: taxi
i have solved the problem.
Thanks!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-21 04:19

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表