Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. #include "DemDynamicObject.h"
  2. #include "DemMeshObj.h"
  3. #include <fstream>
  4. #include "DemTokenStream.h"
  5. #include "DemVertexPN.h"
  6. #include "DemVector2.h"
  7. #include "DemVertexPNT.h"
  8. using namespace std;
  9. namespace DemEngine
  10. {
  11.     DemMeshObj::DemMeshObj(IDirect3DDevice9* dev)
  12.         : DemDynamicObject(),
  13.         m_meshDevice(dev),
  14.         m_vertBuffer(NULL),
  15.         m_indexBuffer(NULL)
  16.     {
  17.         this->m_streams = new DemTokenStream();
  18.         this->m_faceStreams = new DemTokenStream();
  19.         this->m_lineStreams = new DemTokenStream();
  20.         this->m_vertices = 0;
  21.         this->m_normals = 0;
  22.         this->m_texCoords = 0;
  23.         this->m_numVertices = 0;
  24.         this->m_numActualVertices = 0;
  25.         this->m_numFaces = 0;
  26.         this->m_indices = 1;
  27.     }
  28.  
  29.     DemMeshObj::~DemMeshObj()
  30.     {
  31.         SAFE_INTERFACE_RELEASE(m_vertBuffer);
  32.         SAFE_INTERFACE_RELEASE(m_indexBuffer);
  33.         SAFE_INTERFACE_RELEASE(m_vertexDeclaration);
  34.         m_numVertices = 0;
  35.         if (m_vertices != 0) delete[] m_vertices;
  36.         if (m_normals != 0) delete[] m_normals;
  37.         if (m_texCoords != 0) delete[] m_texCoords;
  38.  
  39.         m_vertices = 0;
  40.         m_normals = 0;
  41.         m_texCoords = 0;
  42.     }
  43.  
  44.     bool DemMeshObj::loadFile(DemString file)
  45.     {
  46.         ifstream objFile(file.c_str(), ifstream::in);
  47.  
  48.         if (objFile.is_open() == false)
  49.             return false;
  50.  
  51.         int fileSize = 0;
  52.         objFile.seekg(0, ios::end);
  53.         fileSize = (int)objFile.tellg();
  54.         objFile.seekg(0, ios::beg);
  55.  
  56.         if (fileSize <= 0)
  57.             return false;
  58.  
  59.         char* buffer = new char[fileSize];
  60.  
  61.         if (buffer == 0)
  62.             return false;
  63.  
  64.  
  65.         memset(buffer, '\0', fileSize);
  66.  
  67.         DemString tempLine, token;
  68.  
  69.         objFile.read(buffer, fileSize);
  70.  
  71.         m_streams->SetTokenStream(buffer);
  72.  
  73.         delete[] buffer;
  74.  
  75.         m_streams->ResetTokenStreams();
  76.  
  77.         vector<float> verts, normals, text;
  78.         vector<int> faces;
  79.  
  80.         char lineDelimiters[2] = { '\n', ' ' };
  81.  
  82.         while (m_streams->MoveToNextLine(&tempLine))
  83.         {
  84.             m_lineStreams->SetTokenStream((char*)tempLine.c_str());
  85.             m_streams->GetNextToken(0, 0, 0);
  86.  
  87.             if (!m_lineStreams->GetNextToken(&token, lineDelimiters, 2))
  88.                 continue;
  89.  
  90.             if (strcmp(token.c_str(), "v") == 0)
  91.             {
  92.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  93.                 verts.push_back((float)atof(token.c_str()));
  94.  
  95.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  96.                 verts.push_back((float)atof(token.c_str()));
  97.  
  98.                 m_lineStreams->GetNextToken(&tempLine, lineDelimiters, 2);
  99.                 verts.push_back((float)atof(token.c_str()));
  100.                 m_numActualVertices += 1;
  101.             }
  102.             else if (strcmp(token.c_str(), "vn") == 0)
  103.             {
  104.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  105.                 normals.push_back((float)atof(token.c_str()));
  106.  
  107.                 m_lineStreams->GetNextToken(&tempLine, lineDelimiters, 2);
  108.                 normals.push_back((float)atof(token.c_str()));
  109.  
  110.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  111.                 normals.push_back((float)atof(token.c_str()));
  112.             }
  113.             else if (strcmp(token.c_str(), "vt") == 0)
  114.             {
  115.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  116.                 text.push_back((float)atof(token.c_str()));
  117.  
  118.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  119.                 text.push_back((float)atof(token.c_str()));
  120.  
  121.                 m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
  122.                 text.push_back((float)atof(token.c_str()));
  123.             }
  124.             else if (strcmp(token.c_str(), "f") == 0)
  125.             {
  126.                 char faceTokens[3] = { '\n', ' ', '/' };
  127.  
  128.                 DemString faceIndex;
  129.  
  130.                 m_faceStreams->SetTokenStream((char*)tempLine.c_str());
  131.                 m_faceStreams->GetNextToken(0, 0, 0);
  132.  
  133.                 for (unsigned int i = 0; i < 3; i++)
  134.                 {
  135.                     m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
  136.                     faces.push_back((int)atoi(faceIndex.c_str()));
  137.  
  138.                     m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
  139.                     faces.push_back((int)atoi(faceIndex.c_str()));
  140.  
  141.                     m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
  142.                     faces.push_back((int)atoi(faceIndex.c_str()));
  143.                 }
  144.             }
  145.             else if (strcmp(token.c_str(), "#") == 0)
  146.             {
  147.                 int a = 0;
  148.                 int b = a;
  149.             }
  150.  
  151.             token[0] = '\0';
  152.         }
  153.  
  154.  
  155.         int vIndex = 0, nIndex = 0, tIndex = 0;
  156.         m_numFaces = (int)faces.size() / 9;
  157.  
  158.         m_numVertices = m_numFaces * 3;
  159.  
  160.         m_vertices = new float[m_numVertices * 3];
  161.  
  162.         if ((int)normals.size() != 0)
  163.         {
  164.             m_normals = new float[m_numVertices * 3];
  165.         }
  166.  
  167.         if ((int)text.size() != 0)
  168.         {
  169.             m_texCoords = new float[m_numVertices * 2];
  170.         }
  171.  
  172.         for (unsigned int f = 0; f < (int)faces.size(); f += 3)
  173.         {
  174.             m_vertices[vIndex + 0] = verts[(faces[f + 0] - 1) * 3 + 0];
  175.             m_vertices[vIndex + 1] = verts[(faces[f + 0] - 1) * 3 + 1];
  176.             m_vertices[vIndex + 2] = verts[(faces[f + 0] - 1) * 3 + 2];
  177.             vIndex += 3;
  178.  
  179.             if (m_texCoords)
  180.             {
  181.                 m_texCoords[tIndex + 0] = text[(faces[f + 1] - 1) * 2 + 0];
  182.                 m_texCoords[tIndex + 1] = text[(faces[f + 1] - 1) * 2 + 1];
  183.                 tIndex += 2;
  184.             }
  185.  
  186.             if (m_normals)
  187.             {
  188.                 m_normals[nIndex + 0] = normals[(faces[f + 2] - 1) * 3 + 0];
  189.                 m_normals[nIndex + 1] = normals[(faces[f + 2] - 1) * 3 + 1];
  190.                 m_normals[nIndex + 2] = normals[(faces[f + 2] - 1) * 3 + 2];
  191.                 nIndex += 3;
  192.             }
  193.         }
  194.  
  195.         verts.clear();
  196.         normals.clear();
  197.         text.clear();
  198.         faces.clear();
  199.         return true;
  200.     }
  201.     bool DemMeshObj::loadObjMesh(DemString fileName)
  202.     {
  203.         this->loadFile(fileName);
  204.         DemVertexPNT* vert = 0;
  205.         DemVector3 m_pos;
  206.         DemVector3 m_norm;
  207.         DemVector2 m_tex;
  208.         float* vertPtr = this->getVertices();
  209.         float* texPtr = this->getTextureCoords();
  210.         float* normPtr = this->getNormals();
  211.         m_vertNormCoord.reserve(this->getNumActualVertices());
  212.         for (unsigned int x = 0; x < getNumActualVertices(); x++)
  213.         {
  214.            
  215.             float xPos = *(vertPtr + 0);
  216.             float yPos = *(vertPtr + 1);
  217.             float zPos = *(vertPtr + 2);
  218.             vertPtr += 3;
  219.             m_pos.setVector(xPos, yPos, zPos);
  220.            
  221.             float U = *(texPtr + 0);
  222.             float V = *(texPtr + 1);
  223.             texPtr += 2;
  224.             m_tex.setVector(U, V);
  225.        
  226.             float xNorm = *(normPtr + 0);
  227.             float yNorm = *(normPtr + 1);
  228.             float zNorm = *(normPtr + 2);
  229.             normPtr += 3;
  230.             m_norm.setVector(xNorm, yNorm, zNorm);
  231.            
  232.             m_vertNormCoord.push_back(new DemVertexPNT(m_pos, m_norm, m_tex));
  233.         }
  234.  
  235.         DWORD sizeInBytes = sizeof(DemVertexPNT)* this->getNumActualVertices();// static_cast<DWORD>(m_vertNormCoord.size());
  236.         HRESULT hr = m_meshDevice->CreateVertexBuffer(sizeInBytes,
  237.             0,
  238.             0,
  239.             D3DPOOL_MANAGED,
  240.             &m_vertBuffer,
  241.             NULL);
  242.        
  243.         void* data = 0;
  244.         DemVertexPNT* dVert = 0;
  245.         hr = m_vertBuffer->Lock(0, 0, (void**)&data, 0);
  246.         DemVertexPNT* v = static_cast<DemVertexPNT*>(data);
  247.         char buffer[256];
  248.         unsigned int size = static_cast<unsigned>(m_vertNormCoord.size());
  249.         for (unsigned int i = 0; i < size; ++i)
  250.         {
  251.             v[i] = *this->m_vertNormCoord[i];
  252.             memcpy(data, v, sizeof(DemVertexPNT));
  253. //          sprintf_s(buffer, 256,
  254.     //          "vert #%d: <%.f, %.f, %.f>", i, v[i].getPosition().getX(),
  255.         //      v[i].getPosition().getY(),
  256.             //  v[i].getPosition().getZ());
  257.             //MessageBox(NULL, buffer, NULL, MB_OK);
  258.         }
  259.        
  260.         m_vertBuffer->Unlock();
  261.         int* i = 0;
  262.          m_meshDevice->CreateIndexBuffer(m_numFaces * 3 * sizeof(DWORD),
  263.                 D3DUSAGE_WRITEONLY,
  264.                 D3DFMT_INDEX16,
  265.                 D3DPOOL_DEFAULT,
  266.                 &m_indexBuffer,
  267.                 NULL);
  268.  
  269.          long* indices = 0;
  270.          hr = m_indexBuffer->Lock(0, m_numFaces * 3 * sizeof(m_indices), (void**)&indices, 0);
  271.  
  272.          for (int i = 0; i < this->getNumVertices(); ++i)
  273.          {
  274.              indices[i * 3 + 0] = *(vertPtr + 0);
  275.              indices[i * 3 + 1] = *(vertPtr + 1);
  276.              indices[i * 3 + 2] = *(vertPtr + 2);
  277.          }
  278.          
  279.          hr = m_indexBuffer->Unlock();
  280.        
  281.    
  282.         D3DVERTEXELEMENT9 vertElem[] =
  283.         {
  284.             {0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
  285.             {0,12,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0},
  286.             {0,24,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
  287.            
  288.             D3DDECL_END()
  289.         };
  290.  
  291.         unsigned int vertElemSize = DEM_ARRAYSIZE(vertElem);
  292.  
  293.         hr = m_meshDevice->CreateVertexDeclaration(vertElem, &m_vertexDeclaration);
  294.  
  295.         if (FAILED(hr))
  296.         {
  297.             const char* error = DXGetErrorDescription(hr);
  298.             MessageBox(NULL, error, NULL, MB_OK);
  299.             return false;
  300.         }
  301.         return true;
  302.     }
  303.  
  304.     bool DemMeshObj::createDynamicObject(DemString name)
  305.     {
  306.         return true;
  307.     }
  308.  
  309.     void DemMeshObj::Transform()
  310.     {
  311.         D3DXVECTOR3 position;
  312.         D3DXVECTOR3 scale;
  313.         D3DXVECTOR3 rotation;
  314.         D3DXMATRIX pScale;
  315.         D3DXMATRIX pRotation;
  316.         D3DXMATRIX pPosition;
  317.         D3DXMATRIX pWorld;
  318.  
  319.         position.x = (float)this->mPosition.getX(); // x-position
  320.         position.y = (float)this->mPosition.getY(); // y-position
  321.         position.z = (float)this->mPosition.getZ(); // z-position
  322.  
  323.         scale.x = (float)this->mScale.getX();       // x-scale.
  324.         scale.y = (float)this->mScale.getY();       // y-scale.
  325.         scale.z = (float)this->mScale.getZ();       // z-scale.
  326.  
  327.         rotation.x = (float)this->mRotation.getX(); // x-rotation.
  328.         rotation.y = (float)this->mRotation.getY(); // y-rotation.
  329.         rotation.z = (float)this->mRotation.getZ(); // z-rotation.
  330.  
  331.         float xRot = rotation.x;
  332.         float yRot = rotation.y;
  333.         float zRot = rotation.z;
  334.         D3DXMatrixRotationYawPitchRoll(&pRotation, xRot, yRot, zRot);
  335.  
  336.         D3DXMatrixScaling(&pScale, scale.x
  337.             , scale.y,
  338.             scale.z);
  339.  
  340.         D3DXMatrixTranslation(&pPosition, position.x, position.y, position.z);
  341.  
  342.         pWorld = pRotation * pScale * pPosition;
  343.  
  344.         this->m_meshDevice->SetTransform(D3DTS_WORLD, &pWorld);
  345.  
  346.     }
  347.     void DemMeshObj::draw()
  348.     {
  349.         this->Transform();
  350.         DWORD primCount = static_cast<DWORD>(this->getNumActualVertices()) / 3;
  351.         m_meshDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  352.         m_meshDevice->SetVertexDeclaration(m_vertexDeclaration);
  353.         m_meshDevice->SetStreamSource(0, m_vertBuffer, 0, sizeof(DemVertexPNT));
  354.         m_meshDevice->SetIndices(m_indexBuffer);
  355.         m_meshDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, primCount);
  356.         //m_meshDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, primCount);
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement