[843] | 1 |
|
---|
| 2 | #include "dxstdafx.h"
|
---|
| 3 | #include "Cube.h"
|
---|
| 4 |
|
---|
| 5 | #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
|
---|
| 6 |
|
---|
| 7 | struct Vertex
|
---|
| 8 | {
|
---|
| 9 | float x, y, z;
|
---|
| 10 | float nx, ny, nz;
|
---|
| 11 | float tu, tv;
|
---|
| 12 | };
|
---|
| 13 |
|
---|
| 14 | Cube::Cube(IDirect3DDevice9* pd3dDevice)
|
---|
| 15 | {
|
---|
| 16 | this->pd3dDevice = pd3dDevice;
|
---|
| 17 |
|
---|
| 18 | //pVertexBuffer
|
---|
| 19 |
|
---|
| 20 | int vertexCnt = 24;
|
---|
| 21 | float s = 1; // igazából mindegy a méret, mert csak a kocka skálázódik vele
|
---|
| 22 |
|
---|
| 23 | Vertex g_quadVertices[] =
|
---|
| 24 | {
|
---|
| 25 | // 1 lap = 2 háromszögbõl álló triangle_strip
|
---|
| 26 | // lapok körüljárási irányának megváltoztatása: a középsõ 2 vertex felcserélésével
|
---|
| 27 |
|
---|
| 28 | //x y z normal texcoord
|
---|
| 29 | {-s, s, s, 0,0,-1, 0,0 },
|
---|
| 30 | {-s,-s, s, 0,0,-1, 0,1 },
|
---|
| 31 | { s, s, s, 0,0,-1, 1,0 },
|
---|
| 32 | { s,-s, s, 0,0,-1, 1,1 },
|
---|
| 33 |
|
---|
| 34 | // z érték -1-szeres + 2. és 3. vertex cseréje
|
---|
| 35 | {-s, s, -s, 0,0,1, 0,0 },
|
---|
| 36 | { s, s, -s, 0,0,1, 1,0 },
|
---|
| 37 | {-s,-s, -s, 0,0,1, 0,1 },
|
---|
| 38 | { s,-s, -s, 0,0,1, 1,1 },
|
---|
| 39 |
|
---|
| 40 | // y,z csere
|
---|
| 41 | {-s,-s, s, 0,1,0, 0,0 },
|
---|
| 42 | {-s,-s, -s, 0,1,0, 0,1 },
|
---|
| 43 | { s,-s, s, 0,1,0, 1,0 },
|
---|
| 44 | { s,-s, -s, 0,1,0, 1,1 },
|
---|
| 45 |
|
---|
| 46 | {-s, s, s, 0,-1,0, 0,0 },
|
---|
| 47 | { s, s, s, 0,-1,0, 1,0 },
|
---|
| 48 | {-s, s,-s, 0,-1,0, 0,1 },
|
---|
| 49 | { s, s,-s, 0,-1,0, 1,1 },
|
---|
| 50 |
|
---|
| 51 | // x,z csere
|
---|
| 52 | {-s, s,-s, 1,0,0, 0,0 },
|
---|
| 53 | {-s,-s,-s, 1,0,0, 0,1 },
|
---|
| 54 | {-s, s, s, 1,0,0, 1,0 },
|
---|
| 55 | {-s,-s, s, 1,0,0, 1,1 },
|
---|
| 56 |
|
---|
| 57 | { s, s, -s, -1,0,0, 0,0 },
|
---|
| 58 | { s, s, s, -1,0,0, 1,0 },
|
---|
| 59 | { s,-s, -s, -1,0,0, 0,1 },
|
---|
| 60 | { s,-s, s, -1,0,0, 1,1 },
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | pd3dDevice->CreateVertexBuffer( vertexCnt*sizeof(Vertex), D3DUSAGE_WRITEONLY,
|
---|
| 64 | D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT,
|
---|
| 65 | &pVertexBuffer, NULL );
|
---|
| 66 | void *pVertices = NULL;
|
---|
| 67 |
|
---|
| 68 | pVertexBuffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 );
|
---|
| 69 | memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) );
|
---|
| 70 | pVertexBuffer->Unlock();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void Cube::Destroy() {
|
---|
| 74 | SAFE_RELEASE( pVertexBuffer );
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void Cube::PrepareDrawing()
|
---|
| 78 | {
|
---|
| 79 | pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(Vertex) );
|
---|
| 80 | pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void Cube::DrawFace(int index)
|
---|
| 84 | {
|
---|
| 85 | pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4*index, 2 ); // startVertex, primitiveCnt
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void Cube::Draw()
|
---|
| 89 | {
|
---|
| 90 | for (int index = 0; index < 6; index++)
|
---|
| 91 | pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4*index, 2 ); // startVertex, primitiveCnt
|
---|
| 92 | }
|
---|