directX实验三报告.docx
- 文档编号:10326426
- 上传时间:2023-02-10
- 格式:DOCX
- 页数:34
- 大小:192.94KB
directX实验三报告.docx
《directX实验三报告.docx》由会员分享,可在线阅读,更多相关《directX实验三报告.docx(34页珍藏版)》请在冰豆网上搜索。
directX实验三报告
实验三DirectX彩色图形绘制实验
实验报告
项目1:
DirectX彩色三角形渲染实验
在例程ColorTriangle的基础上,完成以下步骤:
1.修改彩色顶点数据,实现三个不同的彩色三角形渲染。
2.修改三角形顶点的颜色值,使三个三角形分别为红、黄、兰三种不同的颜色。
原代码图像
更改左边三角型渲染模式得到的图像,
将平面模式渲染三角形改为用Gouraud模式渲染三角形
Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
改为
Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
改变顶点颜色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();
将v[1]=ColorVertex(0.0f,1.0f,2.0f,D3DCOLOR_XRGB(0,255,0));
改为
v[1]=ColorVertex(0.0f,1.0f,2.0f,D3DCOLOR_XRGB(255255,0));
得到三个顶点为红黄蓝的三角形
项目2:
DirectX彩色立方体渲染实验
在例程Cub的基础上,完成以下步骤:
a)修改立方体顶点数据,将顶点数据格式从Vertex结构改为ColorVertex结构,顶点颜色都设为红色(D3DCOLOR_XRGB(255,0,0))。
注意Device->CreateVertexBuffer()函数的参数设置,以及ColorVertex顶点数据的设置。
b)修改Display()函数中的Device->SetStreamSource()函数和Device->SetFVF()函数的参数设置,以及增加Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD)的调用。
实现彩色立方体的渲染。
c)列出彩色顶点数据的使用步骤,说明顶点数据结构的定义、缓冲区创建、顶点数据设置、缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染等各个步骤对应的语句。
在例程Cub的基础上,完成以下步骤:
d)修改立方体顶点数据,将顶点数据格式从Vertex结构改为ColorVertex结构,顶点颜色都设为红色(D3DCOLOR_XRGB(255,0,0))。
注意Device->CreateVertexBuffer()函数的参数设置,以及ColorVertex顶点数据的设置。
e)修改Display()函数中的Device->SetStreamSource()函数和Device->SetFVF()函数的参数设置,以及增加Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD)的调用。
实现彩色立方体的渲染。
f)列出彩色顶点数据的使用步骤,说明顶点数据结构的定义、缓冲区创建、顶点数据设置、缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染等各个步骤对应的语句。
顶点数据结构的定义
structColorVertex
{
ColorVertex(){}
ColorVertex(floatx,floaty,floatz,D3DCOLORc)
{
_x=x;_y=y;_z=z;_color=c;
}
float_x,_y,_z;
D3DCOLOR_color;
staticconstDWORDFVF;
};
constDWORDColorVertex:
:
FVF=D3DFVF_XYZ|D3DFVF_DIFFUSE;
缓冲区创建
Device->CreateVertexBuffer(
8*sizeof(ColorVertex),
D3DUSAGE_WRITEONLY,
ColorVertex:
:
FVF,
D3DPOOL_MANAGED,
&VB,
0);
顶点数据设置
ColorVertex*vertices;
VB->Lock(0,0,(void**)&vertices,0);
//verticesofaunitcube
vertices[0]=ColorVertex(-1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[1]=ColorVertex(-1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[2]=ColorVertex(1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[3]=ColorVertex(1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[4]=ColorVertex(-1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[5]=ColorVertex(-1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[6]=ColorVertex(1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,0,0));
vertices[7]=ColorVertex(1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(255,0,0));
VB->Unlock();
缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染
boolDisplay(floattimeDelta)
{
if(Device)
{
//
//spinthecube:
//
D3DXMATRIXRx,Ry;
//rotate45degreesonx-axis
D3DXMatrixRotationX(&Rx,3.14f/4.0f);
//incremementy-rotationangleeachframe
staticfloaty=0.0f;
D3DXMatrixRotationY(&Ry,y);
y+=timeDelta;
//resetangletozerowhenanglereaches2*PI
if(y>=6.28f)
y=0.0f;
//combinex-andy-axisrotationtransformations.
D3DXMATRIXp=Rx*Ry;
Device->SetTransform(D3DTS_WORLD,&p);
//
//drawthescene:
//
Device->Clear(0,0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xffffffff,1.0f,0);
Device->BeginScene();
Device->SetStreamSource(0,VB,0,sizeof(ColorVertex));
Device->SetIndices(IB);
Device->SetFVF(ColorVertex:
:
FVF);
//Drawcube.
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
Device->EndScene();
Device->Present(0,0,0,0);
}
returntrue;
}
项目3:
模型创建渲染实验
在Cube程序的基础上,完成以下步骤:
a)创建立方体,茶壶,圆柱体,圆环,球体等5种内置模型;
b)将5个模型分别移动到不同的位置,按照不同的速度和方向进行旋转。
c)将5个模型分别用不同的材质渲染。
d)将5个模型分别进行不同比例的缩放。
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//File:
cube.cpp
//
//Author:
FrankLuna(C)AllRightsReserved
//
//System:
AMDAthlon1800+XP,512DDR,Geforce3,WindowsXP,MSVC++7.0
//
//Desc:
Rendersaspinningcubeinwireframemode.Demonstratesvertexand
//indexbuffers,worldandviewtransformations,renderstatesand
//drawingcommands.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include"d3dUtility.h"
#include"texcube.h"
//
//Globals
//
IDirect3DDevice9*Device=0;
constintWidth=640;
constintHeight=480;
IDirect3DTexture9*Tex=0;
Cube*Box=0;
IDirect3DVertexBuffer9*VB=0;
IDirect3DIndexBuffer9*IB=0;
D3DXMATRIXWorldMatrix;
ID3DXMesh*Objects[5]={0,0,0,0,0};
D3DMATERIAL9Mtrls[5];
D3DXMATRIXWorlds[5];
D3DXMATRIXscales[5];
//
//ClassesandStructures
//
structVertex
{
Vertex(){}
Vertex(floatx,floaty,floatz,D3DCOLORc)
{
_x=x;_y=y;_z=z;_color=c;
}
float_x,_y,_z;
D3DCOLOR_color;
staticconstDWORDFVF;
};
constDWORDVertex:
:
FVF=D3DFVF_XYZ|D3DFVF_DIFFUSE;
//
//FrameworkFunctions
//
boolSetup()
{
//
//Createvertexandindexbuffers.
//
D3DXCreateTeapot(Device,&Objects[0],0);
D3DXMatrixTranslation(&Worlds[0],0.0f,3.0f,0.0f);
Mtrls[0]=d3d:
:
RED_MTRL;
D3DXMatrixScaling(&scales[0],1.5f,1.5f,1.5f);
D3DXCreateSphere(Device,1.0f,20,20,&Objects[1],0);
D3DXMatrixTranslation(&Worlds[1],0.0f,-2.0f,0.0f);
Mtrls[1]=d3d:
:
BLUE_MTRL;
D3DXMatrixScaling(&scales[1],0.5f,0.5f,0.5f);
D3DXCreateTorus(Device,0.5f,1.0f,20,20,&Objects[2],0);
D3DXMatrixTranslation(&Worlds[2],-3.0f,0.0f,0.0f);
Mtrls[2]=d3d:
:
GREEN_MTRL;
D3DXMatrixScaling(&scales[2],0.3f,0.3f,0.3f);
D3DXCreateCylinder(Device,0.5f,0.5f,2.0f,20,20,&Objects[3],0);
D3DXMatrixTranslation(&Worlds[3],3.0f,0.0f,0.0f);
Mtrls[3]=d3d:
:
YELLOW_MTRL;
D3DXMatrixScaling(&scales[3],1.2f,1.2f,1.2f);
Device->CreateVertexBuffer(
8*sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex:
:
FVF,
D3DPOOL_MANAGED,
&VB,
0);
Device->CreateIndexBuffer(
36*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IB,
0);
//
//Fillthebufferswiththecubedata.
//
//defineuniquevertices:
Vertex*vertices;
VB->Lock(0,0,(void**)&vertices,0);
//verticesofaunitcube
vertices[0]=Vertex(-1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(255,0,100));
vertices[1]=Vertex(-1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(30,255,150));
vertices[2]=Vertex(1.0f,1.0f,-1.0f,D3DCOLOR_XRGB(255,199,0));
vertices[3]=Vertex(1.0f,-1.0f,-1.0f,D3DCOLOR_XRGB(400,200,0));
vertices[4]=Vertex(-1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(255,0,100));
vertices[5]=Vertex(-1.0f,1.0f,1.0f,D3DCOLOR_XRGB(30,255,150));
vertices[6]=Vertex(1.0f,1.0f,1.0f,D3DCOLOR_XRGB(255,199,0));
vertices[7]=Vertex(1.0f,-1.0f,1.0f,D3DCOLOR_XRGB(400,200,0));
//vertices[8]=Vertex(2.0f,-1.0f,1.0f,D3DCOLOR_XRGB(65,211,0));
VB->Unlock();
//definethetrianglesofthecube:
WORD*indices=0;
IB->Lock(0,0,(void**)&indices,0);
//frontside
indices[0]=0;indices[1]=1;indices[2]=2;
indices[3]=0;indices[4]=2;indices[5]=3;
//backside
indices[6]=4;indices[7]=6;indices[8]=5;
indices[9]=4;indices[10]=7;indices[11]=6;
//leftside
indices[12]=4;indices[13]=5;indices[14]=1;
indices[15]=4;indices[16]=1;indices[17]=0;
//rightside
indices[18]=3;indices[19]=2;indices[20]=6;
indices[21]=3;indices[22]=6;indices[23]=7;
//top
indices[24]=1;indices[25]=5;indices[26]=6;
indices[27]=1;indices[28]=6;indices[29]=2;
//bottom
indices[30]=4;indices[31]=0;indices[32]=3;
indices[33]=4;indices[34]=3;indices[35]=7;
//indices[36]=6;indices[37]=7;indices[38]=8;
IB->Unlock();
Box=newCube(Device);
D3DXCreateTextureFromFile(
Device,
"crate.jpg",
&Tex);
//
//Positionandaimthecamera.
//
D3DXVECTOR3position(0.0f,0.0f,5.0f);
D3DXVECTOR3target(0.0f,0.0f,0.0f);
D3DXVECTOR3up(0.0f,1.0f,0.0f);
D3DXMATRIXV;
D3DXMatrixLookAtLH(&V,&position,&target,&up);
Device->SetTransform(D3DTS_VIEW,&V);
//
//Settheprojectionmatrix.
//
D3DXMATRIXproj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI*0.5f,//90-degree
(float)Width/(float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION,&proj);
D3DXVECTOR3dir(1.0f,-0.0f,0.25f);
D3DXCOLORc=d3d:
:
WHITE;
D3DLIGHT9dirLight=d3d:
:
InitDirectionalLight(&dir,&c);
//
//SetandEnablethelight.
//
Device->SetLight(0,&dirLight);
Device->LightEnable(0,true);
//
//Switchtowireframemode.
//
//
//SetTextureFilterStates.
//
Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
Device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
//
//setalphablendingstuff
//
//usealphachannelintextureforalpha
Device->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
Device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
//setblendingfactorssothatalphacomponentdeterminestransparency
Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
//Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
//Device->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
//Device->SetRenderState(D3DRS_LIGHTING,false);
returntrue;
}
voidCleanup()
{
d3d:
:
Release
d3d:
:
Release
d3d:
:
Release
d3d:
:
Delete
for(inti=0;i<5;i++)
d3d:
:
Release
}
boolDisplay(floattimeDel
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- directX 实验 报告