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
680c085b
Commit
680c085b
authored
Dec 23, 2022
by
Administrator
Browse files
draw with triangle index and color constant buffer
parent
74ff7e8a
Changes
4
Hide whitespace changes
Inline
Side-by-side
StudyDx/Graphics.cpp
View file @
680c085b
...
@@ -118,34 +118,28 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -118,34 +118,28 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
float
y
;
float
y
;
float
z
;
float
z
;
}
pos
;
}
pos
;
struct
{
float
r
;
float
g
;
float
b
;
}
color
;
};
};
// create vertex buffer (8 vertices for one cube)
// create vertex buffer (8 vertices for one cube)
const
Vertex
vertices
[]
=
const
Vertex
vertices
[]
=
{
{
// left bottom back
// left bottom back
{
-
1.0
f
,
-
1.0
f
,
-
1.0
f
,
1.0
f
,
0.0
f
,
0.0
f
},
{
-
1.0
f
,
-
1.0
f
,
-
1.0
f
},
// right bottom back
// right bottom back
{
1.0
f
,
-
1.0
f
,
-
1.0
f
,
0.0
f
,
1.0
f
,
0.0
f
},
{
1.0
f
,
-
1.0
f
,
-
1.0
f
},
// left top back
// left top back
{
-
1.0
f
,
1.0
f
,
-
1.0
f
,
0.0
f
,
0.0
f
,
1.0
f
},
{
-
1.0
f
,
1.0
f
,
-
1.0
f
},
// right top back
// right top back
{
1.0
f
,
1.0
f
,
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
},
{
1.0
f
,
1.0
f
,
-
1.0
f
},
// left bottom front
// left bottom front
{
-
1.0
f
,
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
,
1.0
f
},
{
-
1.0
f
,
-
1.0
f
,
1.0
f
},
// right bottom front
// right bottom front
{
1.0
f
,
-
1.0
f
,
1.0
f
,
0.0
f
,
1.0
f
,
1.0
f
},
{
1.0
f
,
-
1.0
f
,
1.0
f
},
// left top front
// left top front
{
-
1.0
f
,
1.0
f
,
1.0
f
,
0.0
f
,
0.0
f
,
0.0
f
},
{
-
1.0
f
,
1.0
f
,
1.0
f
},
// right top front
// right top front
{
1.0
f
,
1.0
f
,
1.0
f
,
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
=
{};
...
@@ -235,6 +229,47 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -235,6 +229,47 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
// Bind constant buffer to vertex
// Bind constant buffer to vertex
pContext
->
VSSetConstantBuffers
(
0u
,
1u
,
pConstantBuffer
.
GetAddressOf
());
pContext
->
VSSetConstantBuffers
(
0u
,
1u
,
pConstantBuffer
.
GetAddressOf
());
// Create constant buffer for color
struct
ColorConstantBuffer
{
struct
{
float
r
;
float
g
;
float
b
;
float
a
;
}
face_colors
[
6
];
};
const
ColorConstantBuffer
ccb
=
{
{
{
1.0
f
,
0.0
f
,
1.0
f
},
{
1.0
f
,
0.0
f
,
0.0
f
},
{
0.0
f
,
1.0
f
,
0.0
f
},
{
0.0
f
,
0.0
f
,
1.0
f
},
{
1.0
f
,
1.0
f
,
0.0
f
},
{
0.0
f
,
1.0
f
,
1.0
f
},
}
};
wrl
::
ComPtr
<
ID3D11Buffer
>
pColorConstantBuffer
;
D3D11_BUFFER_DESC
ccbd
=
{};
ccbd
.
BindFlags
=
D3D11_BIND_CONSTANT_BUFFER
;
ccbd
.
Usage
=
D3D11_USAGE_DEFAULT
;
ccbd
.
CPUAccessFlags
=
0u
;
ccbd
.
MiscFlags
=
0u
;
ccbd
.
ByteWidth
=
sizeof
(
ccb
);
ccbd
.
StructureByteStride
=
0u
;
D3D11_SUBRESOURCE_DATA
ccsd
=
{};
ccsd
.
pSysMem
=
&
ccb
;
GFX_THROW_INFO
(
pDevice
->
CreateBuffer
(
&
ccbd
,
&
ccsd
,
&
pColorConstantBuffer
));
// Bind constant buffer to vertex
pContext
->
PSSetConstantBuffers
(
0u
,
1u
,
pColorConstantBuffer
.
GetAddressOf
());
// create pixel shader
// create pixel shader
wrl
::
ComPtr
<
ID3D11PixelShader
>
pPixelShader
;
wrl
::
ComPtr
<
ID3D11PixelShader
>
pPixelShader
;
wrl
::
ComPtr
<
ID3DBlob
>
pBlob
;
wrl
::
ComPtr
<
ID3DBlob
>
pBlob
;
...
@@ -258,7 +293,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
...
@@ -258,7 +293,7 @@ void Graphics::DrawTestTriangle(float angle, float x, float y)
const
D3D11_INPUT_ELEMENT_DESC
ied
[]
=
const
D3D11_INPUT_ELEMENT_DESC
ied
[]
=
{
{
{
"Position"
,
0
,
DXGI_FORMAT_R32G32B32_FLOAT
,
0
,
0
,
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
,
12
,
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/PixelShader.hlsl
View file @
680c085b
float4
main
(
float3
color
:
Color
)
:
SV_Target
cbuffer
CBuf
{
{
return
float4
(
color
,
1
.
0
f
);
float4
face_color
[
6
];
};
//float4 main( float3 color : Color) : SV_Target
float4
main
(
uint
tid
:
SV_PrimitiveID
)
:
SV_Target
{
//return float4(color, 1.0f);
return
face_color
[
tid
/
2
];
}
}
\ No newline at end of file
StudyDx/StudyDx.vcxproj
View file @
680c085b
...
@@ -180,6 +180,8 @@
...
@@ -180,6 +180,8 @@
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
Pixel
</ShaderType>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
Pixel
</ShaderType>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
4.0
</ShaderModel>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
4.0
</ShaderModel>
</FxCompile>
</FxCompile>
<FxCompile
Include=
"VertexShader.hlsl"
>
<FxCompile
Include=
"VertexShader.hlsl"
>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
...
...
StudyDx/VertexShader.hlsl
View file @
680c085b
struct
VSOut
{
float3
color
:
Color
;
float4
pos
:
SV_Position
;
};
cbuffer
CBuf
cbuffer
CBuf
{
{
matrix
transform
;
matrix
transform
;
};
};
VSOut
main
(
float3
pos
:
Position
,
float3
color
:
Color
)
float4
main
(
float3
pos
:
Position
)
:
SV_Position
{
{
VSOut
vso
;
return
mul
(
float4
(
pos
,
1
.
0
f
),
transform
);;
vso
.
pos
=
mul
(
float4
(
pos
,
1
.
0
f
),
transform
);
vso
.
color
=
color
;
return
vso
;
}
}
\ No newline at end of file
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