Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DemDynamicObject.h"
- #include "DemMeshObj.h"
- #include <fstream>
- #include "DemTokenStream.h"
- #include "DemVertexPN.h"
- #include "DemVector2.h"
- #include "DemVertexPNT.h"
- using namespace std;
- namespace DemEngine
- {
- DemMeshObj::DemMeshObj(IDirect3DDevice9* dev)
- : DemDynamicObject(),
- m_meshDevice(dev),
- m_vertBuffer(NULL),
- m_indexBuffer(NULL)
- {
- this->m_streams = new DemTokenStream();
- this->m_faceStreams = new DemTokenStream();
- this->m_lineStreams = new DemTokenStream();
- this->m_vertices = 0;
- this->m_normals = 0;
- this->m_texCoords = 0;
- this->m_numVertices = 0;
- this->m_numActualVertices = 0;
- this->m_numFaces = 0;
- this->m_indices = 1;
- }
- DemMeshObj::~DemMeshObj()
- {
- SAFE_INTERFACE_RELEASE(m_vertBuffer);
- SAFE_INTERFACE_RELEASE(m_indexBuffer);
- SAFE_INTERFACE_RELEASE(m_vertexDeclaration);
- m_numVertices = 0;
- if (m_vertices != 0) delete[] m_vertices;
- if (m_normals != 0) delete[] m_normals;
- if (m_texCoords != 0) delete[] m_texCoords;
- m_vertices = 0;
- m_normals = 0;
- m_texCoords = 0;
- }
- bool DemMeshObj::loadFile(DemString file)
- {
- ifstream objFile(file.c_str(), ifstream::in);
- if (objFile.is_open() == false)
- return false;
- int fileSize = 0;
- objFile.seekg(0, ios::end);
- fileSize = (int)objFile.tellg();
- objFile.seekg(0, ios::beg);
- if (fileSize <= 0)
- return false;
- char* buffer = new char[fileSize];
- if (buffer == 0)
- return false;
- memset(buffer, '\0', fileSize);
- DemString tempLine, token;
- objFile.read(buffer, fileSize);
- m_streams->SetTokenStream(buffer);
- delete[] buffer;
- m_streams->ResetTokenStreams();
- vector<float> verts, normals, text;
- vector<int> faces;
- char lineDelimiters[2] = { '\n', ' ' };
- while (m_streams->MoveToNextLine(&tempLine))
- {
- m_lineStreams->SetTokenStream((char*)tempLine.c_str());
- m_streams->GetNextToken(0, 0, 0);
- if (!m_lineStreams->GetNextToken(&token, lineDelimiters, 2))
- continue;
- if (strcmp(token.c_str(), "v") == 0)
- {
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- verts.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- verts.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&tempLine, lineDelimiters, 2);
- verts.push_back((float)atof(token.c_str()));
- m_numActualVertices += 1;
- }
- else if (strcmp(token.c_str(), "vn") == 0)
- {
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- normals.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&tempLine, lineDelimiters, 2);
- normals.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- normals.push_back((float)atof(token.c_str()));
- }
- else if (strcmp(token.c_str(), "vt") == 0)
- {
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- text.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- text.push_back((float)atof(token.c_str()));
- m_lineStreams->GetNextToken(&token, lineDelimiters, 2);
- text.push_back((float)atof(token.c_str()));
- }
- else if (strcmp(token.c_str(), "f") == 0)
- {
- char faceTokens[3] = { '\n', ' ', '/' };
- DemString faceIndex;
- m_faceStreams->SetTokenStream((char*)tempLine.c_str());
- m_faceStreams->GetNextToken(0, 0, 0);
- for (unsigned int i = 0; i < 3; i++)
- {
- m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
- faces.push_back((int)atoi(faceIndex.c_str()));
- m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
- faces.push_back((int)atoi(faceIndex.c_str()));
- m_faceStreams->GetNextToken(&faceIndex, faceTokens, 3);
- faces.push_back((int)atoi(faceIndex.c_str()));
- }
- }
- else if (strcmp(token.c_str(), "#") == 0)
- {
- int a = 0;
- int b = a;
- }
- token[0] = '\0';
- }
- int vIndex = 0, nIndex = 0, tIndex = 0;
- m_numFaces = (int)faces.size() / 9;
- m_numVertices = m_numFaces * 3;
- m_vertices = new float[m_numVertices * 3];
- if ((int)normals.size() != 0)
- {
- m_normals = new float[m_numVertices * 3];
- }
- if ((int)text.size() != 0)
- {
- m_texCoords = new float[m_numVertices * 2];
- }
- for (unsigned int f = 0; f < (int)faces.size(); f += 3)
- {
- m_vertices[vIndex + 0] = verts[(faces[f + 0] - 1) * 3 + 0];
- m_vertices[vIndex + 1] = verts[(faces[f + 0] - 1) * 3 + 1];
- m_vertices[vIndex + 2] = verts[(faces[f + 0] - 1) * 3 + 2];
- vIndex += 3;
- if (m_texCoords)
- {
- m_texCoords[tIndex + 0] = text[(faces[f + 1] - 1) * 2 + 0];
- m_texCoords[tIndex + 1] = text[(faces[f + 1] - 1) * 2 + 1];
- tIndex += 2;
- }
- if (m_normals)
- {
- m_normals[nIndex + 0] = normals[(faces[f + 2] - 1) * 3 + 0];
- m_normals[nIndex + 1] = normals[(faces[f + 2] - 1) * 3 + 1];
- m_normals[nIndex + 2] = normals[(faces[f + 2] - 1) * 3 + 2];
- nIndex += 3;
- }
- }
- verts.clear();
- normals.clear();
- text.clear();
- faces.clear();
- return true;
- }
- bool DemMeshObj::loadObjMesh(DemString fileName)
- {
- this->loadFile(fileName);
- DemVertexPNT* vert = 0;
- DemVector3 m_pos;
- DemVector3 m_norm;
- DemVector2 m_tex;
- float* vertPtr = this->getVertices();
- float* texPtr = this->getTextureCoords();
- float* normPtr = this->getNormals();
- m_vertNormCoord.reserve(this->getNumActualVertices());
- for (unsigned int x = 0; x < getNumActualVertices(); x++)
- {
- float xPos = *(vertPtr + 0);
- float yPos = *(vertPtr + 1);
- float zPos = *(vertPtr + 2);
- vertPtr += 3;
- m_pos.setVector(xPos, yPos, zPos);
- float U = *(texPtr + 0);
- float V = *(texPtr + 1);
- texPtr += 2;
- m_tex.setVector(U, V);
- float xNorm = *(normPtr + 0);
- float yNorm = *(normPtr + 1);
- float zNorm = *(normPtr + 2);
- normPtr += 3;
- m_norm.setVector(xNorm, yNorm, zNorm);
- m_vertNormCoord.push_back(new DemVertexPNT(m_pos, m_norm, m_tex));
- }
- DWORD sizeInBytes = sizeof(DemVertexPNT)* this->getNumActualVertices();// static_cast<DWORD>(m_vertNormCoord.size());
- HRESULT hr = m_meshDevice->CreateVertexBuffer(sizeInBytes,
- 0,
- 0,
- D3DPOOL_MANAGED,
- &m_vertBuffer,
- NULL);
- void* data = 0;
- DemVertexPNT* dVert = 0;
- hr = m_vertBuffer->Lock(0, 0, (void**)&data, 0);
- DemVertexPNT* v = static_cast<DemVertexPNT*>(data);
- char buffer[256];
- unsigned int size = static_cast<unsigned>(m_vertNormCoord.size());
- for (unsigned int i = 0; i < size; ++i)
- {
- v[i] = *this->m_vertNormCoord[i];
- memcpy(data, v, sizeof(DemVertexPNT));
- // sprintf_s(buffer, 256,
- // "vert #%d: <%.f, %.f, %.f>", i, v[i].getPosition().getX(),
- // v[i].getPosition().getY(),
- // v[i].getPosition().getZ());
- //MessageBox(NULL, buffer, NULL, MB_OK);
- }
- m_vertBuffer->Unlock();
- int* i = 0;
- m_meshDevice->CreateIndexBuffer(m_numFaces * 3 * sizeof(DWORD),
- D3DUSAGE_WRITEONLY,
- D3DFMT_INDEX16,
- D3DPOOL_DEFAULT,
- &m_indexBuffer,
- NULL);
- long* indices = 0;
- hr = m_indexBuffer->Lock(0, m_numFaces * 3 * sizeof(m_indices), (void**)&indices, 0);
- for (int i = 0; i < this->getNumVertices(); ++i)
- {
- indices[i * 3 + 0] = *(vertPtr + 0);
- indices[i * 3 + 1] = *(vertPtr + 1);
- indices[i * 3 + 2] = *(vertPtr + 2);
- }
- hr = m_indexBuffer->Unlock();
- D3DVERTEXELEMENT9 vertElem[] =
- {
- {0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0},
- {0,12,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_NORMAL,0},
- {0,24,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0},
- D3DDECL_END()
- };
- unsigned int vertElemSize = DEM_ARRAYSIZE(vertElem);
- hr = m_meshDevice->CreateVertexDeclaration(vertElem, &m_vertexDeclaration);
- if (FAILED(hr))
- {
- const char* error = DXGetErrorDescription(hr);
- MessageBox(NULL, error, NULL, MB_OK);
- return false;
- }
- return true;
- }
- bool DemMeshObj::createDynamicObject(DemString name)
- {
- return true;
- }
- void DemMeshObj::Transform()
- {
- D3DXVECTOR3 position;
- D3DXVECTOR3 scale;
- D3DXVECTOR3 rotation;
- D3DXMATRIX pScale;
- D3DXMATRIX pRotation;
- D3DXMATRIX pPosition;
- D3DXMATRIX pWorld;
- position.x = (float)this->mPosition.getX(); // x-position
- position.y = (float)this->mPosition.getY(); // y-position
- position.z = (float)this->mPosition.getZ(); // z-position
- scale.x = (float)this->mScale.getX(); // x-scale.
- scale.y = (float)this->mScale.getY(); // y-scale.
- scale.z = (float)this->mScale.getZ(); // z-scale.
- rotation.x = (float)this->mRotation.getX(); // x-rotation.
- rotation.y = (float)this->mRotation.getY(); // y-rotation.
- rotation.z = (float)this->mRotation.getZ(); // z-rotation.
- float xRot = rotation.x;
- float yRot = rotation.y;
- float zRot = rotation.z;
- D3DXMatrixRotationYawPitchRoll(&pRotation, xRot, yRot, zRot);
- D3DXMatrixScaling(&pScale, scale.x
- , scale.y,
- scale.z);
- D3DXMatrixTranslation(&pPosition, position.x, position.y, position.z);
- pWorld = pRotation * pScale * pPosition;
- this->m_meshDevice->SetTransform(D3DTS_WORLD, &pWorld);
- }
- void DemMeshObj::draw()
- {
- this->Transform();
- DWORD primCount = static_cast<DWORD>(this->getNumActualVertices()) / 3;
- m_meshDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
- m_meshDevice->SetVertexDeclaration(m_vertexDeclaration);
- m_meshDevice->SetStreamSource(0, m_vertBuffer, 0, sizeof(DemVertexPNT));
- m_meshDevice->SetIndices(m_indexBuffer);
- m_meshDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, primCount);
- //m_meshDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, primCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement