directX彩色图形绘制实验实验三报告.doc

上传人:龙*** 文档编号:1040287 上传时间:2018-11-22 格式:DOC 页数:25 大小:273KB
下载 相关 举报
directX彩色图形绘制实验实验三报告.doc_第1页
第1页 / 共25页
directX彩色图形绘制实验实验三报告.doc_第2页
第2页 / 共25页
directX彩色图形绘制实验实验三报告.doc_第3页
第3页 / 共25页
directX彩色图形绘制实验实验三报告.doc_第4页
第4页 / 共25页
directX彩色图形绘制实验实验三报告.doc_第5页
第5页 / 共25页
点击查看更多>>
资源描述

1、实验三 DirectX 彩色图形绘制实验实验报告项目 1: DirectX 彩色三角形渲染实验在例程 ColorTriangle 的基础上,完成以下步骤:1. 修改彩色顶点数据,实现三个不同的彩色三角形渲染。2. 修改三角形顶点的颜色值,使三个三角形分别为红、黄、兰三种不同的颜色。原代码图像更改左边三角型渲染模式得到的图像,将平面模式渲染三角形改为用 Gouraud模式渲染三角形Device-SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);改为Device-SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURA

2、UD);改变顶点颜色ColorVertex* v;原代码语句Triangle-Lock(0, 0, (void*)v0 = ColorVertex(-1.0f, 0.0f, 2.0f, D3DCOLOR_XRGB(255, 0, 0);v1 = ColorVertex( 0.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(0, 255, 0);v2 = ColorVertex( 1.0f, 0.0f, 2.0f, D3DCOLOR_XRGB( 0, 0, 255);Triangle-Unlock();将 v1 = ColorVertex( 0.0f, 1.0f, 2.0f, D3D

3、COLOR_XRGB(0, 255, 0);改为v1 = ColorVertex( 0.0f, 1.0f, 2.0f, D3DCOLOR_XRGB(255 255, 0);得到三个顶点为红黄蓝的三角形项目 2: DirectX 彩色立方体渲染实验在例程 Cub 的基础上,完成以下步骤:a) 修改立方体顶点数据,将顶点数据格式从 Vertex 结构改为 ColorVertex结构,顶点颜色都设为红色(D3DCOLOR_XRGB(255, 0, 0)) 。注意Device-CreateVertexBuffer()函数的参数设置,以及 ColorVertex 顶点数据的设置。b) 修改 Displa

4、y()函数中的 Device-SetStreamSource()函数和 Device-SetFVF()函数的参数设置,以及增加 Device-SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD)的调用。实现彩色立方体的渲染。c) 列出彩色顶点数据的使用步骤,说明顶点数据结构的定义、缓冲区创建、顶点数据设置、缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染等各个步骤对应的语句。在例程 Cub 的基础上,完成以下步骤:d) 修改立方体顶点数据,将顶点数据格式从 Vertex 结构改为 ColorVertex结构,顶点颜色都设为红色(D3DCOLO

5、R_XRGB(255, 0, 0)) 。注意Device-CreateVertexBuffer()函数的参数设置,以及 ColorVertex 顶点数据的设置。e) 修改 Display()函数中的 Device-SetStreamSource()函数和 Device-SetFVF()函数的参数设置,以及增加 Device-SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD)的调用。实现彩色立方体的渲染。f) 列出彩色顶点数据的使用步骤,说明顶点数据结构的定义、缓冲区创建、顶点数据设置、缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染等各个步骤

6、对应的语句。顶点数据结构的定义struct ColorVertexColorVertex()ColorVertex(float x, float y, float z,D3DCOLOR c)_x = x; _y = y; _z = z; _color = c;float _x, _y, _z;D3DCOLOR _color;static const DWORD FVF;const DWORD ColorVertex:FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;缓冲区创建Device-CreateVertexBuffer(8 * sizeof(ColorVertex),

7、D3DUSAGE_WRITEONLY,ColorVertex:FVF,D3DPOOL_MANAGED,顶点数据设置ColorVertex* vertices;VB-Lock(0, 0, (void*)/ vertices of a unit cubevertices0 = ColorVertex(-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices1 = ColorVertex(-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices2 = ColorVertex( 1.0f, 1.0f,

8、 -1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices3 = ColorVertex( 1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices4 = ColorVertex(-1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices5 = ColorVertex(-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0);vertices6 = ColorVertex( 1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255

9、, 0, 0);vertices7 = ColorVertex( 1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0);VB-Unlock();缓冲区数据设置到渲染引擎,渲染状态设置为插值模式,渲染bool Display(float timeDelta)if( Device )/ spin the cube:/D3DXMATRIX Rx, Ry;/ rotate 45 degrees on x-axisD3DXMatrixRotationX(/ incremement y-rotation angle each framestatic float y = 0

10、.0f;D3DXMatrixRotationY(y += timeDelta;/ reset angle to zero when angle reaches 2*PIif( y = 6.28f )y = 0.0f;/ combine x- and y-axis rotation transformations.D3DXMATRIX p = Rx * Ry;Device-SetTransform(D3DTS_WORLD, / draw the scene:/Device-Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.

11、0f, 0);Device-BeginScene();Device-SetStreamSource(0, VB, 0, sizeof(ColorVertex);Device-SetIndices(IB);Device-SetFVF(ColorVertex:FVF);/ Draw cube.Device-DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);Device-SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);Device-EndScene();Device-Present(0,

12、 0, 0, 0);return true;项目 3: 模型创建渲染实验在 Cube 程序的基础上,完成以下步骤:a) 创建立方体,茶壶,圆柱体,圆环,球体等 5 种内置模型;b) 将 5 个模型分别移动到不同的位置,按照不同的速度和方向进行旋转。c) 将 5 个模型分别用不同的材质渲染。d) 将 5 个模型分别进行不同比例的缩放。/ / File: cube.cpp/ / Author: Frank Luna (C) All Rights Reserved/ System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC+

13、7.0 / Desc: Renders a spinning cube in wireframe mode. Demonstrates vertex and / index buffers, world and view transformations, render states and/ drawing commands./ /#include “d3dUtility.h“#include “texcube.h“/ Globals/IDirect3DDevice9* Device = 0; const int Width = 640;const int Height = 480;IDire

14、ct3DTexture9* Tex = 0;Cube* Box = 0;IDirect3DVertexBuffer9* VB = 0;IDirect3DIndexBuffer9* IB = 0;D3DXMATRIX WorldMatrix;ID3DXMesh* Objects5 = 0, 0, 0, 0,0;D3DMATERIAL9 Mtrls5;D3DXMATRIX Worlds5;D3DXMATRIX scales5;/ Classes and Structures/struct VertexVertex()Vertex(float x, float y, float z, D3DCOLO

15、R c)_x = x; _y = y; _z = z;_color = c;float _x, _y, _z;D3DCOLOR _color;static const DWORD FVF;const DWORD Vertex:FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;/ Framework Functions/bool Setup()/ Create vertex and index buffers./D3DXCreateTeapot(Device, D3DXMatrixTranslation(Mtrls0 =d3d: RED_MTRL;D3DXMatrixScali

16、ng(D3DXCreateSphere(Device, 1.0f, 20, 20, D3DXMatrixTranslation(Mtrls1 = d3d:BLUE_MTRL;D3DXMatrixScaling(D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, D3DXMatrixTranslation(Mtrls2 = d3d:GREEN_MTRL;D3DXMatrixScaling(D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, D3DXMatrixTranslation(Mtrls3 = d3d

17、:YELLOW_MTRL;D3DXMatrixScaling(Device-CreateVertexBuffer(8 * sizeof(Vertex), D3DUSAGE_WRITEONLY,Vertex:FVF,D3DPOOL_MANAGED,Device-CreateIndexBuffer(36 * sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,/ Fill the buffers with the cube data./ define unique vertices:Vertex* vertices;VB-L

18、ock(0, 0, (void*)/ vertices of a unit cubevertices0 = Vertex(-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 100);vertices1 = Vertex(-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(30, 255, 150);vertices2 = Vertex(1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 199, 0);vertices3 = Vertex(1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(400, 2

19、00, 0);vertices4 = Vertex(-1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 100);vertices5 = Vertex(-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(30, 255, 150);vertices6 = Vertex(1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 199, 0);vertices7 = Vertex(1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(400, 200, 0);/vertices8 = Vertex( 2.0f, -1.0f

20、, 1.0f, D3DCOLOR_XRGB(65, 211, 0 );VB-Unlock();/ define the triangles of the cube:WORD* indices = 0;IB-Lock(0, 0, (void*)/ front sideindices0 = 0; indices1 = 1; indices2 = 2;indices3 = 0; indices4 = 2; indices5 = 3;/ back sideindices6 = 4; indices7 = 6; indices8 = 5;indices9 = 4; indices10 = 7; indi

21、ces11 = 6;/ left sideindices12 = 4; indices13 = 5; indices14 = 1;indices15 = 4; indices16 = 1; indices17 = 0;/ right sideindices18 = 3; indices19 = 2; indices20 = 6;indices21 = 3; indices22 = 6; indices23 = 7;/ topindices24 = 1; indices25 = 5; indices26 = 6;indices27 = 1; indices28 = 6; indices29 =

22、2;/ bottomindices30 = 4; indices31 = 0; indices32 = 3;indices33 = 4; indices34 = 3; indices35 = 7;/indices36 = 6; indices37 = 7; indices38 = 8;IB-Unlock();Box = new Cube(Device);D3DXCreateTextureFromFile(Device,“crate.jpg“,/ Position and aim the camera./D3DXVECTOR3 position(0.0f, 0.0f,5.0f);D3DXVECT

23、OR3 target(0.0f, 0.0f, 0.0f);D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);D3DXMATRIX V;D3DXMatrixLookAtLH(Device-SetTransform(D3DTS_VIEW, / Set the projection matrix./D3DXMATRIX proj;D3DXMatrixPerspectiveFovLH(Device-SetTransform(D3DTS_PROJECTION, D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f);D3DXCOLOR c = d3d:WHITE;D3DL

24、IGHT9 dirLight = d3d:InitDirectionalLight(/ Set and Enable the light./Device-SetLight(0, Device-LightEnable(0, true);/ Switch to wireframe mode./ / Set Texture Filter States./Device-SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);Device-SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);Device-SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);/ set alpha blending stuff

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 课件讲义

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。