home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CDX.ZIP / Src / C3d / Mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-10  |  5.2 KB  |  176 lines

  1. //////////////////////////////////////////////////////////////////////////////////
  2. // Project Name: [ C3D Class Library - C3D.lib ]
  3. // Author:       [ Dan Farley - 97308096@brookes.ac.uk ]
  4. // Source File:  [ C3D_Mesh Implementation ]
  5. // Revision:     [ 1.6 ]
  6. //////////////////////////////////////////////////////////////////////////////////
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include "C3D.h"
  10.  
  11. //////////////////////////////////////////////////////////////////////////////////
  12. // C3D_Mesh constructor
  13. //////////////////////////////////////////////////////////////////////////////////
  14. C3D_Mesh::C3D_Mesh(void)
  15. {
  16.     m_VertexCount = 0;
  17.     m_IndexCount = 0;
  18.     m_Vertices = NULL;
  19.     m_Indices = NULL;
  20. }
  21.  
  22. //////////////////////////////////////////////////////////////////////////////////
  23. // C3D_Mesh destructor
  24. //////////////////////////////////////////////////////////////////////////////////
  25. C3D_Mesh::~C3D_Mesh(void)
  26. {
  27.     SAFEDELETE(m_Vertices);
  28.     SAFEDELETE(m_Indices);
  29. }
  30.  
  31. //////////////////////////////////////////////////////////////////////////////////
  32. // C3D_Mesh SetPosition
  33. //////////////////////////////////////////////////////////////////////////////////
  34. void C3D_Mesh::SetPosition(float x, float y, float z)
  35. {
  36.     m_Position.x = x;
  37.     m_Position.y = y;
  38.     m_Position.z = z;
  39. }
  40.  
  41. //////////////////////////////////////////////////////////////////////////////////
  42. // C3D_Mesh SetHeading
  43. //////////////////////////////////////////////////////////////////////////////////
  44. void C3D_Mesh::SetHeading(float x, float y, float z)
  45. {
  46.     m_Heading.x;
  47.     m_Heading.y;
  48.     m_Heading.z;
  49. }
  50.  
  51. //////////////////////////////////////////////////////////////////////////////////
  52. // C3D_Mesh CreateCube
  53. //////////////////////////////////////////////////////////////////////////////////
  54. void C3D_Mesh::CreateCube(float l, float w, float h)
  55. {
  56.     m_VertexCount = 8;
  57.     m_Vertices = new C3D_Vertex[m_VertexCount];
  58.  
  59.     m_IndexCount = 36;
  60.     m_Indices = new WORD[m_IndexCount];
  61.  
  62.     m_Vertices[0] = C3D_Vertex(-l, +w, -h, D3DRGB(1,0,0), D3DRGB(0,0,0));
  63.     m_Vertices[1] = C3D_Vertex(+l, +w, -h, D3DRGB(1,0,0), D3DRGB(0,0,0));
  64.     m_Vertices[2] = C3D_Vertex(+l, -w, -h, D3DRGB(0,1,0), D3DRGB(0,0,0));
  65.     m_Vertices[3] = C3D_Vertex(-l, -w, -h, D3DRGB(0,1,0), D3DRGB(0,0,0));
  66.     m_Vertices[4] = C3D_Vertex(-l, +w, +h, D3DRGB(0,0,1), D3DRGB(0,0,0));
  67.     m_Vertices[5] = C3D_Vertex(+l, +w, +h, D3DRGB(0,0,1), D3DRGB(0,0,0));
  68.     m_Vertices[6] = C3D_Vertex(+l, -w, +h, D3DRGB(1,1,0), D3DRGB(0,0,0));
  69.     m_Vertices[7] = C3D_Vertex(-l, -w, +h, D3DRGB(1,1,0), D3DRGB(0,0,0));
  70.  
  71.     // FRONT
  72.     m_Indices[0] = 0; m_Indices[1] = 1; m_Indices[2] = 3;
  73.     m_Indices[3] = 3; m_Indices[4] = 1; m_Indices[5] = 2;
  74.  
  75.     // BACK
  76.     m_Indices[6] = 4; m_Indices[7] = 7; m_Indices[8] = 5;
  77.     m_Indices[9] = 5; m_Indices[10] = 7; m_Indices[11] = 6;
  78.  
  79.     // TOP
  80.     m_Indices[12] = 0; m_Indices[13] = 4; m_Indices[14] = 1;
  81.     m_Indices[15] = 1; m_Indices[16] = 4; m_Indices[17] = 5;
  82.  
  83.     // BOTTOM
  84.     m_Indices[18] = 3; m_Indices[19] = 2; m_Indices[20] = 7;
  85.     m_Indices[21] = 7; m_Indices[22] = 2; m_Indices[23] = 6;
  86.  
  87.     // LEFT
  88.     m_Indices[24] = 7; m_Indices[25] = 4; m_Indices[26] = 0;
  89.     m_Indices[27] = 7; m_Indices[28] = 0; m_Indices[29] = 3;
  90.  
  91.     // RIGHT
  92.     m_Indices[30] = 2; m_Indices[31] = 1; m_Indices[32] = 5;
  93.     m_Indices[33] = 2; m_Indices[34] = 5; m_Indices[35] = 6;
  94. }
  95.  
  96. //////////////////////////////////////////////////////////////////////////////////
  97. // C3D_Mesh CreateSphere
  98. //////////////////////////////////////////////////////////////////////////////////
  99. void C3D_Mesh::CreateSphere(void)
  100. {
  101. }
  102.  
  103. #define GRID_DEPTH 64
  104. #define GRID_WIDTH 64
  105.  
  106. #define VERTEX_COUNT (GRID_DEPTH + 1)
  107. #define VERTEX_SPACING 500.0f
  108.  
  109. //////////////////////////////////////////////////////////////////////////////////
  110. // C3D_Mesh CreateTerrain
  111. //////////////////////////////////////////////////////////////////////////////////
  112. void C3D_Mesh::CreateTerrain(void)
  113. {
  114.     int i, j, loop;
  115.  
  116.     // Seed the random number generator
  117.     srand((unsigned)time( NULL ));
  118.  
  119.     m_VertexCount = VERTEX_COUNT * VERTEX_COUNT;
  120.     m_Vertices = new C3D_Vertex[m_VertexCount];
  121.  
  122.     m_IndexCount = GRID_DEPTH * GRID_WIDTH * 6;
  123.     m_Indices = new WORD[m_IndexCount];
  124.  
  125.     // Create the grid vertices
  126.     float fSize   = VERTEX_SPACING / (VERTEX_COUNT - 1.0f);
  127.     float fOffset = VERTEX_SPACING / 2.0f;
  128.  
  129.     for(i = 0; i < VERTEX_COUNT; i++) 
  130.     {
  131.         for(j = 0; j < VERTEX_COUNT; j++) 
  132.         {
  133.             float x = i * fSize - fOffset;
  134.             float y = -10.0f+rand()%5;
  135.             float z = j * fSize - fOffset;
  136.  
  137.             float r = 0.0f;
  138.             float g = 1.5f - (y / -10.0f);
  139.             float b = 0.0f;
  140.  
  141.             C3D_Vertex v(x, y, z, D3DRGB(r,g,b), 0);
  142.             m_Vertices[j + i * VERTEX_COUNT] = v;
  143.         }
  144.     }
  145.  
  146.     // Create indices into the vertex array
  147.     loop = i = j = 0;
  148.  
  149.     for(j = 0; j < (GRID_DEPTH * GRID_WIDTH * 3); j += (GRID_DEPTH * 3))
  150.     {
  151.         for(i = 0; i < (GRID_DEPTH * 3); i += 3)
  152.         {
  153.             m_Indices[(i+j)]   = loop;
  154.             m_Indices[(i+j)+1] = loop + 1;
  155.             m_Indices[(i+j)+2] = loop + GRID_DEPTH + 1;
  156.             loop++;
  157.         }
  158.         loop++;
  159.     }
  160.  
  161.     i = j = 0;
  162.     loop = 1;
  163.  
  164.     for(j = (GRID_DEPTH * GRID_WIDTH * 3); j < (GRID_DEPTH * GRID_WIDTH * 6); j += (GRID_DEPTH * 3))
  165.     {
  166.         for(i = 0; i < (GRID_DEPTH * 3); i += 3)
  167.         {
  168.             m_Indices[(i+j)]   = loop;
  169.             m_Indices[(i+j)+1] = loop + GRID_DEPTH + 1;
  170.             m_Indices[(i+j)+2] = loop + GRID_DEPTH;
  171.             loop++;
  172.         }
  173.         loop++;
  174.     }
  175. }
  176.