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
PracticeDx
Commits
6f2417ad
Commit
6f2417ad
authored
Dec 22, 2022
by
Administrator
Browse files
add constant buffer for rotation
parent
3417949a
Changes
6
Hide whitespace changes
Inline
Side-by-side
PracticeDx/PracticeDx.vcxproj
View file @
6f2417ad
...
...
@@ -127,6 +127,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile
Include=
"Timer.cpp"
/>
<ClCompile
Include=
"WinMain.cpp"
/>
</ItemGroup>
<ItemGroup>
...
...
@@ -147,6 +148,9 @@
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
</FxCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"Timer.h"
/>
</ItemGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
>
</ImportGroup>
...
...
PracticeDx/PracticeDx.vcxproj.filters
View file @
6f2417ad
...
...
@@ -21,6 +21,9 @@
<ClCompile
Include=
"WinMain.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
<ClCompile
Include=
"Timer.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<FxCompile
Include=
"PixelShader.hlsl"
>
...
...
@@ -30,4 +33,9 @@
<Filter>
Shaders
</Filter>
</FxCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"Timer.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
PracticeDx/Timer.cpp
0 → 100644
View file @
6f2417ad
#include "Timer.h"
using
namespace
std
::
chrono
;
Timer
::
Timer
()
noexcept
{
last
=
steady_clock
::
now
();
}
float
Timer
::
Mark
()
noexcept
{
const
auto
old
=
last
;
last
=
steady_clock
::
now
();
const
duration
<
float
>
frameTime
=
last
-
old
;
return
frameTime
.
count
();
}
float
Timer
::
Peek
()
const
noexcept
{
return
duration
<
float
>
(
steady_clock
::
now
()
-
last
).
count
();
}
PracticeDx/Timer.h
0 → 100644
View file @
6f2417ad
#pragma once
#include <chrono>
class
Timer
{
public:
Timer
()
noexcept
;
float
Mark
()
noexcept
;
float
Peek
()
const
noexcept
;
private:
std
::
chrono
::
steady_clock
::
time_point
last
;
};
\ No newline at end of file
PracticeDx/VertexShader.hlsl
View file @
6f2417ad
...
...
@@ -4,11 +4,16 @@ struct VSOut
float4
pos
:
SV_Position
;
};
cbuffer
CBuf
{
row_major
matrix
transform
;
};
VSOut
main
(
float2
pos
:
Position
,
float3
color
:
Color
)
{
VSOut
vso
;
vso
.
pos
=
float4
(
pos
.
x
,
pos
.
y
,
0
.
0
f
,
1
.
0
f
);
vso
.
pos
=
mul
(
float4
(
pos
.
x
,
pos
.
y
,
0
.
0
f
,
1
.
0
f
)
,
transform
)
;
vso
.
color
=
color
;
return
vso
;
}
}
\ No newline at end of file
PracticeDx/WinMain.cpp
View file @
6f2417ad
...
...
@@ -11,6 +11,9 @@
#include <wrl.h>
#include <array> // to usd std::size
#include <cmath> // to use std::sin and std::cos
#include "Timer.h" // to use timer
Timer
timer
;
// global declarations
Microsoft
::
WRL
::
ComPtr
<
IDXGISwapChain
>
swapchain
;
// the pointer to the swap chain interface
...
...
@@ -64,6 +67,29 @@ const Vertex vertices[] =
};
// Define constant buffer
Microsoft
::
WRL
::
ComPtr
<
ID3D11Buffer
>
pConstantBuffer
;
// Create constant buffer struct
struct
ConstantBuffer
{
struct
{
float
element
[
4
][
4
];
}
transformation
;
};
// create constant buffer
const
float
heightWidthRatio
=
0.3
f
/
0.4
f
;
float
angle
=
0.0
f
;
ConstantBuffer
cb
=
{
{
heightWidthRatio
*
std
::
cos
(
angle
),
-
std
::
sin
(
angle
),
0.0
f
,
0.0
f
,
heightWidthRatio
*
std
::
sin
(
angle
),
std
::
cos
(
angle
),
0.0
f
,
0.0
f
,
0.0
f
,
0.0
f
,
1.0
f
,
0.0
f
,
0.0
f
,
0.0
f
,
0.0
f
,
1.0
f
,
}
};
// Define input layout
ID3D11InputLayout
*
pInputLayout
;
...
...
@@ -100,6 +126,30 @@ void DefineVertexBuffer()
dev
->
CreateBuffer
(
&
bd
,
&
sd
,
&
pVertexBuffer
);
}
void
DefineConstantBuffer
(
float
angle
)
{
cb
.
transformation
.
element
[
0
][
0
]
=
heightWidthRatio
*
std
::
cos
(
angle
);
cb
.
transformation
.
element
[
0
][
1
]
=
-
std
::
sin
(
angle
);
cb
.
transformation
.
element
[
1
][
0
]
=
heightWidthRatio
*
std
::
sin
(
angle
);
cb
.
transformation
.
element
[
1
][
1
]
=
std
::
cos
(
angle
);
D3D11_BUFFER_DESC
cbd
=
{};
cbd
.
BindFlags
=
D3D11_BIND_CONSTANT_BUFFER
;
cbd
.
Usage
=
D3D11_USAGE_DYNAMIC
;
cbd
.
CPUAccessFlags
=
D3D11_CPU_ACCESS_WRITE
;
cbd
.
MiscFlags
=
0u
;
cbd
.
ByteWidth
=
sizeof
(
cb
);
cbd
.
StructureByteStride
=
0u
;
D3D11_SUBRESOURCE_DATA
csd
=
{};
csd
.
pSysMem
=
&
cb
;
// Create constant buffer
dev
->
CreateBuffer
(
&
cbd
,
&
csd
,
&
pConstantBuffer
);
// Bind constant buffer to vertex buffer
devcon
->
VSSetConstantBuffers
(
0u
,
1u
,
pConstantBuffer
.
GetAddressOf
());
}
void
DefineInputLayout
()
{
// Create input elements description, choosing whatever need to send to GPU
...
...
@@ -146,6 +196,7 @@ void drawTriangle()
{
DefineShader
();
DefineVertexBuffer
();
DefineConstantBuffer
(
timer
.
Peek
());
DefineInputLayout
();
DrawPrimitive
();
}
...
...
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