Shading

There are two shading modes that are presently used: flat shading and Gouraud shading.

Like many things in Direct3D, the shading mode is controlled through the Direct3D state machine.

// set flat shading
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);

// set Gouraud shading
Device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
  
  
bool Setup()
{
     // create vertex buffer
     Device->CreateVertexBuffer(
          3 * sizeof(ColorVertex),
          D3DUSAGE WRITEONLY,
          ColorVertex::FVF,
          D3DPOOL MANAGED,
          &Triangle,
          0);

     // fill the buffers with the triangle data
     ColorVertex* v;
     Triangle->Lock(0, 0, (void**)&v, 0);

     v[0] = ColorVertex(-1.0f, 0.0f, 2.0f, D3DCOLOR XRGB(255,   0,
                                                         0));
     v[1] = ColorVertex( 0.0f, 1.0f, 2.0f, D3DCOLOR XRGB(  0, 255,
                                                         0));
     v[2] = ColorVertex( 1.0f, 0.0f, 2.0f, D3DCOLOR XRGB(  0,   0,
                                                         255));

     Triangle->Unlock();

     // set projection matrix
     D3DXMATRIX proj;
     D3DXMatrixPerspectiveFovLH(
          &proj,
          D3DX PI * 0.5f, // 90 - degree
          (float)Width / (float)Height,
          1.0f,
          1000.0f);
     Device->SetTransform(D3DTS PROJECTION, &proj);

     // set the render states
     Device->SetRenderState(D3DRS LIGHTING, false);

     return true;
}

Then, the Display function draws Triangle twice in two different positions and with different shade modes. The position of each triangle is controlled with the world matrix—World.

bool Display(float timeDelta)
{
     if( Device )
     {
         Device->Clear(0, 0, D3DCLEAR TARGET | D3DCLEAR ZBUFFER,
                       0xffffffff, 1.0f, 0);
         Device->BeginScene();

         Device->SetFVF(ColorVertex::FVF);
         Device->SetStreamSource(0, Triangle, 0, sizeof(ColorVertex));
         // draw the triangle to the left with flat shading
         D3DXMatrixTranslation(&World, -1.25f, 0.0f, 0.0f);
         Device->SetTransform(D3DTS WORLD, &World);

         Device->SetRenderState(D3DRS SHADEMODE, D3DSHADE FLAT);
         Device->DrawPrimitive(D3DPT TRIANGLELIST, 0, 1);

         // draw the triangle to the right with gouraud shading
         D3DXMatrixTranslation(&World, 1.25f, 0.0f, 0.0f);
         Device->SetTransform(D3DTS WORLD, &World);

         Device->SetRenderState(D3DRS SHADEMODE, D3DSHADE GOURAUD);
         Device->DrawPrimitive(D3DPT TRIANGLELIST, 0, 1);

         Device->EndScene();
         Device->Present(0, 0, 0, 0);
     }
     return true;
}
flat 和 GOURAUD 两种方式到是好理解,主要是我想看看,他程序的结构,是在setup把vertex的模型写好
,然后在display(){Device->BeginScene();}里面渲染。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值