现在的位置: 首页 > 综合 > 正文

DirectX的顶点缓存和索引绘制球

2014年01月30日 ⁄ 综合 ⁄ 共 4240字 ⁄ 字号 评论关闭

直接在DirectX SDK上的代码修改

InitGeomertry 创建顶点缓存和索引缓存

//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    // Use D3DX to create a texture from a file based image
    if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L"tiger.bmp", &g_pTexture ) ) )
    {
        // If texture is not in current folder, try parent folder
        if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L"..\\tiger.bmp", &g_pTexture ) ) )
        {
            MessageBox( NULL, L"Could not find banana.bmp", L"Textures.exe", MB_OK );
            return E_FAIL;
        }
    }

 int vertexCount =(nRings+1)*(nSegments+1);
 int indexCount = 6*nRings*(nSegments + 1);
    // Create the vertex buffer.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( vertexCount* sizeof( CUSTOMVERTEX ),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }
 
 if (FAILED(g_pd3dDevice->CreateIndexBuffer(indexCount*sizeof(short),
        D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,
        D3DPOOL_DEFAULT,&g_pIB,NULL) ) )
  
                    
 {
  return E_FAIL;
 }
    // Fill the vertex buffer. We are setting the tu and tv texture
    // coordinates, which range from 0.0 to 1.0
 
 FillVertexData(1,vertexCount,indexCount);
    return S_OK;
}

FillVertexData函数填充顶点和索引

BOOL FillVertexData(int r,int VertexCount,int IndexCount)
{

 float fDeltaRingAngle = (D3DX_PI / nRings);
 float fDeltaSegAngle = (2 * D3DX_PI / nSegments);

 
 CUSTOMVERTEX* pVertices;
 if( FAILED( g_pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
  return E_FAIL;
 short *pIndices;
 if(FAILED(g_pIB->Lock(0,sizeof(short)*IndexCount,(void * *)&pIndices,D3DLOCK_DISCARD)))
 {
  return E_FAIL;
 }
 unsigned short wVerticeIndex = 0 ;
 

 for( int ring = 0; ring <= nRings; ring++ ) {
  float r0 = r * sinf (ring * fDeltaRingAngle);
  float y0 = r * cosf (ring * fDeltaRingAngle);
  for(int seg = 0; seg <= nSegments; seg++) {
   float x0 = r0 * sinf(seg * fDeltaSegAngle);
   float z0 = r0 * cosf(seg * fDeltaSegAngle);
   pVertices->position =D3DXVECTOR3(x0,y0,z0);
   pVertices->color = 0xff808080;
   pVertices->tu = (float)ring/nRings;
   pVertices->tv = (float)seg/nSegments;
   pVertices++;
   if (ring != nRings) {
    // each vertex (except the last) has six indices pointing to it
    *pIndices++ = wVerticeIndex + nSegments + 1;
    *pIndices++ = wVerticeIndex;              
    *pIndices++ = wVerticeIndex + nSegments;
    *pIndices++ = wVerticeIndex + nSegments + 1;
    *pIndices++ = wVerticeIndex + 1;
    *pIndices++ = wVerticeIndex;
    wVerticeIndex ++;
   }

  }

 }
 
 g_pVB->Unlock();
 g_pIB->Unlock();
}

Render()每一帧渲染图形

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();

        // Setup our texture. Using textures introduces the texture stage states,
        // which govern how textures get blended together (in the case of multiple
        // textures) and lighting information. In this case, we are modulating
        // (blending) our texture with the diffuse color of the vertices.
        g_pd3dDevice->SetTexture( 0, g_pTexture );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
        g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

        // Render the vertex buffer contents
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
        g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  g_pd3dDevice->SetIndices(g_pIB);

      //  g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, (nRings+1)*(nSegments+1) );
  g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,(nRings+1)*(nSegments+1),
   0,2*nRings*(nSegments+1));
  
        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

 

以下就是效果图,第一个dx程序,留作以后学习纪念。

 

 

抱歉!评论已关闭.