diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 1b79d08..3084822 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -22,6 +22,7 @@ ApplicationClass::~ApplicationClass() bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) { + char modelFilename[128]; char textureFilename[128]; bool result; @@ -52,12 +53,16 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Camera->SetPosition(0.0f, 0.0f, -5.0f); m_Camera->SetRotation(0.0f, 0.0f, 10.0f); - // Create and initialize the model object. - m_Model = new ModelClass; + // Set the file name of the model. + strcpy_s(modelFilename, "cube.txt"); + // Set the name of the texture file that we will be loading. strcpy_s(textureFilename, "stone01.tga"); - result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename); + // Create and initialize the model object. + m_Model = new ModelClass; + + result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename); if (!result) { MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK); diff --git a/enginecustom/cube.txt b/enginecustom/cube.txt new file mode 100644 index 0000000..00b34ba --- /dev/null +++ b/enginecustom/cube.txt @@ -0,0 +1,40 @@ +Vertex Count: 36 + +Data: + +-1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.0 + 1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0 +-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0 +-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0 + 1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0 + 1.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.0 + 1.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.0 + 1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0 + 1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0 + 1.0 -1.0 -1.0 0.0 1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b/enginecustom/enginecustom.vcxproj @@ -70,6 +70,9 @@ + + + 17.0 Win32Proj diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index 6236202..f23eb24 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -110,4 +110,9 @@ assets + + + assets + + \ No newline at end of file diff --git a/enginecustom/modelclass.cpp b/enginecustom/modelclass.cpp index 2c0cee4..25846f3 100644 --- a/enginecustom/modelclass.cpp +++ b/enginecustom/modelclass.cpp @@ -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; } \ No newline at end of file diff --git a/enginecustom/modelclass.h b/enginecustom/modelclass.h index d570397..9ffdcda 100644 --- a/enginecustom/modelclass.h +++ b/enginecustom/modelclass.h @@ -7,7 +7,9 @@ ////////////// #include #include +#include using namespace DirectX; +using namespace std; /////////////////////// // MY CLASS INCLUDES // @@ -29,12 +31,20 @@ private: XMFLOAT3 normal; }; + struct ModelType + { + float x, y, z; + float tu, tv; + float nx, ny, nz; + }; + + public: ModelClass(); ModelClass(const ModelClass&); ~ModelClass(); - bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*); + bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, char*); void Shutdown(); void Render(ID3D11DeviceContext*); @@ -49,10 +59,15 @@ private: bool LoadTexture(ID3D11Device*, ID3D11DeviceContext*, char*); void ReleaseTexture(); + bool LoadModel(char*); + void ReleaseModel(); + private: ID3D11Buffer* m_vertexBuffer, * m_indexBuffer; int m_vertexCount, m_indexCount; TextureClass* m_Texture; + + ModelType* m_model; }; #endif \ No newline at end of file