zoukankan      html  css  js  c++  java
  • 实现画茶壶,圆环,盒子,球体 (2)

    主要代码如下:

    声明

    //定义材质
    D3DMATERIAL9 g_material;
    //定义光源
    D3DLIGHT9 g_light;

    bool InitializeObjects()
    {
     // Set the projection matrix.
     D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
      WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);

     g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);

     // Set default rendering states.  启动光照
     //g_D3DDevice->SetRenderState(D3DRS_LIGHTING, true);
     g_D3DDevice->SetRenderState(D3DRS_AMBIENT,
      D3DCOLOR_COLORVALUE(0.3f,0.3f,0.3f,1.0f));

     //Setup the light source设置光源属性
     g_light.Type = D3DLIGHT_DIRECTIONAL; //类型为方向光
     g_light.Direction= D3DXVECTOR3(0.0f,0.0f,1.0f);//光在世界坐标系中传播方向的向量

     g_light.Diffuse.r = g_light.Diffuse.g = 1;// 该光源所发出的漫射光的颜色(rgba)
     g_light.Diffuse.b = g_light.Diffuse.a = 1;
     g_light.Specular.r = g_light.Specular.g = 1;// 该光源所发出的镜面光的颜色(rgba)
     g_light.Specular.b = g_light.Specular.a = 1;

     g_D3DDevice->SetLight(0,&g_light);// 设置该光源
     g_D3DDevice->LightEnable(0,true);// 启用该光源

     // Setup the material properties for the teapot.
     ZeroMemory(&g_material,sizeof(D3DMATERIAL9));// 将g_material对象的内存块清零
     g_material.Diffuse.r = g_material.Ambient.r = 0.6f;
     g_material.Diffuse.g = g_material.Ambient.g = 0.6f;
     g_material.Diffuse.b = g_material.Ambient.b = 0.7f;
     g_material.Specular.r = 0.4f;
     g_material.Specular.g = 0.4f;
     g_material.Specular.b = 0.4f;
     g_material.Power = 8.0f;

     //Create the objects  创建茶壶
        if(FAILED(D3DXCreateTeapot(g_D3DDevice,&g_teapot,NULL)))
      return false;
        //创建盒子
     if(FAILED(D3DXCreateBox(g_D3DDevice,2,2,2,&g_cube,NULL)))
      return false;
     //创建球体
        if(FAILED(D3DXCreateSphere(g_D3DDevice,1.5,25,25,
      &g_sphere,NULL)))
      return false;
     //创建圆环
        if(FAILED(D3DXCreateTorus(g_D3DDevice,0.5f,1.2f,25,25,
      &g_torus,NULL)))
      return false;

     // Define camera information.
     D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f);//摄像机的位置
     D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f); //观察点
     D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);     //以上方向为准

     // Build view matrix. 创建视图矩阵
     D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
      &lookAtPos, &upDir);

     return true;
    }

    void RenderScene()
    {
     // Clear the back buffer.清空后台缓存为指定色
     g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
      D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

     // Begin the scene.  Start rendering. 启动绘制
     g_D3DDevice->BeginScene();

     // Apply the view (camera).从世界空间到视图空间的视图转换
     g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

     //Draw teapot
     D3DXMatrixTranslation(&g_WorldMatrix,2.0f,-2.0,0.0f);//平移矩阵
     g_D3DDevice->SetTransform(D3DTS_WORLD,&g_WorldMatrix);//从模型空间到世界空间的世界转换
     g_D3DDevice->SetMaterial(&g_material);  // 设置材质
     g_teapot->DrawSubset(0); //绘制茶壶

     // Draw Cube.
     D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0, 0.0f); // 平移矩阵
     g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 从模型空间到世界空间的世界转换
     g_D3DDevice->SetMaterial(&g_material);  // 设置材质
     g_cube->DrawSubset(0);    // 绘制盒子

     // Draw Sphere.
     D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f);   // 平移矩阵
     g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 从模型空间到世界空间的世界转换
     g_D3DDevice->SetMaterial(&g_material);  // 设置材质
     g_sphere->DrawSubset(0);  // 绘制球体

     // Draw Torus.
     D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f);  // 平移矩阵
     g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);   // 从模型空间到世界空间的世界转换
     g_D3DDevice->SetMaterial(&g_material);  // 设置材质
     g_torus->DrawSubset(0);   // 绘制圆环

     // End the scene.  Stop rendering.
     g_D3DDevice->EndScene();

     // Display the scene.
     g_D3DDevice->Present(NULL, NULL, NULL, NULL);
    }

  • 相关阅读:
    顺序的分数 Ordered Fractions
    容易的网络游戏
    Linux常用命令大全(share)
    Linux和UNIX监控
    Linux命令汇总
    优秀员工的10个修养
    ibatis使用--SqlMapClient对象
    一个男人关心的东西 决定了他的层次
    Linux下vi命令大全
    Linux中图形界面和文本模式相互切换
  • 原文地址:https://www.cnblogs.com/batman425/p/3230053.html
Copyright © 2011-2022 走看看