This commit is contained in:
StratiX0
2024-03-25 14:00:08 +01:00
parent 5c9ee0180f
commit 35876a05a5
6 changed files with 180 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ ModelClass::ModelClass()
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_Texture = 0;
m_model = 0;
}
@@ -18,10 +19,16 @@ ModelClass::~ModelClass()
{
}
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* textureFilename)
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* textureFilename)
{
bool result;
// Load in the model data.
result = LoadModel(modelFilename);
if (!result)
{
return false;
}
// Initialize the vertex and index buffers.
result = InitializeBuffers(device);
@@ -48,6 +55,9 @@ void ModelClass::Shutdown()
// Shutdown the vertex and index buffers.
ShutdownBuffers();
// Release the model data.
ReleaseModel();
return;
}
@@ -81,35 +91,46 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
HRESULT result;
// Set the number of vertices in the vertex array.
m_vertexCount = 6;
// Set the number of indices in the index array.
m_indexCount = 6;
// 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);
// 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);
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);
indices[i] = i;
}
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.
//// 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;
@@ -231,5 +252,71 @@ void ModelClass::ReleaseTexture()
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::ReleaseModel()
{
if (m_model)
{
delete[] m_model;
m_model = 0;
}
return;
}