Commit aed9aece authored by Administrator's avatar Administrator
Browse files

Debug: box and triangle defined, but triangle does NOT display

If only define triangle, it can display.
Both box and triangle have same pixel shader constant buffer.
Suspect conflict of pixel shader constant buffer.
parent 2942ed94
#pragma once
// 用于定义顶点的结构,三维坐标
struct StructVertex
struct StructTriVertex
{
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;
};
......@@ -19,5 +34,5 @@ struct StructPixelConstBuf
float g;
float b;
float a;
} face_colors[6];
} face_colors[12];
};
\ No newline at end of file
......@@ -5,7 +5,7 @@ Topology::Topology(Graphics& gfx, D3D11_PRIMITIVE_TOPOLOGY type)
type(type)
{}
void Topology::Bind(Graphics& gfx) noexcept
void Topology::Bind(Graphics& gfx, Logger& logger) noexcept
{
GetContext(gfx)->IASetPrimitiveTopology(type);
}
......@@ -5,7 +5,7 @@ class Topology : public Bindable
{
public:
Topology(Graphics& gfx, D3D11_PRIMITIVE_TOPOLOGY type);
void Bind(Graphics& gfx) noexcept override;
void Bind(Graphics& gfx, Logger& logger) noexcept override;
protected:
D3D11_PRIMITIVE_TOPOLOGY type;
};
\ No newline at end of file
......@@ -6,12 +6,12 @@ TransformCbuf::TransformCbuf(Graphics& gfx, const Drawable& parent)
parent(parent)
{}
void TransformCbuf::Bind(Graphics& gfx) noexcept
void TransformCbuf::Bind(Graphics& gfx, Logger& logger) noexcept
{
vcbuf.Update(gfx,
DirectX::XMMatrixTranspose(
parent.GetTransformXM() * gfx.GetProjection()
)
);
vcbuf.Bind(gfx);
vcbuf.Bind(gfx, logger);
}
......@@ -7,7 +7,7 @@ class TransformCbuf : public Bindable
{
public:
TransformCbuf(Graphics& gfx, const Drawable& parent);
void Bind(Graphics& gfx) noexcept override;
void Bind(Graphics& gfx, Logger& logger) noexcept override;
private:
VertexConstantBuffer<DirectX::XMMATRIX> vcbuf;
const Drawable& parent;
......
......@@ -11,7 +11,7 @@
#include "TransformCBuf.h"
#include "Graphics.h"
Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b)
Triangle::Triangle(Graphics& gfx, float position[][4], float r, float g, float b, Logger& logger)
:
r(r),
g(g),
......@@ -20,7 +20,7 @@ Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b
// Assign position values
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
for (int j = 0; j < 4; j++)
{
this->position[i][j] = position[i][j];
}
......@@ -29,14 +29,22 @@ Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b
/************************************
* 2.3 DefineVertexBuffer
************************************/
const std::vector<StructVertex> vertices =
const std::vector<StructTriVertex> vertices =
{
{ 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[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] }
};
AddBind(std::make_unique<VertexBuffer>(gfx, vertices));
AddBind(std::make_unique<VertexBuffer>(gfx, vertices, logger));
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
logger.PutLog("Triangle::Triangle", std::to_string(this->position[i][j]));
}
}
/************************************
* 2.7 DefineVertexShader
......@@ -64,9 +72,10 @@ Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b
};*/
const std::vector<unsigned short> indices =
{
0,1,2
0,1,2, 0,2,1,
0,1,2, 0,2,1,
};
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices));
AddIndexBuffer(std::make_unique<IndexBuffer>(gfx, indices, logger));
/************************************
* 2.6 DefineColorConstBuffer(PixelConstBuf)
......@@ -85,12 +94,10 @@ Triangle::Triangle(Graphics& gfx, float position[][3], float r, float g, float b
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,0.0f },
{ 1.0f,0.0f,0.0f },
{ 1.0f,0.0f,0.0f },
}
};
......@@ -136,5 +143,5 @@ 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, 20.0f);
DirectX::XMMatrixTranslation(0.0f, 0.0f, 4.0f);
}
\ No newline at end of file
#pragma once
#include "Drawable.h"
#include "Logger.h"
class Triangle : public Drawable
{
public:
Triangle(Graphics& gfx, float position[][3], float r, float g, float b);
Triangle(Graphics& gfx, float position[][4], float r, float g, float b, Logger& logger);
DirectX::XMMATRIX GetTransformXM() const noexcept override;
private:
// positional
float position[3][3];
float position[3][4];
// color
float r;
float g;
......
#pragma once
#include "VertexBuffer.h"
//template<class V>
VertexBuffer::VertexBuffer(Graphics& gfx, const std::vector<StructVertex>& vertices)
VertexBuffer::VertexBuffer(Graphics& gfx, const std::vector<StructTriVertex>& vertices, Logger& logger)
:
stride(sizeof(StructVertex))
stride(sizeof(StructTriVertex))
{
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(StructVertex) * vertices.size());
bd.StructureByteStride = sizeof(StructVertex);
bd.ByteWidth = UINT(sizeof(StructTriVertex) * vertices.size());
bd.StructureByteStride = sizeof(StructTriVertex);
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));
}
void VertexBuffer::Bind(Graphics& gfx) noexcept
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", "ByteWidth: " + std::to_string(bd.ByteWidth));
logger.PutLog("VertexBuffer::VertexBuffer", "StructureByteStride: " + std::to_string(bd.StructureByteStride));
}
void VertexBuffer::Bind(Graphics& gfx, Logger& logger) noexcept
{
const UINT offset = 0u;
GetContext(gfx)->IASetVertexBuffers(0u, 1u, pVertexBuffer.GetAddressOf(), &stride, &offset);
logger.PutLog("VertexBuffer::Bind", "stride: " + std::to_string(stride));
logger.PutLog("VertexBuffer::Bind", "offset: " + std::to_string(offset));
}
......@@ -8,9 +8,10 @@
class VertexBuffer : public Bindable
{
public:
VertexBuffer(Graphics& gfx, const std::vector<StructVertex>& vertices);
VertexBuffer(Graphics& gfx, const std::vector<StructTriVertex>& vertices, Logger& logger);
VertexBuffer(Graphics& gfx, const std::vector<StructBoxVertex>& vertices, Logger& logger);
void Bind(Graphics& gfx) noexcept override;
void Bind(Graphics& gfx, Logger& logger) noexcept override;
protected:
UINT stride;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
......
......@@ -14,7 +14,7 @@ VertexShader::VertexShader(Graphics& gfx, const std::wstring& path)
);
}
void VertexShader::Bind(Graphics& gfx) noexcept
void VertexShader::Bind(Graphics& gfx, Logger& logger) noexcept
{
GetContext(gfx)->VSSetShader(pVertexShader.Get(), nullptr, 0u);
}
......
......@@ -6,7 +6,7 @@ class VertexShader : public Bindable
{
public:
VertexShader(Graphics& gfx, const std::wstring& path);
void Bind(Graphics& gfx) noexcept override;
void Bind(Graphics& gfx, Logger& logger) noexcept override;
ID3DBlob* GetBytecode() const noexcept;
protected:
Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob;
......
......@@ -21,6 +21,10 @@ Timer timer;
#include <memory>
#include "Triangle.h"
#include "Box.h"
#include "Logger.h"
Logger logger("log.txt");
/************************************
* 1.1 CreateDeviceAndSwapChain
......@@ -202,6 +206,9 @@ void DefineVertexBuffer()
bd.ByteWidth = sizeof(vertices);
bd.StructureByteStride = sizeof(Vertex);
logger.PutLog("DefineVertexBuffer", "ByteWidth: " + std::to_string(sizeof(vertices)));
logger.PutLog("DefineVertexBuffer", "StructureByteStride: " + std::to_string(sizeof(Vertex)));
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices;
......@@ -227,6 +234,9 @@ void DefineIndexBuffer()
ibd.ByteWidth = sizeof(indices);
ibd.StructureByteStride = sizeof(unsigned short);
logger.PutLog("DefineIndexBuffer", "size of ByteWidth: " + std::to_string(ibd.ByteWidth));
logger.PutLog("DefineIndexBuffer", "size of StructureByteStride: " + std::to_string(ibd.StructureByteStride));
D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices;
......@@ -247,13 +257,13 @@ void DefineTransConstBuffer(float angle, float z)
// transpose the matrix to change from row_major to column_major
dx::XMMatrixTranspose(
// rotate Z axis
dx::XMMatrixRotationZ(-angle)
//dx::XMMatrixRotationZ(-angle)
// rotate X axis
* dx::XMMatrixRotationX(-angle)
//* dx::XMMatrixRotationX(-angle)
// scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation
* dx::XMMatrixTranslation(0.0f, 0.0f, z)
dx::XMMatrixTranslation(0.0f, 0.0f, z)
// projection
* dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.5f, 10.0f)
)
......@@ -446,7 +456,7 @@ void drawSingleTriangle(float angle, float z)
void drawTriangle()
{
drawSingleTriangle(timer.Peek(), 4.0f);
drawSingleTriangle(-timer.Peek(), 4.5f);
//drawSingleTriangle(-timer.Peek(), 4.5f);
}
/************************************
......@@ -629,6 +639,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
logger.start();
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
......@@ -674,14 +685,41 @@ int WINAPI WinMain(HINSTANCE hInstance,
// set up and initialize Direct3D
//InitD3D(hWnd);
//std::unique_ptr<Graphics> pGfx = std::make_unique<Graphics>(hWnd);
Graphics gfx(hWnd);
float pos[3][3] =
gfx.SetProjection(DirectX::XMMatrixPerspectiveLH(1.0f, 3.0f / 4.0f, 0.5f, 40.0f));
float triPos[3][4] =
{
{0.0f, 1.0f, 0.0f},
{1.0f, -1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f}
{0.0f, 1.0f, 0.0f, 0.0f},
{1.0f, -1.0f, 0.0f, 0.0f},
{-1.0f, -1.0f, 0.0f, 0.0f}
};
Triangle tri(gfx, pos, 1.0f, 1.0f, 1.0f);
Triangle tri(gfx, triPos, 1.0f, 1.0f, 1.0f, logger);
tri.ExecuteBind(gfx, logger);
float boxPos[8][6] =
{
// 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 },
};
Box box(gfx, boxPos, 1.0f, 1.0f, 1.0f, logger);
box.ExecuteBind(gfx, logger);
// enter the main loop:
......@@ -709,7 +747,10 @@ int WINAPI WinMain(HINSTANCE hInstance,
{
//RenderFrame();
gfx.RendorFrame();
tri.Draw(gfx);
box.ExecuteBind(gfx, logger);
box.Draw(gfx, logger);
tri.ExecuteBind(gfx, logger);
tri.Draw(gfx, logger);
gfx.EndFrame();
}
}
......@@ -717,6 +758,8 @@ int WINAPI WinMain(HINSTANCE hInstance,
// clean up DirectX and COM
CleanD3D();
logger.end();
// return this part of the WM_QUIT message to Windows
return msg.wParam;
}
......
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