Direct3D中的绘制(5)

Stella981
• 阅读 530

这个程序创建并渲染几种不同的能够使用D3DXCreate*函数创建的3D物体。

Uses a left-handed coordinate system to create a mesh containing an axis-aligned box.

HRESULT D3DXCreateBox(  LPDIRECT3DDEVICE9 pDevice,  FLOAT Width,  FLOAT Height,  FLOAT Depth,  LPD3DXMESH * ppMesh,  LPD3DXBUFFER * ppAdjacency);
Parameters

pDevice

[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the created box mesh.

Width

[in] Width of the box, along the x-axis.

Height

[in] Height of the box, along the y-axis.

Depth

[in] Depth of the box, along the z-axis.

ppMesh

[out] Address of a pointer to the output shape, an ID3DXMesh interface.

ppAdjacency

[out] Address of a pointer to an ID3DXBuffer interface. When the method returns, this parameter is filled with an array of three DWORDs per face that specify the three neighbors for each face in the mesh. NULL can be specified.

Return Values

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.

Remarks

The created box is centered at the origin.

This function creates a mesh with the D3DXMESH_MANAGED creation option and D3DFVF_XYZ | D3DFVF_NORMAL flexible vertex format (FVF).

Uses a left-handed coordinate system to create a mesh containing a cylinder.

HRESULT D3DXCreateCylinder(  LPDIRECT3DDEVICE9 pDevice,  FLOAT Radius1,  FLOAT Radius2,  FLOAT Length,  UINT Slices,  UINT Stacks,  LPD3DXMESH * ppMesh,  LPD3DXBUFFER * ppAdjacency);
Parameters

pDevice

[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the created cylinder mesh.

Radius1

[in] Radius at the negative Z end. Value should be greater than or equal to 0.0f.

Radius2

[in] Radius at the positive Z end. Value should be greater than or equal to 0.0f.

Length

[in] Length of the cylinder along the z-axis.

Slices

[in] Number of slices about the main axis.

Stacks

[in] Number of stacks along the main axis.

ppMesh

[out] Address of a pointer to the output shape, an ID3DXMesh interface.

ppAdjacency

[out] Address of a pointer to an ID3DXBuffer interface. When the method returns, this parameter is filled with an array of three DWORDs per face that specify the three neighbors for each face in the mesh. NULL can be specified.

Return Values

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.

Remarks

The created cylinder is centered at the origin, and its axis is aligned with the z-axis.

This function creates a mesh with the D3DXMESH_MANAGED creation option and D3DFVF_XYZ | D3DFVF_NORMAL flexible vertex format (FVF).

Uses a left-handed coordinate system to create a mesh containing a torus.

HRESULT D3DXCreateTorus(  LPDIRECT3DDEVICE9 pDevice,  FLOAT InnerRadius,  FLOAT OuterRadius,  UINT Sides,  UINT Rings,  LPD3DXMESH * ppMesh,  LPD3DXBUFFER * ppAdjacency);
Parameters

pDevice

[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the created torus mesh.

InnerRadius

[in] Inner-radius of the torus. Value should be greater than or equal to 0.0f.

OuterRadius

[in] Outer-radius of the torus. Value should be greater than or equal to 0.0f.

Sides

[in] Number of sides in a cross-section. Value must be greater than or equal to 3.

Rings

[in] Number of rings making up the torus. Value must be greater than or equal to 3.

ppMesh

[out] Address of a pointer to the output shape, an ID3DXMesh interface.

ppAdjacency

[out] Address of a pointer to an ID3DXBuffer interface. When the method returns, this parameter is filled with an array of three DWORDs per face that specify the three neighbors for each face in the mesh. NULL can be specified.

Return Values

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.

Remarks

The created torus is centered at the origin, and its axis is aligned with the z-axis. The inner radius of the torus is the radius of the cross-section (the minor radius), and the outer radius of the torus is the radius of the central hole.

This function returns a mesh that can be used later for drawing or manipulation by the application.

This function creates a mesh with the D3DXMESH_MANAGED creation option and D3DFVF_XYZ | D3DFVF_NORMAL flexible vertex format (FVF).

Uses a left-handed coordinate system to create a mesh containing a sphere.

HRESULT D3DXCreateSphere(  LPDIRECT3DDEVICE9 pDevice,  FLOAT Radius,  UINT Slices,  UINT Stacks,  LPD3DXMESH * ppMesh,  LPD3DXBUFFER * ppAdjacency);
Parameters

pDevice

[in] Pointer to an IDirect3DDevice9 interface, representing the device associated with the created sphere mesh.

Radius

[in] Radius of the sphere. This value should be greater than or equal to 0.0f.

Slices

[in] Number of slices about the main axis.

Stacks

[in] Number of stacks along the main axis.

ppMesh

[out] Address of a pointer to the output shape, an ID3DXMesh interface.

ppAdjacency

[out] Address of a pointer to an ID3DXBuffer interface. When the method returns, this parameter is filled with an array of three DWORDs per face that specify the three neighbors for each face in the mesh. NULL can be specified.

Return Values

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.

Remarks

The created sphere is centered at the origin, and its axis is aligned with the z-axis.

This function creates a mesh with the D3DXMESH_MANAGED creation option and D3DFVF_XYZ | D3DFVF_NORMAL flexible vertex format (FVF).

Builds a matrix using the specified offsets.

D3DXMATRIX * D3DXMatrixTranslation(  D3DXMATRIX * pOut,  FLOAT x,  FLOAT y,  FLOAT z);
Parameters

pOut

[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.

x

[in] X-coordinate offset.

y

[in] Y-coordinate offset.

z

[in] Z-coordinate offset.

Return Values

Pointer to a D3DXMATRIX structure that contains a translated transformation matrix.

Remarks

The return value for this function is the same value returned in the pOut parameter. In this way, the D3DXMATRIXTranslation can be used as a parameter for another function.

运行截图:

Direct3D中的绘制(5)

源程序:

/**************************************************************************************
Renders several D3DX shapes in wireframe mode and has the camera fly around the scene. 
Demonstrates the D3DXCreate* functions, and demonstrates more complex transformations
used to position the objects in the world and move the camera around the world.
**************************************************************************************/
#include "d3dUtility.h"
#pragma warning(disable : 4100)
#define TEAPOT_MESH        0
#define BOX_MESH        1
#define CYLINDER_MESH    2
#define TORUS_MESH        3
#define SPHERE_MESH        4
#define NUM_MESH        5
const int WIDTH  = 640;
const int HEIGHT = 480;
IDirect3DDevice9*    g_d3d_device = NULL;
ID3DXMesh*            g_object_meshes[NUM_MESH];
// world matrices for each object.
// these matrices specify the location of the objects in the world.
D3DXMATRIX g_object_world_matrices[NUM_MESH];
////////////////////////////////////////////////////////////////////////////////////////////////////
bool setup()
{   
// create the teapot geometry
    D3DXCreateTeapot(g_d3d_device, &g_object_meshes[TEAPOT_MESH], NULL);
    D3DXCreateBox(g_d3d_device, 2.0f, 2.0f, 2.0f, &g_object_meshes[BOX_MESH], NULL);
    D3DXCreateCylinder(g_d3d_device, 1.0f, 1.0f, 3.0f, 10, 10, &g_object_meshes[CYLINDER_MESH], NULL);
    D3DXCreateTorus(g_d3d_device, 1.0f, 3.0f, 10, 10, &g_object_meshes[TORUS_MESH], NULL);
    D3DXCreateSphere(g_d3d_device, 1.0f, 10, 10, &g_object_meshes[SPHERE_MESH], NULL);
// Build world matrices - position the objects in world space.
// For example, g_object_world_matrices[1] will position g_object_meshes[1] at (-5, 0, 5).
// Likewise, g_object_world_matrices[2] will position g_object_meshes[2] at (5, 0, -5).
    D3DXMatrixTranslation(&g_object_world_matrices[TEAPOT_MESH],    0.0f, 0.0f, 0.0f);
    D3DXMatrixTranslation(&g_object_world_matrices[BOX_MESH],       -5.0f, 0.0f,  5.0f);
    D3DXMatrixTranslation(&g_object_world_matrices[CYLINDER_MESH],  5.0f, 0.0f,  5.0f);
    D3DXMatrixTranslation(&g_object_world_matrices[TORUS_MESH],       -5.0f, 0.0f, -5.0f);
    D3DXMatrixTranslation(&g_object_world_matrices[SPHERE_MESH],    5.0f, 0.0f, -5.0f);
// set the projection matrix
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
    g_d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
// set wireframe mode render state
    g_d3d_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
void cleanup()
{
for(int i = 0; i < NUM_MESH; i++)
        safe_release<ID3DXMesh*>(g_object_meshes[i]);
}
bool display(float time_delta)
{
// Animate the camera:
//
// The camera will circle around the center of the scene.  We use the sin and cos functions
// to generate points on the circle, then scale them by 10 to further the radius. 
// In addition the camera will move up and down as it circles about the scene.
static float angle = (3.0f * D3DX_PI) / 2.0f;
static float camera_height = 0.0f;
static float camera_height_dir = 10.0f;
    D3DXVECTOR3 position(cosf(angle) * 10.0f, camera_height, sinf(angle) * 10.0f);
// the camera is targetted at the origin of the world
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
// the worlds up vector
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX view_matrix;
    D3DXMatrixLookAtLH(&view_matrix, &position, &target, &up);
    g_d3d_device->SetTransform(D3DTS_VIEW, &view_matrix);
// compute the position for the next frame
    angle += time_delta;
if(angle >= 6.28f)
        angle = 0.0f;
// compute the height of the camera for the next frame
    camera_height += camera_height_dir * time_delta;
if(camera_height >= 20.0f || camera_height <= -20.0f)
        camera_height_dir = -camera_height_dir;
// draw the scene
    g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
    g_d3d_device->BeginScene();
for(int i = 0; i < NUM_MESH; i++)
    {
// set the world matrix that positions the object
        g_d3d_device->SetTransform(D3DTS_WORLD, &g_object_world_matrices[i]);
// draw the object using the previously set world matrix
        g_object_meshes[i]->DrawSubset(0);
    }
    g_d3d_device->EndScene();
    g_d3d_device->Present(NULL, NULL, NULL, NULL);
return true;
}
LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
{
switch(msg)
    {
case WM_DESTROY:
        PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(word_param == VK_ESCAPE)
            DestroyWindow(hwnd);
break;
    }
return DefWindowProc(hwnd, msg, word_param, long_param);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
{
if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_d3d_device))
    {
        MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
return 0;
    }
if(! setup())
    {
        MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
return 0;
    }
    enter_msg_loop(display);
    cleanup();
    g_d3d_device->Release();
return 0;
}

下载源程序

点赞
收藏
评论区
推荐文章
秃头王路飞 秃头王路飞
4个月前
webpack5手撸vue2脚手架
webpack5手撸vue相信工作个12年的小伙伴们在面试的时候多多少少怕被问到关于webpack方面的知识,本菜鸟最近闲来无事,就尝试了手撸了下vue2的脚手架,第一次发帖实在是没有经验,望海涵。languageJavaScript"name":"vuecliversion2","version":"1.0.0","desc
光头强的博客 光头强的博客
4个月前
Java面向对象试题
1、请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。创建一个接口A,接口里有一个抽象方法fly()。创建一个Bird类继承Animal类并实现接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿吃虫”。在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方
刚刚好 刚刚好
4个月前
css问题
1、在IOS中图片不显示(给图片加了圆角或者img没有父级)<div<imgsrc""/</divdiv{width:20px;height:20px;borderradius:20px;overflow:h
blmius blmius
1年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
小森森 小森森
4个月前
校园表白墙微信小程序V1.0 SayLove -基于微信云开发-一键快速搭建,开箱即用
后续会继续更新,敬请期待2.0全新版本欢迎添加左边的微信一起探讨!项目地址:(https://www.aliyun.com/activity/daily/bestoffer?userCodesskuuw5n)\2.Bug修复更新日历2.情侣脸功能大家不要使用了,现在阿里云的接口已经要收费了(土豪请随意),\\和注意
晴空闲云 晴空闲云
4个月前
css中box-sizing解放盒子实际宽高计算
我们知道传统的盒子模型,如果增加内边距padding和边框border,那么会撑大整个盒子,造成盒子的宽度不好计算,在实务中特别不方便。boxsizing可以设置盒模型的方式,可以很好的设置固定宽高的盒模型。盒子宽高计算假如我们设置如下盒子:宽度和高度均为200px,那么这会这个盒子实际的宽高就都是200px。但是当我们设置这个盒子的边框和内间距的时候,那
艾木酱 艾木酱
3个月前
快速入门|使用MemFire Cloud构建React Native应用程序
MemFireCloud是一款提供云数据库,用户可以创建云数据库,并对数据库进行管理,还可以对数据库进行备份操作。它还提供后端即服务,用户可以在1分钟内新建一个应用,使用自动生成的API和SDK,访问云数据库、对象存储、用户认证与授权等功能,可专
NVIDIA安培架构下MIG技术分析
关键词:NVIDIA、MIG、安培一什么是MIG2020年5月,NVIDIA发布了最新的GPU架构:安培,以及基于安培架构的最新的GPU:A100。安培提供了许多新的特性,MIG是其中一项非常重要的新特性。MIG的全名是MultiInstanceGPU。NVIDIA安培架构中的MIG模式可以在A100GPU上并行运行七个作业。多实
helloworld_28799839 helloworld_28799839
4个月前
常用知识整理
Javascript判断对象是否为空jsObject.keys(myObject).length0经常使用的三元运算我们经常遇到处理表格列状态字段如status的时候可以用到vue
helloworld_34035044 helloworld_34035044
7个月前
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
密钥管理系统-为你的天翼云资产上把“锁
本文关键词:数据安全,密码机,密钥管理一、你的云上资产真的安全么?1.2021年1月,巴西的一个数据库30TB数据被破坏,泄露的数据包含有1.04亿辆汽车和约4000万家公司的详细信息,受影响的人员数量可能有2.2亿;2.2021年2月,广受欢迎的音频聊天室应用Clubhouse的用户数据被恶意黑客或间谍窃取。据悉,一位身份不明的用户能够将Clubho