Advertisement
Guest User

Terrain Files

a guest
Mar 2nd, 2012
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.52 KB | None | 0 0
  1. // Terrain.h ----------------------------------------------
  2. #pragma once
  3.  
  4. #include <glaux.h> // Include glaux.h Global
  5. #include <vector> // Include vector Global
  6.  
  7. struct vec3{
  8. float x;
  9. float y;
  10. float z;
  11. };
  12.  
  13. class CTerrain : public C3DObject
  14. {
  15. public:
  16.     CTerrain(void); // Constructor
  17.     CTerrain(char * f_, float x_, float y_, float z_, float w_, float h_, float d_, int id_); // Constructor with Overloads
  18.     ~CTerrain(void); // Destructor
  19.     void Render(); // Render
  20.     float getYAt(float x_, float z_); // Return the Height
  21. private:
  22.     int m_iMapVerticesx; // ???
  23.     int m_iMapVerticesz; // ???
  24.     float m_fTerrainStepWidth; // ???
  25.     float m_fTerrainStepHeight; // ???
  26.     AUX_RGBImageRec * m_pImage_RGB; // ???
  27.     void LoadBitmapFile(char *filename); // Load Bitmap Data
  28.     void UnloadBitmapFile(); // Unload Bitmap Data
  29.     void CreateDisplayList(); // ???
  30.     vec3 * ComputeCrossProduct(int x1,int z1,int x2,int z2,int x3,int z3); // Compute Cross Product
  31.     bool bDisplayListCreated; // Display list created
  32.     GLuint m_iDisplayList; // ???
  33.     void ComputeVertexHeights(); // Calculate Vertex Height
  34.     void ComputeNormals(); // Calculate Normals
  35.     std::vector <float> m_vHeights; // ???
  36.     float * ptr; // Store Height Data
  37.     std::vector <vec3 *> m_vNormals; // ???
  38.     void NormalizeVector(vec3 *v); // Normalize Vectors
  39.     void AddVector(vec3 *a, vec3 *b); // Add Vectors
  40.     void DumpVertexData(); // Used for Debugging
  41.     void InitPhysics(); // Initialize Physics
  42.     // Comment This /\ //
  43. };
  44.  
  45. // Terrain.cpp ----------------------------------------------
  46.  
  47. #include "stdafx.h" // Include stdafx.h Local
  48. #include "3DObject.h" // Include C3DObject.h Local
  49. #include "Terrain.h" // Include Terrain.h Local
  50. #include "Physics.h" // Include Physics.h Local
  51. #include <math.h> // Include math.h Global
  52. #include "Sprite.h" // Include Sprite.h Local
  53. #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
  54. #include "btBulletDynamicsCommon.h"
  55.  
  56. CTerrain::CTerrain(void)
  57. {
  58.  
  59. } // Constructor
  60.  
  61. CTerrain::CTerrain(char * f_, float x_, float y_, float z_, float w_, float h_, float d_, int id_):C3DObject(x_,y_,z_,w_,h_,d_,id_,TTERRAIN)
  62. {
  63.     LoadBitmapFile(f_);
  64.     m_iMapVerticesx = m_pImage_RGB->sizeX;
  65.     m_iMapVerticesz = m_pImage_RGB->sizeY;
  66.     m_fW = m_pImage_RGB->sizeX; // Set the Width
  67.     m_fD = m_pImage_RGB->sizeY; // Set the Depth
  68.     bDisplayListCreated = 0;
  69.     m_vHeights.resize(m_iMapVerticesx * m_iMapVerticesz);
  70.     m_vNormals.resize(m_iMapVerticesx * m_iMapVerticesz);
  71.     m_fTerrainStepWidth = 1.0f / m_iMapVerticesx;
  72.     m_fTerrainStepHeight = 1.0f / m_iMapVerticesz;
  73.     ComputeVertexHeights();
  74.     ComputeNormals();
  75. } // Constructor with Overloads
  76.  
  77. CTerrain::~CTerrain(void)
  78. {
  79.     UnloadBitmapFile();
  80. } // Destructor
  81.  
  82. void CTerrain::InitPhysics()
  83. {
  84.     if(CPhysics::getInstance()->Init)
  85.     {
  86.         ptr = static_cast<float *>(&m_vHeights[0]);
  87.         btHeightfieldTerrainShape * m_cColShape = new btHeightfieldTerrainShape(m_fW,m_fD,ptr,1,0,m_fH,1,PHY_FLOAT,false); // Create Terrain
  88.         CPhysics::getInstance()->m_collisionShapes.push_back(m_cColShape); // Create Collision Shape
  89.         btTransform m_cTransform; // Define Transform
  90.         m_cTransform.setIdentity(); // Set Transform
  91.         btScalar mass(0); // Set the Mass
  92.         btVector3 localInertia(0,0,0); // Set Inertia
  93.         m_cTransform.setOrigin(SCALING * btVector3(m_fX,m_fY*0.5f,m_fZ)); // Set Position
  94.         btDefaultMotionState * myMotionState = new btDefaultMotionState(m_cTransform); // Create Motion State
  95.         btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,m_cColShape,localInertia); // Create Rigid Body Info
  96.         m_rBound = new btRigidBody(rbInfo); // Create new Rigid Body
  97.         CPhysics::getInstance()->dynamicsWorld->addRigidBody(m_rBound); // Add to Dynamic World
  98.         m_bPhysics = true; // Physics Enabled
  99.     }
  100.     else
  101.     {
  102.         m_bPhysics = false; // Physics Disabled
  103.     }
  104. } // Enable Physics
  105.  
  106. void CTerrain::ComputeVertexHeights()
  107. {
  108.     float xfactor = 1.0 / (m_iMapVerticesx - 1);
  109.     float zfactor = 1.0 / (m_iMapVerticesz - 1);
  110.     for(int z = 0;z < (m_iMapVerticesz-1);z++)
  111.     {
  112.         for(int x = 0;x < (m_iMapVerticesx-1);x++)
  113.         {  
  114.             //char txt[64];
  115.             int vertxindex = x;
  116.             int vertzindex = z;            
  117.             float xprop = xfactor * vertxindex;
  118.             float zprop = zfactor * vertzindex;
  119.             float y = (m_pImage_RGB->data[(vertzindex*m_iMapVerticesz+vertxindex)*3]) / 255.0;
  120.             m_vHeights[vertxindex + m_iMapVerticesx * vertzindex] = y;
  121.             vertxindex = x;
  122.             vertzindex = z + 1;            
  123.             xprop = xfactor * vertxindex;
  124.             zprop = zfactor * vertzindex;
  125.             glTexCoord2f(xprop,zprop);
  126.             y = (m_pImage_RGB->data[(vertzindex*m_iMapVerticesz+vertxindex)*3]) / 255.0;
  127.             m_vHeights[vertxindex + m_iMapVerticesx * vertzindex] = y; 
  128.             vertxindex = x + 1;
  129.             vertzindex = z;            
  130.             xprop = xfactor * vertxindex;
  131.             zprop = zfactor * vertzindex;
  132.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  133.             m_vHeights[vertxindex + m_iMapVerticesx * vertzindex] = y;
  134.             vertxindex = x + 1;
  135.             vertzindex = z + 1;            
  136.             xprop = xfactor * vertxindex;
  137.             zprop = zfactor * vertzindex;
  138.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  139.             m_vHeights[vertxindex + m_iMapVerticesx * vertzindex] = y;
  140.         }
  141.     }
  142. } // Calculate Vertex Height
  143.  
  144. void CTerrain::CreateDisplayList()
  145. {
  146.     m_iDisplayList = glGenLists(1);
  147.     glNewList(m_iDisplayList,GL_COMPILE);
  148.     float xfactor = 1.0 / (m_iMapVerticesx - 1);
  149.     float zfactor = 1.0 / (m_iMapVerticesz - 1);
  150.     for (int z = 0;z < (m_iMapVerticesz - 1);z++)
  151.     {
  152.         for (int x = 0;x < (m_iMapVerticesx - 1);x++)
  153.         {  
  154.             glBegin(GL_TRIANGLES);
  155.             int vertxindex = x;
  156.             int vertzindex = z;            
  157.             float xprop = xfactor * vertxindex;
  158.             float zprop = zfactor * vertzindex;
  159.             float y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  160.             vec3 * norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  161.             glNormal3f(norm->x,norm->y,norm->z);
  162.             glTexCoord2f(xprop,zprop);
  163.             glVertex3f(-0.5 + xprop,y,-0.5 + zprop); // V0 
  164.             vertxindex = x;
  165.             vertzindex = z + 1;            
  166.             xprop = xfactor * vertxindex;
  167.             zprop = zfactor * vertzindex;
  168.             norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  169.             glNormal3f(norm->x,norm->y,norm->z);
  170.             glTexCoord2f(xprop,zprop);
  171.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  172.             glVertex3f(-0.5 + xprop,y,-0.5 + zprop); // V1 
  173.             vertxindex = x + 1;
  174.             vertzindex = z;            
  175.             xprop = xfactor * vertxindex;
  176.             zprop = zfactor * vertzindex;
  177.             norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  178.             glNormal3f(norm->x,norm->y,norm->z);
  179.             glTexCoord2f(xprop,zprop);
  180.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  181.             glVertex3f(-0.5 + xprop,y,-0.5 + zprop); // V2
  182.             vertxindex = x + 1;
  183.             vertzindex = z;            
  184.             xprop = xfactor * vertxindex;
  185.             zprop = zfactor * vertzindex;
  186.             norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  187.             glNormal3f(norm->x,norm->y,norm->z);
  188.             glTexCoord2f(xprop,zprop);
  189.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  190.             glVertex3f(-0.5+xprop,y,-0.5+zprop); // V3
  191.             vertxindex = x;
  192.             vertzindex = z + 1;            
  193.             xprop = xfactor * vertxindex;
  194.             zprop = zfactor * vertzindex;
  195.             norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  196.             glNormal3f(norm->x,norm->y,norm->z);
  197.             glTexCoord2f(xprop,zprop);
  198.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  199.             glVertex3f(-0.5 + xprop,y,-0.5 + zprop); // V1
  200.             vertxindex = x + 1;
  201.             vertzindex = z + 1;            
  202.             xprop = xfactor * vertxindex;
  203.             zprop = zfactor * vertzindex;
  204.             norm = m_vNormals[vertxindex + m_iMapVerticesx * vertzindex];
  205.             glNormal3f(norm->x,norm->y,norm->z);
  206.             glTexCoord2f(xprop,zprop);
  207.             y = (m_pImage_RGB->data[(vertzindex * m_iMapVerticesz + vertxindex) * 3]) / 255.0;
  208.             glVertex3f(-0.5 + xprop,y,-0.5 + zprop); // V3
  209.             glEnd();
  210.         }
  211.     }
  212.     glEndList();
  213.     GLenum error = glGetError();
  214.     int x = 1;
  215. }
  216.  
  217. void CTerrain::Render()
  218. {
  219.     if(!bDisplayListCreated)
  220.     {
  221.         bDisplayListCreated = true;
  222.         CreateDisplayList();
  223.         InitPhysics();
  224.     }
  225.     glPushMatrix();
  226.     glDisable(GL_CULL_FACE);
  227.     glEnable(GL_TEXTURE_2D);
  228.     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  229.     glBindTexture(GL_TEXTURE_2D, m_iTextureID);
  230.     GLfloat white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
  231.     GLfloat grey[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
  232.     glMaterialfv(GL_FRONT, GL_DIFFUSE, white);
  233.     glMaterialfv(GL_FRONT, GL_AMBIENT, grey);
  234.     glTranslatef(m_fX,m_fY,m_fZ);
  235.     glScalef(m_fW, m_fH, m_fD);
  236.     glCallList(m_iDisplayList);
  237.     glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL );
  238.     glEnable(GL_CULL_FACE);
  239.     glPopMatrix();
  240.     return;
  241. }
  242.  
  243. void CTerrain::LoadBitmapFile(char *filename)
  244. {
  245.     m_pImage_RGB = auxDIBImageLoadA(filename);
  246. } // Load Bitmap Data
  247.  
  248. void CTerrain::UnloadBitmapFile()
  249. {
  250.     if(m_pImage_RGB)
  251.     {
  252.         if(m_pImage_RGB->data)
  253.         {
  254.             free(m_pImage_RGB->data);
  255.         }
  256.         free(m_pImage_RGB);
  257.     }
  258. } // Unload Bitmap Data
  259.  
  260. float CTerrain::getYAt(float x_,float z_)
  261. {
  262.     float x_relative = x_ - m_fX;
  263.     float z_relative = z_ - m_fZ;
  264.     x_relative /= m_fW;
  265.     z_relative /= m_fD;
  266.     x_relative += 0.5;
  267.     z_relative += 0.5;
  268.     //float xmin,xmax,zmin,zmax;
  269.     if(x_relative < 0.0 || x_relative > 1.0)
  270.     {
  271.         return 0;
  272.     }
  273.     if(z_relative < 0.0 || z_relative > 1.0)
  274.     {
  275.         return 0;
  276.     }
  277.     int whichvertexx, whichvertexz;
  278.     whichvertexx = (int)(x_relative * (m_iMapVerticesx - 1));
  279.     whichvertexz = (int)(z_relative * (m_iMapVerticesz - 1));
  280.  
  281.     int col0 = whichvertexx;
  282.     int row0 = whichvertexz;
  283.     int col1 = col0 + 1;
  284.     int row1 = row0 + 1;
  285.     if(col1 > m_iMapVerticesx)
  286.     {
  287.         col1 = 0;
  288.     }
  289.     if(row1 > m_iMapVerticesz)
  290.     {
  291.         row1 = 0;
  292.     }
  293.     // get the four corner heights of the cell from the height field
  294.     int h00 = m_pImage_RGB->data[3 * (col0 + row0 * m_iMapVerticesz)];
  295.     int h01 = m_pImage_RGB->data[3 * (col1 + row0 * m_iMapVerticesz)];
  296.     int h11 = m_pImage_RGB->data[3 * (col1 + row1 * m_iMapVerticesz)];
  297.     int h10 = m_pImage_RGB->data[3 * (col0 + row1 * m_iMapVerticesz)];
  298.     float tx = (x_relative - float(col0) / m_iMapVerticesx) * 64;
  299.     float tz = (z_relative - float(row0) / m_iMapVerticesz) * 64;
  300.     float txtz = tx * tz;
  301.     float final_height = (float)h00 * (1.0f - tz - tx + txtz) + (float)h01 * (tx - txtz) + (float)h11 * txtz + (float)h10 * (tz - txtz);
  302.     final_height *= m_fH / 255.0;
  303.     return final_height;
  304. } // Return the Height
  305.  
  306. vec3 * CTerrain::ComputeCrossProduct(int x1,int z1,int x2,int z2,int x3,int z3)
  307. {
  308.     float v1[3],v2[3];
  309.     v1[0] = (x2-x1) * m_fTerrainStepWidth;
  310.     v1[1] = -m_vHeights[z1 * m_iMapVerticesx + x1] + m_vHeights[z2 * m_iMapVerticesx + x2];
  311.     v1[2] = (z2-z1) * m_fTerrainStepHeight;
  312.     v2[0] = (x3-x1) * m_fTerrainStepWidth;
  313.     v2[1] = -m_vHeights[z1 * m_iMapVerticesx + x1] + m_vHeights[z3 * m_iMapVerticesx + x3];
  314.     v2[2] = (z3-z1) * m_fTerrainStepHeight;
  315.     vec3 * pAuxNormal = new vec3();
  316.     pAuxNormal->x = v1[1] * v2[2] - v1[2] * v2[1];
  317.     pAuxNormal->y = v1[2] * v2[0] - v1[0] * v2[2];
  318.     pAuxNormal->z = v1[0] * v2[1] - v1[1] * v2[0];
  319.     return(pAuxNormal);
  320. } // Compute Cross Product
  321.  
  322. void CTerrain::ComputeNormals()
  323. {
  324.     float xfactor = 1.0 / (m_iMapVerticesx - 1);
  325.     float zfactor = 1.0 / (m_iMapVerticesz - 1);
  326.     for (int z = 0;z < (m_iMapVerticesz);z++)
  327.     {
  328.         for (int x = 0;x < (m_iMapVerticesx);x++)
  329.         {  
  330.             vec3 * norm1 = new vec3();
  331.             vec3 * norm2 = new vec3();
  332.             if(x == 0 && z == 0) {
  333.                 norm1 = ComputeCrossProduct(0,0, 0,1, 1,0);
  334.                 NormalizeVector(norm1);            
  335.             }
  336.             else if(x == m_iMapVerticesx-1 && z == m_iMapVerticesz-1)
  337.             {
  338.                 norm1 = ComputeCrossProduct(x,z, x,z-1, x-1,z);
  339.                 NormalizeVector(norm1);            
  340.             }
  341.             else if(x == 0 && z == m_iMapVerticesz-1)
  342.             {
  343.                 norm1 = ComputeCrossProduct(x,z, x+1,z,x,z-1 );
  344.                 NormalizeVector(norm1);            
  345.             }
  346.             else if(x == m_iMapVerticesx-1 && z == 0)
  347.             {
  348.                 norm1 = ComputeCrossProduct(x,z, x-1,z, x,z+1);
  349.                 NormalizeVector(norm1);            
  350.             }
  351.             else if (z == 0)
  352.             {
  353.                 norm1 = ComputeCrossProduct(x,0, x-1,0, x,1);
  354.                 NormalizeVector(norm1);
  355.                 norm2 = ComputeCrossProduct(x,0,x,1,x+1,0);
  356.                 NormalizeVector(norm2);
  357.                 AddVector(norm1,norm2);
  358.                 NormalizeVector(norm1);
  359.             }
  360.             else if (x == 0)
  361.             {
  362.                 norm1 = ComputeCrossProduct(0,z,1,z, 0,z-1 );
  363.                 NormalizeVector(norm1);
  364.                 norm2 = ComputeCrossProduct(0,z,0,z+1,1,z);
  365.                 NormalizeVector(norm2);
  366.                 AddVector(norm1,norm2);
  367.                 NormalizeVector(norm1);
  368.             }
  369.             else if (x == m_iMapVerticesx-1)
  370.             {
  371.                 norm1 = ComputeCrossProduct(x,z, x,z-1, x-1,z);
  372.                 NormalizeVector(norm1);
  373.                 norm2 = ComputeCrossProduct(x,z,x-1,z,x,z+1);
  374.                 NormalizeVector(norm2);
  375.                 AddVector(norm1,norm2);
  376.                 NormalizeVector(norm1);
  377.             }
  378.             else if (z == m_iMapVerticesz-1)
  379.             {
  380.                 norm1 = ComputeCrossProduct(x,z, x+1,z, x,z-1);
  381.                 NormalizeVector(norm1);
  382.                 norm2 = ComputeCrossProduct(x,z,x,z-1,x-1,z);
  383.                 NormalizeVector(norm2);
  384.                 AddVector(norm1,norm2);
  385.                 NormalizeVector(norm1);
  386.             }
  387.             else
  388.             {
  389.                 norm1 = ComputeCrossProduct(x,z, x-1,z, x-1,z+1);
  390.                 NormalizeVector(norm1);
  391.                 norm2 = ComputeCrossProduct(x,z, x-1,z+1, x,z+1);
  392.                 NormalizeVector(norm2);
  393.                 AddVector(norm1,norm2);
  394.                 norm2 = ComputeCrossProduct(x,z, x,z+1, x+1,z+1);
  395.                 NormalizeVector(norm2);
  396.                 AddVector(norm1,norm2);
  397.                 norm2 = ComputeCrossProduct(x,z, x+1,z+1, x+1,z);
  398.                 NormalizeVector(norm2);
  399.                 AddVector(norm1,norm2);
  400.                 norm2 = ComputeCrossProduct(x,z, x+1,z, x+1,z-1);
  401.                 NormalizeVector(norm2);
  402.                 AddVector(norm1,norm2);
  403.                 norm2 = ComputeCrossProduct(x,z, x+1,z-1, x,z-1);
  404.                 NormalizeVector(norm2);
  405.                 AddVector(norm1,norm2);
  406.                 norm2 = ComputeCrossProduct(x,z, x,z-1, x-1,z-1);
  407.                 NormalizeVector(norm2);
  408.                 AddVector(norm1,norm2);
  409.                 norm2 = ComputeCrossProduct(x,z, x-1,z-1, x-1,z);
  410.                 NormalizeVector(norm2);
  411.                 AddVector(norm1,norm2);
  412.                 NormalizeVector(norm1);
  413.             }
  414.             int index = x + m_iMapVerticesx * z;
  415.             m_vNormals[index] = norm1;
  416.         }
  417.     }
  418. } // Calculate Normals
  419.  
  420. void CTerrain::NormalizeVector(vec3 *v)
  421. {
  422.     double d;
  423.     d = sqrt((v->x * v->x) + (v->y * v->y) + (v->z * v->z));
  424.     v->x /= d;
  425.     v->y /= d;
  426.     v->z /= d;
  427. } // Normalize Vectors
  428.  
  429. void CTerrain::AddVector(vec3 *a, vec3 *b)
  430. {
  431.     a->x += b->x;
  432.     a->y += b->y;
  433.     a->z += b->z;
  434. } // Add Vectors
  435.  
  436. void CTerrain::DumpVertexData()
  437. {
  438.     /*
  439.     int x=0;
  440.     int z=0;
  441.     for(int i=0;i<m_vHeights.size();i++)
  442.     {
  443.         float value=m_vHeights[i];
  444.         vec3 * normal=m_vNormals[i];
  445.         char txt[200];
  446.         sprintf(txt,"(%d,%d) y=%2.2f, normal =(%2.2f,%2.2f,%2.2f)\n",x,z,value,normal->x,normal->y,normal->z);
  447.         // Output stuff goes here!
  448.         x++;
  449.         if (x>=m_iMapVerticesx)
  450.         {
  451.             x=0;
  452.             z++;
  453.         }
  454.     }
  455.     */
  456. } // Used for Debugging
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement