Commit b9789a92 authored by Administrator's avatar Administrator
Browse files

added color array in constructor, added transformation update logic in box and triangle.

parent aed9aece
......@@ -11,55 +11,43 @@
#include "TransformCBuf.h"
#include "Graphics.h"
Box::Box(Graphics& gfx, float position[][6], float r, float g, float b, Logger& logger)
:
r(r),
g(g),
b(b)
Box::Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger)
{
// Assign position values
/*for (int i = 0; i < 3; i++)
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 4; j++)
for (int j = 0; j < 3; j++)
{
this->position[i][j] = position[i][j];
}
}*/
}
// Assign color values
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 3; j++)
{
this->color[i][j] = color[i][j];
}
}
/************************************
* 2.3 DefineVertexBuffer
************************************/
const std::vector<StructBoxVertex> vertices =
const std::vector<StructVertex> vertices =
{
// left bottom back
{ -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
// right bottom back
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f },
// left top back
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f },
// right top back
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
// left bottom front
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
// right bottom front
{ 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f },
// left top front
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f },
// right top front
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ position[0][0], position[0][1], position[0][2] },
{ position[1][0], position[1][1], position[1][2] },
{ position[2][0], position[2][1], position[2][2] },
{ position[3][0], position[3][1], position[3][2] },
{ position[4][0], position[4][1], position[4][2] },
{ position[5][0], position[5][1], position[5][2] },
{ position[6][0], position[6][1], position[6][2] },
{ position[7][0], position[7][1], position[7][2] },
};
AddBind(std::make_unique<VertexBuffer>(gfx, vertices, logger));
/*for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
logger.PutLog("Box::Box", std::to_string(this->position[i][j]));
}
}*/
/************************************
* 2.7 DefineVertexShader
************************************/
......@@ -84,42 +72,27 @@ Box::Box(Graphics& gfx, float position[][6], float r, float g, float b, Logger&
0,4,2, 2,4,6,
0,1,4, 1,5,4
};
/*const std::vector<unsigned short> indices =
{
0,1,2, 0,2,1,
0,1,2, 0,2,1
};*/
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
/************************************
* 2.6 DefineColorConstBuffer(PixelConstBuf)
************************************/
/*const StructPixelConstBuf cb2 =
{
{
{ 1.0f,0.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,1.0f,1.0f },
}
};*/
const StructPixelConstBuf cb2 =
{
{
{ 1.0f,0.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,1.0f,1.0f },
{ 1.0f,0.0f,1.0f },
{ 1.0f,0.0f,0.0f },
{ 0.0f,1.0f,0.0f },
{ 0.0f,0.0f,1.0f },
{ 1.0f,1.0f,0.0f },
{ 0.0f,1.0f,1.0f },
{ color[0][0], color[0][1], color[0][2] },
{ color[0][0], color[0][1], color[0][2] },
{ color[1][0], color[1][1], color[1][2] },
{ color[1][0], color[1][1], color[1][2] },
{ color[2][0], color[2][1], color[2][2] },
{ color[2][0], color[2][1], color[2][2] },
{ color[3][0], color[3][1], color[3][2] },
{ color[3][0], color[3][1], color[3][2] },
{ color[4][0], color[4][1], color[4][2] },
{ color[4][0], color[4][1], color[4][2] },
{ color[5][0], color[5][1], color[5][2] },
{ color[5][0], color[5][1], color[5][2] },
}
};
......@@ -146,17 +119,18 @@ Box::Box(Graphics& gfx, float position[][6], float r, float g, float b, Logger&
}
/*
void Box::Update(float dt) noexcept
{
roll += droll * dt;
/*roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
chi += dchi * dt;*/
dx = dt;
//dy = dt;
//dz = dt;
}
*/
DirectX::XMMATRIX Box::GetTransformXM() const noexcept
{
......@@ -165,5 +139,6 @@ DirectX::XMMATRIX Box::GetTransformXM() const noexcept
DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) *
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/
return
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f);
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
* DirectX::XMMatrixTranslation(dx, dy, dz);
}
\ No newline at end of file
......@@ -5,13 +5,16 @@
class Box : public Drawable
{
public:
Box(Graphics& gfx, float position[][6], float r, float g, float b, Logger& logger);
Box(Graphics& gfx, float position[][3], float color[][3], Logger& logger);
void Update(float dt) noexcept override;
DirectX::XMMATRIX GetTransformXM() const noexcept override;
private:
// positional
float position[8][6];
float position[8][3];
// color
float r;
float g;
float b;
float color[6][3];
// trasformation
float dx = 0.0f;
float dy = 0.0f;
float dz = 0.0f;
};
\ No newline at end of file
......@@ -17,7 +17,7 @@ public:
virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0;
void Draw(Graphics& gfx, Logger& logger) const noexcept;
//virtual void Update(float dt) noexcept = 0;
virtual void Update(float dt) noexcept = 0;
void AddBind(std::unique_ptr<Bindable> bind) noexcept;
void AddIndexBuffer(std::unique_ptr<class IndexBuffer> ibuf) noexcept;
void ExecuteBind(Graphics& gfx, Logger& logger) noexcept;
......
#pragma once
// 用于定义顶点的结构,三维坐标
struct StructTriVertex
struct StructVertex
{
struct
{
float x;
float y;
float z;
float dummy;
} pos;
};
// 用于定义顶点的结构,三维坐标
struct StructBoxVertex
{
struct
{
float x;
float y;
float z;
float r;
float g;
float b;
} pos;
};
......
......@@ -11,36 +11,41 @@
#include "TransformCBuf.h"
#include "Graphics.h"
Triangle::Triangle(Graphics& gfx, float position[][4], float r, float g, float b, Logger& logger)
:
r(r),
g(g),
b(b)
Triangle::Triangle(Graphics& gfx, float position[][3], float color[][3], Logger& logger)
{
// Assign position values
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
for (int j = 0; j < 3; j++)
{
this->position[i][j] = position[i][j];
}
}
// Assign color values
for (int i = 0; i < 1; i++)
{
for (int j = 0; j < 3; j++)
{
this->color[i][j] = color[i][j];
}
}
/************************************
* 2.3 DefineVertexBuffer
************************************/
const std::vector<StructTriVertex> vertices =
const std::vector<StructVertex> vertices =
{
{ position[0][0], position[0][1], position[0][2], position[0][3] },
{ position[1][0], position[1][1], position[1][2], position[1][3] },
{ position[2][0], position[2][1], position[2][2], position[2][3] }
{ position[0][0], position[0][1], position[0][2] },
{ position[1][0], position[1][1], position[1][2] },
{ position[2][0], position[2][1], position[2][2] }
};
AddBind(std::make_unique<VertexBuffer>(gfx, vertices, logger));
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
for (int j = 0; j < 3; j++)
{
logger.PutLog("Triangle::Triangle", std::to_string(this->position[i][j]));
}
......@@ -72,8 +77,7 @@ Triangle::Triangle(Graphics& gfx, float position[][4], float r, float g, float b
};*/
const std::vector<unsigned short> indices =
{
0,1,2, 0,2,1,
0,1,2, 0,2,1,
0,1,2
};
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
......@@ -93,12 +97,7 @@ Triangle::Triangle(Graphics& gfx, float position[][4], float r, float g, float b
};*/
const StructPixelConstBuf cb2 =
{
{
{ 1.0f,0.0f,0.0f },
{ 1.0f,0.0f,0.0f },
{ 1.0f,0.0f,0.0f },
{ 1.0f,0.0f,0.0f },
}
{ color[0][0], color[0][1], color[0][2] }
};
AddBind(std::make_unique<PixelConstantBuffer<StructPixelConstBuf>>(gfx, cb2));
......@@ -124,17 +123,20 @@ Triangle::Triangle(Graphics& gfx, float position[][4], float r, float g, float b
}
/*
void Box::Update(float dt) noexcept
void Triangle::Update(float dt) noexcept
{
roll += droll * dt;
/*roll += droll * dt;
pitch += dpitch * dt;
yaw += dyaw * dt;
theta += dtheta * dt;
phi += dphi * dt;
chi += dchi * dt;
chi += dchi * dt;*/
dx = dt;
dy = dt;
dz = dt;
}
*/
DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept
{
......@@ -143,5 +145,6 @@ DirectX::XMMATRIX Triangle::GetTransformXM() const noexcept
DirectX::XMMatrixRotationRollPitchYaw(theta, phi, chi) *
DirectX::XMMatrixTranslation(0.0f, 0.0f, 20.0f);*/
return
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f);
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
* DirectX::XMMatrixTranslation(dx, dy, dz);
}
\ No newline at end of file
......@@ -5,13 +5,16 @@
class Triangle : public Drawable
{
public:
Triangle(Graphics& gfx, float position[][4], float r, float g, float b, Logger& logger);
Triangle(Graphics& gfx, float position[][3], float color[][3], Logger& logger);
void Update(float dt) noexcept override;
DirectX::XMMATRIX GetTransformXM() const noexcept override;
private:
// positional
float position[3][4];
float position[3][3];
// color
float r;
float g;
float b;
float color[1][3];
// trasformation
float dx = 0.0f;
float dy = 0.0f;
float dz = 0.0f;
};
\ No newline at end of file
#pragma once
#include "VertexBuffer.h"
VertexBuffer::VertexBuffer(Graphics& gfx, const std::vector<StructTriVertex>& vertices, Logger& logger)
VertexBuffer::VertexBuffer(Graphics& gfx, const std::vector<StructVertex>& vertices, Logger& logger)
:
stride(sizeof(StructTriVertex))
stride(sizeof(StructVertex))
{
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = UINT(sizeof(StructTriVertex) * vertices.size());
bd.StructureByteStride = sizeof(StructTriVertex);
bd.ByteWidth = UINT(sizeof(StructVertex) * vertices.size());
bd.StructureByteStride = sizeof(StructVertex);
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
GetDevice(gfx)->CreateBuffer(&bd, &sd, &pVertexBuffer);
logger.PutLog("VertexBuffer::VertexBuffer", "size of vertices: " + std::to_string(vertices.size()));
logger.PutLog("VertexBuffer::VertexBuffer", "size of StructTriVertex: " + std::to_string(sizeof(StructTriVertex)));
logger.PutLog("VertexBuffer::VertexBuffer", "ByteWidth: " + std::to_string(bd.ByteWidth));
logger.PutLog("VertexBuffer::VertexBuffer", "StructureByteStride: " + std::to_string(bd.StructureByteStride));
}
VertexBuffer::VertexBuffer(Graphics& gfx, const std::vector<StructBoxVertex>& vertices, Logger& logger)
:
stride(sizeof(StructBoxVertex))
{
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = UINT(sizeof(StructBoxVertex) * vertices.size());
bd.StructureByteStride = sizeof(StructBoxVertex);
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
GetDevice(gfx)->CreateBuffer(&bd, &sd, &pVertexBuffer);
logger.PutLog("VertexBuffer::VertexBuffer", "size of vertices: " + std::to_string(vertices.size()));
logger.PutLog("VertexBuffer::VertexBuffer", "size of StructBoxVertex: " + std::to_string(sizeof(StructBoxVertex)));
logger.PutLog("VertexBuffer::VertexBuffer", "size of StructVertex: " + std::to_string(sizeof(StructVertex)));
logger.PutLog("VertexBuffer::VertexBuffer", "ByteWidth: " + std::to_string(bd.ByteWidth));
logger.PutLog("VertexBuffer::VertexBuffer", "StructureByteStride: " + std::to_string(bd.StructureByteStride));
}
......
......@@ -8,8 +8,7 @@
class VertexBuffer : public Bindable
{
public:
VertexBuffer(Graphics& gfx, const std::vector<StructTriVertex>& vertices, Logger& logger);
VertexBuffer(Graphics& gfx, const std::vector<StructBoxVertex>& vertices, Logger& logger);
VertexBuffer(Graphics& gfx, const std::vector<StructVertex>& vertices, Logger& logger);
void Bind(Graphics& gfx, Logger& logger) noexcept override;
protected:
......
......@@ -26,6 +26,8 @@ Timer timer;
Logger logger("log.txt");
#include <cmath>
/************************************
* 1.1 CreateDeviceAndSwapChain
************************************/
......@@ -689,37 +691,51 @@ int WINAPI WinMain(HINSTANCE hInstance,
Graphics gfx(hWnd);
gfx.SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 3.0f / 4.0f, 0.5f, 40.0f));
float triPos[3][4] =
float triPos[3][3] =
{
{0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, -1.0f, 0.0f, 0.0f},
{-1.0f, -1.0f, 0.0f, 0.0f}
{0.0f, 1.0f, -1.1f},
{1.0f, -1.0f, -1.1f},
{-1.0f, -1.0f, -1.1f}
};
Triangle tri(gfx, triPos, 1.0f, 1.0f, 1.0f, logger);
tri.ExecuteBind(gfx, logger);
float triCol[1][3] =
{
{1.0f, 0.2f, 0.0f}
};
Triangle tri(gfx, triPos, triCol, logger);
float boxPos[8][6] =
float boxPos[8][3] =
{
// left bottom back
{ -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, -1.0f },
// right bottom back
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f },
// left top back
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f },
// right top back
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f },
// left bottom front
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ -1.0f, -1.0f, 1.0f },
// right bottom front
{ 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f },
// left top front
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f },
// right top front
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f },
};
Box box(gfx, boxPos, 1.0f, 1.0f, 1.0f, logger);
box.ExecuteBind(gfx, logger);
float boxCol[6][3] =
{
{ 0.0f, 1.0f, 0.0f }, // Green
{ 1.0f, 1.0f, 0.0f }, // Yellow
{ 1.0f, 0.0f, 0.0f }, // Red
{ 0.0f, 0.0f, 1.0f }, // Blue
{ 0.0f, 1.0f, 1.0f }, // Cyan
{ 1.0f, 0.0f, 1.0f }, // Magenta
};
Box box(gfx, boxPos, boxCol, logger);
long seed = 0;
seed = seed % 10000;
// enter the main loop:
......@@ -747,8 +763,16 @@ int WINAPI WinMain(HINSTANCE hInstance,
{
//RenderFrame();
gfx.RendorFrame();
seed++;
auto dt = sin(seed / 1000);
box.Update(dt);
//logger.PutLog("box.update", std::to_string(dt));
box.ExecuteBind(gfx, logger);
box.Draw(gfx, logger);
//tri.Update(dt);
tri.ExecuteBind(gfx, logger);
tri.Draw(gfx, logger);
gfx.EndFrame();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment