402 lines
9.2 KiB
C++
402 lines
9.2 KiB
C++
#include "modelclass.h"
|
|
|
|
|
|
ModelClass::ModelClass()
|
|
{
|
|
m_vertexBuffer = 0;
|
|
m_indexBuffer = 0;
|
|
m_Texture = 0;
|
|
m_model = 0;
|
|
}
|
|
|
|
|
|
ModelClass::ModelClass(const ModelClass& other)
|
|
{
|
|
}
|
|
|
|
|
|
ModelClass::~ModelClass()
|
|
{
|
|
}
|
|
|
|
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* outputModelFilename, char* textureFilename)
|
|
{
|
|
bool result;
|
|
|
|
ConvertObjToTxt(modelFilename, outputModelFilename);
|
|
|
|
// Load in the model data.
|
|
result = LoadModel(outputModelFilename);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Initialize the vertex and index buffers.
|
|
result = InitializeBuffers(device);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
// Load the texture for this model.
|
|
result = LoadTexture(device, deviceContext, textureFilename);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void ModelClass::Shutdown()
|
|
{
|
|
// Release the model texture.
|
|
ReleaseTexture();
|
|
|
|
// Shutdown the vertex and index buffers.
|
|
ShutdownBuffers();
|
|
|
|
// Release the model data.
|
|
ReleaseModel();
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
void ModelClass::Render(ID3D11DeviceContext* deviceContext)
|
|
{
|
|
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
|
RenderBuffers(deviceContext);
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
int ModelClass::GetIndexCount()
|
|
{
|
|
return m_indexCount;
|
|
}
|
|
|
|
ID3D11ShaderResourceView* ModelClass::GetTexture()
|
|
{
|
|
return m_Texture->GetTexture();
|
|
}
|
|
|
|
|
|
bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
|
{
|
|
VertexType* vertices;
|
|
unsigned long* indices;
|
|
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
|
|
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
|
HRESULT result;
|
|
|
|
|
|
// Create the vertex array.
|
|
vertices = new VertexType[m_vertexCount];
|
|
|
|
// Create the index array.
|
|
indices = new unsigned long[m_indexCount];
|
|
|
|
// Load the vertex array and index array with data.
|
|
for (int i = 0; i < m_vertexCount; i++)
|
|
{
|
|
vertices[i].position = XMFLOAT3(m_model[i].x, m_model[i].y, m_model[i].z);
|
|
vertices[i].texture = XMFLOAT2(m_model[i].tu, m_model[i].tv);
|
|
vertices[i].normal = XMFLOAT3(m_model[i].nx, m_model[i].ny, m_model[i].nz);
|
|
|
|
indices[i] = i;
|
|
}
|
|
|
|
|
|
//// Create the vertex array.
|
|
//vertices = new VertexType[m_vertexCount];
|
|
|
|
//// Create the index array.
|
|
//indices = new unsigned long[m_indexCount];
|
|
|
|
//// Load the vertex array with data.
|
|
//vertices[0].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom left.
|
|
//vertices[0].texture = XMFLOAT2(0.0f, 1.0f);
|
|
//vertices[0].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
|
|
|
|
//vertices[1].position = XMFLOAT3(0.0f, 1.0f, 0.0f); // Top middle.
|
|
//vertices[1].texture = XMFLOAT2(0.5f, 0.0f);
|
|
//vertices[1].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
|
|
|
|
//vertices[2].position = XMFLOAT3(1.0f, -1.0f, 0.0f); // Bottom right.
|
|
//vertices[2].texture = XMFLOAT2(1.0f, 1.0f);
|
|
//vertices[2].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
|
|
|
|
//// Load the index array with data.
|
|
//indices[0] = 0; // Bottom left.
|
|
//indices[1] = 1; // Top middle.
|
|
//indices[2] = 2; // Bottom right.
|
|
|
|
// Set up the description of the static vertex buffer.
|
|
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
|
|
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
|
|
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
vertexBufferDesc.CPUAccessFlags = 0;
|
|
vertexBufferDesc.MiscFlags = 0;
|
|
vertexBufferDesc.StructureByteStride = 0;
|
|
|
|
// Give the subresource structure a pointer to the vertex data.
|
|
vertexData.pSysMem = vertices;
|
|
vertexData.SysMemPitch = 0;
|
|
vertexData.SysMemSlicePitch = 0;
|
|
|
|
// Now create the vertex buffer.
|
|
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
|
|
if (FAILED(result))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Set up the description of the static index buffer.
|
|
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
|
|
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
|
|
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
|
indexBufferDesc.CPUAccessFlags = 0;
|
|
indexBufferDesc.MiscFlags = 0;
|
|
indexBufferDesc.StructureByteStride = 0;
|
|
|
|
// Give the subresource structure a pointer to the index data.
|
|
indexData.pSysMem = indices;
|
|
indexData.SysMemPitch = 0;
|
|
indexData.SysMemSlicePitch = 0;
|
|
|
|
// Create the index buffer.
|
|
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
|
|
if (FAILED(result))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Release the arrays now that the vertex and index buffers have been created and loaded.
|
|
delete[] vertices;
|
|
vertices = 0;
|
|
|
|
delete[] indices;
|
|
indices = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void ModelClass::ShutdownBuffers()
|
|
{
|
|
// Release the index buffer.
|
|
if (m_indexBuffer)
|
|
{
|
|
m_indexBuffer->Release();
|
|
m_indexBuffer = 0;
|
|
}
|
|
|
|
// Release the vertex buffer.
|
|
if (m_vertexBuffer)
|
|
{
|
|
m_vertexBuffer->Release();
|
|
m_vertexBuffer = 0;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
|
|
{
|
|
unsigned int stride;
|
|
unsigned int offset;
|
|
|
|
|
|
// Set vertex buffer stride and offset.
|
|
stride = sizeof(VertexType);
|
|
offset = 0;
|
|
|
|
// Set the vertex buffer to active in the input assembler so it can be rendered.
|
|
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
|
|
|
|
// Set the index buffer to active in the input assembler so it can be rendered.
|
|
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
|
|
|
|
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
|
|
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
|
|
return;
|
|
}
|
|
|
|
bool ModelClass::LoadTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename)
|
|
{
|
|
bool result;
|
|
|
|
|
|
// Create and initialize the texture object.
|
|
m_Texture = new TextureClass;
|
|
|
|
result = m_Texture->Initialize(device, deviceContext, filename);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ModelClass::ReleaseTexture()
|
|
{
|
|
// Release the texture object.
|
|
if (m_Texture)
|
|
{
|
|
m_Texture->Shutdown();
|
|
delete m_Texture;
|
|
m_Texture = 0;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
bool ModelClass::LoadModel(char* filename)
|
|
{
|
|
ifstream fin;
|
|
char input;
|
|
int i;
|
|
|
|
|
|
// Open the model file.
|
|
fin.open(filename);
|
|
|
|
// If it could not open the file then exit.
|
|
if (fin.fail())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Read up to the value of vertex count.
|
|
fin.get(input);
|
|
while (input != ':')
|
|
{
|
|
fin.get(input);
|
|
}
|
|
|
|
// Read in the vertex count.
|
|
fin >> m_vertexCount;
|
|
|
|
// Set the number of indices to be the same as the vertex count.
|
|
m_indexCount = m_vertexCount;
|
|
|
|
// Create the model using the vertex count that was read in.
|
|
m_model = new ModelType[m_vertexCount];
|
|
|
|
// Read up to the beginning of the data.
|
|
fin.get(input);
|
|
while (input != ':')
|
|
{
|
|
fin.get(input);
|
|
}
|
|
fin.get(input);
|
|
fin.get(input);
|
|
|
|
// Read in the vertex data.
|
|
for (i = 0; i < m_vertexCount; i++)
|
|
{
|
|
fin >> m_model[i].x >> m_model[i].y >> m_model[i].z;
|
|
fin >> m_model[i].tu >> m_model[i].tv;
|
|
fin >> m_model[i].nx >> m_model[i].ny >> m_model[i].nz;
|
|
}
|
|
|
|
// Close the model file.
|
|
fin.close();
|
|
|
|
return true;
|
|
}
|
|
|
|
void ModelClass::ConvertObjToTxt(const std::string& inputFilename, const std::string& outputFilename) {
|
|
std::ifstream inputFile(inputFilename);
|
|
std::ofstream outputFile(outputFilename);
|
|
|
|
std::string line;
|
|
std::vector<XMFLOAT3> positions;
|
|
std::vector<XMFLOAT2> texCoords;
|
|
std::vector<XMFLOAT3> normals;
|
|
std::vector<ModelType> vertices;
|
|
|
|
while (std::getline(inputFile, line)) {
|
|
std::istringstream iss(line);
|
|
std::string prefix;
|
|
|
|
if (!(iss >> prefix)) { break; }
|
|
|
|
if (prefix == "v") {
|
|
XMFLOAT3 pos;
|
|
if (!(iss >> pos.x >> pos.y >> pos.z)) { break; }
|
|
positions.push_back(pos);
|
|
}
|
|
else if (prefix == "vt") {
|
|
XMFLOAT2 texCoord;
|
|
if (!(iss >> texCoord.x >> texCoord.y)) { break; }
|
|
texCoords.push_back(texCoord);
|
|
}
|
|
else if (prefix == "vn") {
|
|
XMFLOAT3 normal;
|
|
if (!(iss >> normal.x >> normal.y >> normal.z)) { break; }
|
|
normals.push_back(normal);
|
|
}
|
|
else if (prefix == "f") {
|
|
std::vector<int> posIndices, texIndices, normIndices;
|
|
char slash; // To skip slashes
|
|
int posIndex, texIndex, normIndex;
|
|
|
|
// Read all indices of the face
|
|
while (iss >> posIndex >> slash >> texIndex >> slash >> normIndex) {
|
|
posIndices.push_back(posIndex);
|
|
texIndices.push_back(texIndex);
|
|
normIndices.push_back(normIndex);
|
|
}
|
|
|
|
// For each triangle in the face
|
|
for (size_t i = 1; i < posIndices.size() - 1; ++i) {
|
|
ModelType v[3]; // Vertices of the triangle
|
|
|
|
// Indices of the vertices of the triangle
|
|
int indices[3] = { 0, i, i + 1 };
|
|
|
|
for (int j = 0; j < 3; ++j) {
|
|
// .obj indices start at 1, so subtract 1 to get 0-based indices
|
|
v[j].x = positions[posIndices[indices[j]] - 1].x;
|
|
v[j].y = positions[posIndices[indices[j]] - 1].y;
|
|
v[j].z = positions[posIndices[indices[j]] - 1].z;
|
|
v[j].tu = texCoords[texIndices[indices[j]] - 1].x;
|
|
v[j].tv = texCoords[texIndices[indices[j]] - 1].y;
|
|
v[j].nx = normals[normIndices[indices[j]] - 1].x;
|
|
v[j].ny = normals[normIndices[indices[j]] - 1].y;
|
|
v[j].nz = normals[normIndices[indices[j]] - 1].z;
|
|
|
|
vertices.push_back(v[j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Write to output file in the desired format
|
|
outputFile << "Vertex Count: " << vertices.size() << "\n\n";
|
|
outputFile << "Data:\n\n";
|
|
|
|
for (const ModelType& v : vertices) {
|
|
outputFile << v.x << " " << v.y << " " << v.z << " ";
|
|
outputFile << v.tu << " " << v.tv << " ";
|
|
outputFile << v.nx << " " << v.ny << " " << v.nz << "\n";
|
|
}
|
|
}
|
|
|
|
void ModelClass::ReleaseModel()
|
|
{
|
|
if (m_model)
|
|
{
|
|
delete[] m_model;
|
|
m_model = 0;
|
|
}
|
|
|
|
return;
|
|
} |