el cube
This commit is contained in:
parent
5c9ee0180f
commit
35876a05a5
@ -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);
|
||||
|
40
enginecustom/cube.txt
Normal file
40
enginecustom/cube.txt
Normal file
@ -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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.0 1.0 0.0 1.0 0.0 -1.0 0.0
|
||||
1.0 -1.0 -1.0 1.0 0.0 0.0 -1.0 0.0
|
||||
1.0 -1.0 1.0 1.0 1.0 0.0 -1.0 0.0
|
@ -70,6 +70,9 @@
|
||||
<ItemGroup>
|
||||
<Image Include="stone01.tga" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="cube.txt" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
|
@ -110,4 +110,9 @@
|
||||
<Filter>assets</Filter>
|
||||
</Image>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="cube.txt">
|
||||
<Filter>assets</Filter>
|
||||
</Text>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -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;
|
||||
}
|
@ -7,7 +7,9 @@
|
||||
//////////////
|
||||
#include <d3d11.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user