- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
render_dx9.cpp
资源名称:chap03.rar [点击查看]
上传用户:gzqinmao
上传日期:2022-07-13
资源大小:472k
文件大小:5k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "Gut.h"
- #include "render_data.h"
- #include <d3dx9.h>
- #include <d3dx9math.h>
- bool InitResourceDX9(void)
- {
- // 获得Direct3D 9设备
- LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
- // 设置视角转换矩阵
- Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 0.1f, 100.0f);
- device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
- // 关闭光照
- device->SetRenderState(D3DRS_LIGHTING, FALSE);
- // 改变三角形正面的法线
- device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
- return true;
- }
- bool ReleaseResourceDX9(void)
- {
- return true;
- }
- void ResizeWindowDX9(int width, int height)
- {
- // 获得Direct3D 9设备
- LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
- D3DPRESENT_PARAMETERS d3dpresent;
- ZeroMemory( &d3dpresent, sizeof(d3dpresent) );
- d3dpresent.Windowed = TRUE; // 使用窗口模式
- d3dpresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpresent.BackBufferFormat = D3DFMT_UNKNOWN; // 使用窗口模式可以不去设置
- d3dpresent.BackBufferCount = 1; // 提供一块backbuffer
- d3dpresent.EnableAutoDepthStencil = TRUE; // 自动打开DepthStencil Buffer
- d3dpresent.AutoDepthStencilFormat = D3DFMT_D24S8; // DepthStencil Buffer模式
- device->Reset(&d3dpresent);
- // 投影矩阵, 重设水平和垂直方向的视角.
- float aspect = (float) height / (float) width;
- Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, aspect, 0.1f, 100.0f);
- device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
- // 关闭光照
- device->SetRenderState(D3DRS_LIGHTING, FALSE);
- // 改变三角形正面的法线
- device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
- }
- // 使用DirectX 9来绘图
- void RenderFrameDX9(void)
- {
- LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
- // `清除画面`
- device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
- // `开始下绘图指令`
- device->BeginScene();
- // `计算出一个可以转换到镜头坐标系的矩阵`
- Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
- device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
- // `设置数据格式`
- // D3DFVF_XYZ = `使用3个浮点数来记录位置`
- // D3DFVF_DIFFUSE = `使用32bits整数类型来记录BGRA颜色`
- device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
- D3DXMATRIX temp_matrix;
- // `太阳, 代入鼠标的旋转控制`
- D3DXMATRIX sun_matrix, sun_ry, sun_rx;
- D3DXMatrixRotationX(&sun_rx, g_fRotate_X);
- D3DXMatrixRotationY(&sun_ry, g_fRotate_Y);
- D3DXMatrixMultiply(&sun_matrix, &sun_ry, &sun_rx);
- device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &sun_matrix);
- device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles,
- g_pSphereIndices, D3DFMT_INDEX16, g_pSunVertices, sizeof(Vertex_VC) );
- // `水星`
- float mercury_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_mercury);
- D3DXMATRIX mercury_matrix, mercury_translate, mercury_rotate;
- D3DXMatrixRotationY(&mercury_rotate, mercury_rotate_y);
- D3DXMatrixTranslation(&mercury_translate, mercury_to_sun_distance, 0.0f, 0.0f);
- D3DXMatrixMultiply(&temp_matrix, &mercury_translate, &mercury_rotate);
- D3DXMatrixMultiply(&mercury_matrix, &temp_matrix, &sun_matrix);
- device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &mercury_matrix);
- device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
- // `金星`
- float venus_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year_venus);
- D3DXMATRIX venus_matrix, venus_rotate, venus_translate;
- D3DXMatrixRotationY(&venus_rotate, venus_rotate_y);
- D3DXMatrixTranslation(&venus_translate, venus_to_sun_distance, 0.0f, 0.0f);
- D3DXMatrixMultiply(&temp_matrix, &venus_translate, &venus_rotate);
- D3DXMatrixMultiply(&venus_matrix, &temp_matrix, &sun_matrix);
- device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &venus_matrix);
- device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
- // `地球`
- float earth_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_year);
- D3DXMATRIX earth_matrix, earth_rotate, earth_translate;
- D3DXMatrixRotationY(&earth_rotate, earth_rotate_y);
- D3DXMatrixTranslation(&earth_translate, earth_to_sun_distance, 0.0f, 0.0f);
- D3DXMatrixMultiply(&temp_matrix, &earth_translate, &earth_rotate);
- D3DXMatrixMultiply(&earth_matrix, &temp_matrix, &sun_matrix);
- device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &earth_matrix);
- device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pEarthVertices, sizeof(Vertex_VC) );
- // `月亮`
- float moon_rotate_y = 2.0f * MATH_PI * (g_simulation_days / days_a_month);
- D3DXMATRIX moon_matrix, moon_rotate, moon_translate;
- D3DXMatrixRotationY(&moon_rotate, moon_rotate_y);
- D3DXMatrixTranslation(&moon_translate, moon_to_earth_distance, 0.0f, 0.0f);
- D3DXMatrixMultiply(&temp_matrix, &moon_translate, &moon_rotate);
- D3DXMatrixMultiply(&moon_matrix, &temp_matrix, &earth_matrix);
- device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &moon_matrix);
- device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );
- // `声明所有的绘图指令都下完了`
- device->EndScene();
- // `把背景backbuffer的画面显示出来`
- device->Present( NULL, NULL, NULL, NULL );
- }