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