Ajout Sprites + timer + ObjToTxt enlever
ObjToTxt à remplacer pour directement ouvrir un .obj
This commit is contained in:
parent
4f4e4bca44
commit
99af9f5f64
421
enginecustom/Spriteclass.cpp
Normal file
421
enginecustom/Spriteclass.cpp
Normal file
@ -0,0 +1,421 @@
|
||||
#include "spriteclass.h"
|
||||
|
||||
|
||||
SpriteClass::SpriteClass()
|
||||
{
|
||||
m_vertexBuffer = 0;
|
||||
m_indexBuffer = 0;
|
||||
m_Textures = 0;
|
||||
}
|
||||
|
||||
|
||||
SpriteClass::SpriteClass(const SpriteClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SpriteClass::~SpriteClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool SpriteClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, int screenWidth, int screenHeight, char* spriteFilename, int renderX, int renderY)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Store the screen size.
|
||||
m_screenWidth = screenWidth;
|
||||
m_screenHeight = screenHeight;
|
||||
|
||||
// Store where the sprite should be rendered to.
|
||||
m_renderX = renderX;
|
||||
m_renderY = renderY;
|
||||
|
||||
// Initialize the frame time for this sprite object.
|
||||
m_frameTime = 0;
|
||||
|
||||
// Initialize the vertex and index buffer that hold the geometry for the sprite bitmap.
|
||||
result = InitializeBuffers(device);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load the textures for this sprite.
|
||||
result = LoadTextures(device, deviceContext, spriteFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::Shutdown()
|
||||
{
|
||||
// Release the textures used for this sprite.
|
||||
ReleaseTextures();
|
||||
|
||||
// Release the vertex and index buffers.
|
||||
ShutdownBuffers();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::Render(ID3D11DeviceContext* deviceContext)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Update the buffers if the position of the sprite has changed from its original position.
|
||||
result = UpdateBuffers(deviceContext);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
RenderBuffers(deviceContext);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpriteClass::Update(float frameTime)
|
||||
{
|
||||
// Increment the frame time each frame.
|
||||
m_frameTime += frameTime;
|
||||
|
||||
// Check if the frame time has reached the cycle time.
|
||||
if (m_frameTime >= m_cycleTime)
|
||||
{
|
||||
// If it has then reset the frame time and cycle to the next sprite in the texture array.
|
||||
m_frameTime -= m_cycleTime;
|
||||
|
||||
m_currentTexture++;
|
||||
|
||||
// If we are at the last sprite texture then go back to the beginning of the texture array to the first texture again.
|
||||
if (m_currentTexture == m_textureCount)
|
||||
{
|
||||
m_currentTexture = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int SpriteClass::GetIndexCount()
|
||||
{
|
||||
return m_indexCount;
|
||||
}
|
||||
|
||||
ID3D11ShaderResourceView* SpriteClass::GetTexture()
|
||||
{
|
||||
return m_Textures[m_currentTexture].GetTexture();
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::InitializeBuffers(ID3D11Device* device)
|
||||
{
|
||||
VertexType* vertices;
|
||||
unsigned long* indices;
|
||||
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
|
||||
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
||||
HRESULT result;
|
||||
int i;
|
||||
|
||||
|
||||
// Initialize the previous rendering position to negative one.
|
||||
m_prevPosX = -1;
|
||||
m_prevPosY = -1;
|
||||
|
||||
// Set the number of vertices in the vertex array.
|
||||
m_vertexCount = 6;
|
||||
|
||||
// Set the number of indices in the index array.
|
||||
m_indexCount = m_vertexCount;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Create the index array.
|
||||
indices = new unsigned long[m_indexCount];
|
||||
|
||||
// Initialize vertex array to zeros at first.
|
||||
memset(vertices, 0, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Load the index array with data.
|
||||
for (i = 0; i < m_indexCount; i++)
|
||||
{
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
// Set up the description of the dynamic vertex buffer.
|
||||
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
|
||||
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
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 finally create the vertex buffer.
|
||||
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set up the description of the 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 SpriteClass::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;
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::UpdateBuffers(ID3D11DeviceContext* deviceContent)
|
||||
{
|
||||
float left, right, top, bottom;
|
||||
VertexType* vertices;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
VertexType* dataPtr;
|
||||
HRESULT result;
|
||||
|
||||
|
||||
// If the position we are rendering this bitmap to hasn't changed then don't update the vertex buffer.
|
||||
if ((m_prevPosX == m_renderX) && (m_prevPosY == m_renderY))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// If the rendering location has changed then store the new position and update the vertex buffer.
|
||||
m_prevPosX = m_renderX;
|
||||
m_prevPosY = m_renderY;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Calculate the screen coordinates of the left side of the bitmap.
|
||||
left = (float)((m_screenWidth / 2) * -1) + (float)m_renderX;
|
||||
|
||||
// Calculate the screen coordinates of the right side of the bitmap.
|
||||
right = left + (float)m_bitmapWidth;
|
||||
|
||||
// Calculate the screen coordinates of the top of the bitmap.
|
||||
top = (float)(m_screenHeight / 2) - (float)m_renderY;
|
||||
|
||||
// Calculate the screen coordinates of the bottom of the bitmap.
|
||||
bottom = top - (float)m_bitmapHeight;
|
||||
|
||||
// Load the vertex array with data.
|
||||
// First triangle.
|
||||
vertices[0].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[0].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[1].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[1].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
vertices[2].position = XMFLOAT3(left, bottom, 0.0f); // Bottom left.
|
||||
vertices[2].texture = XMFLOAT2(0.0f, 1.0f);
|
||||
|
||||
// Second triangle.
|
||||
vertices[3].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[3].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[4].position = XMFLOAT3(right, top, 0.0f); // Top right.
|
||||
vertices[4].texture = XMFLOAT2(1.0f, 0.0f);
|
||||
|
||||
vertices[5].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[5].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
// Lock the vertex buffer.
|
||||
result = deviceContent->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (VertexType*)mappedResource.pData;
|
||||
|
||||
// Copy the data into the vertex buffer.
|
||||
memcpy(dataPtr, (void*)vertices, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Unlock the vertex buffer.
|
||||
deviceContent->Unmap(m_vertexBuffer, 0);
|
||||
|
||||
// Release the pointer reference.
|
||||
dataPtr = 0;
|
||||
|
||||
// Release the vertex array as it is no longer needed.
|
||||
delete[] vertices;
|
||||
vertices = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::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 SpriteClass::LoadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename)
|
||||
{
|
||||
char textureFilename[128];
|
||||
std::ifstream fin;
|
||||
int i, j;
|
||||
char input;
|
||||
bool result;
|
||||
|
||||
|
||||
// Open the sprite info data file.
|
||||
fin.open(filename);
|
||||
if (fin.fail())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read in the number of textures.
|
||||
fin >> m_textureCount;
|
||||
|
||||
// Create and initialize the texture array with the texture count from the file.
|
||||
m_Textures = new TextureClass[m_textureCount];
|
||||
|
||||
// Read to start of next line.
|
||||
fin.get(input);
|
||||
|
||||
// Read in each texture file name.
|
||||
for (i = 0; i < m_textureCount; i++)
|
||||
{
|
||||
j = 0;
|
||||
fin.get(input);
|
||||
while (input != '\n')
|
||||
{
|
||||
textureFilename[j] = input;
|
||||
j++;
|
||||
fin.get(input);
|
||||
}
|
||||
textureFilename[j] = '\0';
|
||||
|
||||
// Once you have the filename then load the texture in the texture array.
|
||||
result = m_Textures[i].Initialize(device, deviceContext, textureFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Read in the cycle time.
|
||||
fin >> m_cycleTime;
|
||||
|
||||
// Convert the integer milliseconds to float representation.
|
||||
m_cycleTime = m_cycleTime * 0.001f;
|
||||
|
||||
// Close the file.
|
||||
fin.close();
|
||||
|
||||
// Get the dimensions of the first texture and use that as the dimensions of the 2D sprite images.
|
||||
m_bitmapWidth = m_Textures[0].GetWidth();
|
||||
m_bitmapHeight = m_Textures[0].GetHeight();
|
||||
|
||||
// Set the starting texture in the cycle to be the first one in the list.
|
||||
m_currentTexture = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpriteClass::ReleaseTextures()
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
// Release the texture objects.
|
||||
if (m_Textures)
|
||||
{
|
||||
for (i = 0; i < m_textureCount; i++)
|
||||
{
|
||||
m_Textures[i].Shutdown();
|
||||
}
|
||||
|
||||
delete[] m_Textures;
|
||||
m_Textures = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::SetRenderLocation(int x, int y)
|
||||
{
|
||||
m_renderX = x;
|
||||
m_renderY = y;
|
||||
return;
|
||||
}
|
63
enginecustom/Spriteclass.h
Normal file
63
enginecustom/Spriteclass.h
Normal file
@ -0,0 +1,63 @@
|
||||
#ifndef _SPRITECLASS_H_
|
||||
#define _SPRITECLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "textureclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: SpriteClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class SpriteClass
|
||||
{
|
||||
private:
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
SpriteClass();
|
||||
SpriteClass(const SpriteClass&);
|
||||
~SpriteClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, int, int, char*, int, int);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*);
|
||||
void Update(float);
|
||||
|
||||
int GetIndexCount();
|
||||
ID3D11ShaderResourceView* GetTexture();
|
||||
|
||||
void SetRenderLocation(int, int);
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*);
|
||||
void ShutdownBuffers();
|
||||
bool UpdateBuffers(ID3D11DeviceContext*);
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
|
||||
bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, char*);
|
||||
void ReleaseTextures();
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount, m_screenWidth, m_screenHeight, m_bitmapWidth, m_bitmapHeight, m_renderX, m_renderY, m_prevPosX, m_prevPosY;
|
||||
TextureClass* m_Textures;
|
||||
float m_frameTime, m_cycleTime;
|
||||
int m_currentTexture, m_textureCount;
|
||||
};
|
||||
|
||||
#endif
|
@ -213,9 +213,9 @@ void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight)
|
||||
}
|
||||
else
|
||||
{
|
||||
// If windowed then set it to 800x600 resolution.
|
||||
screenWidth = 800;
|
||||
screenHeight = 600;
|
||||
// If windowed then set it to 1600x900 resolution.
|
||||
screenWidth = 1600;
|
||||
screenHeight = 900;
|
||||
|
||||
// Place the window in the middle of the screen.
|
||||
posX = (GetSystemMetrics(SM_CXSCREEN) - screenWidth) / 2;
|
||||
|
63
enginecustom/Timerclass.cpp
Normal file
63
enginecustom/Timerclass.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include "timerclass.h"
|
||||
|
||||
|
||||
TimerClass::TimerClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TimerClass::TimerClass(const TimerClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TimerClass::~TimerClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool TimerClass::Initialize()
|
||||
{
|
||||
INT64 frequency;
|
||||
|
||||
|
||||
// Get the cycles per second speed for this system.
|
||||
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
|
||||
if (frequency == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store it in floating point.
|
||||
m_frequency = (float)frequency;
|
||||
|
||||
// Get the initial start time.
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)&m_startTime);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TimerClass::Frame()
|
||||
{
|
||||
INT64 currentTime;
|
||||
INT64 elapsedTicks;
|
||||
|
||||
|
||||
// Query the current time.
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)¤tTime);
|
||||
|
||||
// Calculate the difference in time since the last time we queried for the current time.
|
||||
elapsedTicks = currentTime - m_startTime;
|
||||
|
||||
// Calculate the frame time.
|
||||
m_frameTime = (float)elapsedTicks / m_frequency;
|
||||
|
||||
// Restart the timer.
|
||||
m_startTime = currentTime;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
float TimerClass::GetTime()
|
||||
{
|
||||
return m_frameTime;
|
||||
}
|
32
enginecustom/Timerclass.h
Normal file
32
enginecustom/Timerclass.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef _TIMERCLASS_H_
|
||||
#define _TIMERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TimerClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TimerClass
|
||||
{
|
||||
public:
|
||||
TimerClass();
|
||||
TimerClass(const TimerClass&);
|
||||
~TimerClass();
|
||||
|
||||
bool Initialize();
|
||||
void Frame();
|
||||
|
||||
float GetTime();
|
||||
|
||||
private:
|
||||
float m_frequency;
|
||||
INT64 m_startTime;
|
||||
float m_frameTime;
|
||||
};
|
||||
|
||||
#endif
|
@ -9,6 +9,8 @@ ApplicationClass::ApplicationClass()
|
||||
m_Light = 0;
|
||||
m_TextureShader = 0;
|
||||
m_Bitmap = 0;
|
||||
m_Sprite = 0;
|
||||
m_Timer = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -28,6 +30,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
char outputModelFilename[128];
|
||||
char textureFilename[128];
|
||||
char bitmapFilename[128];
|
||||
char spriteFilename[128];
|
||||
bool result;
|
||||
|
||||
|
||||
@ -67,6 +70,27 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the sprite info file we will be using.
|
||||
strcpy_s(spriteFilename, "sprite_data_01.txt");
|
||||
|
||||
// Create and initialize the sprite object.
|
||||
m_Sprite = new SpriteClass;
|
||||
|
||||
result = m_Sprite->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, spriteFilename, 50, 50);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the timer object.
|
||||
m_Timer = new TimerClass;
|
||||
|
||||
result = m_Timer->Initialize();
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the file name of the bitmap file.
|
||||
strcpy_s(bitmapFilename, "stone01.tga");
|
||||
|
||||
@ -80,9 +104,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
|
||||
// Set the file name of the model.
|
||||
strcpy_s(modelFilename, "sphere.obj");
|
||||
|
||||
strcpy_s(outputModelFilename, "output.txt");
|
||||
strcpy_s(modelFilename, "cube.txt");
|
||||
|
||||
// Set the name of the texture file that we will be loading.
|
||||
strcpy_s(textureFilename, "stone01.tga");
|
||||
@ -90,7 +112,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
// Create and initialize the model object.
|
||||
m_Model = new ModelClass;
|
||||
|
||||
result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, outputModelFilename, textureFilename);
|
||||
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);
|
||||
@ -106,11 +128,12 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the light object.
|
||||
m_Light = new LightClass;
|
||||
|
||||
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
m_Light->SetDirection(0.0f, 0.0f, 1.0f);
|
||||
m_Light->SetDirection(-1.0f, -1.0f, 1.0f);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -118,6 +141,21 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
|
||||
void ApplicationClass::Shutdown()
|
||||
{
|
||||
// Release the timer object.
|
||||
if (m_Timer)
|
||||
{
|
||||
delete m_Timer;
|
||||
m_Timer = 0;
|
||||
}
|
||||
|
||||
// Release the sprite object.
|
||||
if (m_Sprite)
|
||||
{
|
||||
m_Sprite->Shutdown();
|
||||
delete m_Sprite;
|
||||
m_Sprite = 0;
|
||||
}
|
||||
|
||||
// Release the light object.
|
||||
if (m_Light)
|
||||
{
|
||||
@ -186,6 +224,7 @@ void ApplicationClass::Shutdown()
|
||||
|
||||
bool ApplicationClass::Frame()
|
||||
{
|
||||
float frameTime;
|
||||
static float rotation = 0.0f;
|
||||
static float x = 2.f;
|
||||
static float y = 0.f;
|
||||
@ -215,6 +254,15 @@ bool ApplicationClass::Frame()
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update the system stats.
|
||||
m_Timer->Frame();
|
||||
|
||||
// Get the current frame time.
|
||||
frameTime = m_Timer->GetTime();
|
||||
|
||||
// Update the sprite object using the frame time.
|
||||
m_Sprite->Update(frameTime);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -240,6 +288,20 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
// Turn off the Z buffer to begin all 2D rendering.
|
||||
m_Direct3D->TurnZBufferOff();
|
||||
|
||||
// Put the sprite vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
result = m_Sprite->Render(m_Direct3D->GetDeviceContext());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Render the sprite with the texture shader.
|
||||
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Sprite->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, m_Sprite->GetTexture());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Put the bitmap vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
result = m_Bitmap->Render(m_Direct3D->GetDeviceContext());
|
||||
if (!result)
|
||||
@ -247,7 +309,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
return false;
|
||||
}
|
||||
|
||||
m_Bitmap->SetRenderLocation(300, 50);
|
||||
m_Bitmap->SetRenderLocation(1200, 50);
|
||||
|
||||
// Render the bitmap with the texture shader.
|
||||
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Bitmap->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, m_Bitmap->GetTexture());
|
||||
|
@ -12,6 +12,8 @@
|
||||
#include "lightclass.h"
|
||||
#include "bitmapclass.h"
|
||||
#include "textureshaderclass.h"
|
||||
#include "spriteclass.h"
|
||||
#include "timerclass.h"
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
@ -46,6 +48,8 @@ private:
|
||||
LightClass* m_Light;
|
||||
TextureShaderClass* m_TextureShader;
|
||||
BitmapClass* m_Bitmap;
|
||||
SpriteClass* m_Sprite;
|
||||
TimerClass* m_Timer;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -30,9 +30,11 @@
|
||||
<ClCompile Include="Lightshaderclass.cpp" />
|
||||
<ClCompile Include="Main.cpp" />
|
||||
<ClCompile Include="modelclass.cpp" />
|
||||
<ClCompile Include="Spriteclass.cpp" />
|
||||
<ClCompile Include="Systemclass.cpp" />
|
||||
<ClCompile Include="textureclass.cpp" />
|
||||
<ClCompile Include="textureshaderclass.cpp" />
|
||||
<ClCompile Include="Timerclass.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\dx11win10tut06_src\source\lightclass.h" />
|
||||
@ -45,9 +47,11 @@
|
||||
<ClInclude Include="Lightclass.h" />
|
||||
<ClInclude Include="Lightshaderclass.h" />
|
||||
<ClInclude Include="modelclass.h" />
|
||||
<ClInclude Include="Spriteclass.h" />
|
||||
<ClInclude Include="systemclass.h" />
|
||||
<ClInclude Include="textureclass.h" />
|
||||
<ClInclude Include="textureshaderclass.h" />
|
||||
<ClInclude Include="Timerclass.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Light.ps" />
|
||||
@ -76,20 +80,11 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="stone01.tga" />
|
||||
<Image Include="wall.tga" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="cube.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="cylinder.obj">
|
||||
<FileType>Document</FileType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="cube.obj">
|
||||
<FileType>Document</FileType>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="sphere.obj">
|
||||
<FileType>Document</FileType>
|
||||
|
@ -63,6 +63,18 @@
|
||||
<ClCompile Include="Lightclass.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="bitmapclass.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="textureshaderclass.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Spriteclass.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Timerclass.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="systemclass.h">
|
||||
@ -101,6 +113,18 @@
|
||||
<ClInclude Include="Lightclass.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="bitmapclass.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="textureshaderclass.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Spriteclass.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Timerclass.h">
|
||||
<Filter>Fichiers d%27en-tête</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
@ -122,8 +146,20 @@
|
||||
<None Include="Light.ps">
|
||||
<Filter>shader</Filter>
|
||||
</None>
|
||||
<None Include="texture.ps">
|
||||
<Filter>texture</Filter>
|
||||
</None>
|
||||
<None Include="texture.vs">
|
||||
<Filter>texture</Filter>
|
||||
</None>
|
||||
<None Include="sphere.obj">
|
||||
<Filter>assets</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Image Include="wall.tga">
|
||||
<Filter>assets</Filter>
|
||||
</Image>
|
||||
<Image Include="stone01.tga">
|
||||
<Filter>assets</Filter>
|
||||
</Image>
|
||||
|
@ -19,14 +19,12 @@ ModelClass::~ModelClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* outputModelFilename, char* textureFilename)
|
||||
bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* textureFilename)
|
||||
{
|
||||
bool result;
|
||||
|
||||
ConvertObjToTxt(modelFilename, outputModelFilename);
|
||||
|
||||
// Load in the model data.
|
||||
result = LoadModel(outputModelFilename);
|
||||
result = LoadModel(modelFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
@ -91,7 +89,7 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
||||
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
|
||||
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
||||
HRESULT result;
|
||||
|
||||
int i;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
@ -100,7 +98,7 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
||||
indices = new unsigned long[m_indexCount];
|
||||
|
||||
// Load the vertex array and index array with data.
|
||||
for (int i = 0; i < m_vertexCount; i++)
|
||||
for (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);
|
||||
@ -109,31 +107,6 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
|
||||
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;
|
||||
@ -312,84 +285,6 @@ bool ModelClass::LoadModel(char* filename)
|
||||
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)
|
||||
|
@ -64,7 +64,7 @@ public:
|
||||
ModelClass(const ModelClass&);
|
||||
~ModelClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, char*, char*);
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, char*);
|
||||
void Shutdown();
|
||||
void Render(ID3D11DeviceContext*);
|
||||
|
||||
@ -82,8 +82,6 @@ private:
|
||||
bool LoadModel(char*);
|
||||
void ReleaseModel();
|
||||
|
||||
void ConvertObjToTxt(const std::string&, const std::string&);
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount;
|
||||
|
@ -1,40 +0,0 @@
|
||||
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.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 1.0 0.0 0.0
|
||||
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.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 -1.0 0.0 0.0
|
||||
-1.0 1.0 1.0 0.0 0.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 0.0 1.0 0.0 1.0 0.0
|
||||
-1.0 1.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
|
||||
-1.0 -1.0 -1.0 0.0 0.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 0.0 1.0 0.0 -1.0 0.0
|
||||
-1.0 -1.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
|
@ -1,2 +0,0 @@
|
||||
# Blender 4.0.1 MTL File: 'None'
|
||||
# www.blender.org
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
BIN
enginecustom/sprite01.tga
Normal file
BIN
enginecustom/sprite01.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
enginecustom/sprite02.tga
Normal file
BIN
enginecustom/sprite02.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
enginecustom/sprite03.tga
Normal file
BIN
enginecustom/sprite03.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
enginecustom/sprite04.tga
Normal file
BIN
enginecustom/sprite04.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
6
enginecustom/sprite_data_01.txt
Normal file
6
enginecustom/sprite_data_01.txt
Normal file
@ -0,0 +1,6 @@
|
||||
4
|
||||
sprite01.tga
|
||||
sprite02.tga
|
||||
sprite03.tga
|
||||
sprite04.tga
|
||||
250
|
BIN
enginecustom/wall.tga
Normal file
BIN
enginecustom/wall.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 768 KiB |
Loading…
x
Reference in New Issue
Block a user