游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2614|回复: 6

初学者求救:生成球体的这段代码我不太清楚?

[复制链接]

2

主题

8

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2006-4-19 20:58:00 | 显示全部楼层 |阅读模式
生成球体的这段代码我不太清楚,高手可以给我讲讲么?
if(nCurrentRing != m_nRings)
            {
                *pIndices = wVertexIndex;
                pIndices++;
                *pIndices = wVertexIndex + (WORD)(m_nSegments + 1);
                pIndices++;
                wVertexIndex++;
            }
是在D3D中的。希望哪个高手能帮帮我呀!

7

主题

299

帖子

305

积分

中级会员

Rank: 3Rank: 3

积分
305
发表于 2006-4-19 21:19:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

不懂 没有上下文

10

主题

23

帖子

29

积分

注册会员

Rank: 2

积分
29
发表于 2006-4-19 22:12:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

只看这段代码,只是把顶点缓冲与索引缓冲一一对应,代码上面应该还有一个for循环包含这段代码.楼主如果明白索引缓冲的话.............

2

主题

8

帖子

8

积分

新手上路

Rank: 1

积分
8
 楼主| 发表于 2006-4-21 17:24:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

bool CSphere::UpdateVertices()
{
    //Code adapted from a sample by "Laurent" posted on the GameDev.net DirectX forum
    //http://www.gamedev.net/community/forums/topic.asp?topic_id=85779

    WORD* pIndices;
    SPHERE_CUSTOMVERTEX* pVertex;
    WORD wVertexIndex = 0;
    int nCurrentRing;
    int nCurrentSegment;
    D3DXVECTOR3 vNormal;

    //Lock the vertex buffer
    if(FAILED(m_pVertexBuffer->Lock(0, 0, (BYTE**)&pVertex, 0)))
    {
        LogError("<li>CSphere: Unable to lock vertex buffer.");
        return false;
    }

    //Lock the index buffer
    if(FAILED(m_pIndexBuffer->Lock(0, m_dwNumOfIndices, (BYTE**)&pIndices, 0)))
    {
        LogError("<li>CSphere: Unable to lock index buffer.");
        return false;
    }

    //Establish constants used in sphere generation
    FLOAT rDeltaRingAngle = (D3DX_PI / m_nRings);
    FLOAT rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);

    //Generate the group of rings for the sphere
    for(nCurrentRing = 0; nCurrentRing < m_nRings + 1; nCurrentRing++)
    {
        FLOAT r0 = sinf(nCurrentRing * rDeltaRingAngle);
        FLOAT y0 = cosf(nCurrentRing * rDeltaRingAngle);

        //Generate the group of segments for the current ring
        for(nCurrentSegment = 0; nCurrentSegment < m_nSegments + 1;
            nCurrentSegment++)
        {
            FLOAT x0 = r0 * sinf(nCurrentSegment * rDeltaSegAngle);
            FLOAT z0 = r0 * cosf(nCurrentSegment * rDeltaSegAngle);

            vNormal.x = x0;
            vNormal.y = y0;
            vNormal.z = z0;
   
            D3DXVec3Normalize(&vNormal, &vNormal);

            //Add one vertex to the strip which makes up the sphere
            pVertex->x = x0;
            pVertex->y = y0;
            pVertex->z = z0;
            pVertex->nx = vNormal.x;
            pVertex->ny = vNormal.y;
            pVertex->nz = vNormal.z;
            pVertex->tu = 1.0f - ((FLOAT)nCurrentSegment / (FLOAT)m_nSegments);
            pVertex->tv = (FLOAT)nCurrentRing / (FLOAT)m_nRings;

            pVertex++;
            
            //Add two indices except for the last ring
            if(nCurrentRing != m_nRings)
            {
                *pIndices = wVertexIndex;
                pIndices++;
               
                *pIndices = wVertexIndex + (WORD)(m_nSegments + 1);
                pIndices++;
               
                wVertexIndex++;
            }
        }
    }

    if(FAILED(m_pIndexBuffer->Unlock()))
    {
        LogError("<li>CSphere: Unable to unlock index buffer.");
        return false;
    }

    if(FAILED(m_pVertexBuffer->Unlock()))
    {
        LogError("<li>CSphere: Unable to unlock vertex buffer.");
        return false;
    }

    return true;
}

2

主题

8

帖子

8

积分

新手上路

Rank: 1

积分
8
 楼主| 发表于 2006-4-21 17:25:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

上面就是生成球体的那段代码?谢谢高手咯!

13

主题

978

帖子

978

积分

高级会员

Rank: 4

积分
978
发表于 2006-4-21 20:03:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

球体?
印象中D3DX有生成球体mesh的函数(我是不是太懒了?)

0

主题

25

帖子

29

积分

注册会员

Rank: 2

积分
29
发表于 2006-4-24 12:20:00 | 显示全部楼层

Re:初学者求救:生成球体的这段代码我不太清楚?

D3D有生成SPHERE的函数,但是楼主的意思应该是自己画,先是锁定顶点缓冲区,和索引缓冲区,然后用三角函数确定球体上每一个点,但是我怎么没找到索引定义的数组呢?最好在把你的Render()拿出来看看 [em11]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-24 07:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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