Commit d40ceedc authored by Administrator's avatar Administrator
Browse files

modify code to draw cube

parent 58bfba78
...@@ -9,10 +9,10 @@ cbuffer CBuf ...@@ -9,10 +9,10 @@ cbuffer CBuf
matrix transform; matrix transform;
}; };
VSOut main(float2 pos : Position, float3 color : Color) VSOut main(float3 pos : Position, float3 color : Color)
{ {
VSOut vso; VSOut vso;
vso.pos = mul(float4(pos.x, pos.y, 0.0f, 1.0f), transform); vso.pos = mul(float4(pos, 1.0f), transform);
vso.color = color; vso.color = color;
return vso; return vso;
......
...@@ -40,42 +40,51 @@ struct Vertex ...@@ -40,42 +40,51 @@ struct Vertex
{ {
float x; float x;
float y; float y;
float z;
float r; float r;
float g; float g;
float b; float b;
}; };
// create vertex buffer (1 2d triangle at center of screen) // create vertex buffer (8 vertices for one cube)
const Vertex vertices[] = const Vertex vertices[] =
{ {
// center // left bottom back
{ 0.0f, 0.5f, 1.0f, 0.0f, 0.0f }, { -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f }, // right bottom back
{ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f }, { 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f },
// left top back
// left top { -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f },
{ -0.3f, 0.3f, 0.0f, 1.0f, 0.0f }, // right top back
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
// right top
{ 0.3f, 0.3f, 0.0f, 0.0f, 1.0f }, // left bottom front
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
// center bottom // right bottom front
{ 0.0f, -0.8f, 1.0f, 0.0f, 0.0f }, { 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 },
}; };
// Define index buffer // Define index buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer; Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
// create index buffer // Create index buffer
const unsigned short indices[] = const unsigned short indices[] =
{ {
// draw center // back surface
0, 1, 2, 0, 2, 1, 2, 3, 1,
// draw left top // right surface
0, 2, 3, 1, 3, 5, 3, 7, 5,
// draw right top // top surface
0, 4, 1, 2, 6, 3, 3, 6, 7,
// draw bottom // front surface
1, 5, 2, 4, 5, 7, 4, 7, 6,
// left surface
0, 4, 2, 2, 4, 6,
// bottom surface
0, 1, 4, 1, 5, 4,
}; };
// Define constant buffer // Define constant buffer
...@@ -94,7 +103,14 @@ ConstantBuffer cb = ...@@ -94,7 +103,14 @@ ConstantBuffer cb =
dx::XMMatrixTranspose( dx::XMMatrixTranspose(
// rotate Z axis // rotate Z axis
dx::XMMatrixRotationZ(-angle) dx::XMMatrixRotationZ(-angle)
* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f) // rotate X axis
* dx::XMMatrixRotationX(-angle)
// scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation
* dx::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
// projection
* dx::XMMatrixPerspectiveLH( 1.0f, heightWidthRatio, 0.1f, 10.0f)
) )
}; };
...@@ -162,7 +178,14 @@ void DefineConstantBuffer(float angle) ...@@ -162,7 +178,14 @@ void DefineConstantBuffer(float angle)
dx::XMMatrixTranspose( dx::XMMatrixTranspose(
// rotate Z axis // rotate Z axis
dx::XMMatrixRotationZ(-angle) dx::XMMatrixRotationZ(-angle)
* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f) // rotate X axis
* dx::XMMatrixRotationX(-angle)
// scaling
//* dx::XMMatrixScaling(heightWidthRatio, 1.0f, 1.0f)
// translation
* dx::XMMatrixTranslation(0.0f, 0.0f, 4.0f)
// projection
* dx::XMMatrixPerspectiveLH(1.0f, heightWidthRatio, 0.5f, 10.0f)
) )
}; };
...@@ -188,8 +211,10 @@ void DefineInputLayout() ...@@ -188,8 +211,10 @@ void DefineInputLayout()
// Create input elements description, choosing whatever need to send to GPU // Create input elements description, choosing whatever need to send to GPU
const D3D11_INPUT_ELEMENT_DESC ied[] = const D3D11_INPUT_ELEMENT_DESC ied[] =
{ {
{ "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 }, //{ "Position",0,DXGI_FORMAT_R32G32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,8,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
//{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,8,D3D11_INPUT_PER_VERTEX_DATA,0 },
{ "Color",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
// Create input layout, using the description Defined above // Create input layout, using the description Defined above
......
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