DirectX3D中繪製立方體

2021-06-01 04:43:28 字數 3517 閱讀 3881

idirect3ddevice9* g_device = null;

idirect3dvertexbuffer9* g_vb = 0; //立方體頂點

idirect3dindexbuffer9* g_ib = 0; //索引資料

struct vertex 

vertex(float x, float y, float z)

float _x, _y, _z;

static const dword fvf;

};const dword vertex::fvf = d3dfvf_xyz;

//建立頂點,索引快取

g_device->createvertexbuffer(8*sizeof(vertex),

d3dusage_writeonly,

vertex::fvf,

d3dpool_managed,

&g_vb,

0);g_device->createindexbuffer(36*sizeof(dword),

d3dusage_writeonly,

d3dfmt_index16,

d3dpool_managed,

&g_ib,

0);//向立方體的頂點快取填充資料

vertex* vertices;

g_vb->lock(0, 0, (void**)&vertices, 0);

vertices[0]=vertex(-1.0f, -1.0f, -1.0f);

vertices[1]=vertex(-1.0f, 1.0f, -1.0f);

vertices[2]=vertex(1.0f, 1.0f, -1.0f);

vertices[3]=vertex(1.0f, -1.0f, -1.0f);

vertices[4]=vertex(-1.0f, -1.0f, 1.0f);

vertices[5]=vertex(-1.0f, 1.0f, 1.0f);

vertices[6]=vertex(1.0f, 1.0f, 1.0f);

vertices[7]=vertex(1.0f, -1.0f, 1.0f);

g_vb->unlock();

//定義立方體的三角形

word* indices = 0;

g_ib->lock(0,0,(void**)&indices, 0);

//前面

indices[0]=0; indices[1]=1; indices[2]=2;

indices[3]=0; indices[4]=2; indices[5]=3;

//背面

indices[6]=4; indices[7]=6; indices[8]=5;

indices[9]=4; indices[10]=7; indices[11]=6;

//左面

indices[12]=4; indices[13]=5; indices[14]=1;

indices[15]=4; indices[16]=1; indices[17]=0;

//右面

indices[18]=3; indices[19]=2; indices[20]=6;

indices[21]=3; indices[22]=6; indices[23]=7;

//頂部

indices[24]=1; indices[25]=5; indices[26]=6;

indices[27]=1; indices[28]=6; indices[29]=2;

//底部

indices[30]=4; indices[31]=0; indices[32]=3;

indices[33]=4; indices[34]=3; indices[35]=7;

g_ib->unlock();

//照相機位置(檢視矩陣)

d3dxvector3 poistion(0.0f,0.0f,-5.0f);

d3dxvector3 target(0.0f,0.0f,0.0f);

d3dxvector3 up(0.0f,1.0f,0.0f);

d3dxmatrix v;

d3dxmatrixlookatlh(&v, &poistion, &target, &up);

g_device->settransform(d3dts_view, &v);

//投影矩陣

d3dxmatrix proj;

d3dxmatrixperspectivefovlh(&proj,

d3dx_pi*0.5f, //90-degree

(float)window_width/(float)window_height,

1.0f,

1000.0f);

g_device->settransform(d3dts_projection, &proj);

//渲染狀態(填充模式:框架填充)

g_device->setrenderstate(d3drs_fillmode, d3dfill_wireframe);

if (g_device)

{ //旋轉立方體

d3dxmatrix rx, ry;

//x軸旋轉45弧度

d3dxmatrixrotationx(&rx, 3.14f/4.0f);

//每一幀中增加y軸的弧度

static float y = 0.0f;

d3dxmatrixrotationy(&ry, y);

y += timedelta;

//當y軸旋轉2週時,重新回到0弧度

if (y>=6.28f)

y=0.0f;

//結合x軸與y軸的旋轉矩陣

d3dxmatrix p = rx * ry;

g_device->settransform(d3dts_world, &p);

//清空目標快取和深度快取

g_device->clear(0,0,d3dclear_target | d3dclear_zbuffer,

0xffffffff, 1.0f, 0);

//開始繪製場景

g_device->beginscene();

g_device->setstreamsource(0, g_vb, 0,sizeof(vertex)); //設定資源流

g_device->setindices(g_ib); //設定索引快取

g_device->setfvf(vertex::fvf); //設定頂點格式

//利用索引快取繪製

g_device->drawindexedprimitive(d3dpt_********list,0,0,8,0,12);

g_device->endscene();//結束繪製場景

//翻轉表面

g_device->present(0,0,0,0);

XNA 3D 繪製立方體

一.要點 繪製立方體 或其他3d圖形 的方法與繪製三角形的方法類似,任何乙個3d圖形的輪廓都有一系列三角形構成.為減少資料冗餘,在繪製複雜3d圖形時,應使用 graphicsdevice.drawindexedprimitives 方法,而不是繪製三角形時所使用的graphicsdevice.dra...

Qt使用QPainter繪製3D立方體

1.實現思路 網上有另一篇類似的,不過他不是用的 qt 自帶的矩陣運算類 實現思路有點類似使用 opengl 畫立方體,先準備頂點資料 立方體前後四個頂點,從右上角開始順時針 vertexarr qvector,qvector3d,qvector3d,qvector3d,qvector3d,qvec...

OpenGL WebGL 繪製立方體

include 繪製立方體 將立方體的八個頂點儲存到乙個陣列裡面 static const float vertex list 3 將要使用的頂點的序號儲存到乙個陣列裡面 static const glint index list 2 繪製立方體 void drawcube void glend s...