游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2179|回复: 2

DirectX创建圆柱体的问题(在线等,急)

[复制链接]

2

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2009-7-5 23:27:00 | 显示全部楼层 |阅读模式
今天看到一个例程,是用directx来创建圆柱体.代码如下
YLINDER_CUSTOMVERTEX* pVertex;
        WORD wVertexIndex = 0;
        int nCurrentSegment;

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

        float rDeltaSegAngle = (2.0f * D3DX_PI / m_nSegments);
        float rSegmentLength = 1.0f / (float)m_nSegments;

        //Create the sides triangle strip
        for(nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++)
        {
                float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);//为什么是sin
                float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);//为什么是cos

                pVertex->x = x0;
                pVertex->y = 0.0f + (m_rHeight / 2.0f);
                pVertex->z = z0;
                pVertex->nx = x0;
                pVertex->ny = 0.0f;
                pVertex->nz = z0;
                pVertex->tu = 1.0f - (rSegmentLength * (float)nCurrentSegment);
                pVertex->tv = 0.0f;
                pVertex++;

                pVertex->x = x0;
                pVertex->y = 0.0f - (m_rHeight / 2.0f);
                pVertex->z = z0;
                pVertex->nx = x0;
                pVertex->ny = 0.0f;
                pVertex->nz = z0;
                pVertex->tu = 1.0f - (rSegmentLength * (float)nCurrentSegment);
                pVertex->tv = 1.0f;
                pVertex++;
        }
       
        //Create the top triangle fan: Center
        pVertex->x = 0.0f;
        pVertex->y = 0.0f + (m_rHeight / 2.0f);
        pVertex->z = 0.0f;
        pVertex->nx = 0.0f;
        pVertex->ny = 1.0f;
        pVertex->nz = 0.0f;
        pVertex->tu = 0.5f;
        pVertex->tv = 0.5f;
        pVertex++;

        //Create the top triangle fan: Edges
        for(nCurrentSegment = 0; nCurrentSegment <= m_nSegments; nCurrentSegment++)
        {
                float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
                float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
       
                pVertex->x = x0;
                pVertex->y = 0.0f + (m_rHeight / 2.0f);
                pVertex->z = z0;
                pVertex->nx = 0.0f;
                pVertex->ny = 1.0f;
                pVertex->nz = 0.0f;

                float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
                float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;

                pVertex->tu = tu0;
                pVertex->tv = tv0;
                pVertex++;
        }

        //Create the bottom triangle fan: Center
        pVertex->x = 0.0f;
        pVertex->y = 0.0f - (m_rHeight / 2.0f);
        pVertex->z = 0.0f;
        pVertex->nx = 0.0f;
        pVertex->ny = -1.0f;
        pVertex->nz = 0.0f;
        pVertex->tu = 0.5f;
        pVertex->tv = 0.5f;
        pVertex++;

        //Create the bottom triangle fan: Edges
        for(nCurrentSegment = m_nSegments; nCurrentSegment >= 0; nCurrentSegment--)
        {
                float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
                float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
       
                pVertex->x = x0;
                pVertex->y = 0.0f - (m_rHeight / 2.0f);
                pVertex->z = z0;
                pVertex->nx = 0.0f;
                pVertex->ny = -1.0f;
                pVertex->nz = 0.0f;


                float tu0 = (0.5f * sinf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;
                float tv0 = (0.5f * cosf(nCurrentSegment * rDeltaSegAngle)) + 0.5f;

                pVertex->tu = tu0;
                pVertex->tv = tv0;
                pVertex++;
        }

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

        return true;  
这里觉得这两句
                float x0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
                float z0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
应该改为             float x0 = m_rRadius * cosf(nCurrentSegment * rDeltaSegAngle);
                float z0 = m_rRadius * sinf(nCurrentSegment * rDeltaSegAngle);
但改了之后效果很别扭。但是左思右想不明白 x = r*cos(),z=r*sin();才是正确的,这里为什么反的得到的效果才正确。
望哪位大哥指点迷津

恩。如果是与Z轴的夹角,拿侧面来说,第一个点与Z轴的角度为0,那也就是说是朝里面的侧面的中间的点为第一个点,再依顺时针方向渲染,对吧

4

主题

31

帖子

31

积分

注册会员

Rank: 2

积分
31
发表于 2009-7-5 23:41:00 | 显示全部楼层

Re:DirectX创建圆柱体的问题(在线等,急)

这里的角度 是在X0Z平面上 与Z轴的夹角 而不是与X轴的夹角

2

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2009-7-6 00:12:00 | 显示全部楼层

Re:DirectX创建圆柱体的问题(在线等,急)

谢了,明白了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-19 19:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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