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
hw3d
Commits
e16cbfd1
Commit
e16cbfd1
authored
May 02, 2019
by
chili
Browse files
cone added but it's weird :excitement:
parent
d6e000ca
Changes
8
Hide whitespace changes
Inline
Side-by-side
hw3d/App.cpp
View file @
e16cbfd1
#include "App.h"
#include "Box.h"
#include "Cylinder.h"
#include "Pyramid.h"
#include <memory>
#include <algorithm>
#include "ChiliMath.h"
...
...
@@ -40,6 +41,11 @@ App::App()
gfx
,
rng
,
adist
,
ddist
,
odist
,
rdist
,
bdist
,
tdist
);
case
2
:
return
std
::
make_unique
<
Pyramid
>
(
gfx
,
rng
,
adist
,
ddist
,
odist
,
rdist
,
tdist
);
default:
assert
(
false
&&
"impossible drawable option in factory"
);
return
{};
...
...
@@ -48,7 +54,7 @@ App::App()
private:
Graphics
&
gfx
;
std
::
mt19937
rng
{
std
::
random_device
{}()
};
std
::
uniform_int_distribution
<
int
>
sdist
{
0
,
1
};
std
::
uniform_int_distribution
<
int
>
sdist
{
0
,
2
};
std
::
uniform_real_distribution
<
float
>
adist
{
0.0
f
,
PI
*
2.0
f
};
std
::
uniform_real_distribution
<
float
>
ddist
{
0.0
f
,
PI
*
0.5
f
};
std
::
uniform_real_distribution
<
float
>
odist
{
0.0
f
,
PI
*
0.08
f
};
...
...
hw3d/BlendedPhongPS.hlsl
0 → 100644
View file @
e16cbfd1
cbuffer
LightCBuf
{
float3
lightPos
;
float3
ambient
;
float3
diffuseColor
;
float
diffuseIntensity
;
float
attConst
;
float
attLin
;
float
attQuad
;
};
cbuffer
ObjectCBuf
{
float
specularIntensity
;
float
specularPower
;
float
padding
[
2
];
};
float4
main
(
float3
worldPos
:
Position
,
float3
n
:
Normal
,
float3
color
:
Color
)
:
SV_Target
{
// fragment to light vector data
const
float3
vToL
=
lightPos
-
worldPos
;
const
float
distToL
=
length
(
vToL
);
const
float3
dirToL
=
vToL
/
distToL
;
// attenuation
const
float
att
=
1
.
0
f
/
(
attConst
+
attLin
*
distToL
+
attQuad
*
(
distToL
*
distToL
));
// diffuse intensity
const
float3
diffuse
=
diffuseColor
*
diffuseIntensity
*
att
*
max
(
0
.
0
f
,
dot
(
dirToL
,
n
)
);
// reflected light vector
const
float3
w
=
n
*
dot
(
vToL
,
n
);
const
float3
r
=
w
*
2
.
0
f
-
vToL
;
// calculate specular intensity based on angle between viewing vector and reflection vector, narrow with power function
const
float3
specular
=
att
*
(
diffuseColor
*
diffuseIntensity
)
*
specularIntensity
*
pow
(
max
(
0
.
0
f
,
dot
(
normalize
(
-
r
),
normalize
(
worldPos
)
)
),
specularPower
);
// final color
return
float4
(
saturate
((
diffuse
+
ambient
+
specular
)
*
color
),
1
.
0
f
);
}
\ No newline at end of file
hw3d/BlendedPhongVS.hlsl
0 → 100644
View file @
e16cbfd1
cbuffer
CBuf
{
matrix
modelView
;
matrix
modelViewProj
;
};
struct
VSOut
{
float3
worldPos
:
Position
;
float3
normal
:
Normal
;
float3
color
:
Color
;
float4
pos
:
SV_Position
;
};
VSOut
main
(
float3
pos
:
Position
,
float3
n
:
Normal
,
float3
color
:
Color
)
{
VSOut
vso
;
vso
.
worldPos
=
(
float3
)
mul
(
float4
(
pos
,
1
.
0
f
),
modelView
);
vso
.
normal
=
mul
(
n
,(
float3x3
)
modelView
);
vso
.
pos
=
mul
(
float4
(
pos
,
1
.
0
f
),
modelViewProj
);
vso
.
color
=
color
;
return
vso
;
}
\ No newline at end of file
hw3d/Cone.h
View file @
e16cbfd1
...
...
@@ -20,7 +20,7 @@ public:
for
(
int
iLong
=
0
;
iLong
<
longDiv
;
iLong
++
)
{
vertices
.
emplace_back
();
auto
v
=
dx
::
XMVector3Transform
(
auto
v
=
dx
::
XMVector3Transform
(
base
,
dx
::
XMMatrixRotationZ
(
longitudeAngle
*
iLong
)
);
...
...
@@ -35,7 +35,7 @@ public:
vertices
.
back
().
pos
=
{
0.0
f
,
0.0
f
,
1.0
f
};
const
auto
iTip
=
(
unsigned
short
)(
vertices
.
size
()
-
1
);
// base indices
std
::
vector
<
unsigned
short
>
indices
;
for
(
unsigned
short
iLong
=
0
;
iLong
<
longDiv
;
iLong
++
)
...
...
@@ -56,6 +56,69 @@ public:
return
{
std
::
move
(
vertices
),
std
::
move
(
indices
)
};
}
template
<
class
V
>
static
IndexedTriangleList
<
V
>
MakeTesselatedIndependentFaces
(
int
longDiv
)
{
namespace
dx
=
DirectX
;
assert
(
longDiv
>=
3
);
const
auto
base
=
dx
::
XMVectorSet
(
1.0
f
,
0.0
f
,
-
1.0
f
,
0.0
f
);
const
float
longitudeAngle
=
2.0
f
*
PI
/
longDiv
;
std
::
vector
<
V
>
vertices
;
// cone vertices
const
auto
iCone
=
(
unsigned
short
)
vertices
.
size
();
for
(
int
iLong
=
0
;
iLong
<
longDiv
;
iLong
++
)
{
const
float
thetas
[]
=
{
longitudeAngle
*
iLong
,
longitudeAngle
*
(((
iLong
+
1
)
==
longDiv
)
?
0
:
(
iLong
+
1
))
};
vertices
.
emplace_back
();
vertices
.
back
().
pos
=
{
0.0
f
,
0.0
f
,
1.0
f
};
for
(
auto
theta
:
thetas
)
{
vertices
.
emplace_back
();
const
auto
v
=
dx
::
XMVector3Transform
(
base
,
dx
::
XMMatrixRotationZ
(
theta
)
);
dx
::
XMStoreFloat3
(
&
vertices
.
back
().
pos
,
v
);
}
}
// base vertices
const
auto
iBaseCenter
=
(
unsigned
short
)
vertices
.
size
();
vertices
.
emplace_back
();
vertices
.
back
().
pos
=
{
0.0
f
,
0.0
f
,
-
1.0
f
};
const
auto
iBaseEdge
=
(
unsigned
short
)
vertices
.
size
();
for
(
int
iLong
=
0
;
iLong
<
longDiv
;
iLong
++
)
{
vertices
.
emplace_back
();
auto
v
=
dx
::
XMVector3Transform
(
base
,
dx
::
XMMatrixRotationZ
(
longitudeAngle
*
iLong
)
);
dx
::
XMStoreFloat3
(
&
vertices
.
back
().
pos
,
v
);
}
std
::
vector
<
unsigned
short
>
indices
;
// cone indices
for
(
unsigned
short
i
=
0
;
i
<
longDiv
*
3
;
i
++
)
{
indices
.
push_back
(
i
+
iCone
);
}
// base indices
for
(
unsigned
short
iLong
=
0
;
iLong
<
longDiv
;
iLong
++
)
{
indices
.
push_back
(
iBaseCenter
);
indices
.
push_back
(
(
iLong
+
1
)
%
longDiv
+
iBaseEdge
);
indices
.
push_back
(
iLong
+
iBaseEdge
);
}
return
{
std
::
move
(
vertices
),
std
::
move
(
indices
)
};
}
template
<
class
V
>
static
IndexedTriangleList
<
V
>
Make
()
{
return
MakeTesselated
<
V
>
(
24
);
...
...
hw3d/Pyramid.cpp
View file @
e16cbfd1
...
...
@@ -2,25 +2,17 @@
#include "BindableBase.h"
#include "GraphicsThrowMacros.h"
#include "Cone.h"
#include <array>
Pyramid
::
Pyramid
(
Graphics
&
gfx
,
std
::
mt19937
&
rng
,
Pyramid
::
Pyramid
(
Graphics
&
gfx
,
std
::
mt19937
&
rng
,
std
::
uniform_real_distribution
<
float
>&
adist
,
std
::
uniform_real_distribution
<
float
>&
ddist
,
std
::
uniform_real_distribution
<
float
>&
odist
,
std
::
uniform_real_distribution
<
float
>&
rdist
)
std
::
uniform_real_distribution
<
float
>&
rdist
,
std
::
uniform_int_distribution
<
int
>&
tdist
)
:
r
(
rdist
(
rng
)
),
droll
(
ddist
(
rng
)
),
dpitch
(
ddist
(
rng
)
),
dyaw
(
ddist
(
rng
)
),
dphi
(
odist
(
rng
)
),
dtheta
(
odist
(
rng
)
),
dchi
(
odist
(
rng
)
),
chi
(
adist
(
rng
)
),
theta
(
adist
(
rng
)
),
phi
(
adist
(
rng
)
)
TestObject
(
gfx
,
rng
,
adist
,
ddist
,
odist
,
rdist
)
{
namespace
dx
=
DirectX
;
...
...
@@ -29,39 +21,37 @@ Pyramid::Pyramid( Graphics& gfx,
struct
Vertex
{
dx
::
XMFLOAT3
pos
;
struct
{
unsigned
char
r
;
unsigned
char
g
;
unsigned
char
b
;
unsigned
char
a
;
}
color
;
dx
::
XMFLOAT3
n
;
std
::
array
<
char
,
4
>
color
;
char
padding
;
};
auto
model
=
Cone
::
MakeTesselated
<
Vertex
>
(
4
);
auto
model
=
Cone
::
MakeTesselated
IndependentFaces
<
Vertex
>
(
tdist
(
rng
)
);
// set vertex colors for mesh
model
.
vertices
[
0
].
color
=
{
255
,
255
,
0
};
model
.
vertices
[
1
].
color
=
{
255
,
255
,
0
};
model
.
vertices
[
2
].
color
=
{
255
,
255
,
0
};
model
.
vertices
[
3
].
color
=
{
255
,
255
,
0
};
model
.
vertices
[
4
].
color
=
{
255
,
255
,
80
};
model
.
vertices
[
5
].
color
=
{
255
,
10
,
0
};
// deform mesh linearly
for
(
auto
&
v
:
model
.
vertices
)
{
v
.
color
=
{
(
char
)
40
,(
char
)
40
,(
char
)
255
};
}
model
.
vertices
.
front
().
color
=
{
(
char
)
255
,(
char
)
20
,(
char
)
20
};
// very first vertex is the cone tip
// squash mesh a bit in the z direction
model
.
Transform
(
dx
::
XMMatrixScaling
(
1.0
f
,
1.0
f
,
0.7
f
)
);
// add normals
model
.
SetNormalsIndependentFlat
();
AddStaticBind
(
std
::
make_unique
<
VertexBuffer
>
(
gfx
,
model
.
vertices
)
);
auto
pvs
=
std
::
make_unique
<
VertexShader
>
(
gfx
,
L"
Color
BlendVS.cso"
);
auto
pvs
=
std
::
make_unique
<
VertexShader
>
(
gfx
,
L"Blend
edPhong
VS.cso"
);
auto
pvsbc
=
pvs
->
GetBytecode
();
AddStaticBind
(
std
::
move
(
pvs
)
);
AddStaticBind
(
std
::
make_unique
<
PixelShader
>
(
gfx
,
L"
Color
BlendPS.cso"
)
);
AddStaticBind
(
std
::
make_unique
<
PixelShader
>
(
gfx
,
L"Blend
edPhong
PS.cso"
)
);
AddStaticIndexBuffer
(
std
::
make_unique
<
IndexBuffer
>
(
gfx
,
model
.
indices
)
);
const
std
::
vector
<
D3D11_INPUT_ELEMENT_DESC
>
ied
=
{
{
"Position"
,
0
,
DXGI_FORMAT_R32G32B32_FLOAT
,
0
,
0
,
D3D11_INPUT_PER_VERTEX_DATA
,
0
},
{
"Color"
,
0
,
DXGI_FORMAT_R8G8B8A8_UNORM
,
0
,
12
,
D3D11_INPUT_PER_VERTEX_DATA
,
0
},
{
"Normal"
,
0
,
DXGI_FORMAT_R32G32B32_FLOAT
,
0
,
12
,
D3D11_INPUT_PER_VERTEX_DATA
,
0
},
{
"Color"
,
0
,
DXGI_FORMAT_R8G8B8A8_UNORM
,
0
,
24
,
D3D11_INPUT_PER_VERTEX_DATA
,
0
},
};
AddStaticBind
(
std
::
make_unique
<
InputLayout
>
(
gfx
,
ied
,
pvsbc
)
);
...
...
@@ -74,21 +64,3 @@ Pyramid::Pyramid( Graphics& gfx,
AddBind
(
std
::
make_unique
<
TransformCbuf
>
(
gfx
,
*
this
)
);
}
void
Pyramid
::
Update
(
float
dt
)
noexcept
{
roll
+=
droll
*
dt
;
pitch
+=
dpitch
*
dt
;
yaw
+=
dyaw
*
dt
;
theta
+=
dtheta
*
dt
;
phi
+=
dphi
*
dt
;
chi
+=
dchi
*
dt
;
}
DirectX
::
XMMATRIX
Pyramid
::
GetTransformXM
()
const
noexcept
{
namespace
dx
=
DirectX
;
return
dx
::
XMMatrixRotationRollPitchYaw
(
pitch
,
yaw
,
roll
)
*
dx
::
XMMatrixTranslation
(
r
,
0.0
f
,
0.0
f
)
*
dx
::
XMMatrixRotationRollPitchYaw
(
theta
,
phi
,
chi
);
}
hw3d/Pyramid.h
View file @
e16cbfd1
#pragma once
#include "
DrawableBase
.h"
#include "
TestObject
.h"
class
Pyramid
:
public
DrawableBase
<
Pyramid
>
class
Pyramid
:
public
TestObject
<
Pyramid
>
{
public:
Pyramid
(
Graphics
&
gfx
,
std
::
mt19937
&
rng
,
std
::
uniform_real_distribution
<
float
>&
adist
,
std
::
uniform_real_distribution
<
float
>&
ddist
,
std
::
uniform_real_distribution
<
float
>&
odist
,
std
::
uniform_real_distribution
<
float
>&
rdist
);
void
Update
(
float
dt
)
noexcept
override
;
DirectX
::
XMMATRIX
GetTransformXM
()
const
noexcept
override
;
private:
// positional
float
r
;
float
roll
=
0.0
f
;
float
pitch
=
0.0
f
;
float
yaw
=
0.0
f
;
float
theta
;
float
phi
;
float
chi
;
// speed (delta/s)
float
droll
;
float
dpitch
;
float
dyaw
;
float
dtheta
;
float
dphi
;
float
dchi
;
std
::
uniform_real_distribution
<
float
>&
rdist
,
std
::
uniform_int_distribution
<
int
>&
tdist
);
};
\ No newline at end of file
hw3d/hw3d.vcxproj
View file @
e16cbfd1
...
...
@@ -254,6 +254,34 @@
<None
Include=
"DXTrace.inl"
/>
</ItemGroup>
<ItemGroup>
<FxCompile
Include=
"BlendedPhongPS.hlsl"
>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
Pixel
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
Pixel
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
Pixel
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
Pixel
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
4.0
</ShaderModel>
</FxCompile>
<FxCompile
Include=
"BlendedPhongVS.hlsl"
>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
Vertex
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
Vertex
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
Vertex
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
4.0
</ShaderModel>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
Vertex
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
4.0
</ShaderModel>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
<ObjectFileOutput
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
$(ProjectDir)%(Filename).cso
</ObjectFileOutput>
</FxCompile>
<FxCompile
Include=
"ColorBlendPS.hlsl"
>
<ShaderType
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
Pixel
</ShaderType>
<ShaderModel
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
4.0
</ShaderModel>
...
...
hw3d/hw3d.vcxproj.filters
View file @
e16cbfd1
...
...
@@ -370,5 +370,11 @@
<FxCompile
Include=
"IndexedPhongPS.hlsl"
>
<Filter>
Shader
</Filter>
</FxCompile>
<FxCompile
Include=
"BlendedPhongPS.hlsl"
>
<Filter>
Shader
</Filter>
</FxCompile>
<FxCompile
Include=
"BlendedPhongVS.hlsl"
>
<Filter>
Shader
</Filter>
</FxCompile>
</ItemGroup>
</Project>
\ 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