Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
StudyDx
Commits
74ff7e8a
Commit
74ff7e8a
authored
Dec 23, 2022
by
Administrator
Browse files
modify code to draw cube
parent
7617146f
Changes
2
Show whitespace changes
Inline
Side-by-side
StudyDx/Graphics.cpp
View file @
74ff7e8a
...
@@ -116,6 +116,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -116,6 +116,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
{
{
float
x
;
float
x
;
float
y
;
float
y
;
float
z
;
}
pos
;
}
pos
;
struct
struct
{
{
...
@@ -125,19 +126,26 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -125,19 +126,26 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
}
color
;
}
color
;
};
};
// 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.0
f
,
0.5
f
,
1.0
f
,
0.0
f
,
0.0
f
},
{
-
1.0
f
,
-
1.0
f
,
-
1.0
f
,
1.0
f
,
0.0
f
,
0.0
f
},
{
0.5
f
,
-
0.5
f
,
0.0
f
,
1.0
f
,
0.0
f
},
// right bottom back
{
-
0.5
f
,
-
0.5
f
,
0.0
f
,
0.0
f
,
1.0
f
},
{
1.0
f
,
-
1.0
f
,
-
1.0
f
,
0.0
f
,
1.0
f
,
0.0
f
},
// left top
// left top back
{
-
0.3
f
,
0.3
f
,
0.0
f
,
1.0
f
,
0.0
f
},
{
-
1.0
f
,
1.0
f
,
-
1.0
f
,
0.0
f
,
0.0
f
,
1.0
f
},
// right top
// right top back
{
0.3
f
,
0.3
f
,
0.0
f
,
0.0
f
,
1.0
f
},
{
1.0
f
,
1.0
f
,
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
},
// center bottom
{
0.0
f
,
-
0.8
f
,
1.0
f
,
0.0
f
,
0.0
f
},
// left bottom front
{
-
1.0
f
,
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
,
1.0
f
},
// right bottom front
{
1.0
f
,
-
1.0
f
,
1.0
f
,
0.0
f
,
1.0
f
,
1.0
f
},
// left top front
{
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
,
0.0
f
,
0.0
f
},
// right top front
{
1.0
f
,
1.0
f
,
1.0
f
,
1.0
f
,
1.0
f
,
1.0
f
},
};
};
wrl
::
ComPtr
<
ID3D11Buffer
>
pVertexBuffer
;
wrl
::
ComPtr
<
ID3D11Buffer
>
pVertexBuffer
;
D3D11_BUFFER_DESC
bd
=
{};
D3D11_BUFFER_DESC
bd
=
{};
...
@@ -159,10 +167,18 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -159,10 +167,18 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
// Create index buffer
// Create index buffer
const
unsigned
short
indices
[]
=
const
unsigned
short
indices
[]
=
{
{
0
,
1
,
2
,
// back surface
0
,
2
,
3
,
0
,
2
,
1
,
2
,
3
,
1
,
0
,
4
,
1
,
// right surface
2
,
1
,
5
,
1
,
3
,
5
,
3
,
7
,
5
,
// top surface
2
,
6
,
3
,
3
,
6
,
7
,
// front surface
4
,
5
,
7
,
4
,
7
,
6
,
// left surface
0
,
4
,
2
,
2
,
4
,
6
,
// bottom surface
0
,
1
,
4
,
1
,
5
,
4
,
};
};
wrl
::
ComPtr
<
ID3D11Buffer
>
pIndexBuffer
;
wrl
::
ComPtr
<
ID3D11Buffer
>
pIndexBuffer
;
D3D11_BUFFER_DESC
ibd
=
{};
D3D11_BUFFER_DESC
ibd
=
{};
...
@@ -194,10 +210,14 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -194,10 +210,14 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
dx
::
XMMatrixTranspose
(
dx
::
XMMatrixTranspose
(
// rotate Z axis
// rotate Z axis
dx
::
XMMatrixRotationZ
(
-
angle
)
dx
::
XMMatrixRotationZ
(
-
angle
)
// scale to avoid distortion
// rotate X axis
*
dx
::
XMMatrixScaling
(
3.0
f
/
4.0
f
,
1.0
f
,
1.0
f
)
*
dx
::
XMMatrixRotationX
(
-
angle
)
// rotate Y axis
*
dx
::
XMMatrixRotationY
(
-
angle
)
// attach the image to mouse
// attach the image to mouse
*
dx
::
XMMatrixTranslation
(
x
,
y
,
0.0
f
)
*
dx
::
XMMatrixTranslation
(
x
,
y
,
4.0
f
)
// projection
*
dx
::
XMMatrixPerspectiveLH
(
1.0
f
,
heightWidthRatio
,
0.5
f
,
10.0
f
)
)
)
};
};
wrl
::
ComPtr
<
ID3D11Buffer
>
pConstantBuffer
;
wrl
::
ComPtr
<
ID3D11Buffer
>
pConstantBuffer
;
...
@@ -237,8 +257,8 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -237,8 +257,8 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
wrl
::
ComPtr
<
ID3D11InputLayout
>
pInputLayout
;
wrl
::
ComPtr
<
ID3D11InputLayout
>
pInputLayout
;
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
B32
_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
},
};
};
GFX_THROW_INFO
(
pDevice
->
CreateInputLayout
(
GFX_THROW_INFO
(
pDevice
->
CreateInputLayout
(
ied
,
(
UINT
)
std
::
size
(
ied
),
ied
,
(
UINT
)
std
::
size
(
ied
),
...
...
StudyDx/VertexShader.hlsl
View file @
74ff7e8a
...
@@ -9,10 +9,10 @@ cbuffer CBuf
...
@@ -9,10 +9,10 @@ cbuffer CBuf
matrix
transform
;
matrix
transform
;
};
};
VSOut
main
(
float
2
pos
:
Position
,
float3
color
:
Color
)
VSOut
main
(
float
3
pos
:
Position
,
float3
color
:
Color
)
{
{
VSOut
vso
;
VSOut
vso
;
vso
.
pos
=
mul
(
float4
(
pos
.
x
,
pos
.
y
,
0
.
0
f
,
1
.
0
f
),
transform
);
vso
.
pos
=
mul
(
float4
(
pos
,
1
.
0
f
),
transform
);
vso
.
color
=
color
;
vso
.
color
=
color
;
return
vso
;
return
vso
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment