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
c60c4cb1
Commit
c60c4cb1
authored
Jul 27, 2019
by
chili
Browse files
unified bind container with shared_ptr
parent
dd684b5e
Changes
9
Show whitespace changes
Inline
Side-by-side
hw3d/Drawable.cpp
View file @
c60c4cb1
...
...
@@ -11,22 +11,16 @@ void Drawable::Draw( Graphics& gfx ) const noxnd
{
b
->
Bind
(
gfx
);
}
for
(
auto
&
b
:
GetStaticBinds
()
)
{
b
->
Bind
(
gfx
);
}
gfx
.
DrawIndexed
(
pIndexBuffer
->
GetCount
()
);
}
void
Drawable
::
AddBind
(
std
::
unique
_ptr
<
Bindable
>
bind
)
noxnd
void
Drawable
::
AddBind
(
std
::
shared
_ptr
<
Bindable
>
bind
)
noxnd
{
assert
(
"*Must* use AddIndexBuffer to bind index buffer"
&&
typeid
(
*
bind
)
!=
typeid
(
IndexBuffer
)
);
// special case for index buffer
if
(
typeid
(
*
bind
)
==
typeid
(
IndexBuffer
)
)
{
assert
(
"Binding multiple index buffers not allowed"
&&
pIndexBuffer
==
nullptr
);
pIndexBuffer
=
&
static_cast
<
IndexBuffer
&>
(
*
bind
);
}
binds
.
push_back
(
std
::
move
(
bind
)
);
}
\ No newline at end of file
void
Drawable
::
AddIndexBuffer
(
std
::
unique_ptr
<
IndexBuffer
>
ibuf
)
noxnd
{
assert
(
"Attempting to add index buffer a second time"
&&
pIndexBuffer
==
nullptr
);
pIndexBuffer
=
ibuf
.
get
();
binds
.
push_back
(
std
::
move
(
ibuf
)
);
}
\ No newline at end of file
hw3d/Drawable.h
View file @
c60c4cb1
...
...
@@ -2,6 +2,7 @@
#include "Graphics.h"
#include <DirectXMath.h>
#include "ConditionalNoexcept.h"
#include <memory>
namespace
Bind
{
...
...
@@ -11,15 +12,11 @@ namespace Bind
class
Drawable
{
template
<
class
T
>
friend
class
DrawableBase
;
public:
Drawable
()
=
default
;
Drawable
(
const
Drawable
&
)
=
delete
;
virtual
DirectX
::
XMMATRIX
GetTransformXM
()
const
noexcept
=
0
;
void
Draw
(
Graphics
&
gfx
)
const
noxnd
;
virtual
void
Update
(
float
dt
)
noexcept
{}
virtual
~
Drawable
()
=
default
;
protected:
template
<
class
T
>
...
...
@@ -34,11 +31,8 @@ protected:
}
return
nullptr
;
}
void
AddBind
(
std
::
unique_ptr
<
Bind
::
Bindable
>
bind
)
noxnd
;
void
AddIndexBuffer
(
std
::
unique_ptr
<
Bind
::
IndexBuffer
>
ibuf
)
noxnd
;
private:
virtual
const
std
::
vector
<
std
::
unique_ptr
<
Bind
::
Bindable
>>&
GetStaticBinds
()
const
noexcept
=
0
;
void
AddBind
(
std
::
shared_ptr
<
Bind
::
Bindable
>
bind
)
noxnd
;
private:
const
Bind
::
IndexBuffer
*
pIndexBuffer
=
nullptr
;
std
::
vector
<
std
::
unique
_ptr
<
Bind
::
Bindable
>>
binds
;
std
::
vector
<
std
::
shared
_ptr
<
Bind
::
Bindable
>>
binds
;
};
\ No newline at end of file
hw3d/DrawableBase.h
deleted
100644 → 0
View file @
dd684b5e
#pragma once
#include "Drawable.h"
#include "IndexBuffer.h"
#include "ConditionalNoexcept.h"
template
<
class
T
>
class
DrawableBase
:
public
Drawable
{
protected:
static
bool
IsStaticInitialized
()
noexcept
{
return
!
staticBinds
.
empty
();
}
static
void
AddStaticBind
(
std
::
unique_ptr
<
Bind
::
Bindable
>
bind
)
noxnd
{
assert
(
"*Must* use AddStaticIndexBuffer to bind index buffer"
&&
typeid
(
*
bind
)
!=
typeid
(
Bind
::
IndexBuffer
)
);
staticBinds
.
push_back
(
std
::
move
(
bind
)
);
}
void
AddStaticIndexBuffer
(
std
::
unique_ptr
<
Bind
::
IndexBuffer
>
ibuf
)
noxnd
{
assert
(
"Attempting to add index buffer a second time"
&&
pIndexBuffer
==
nullptr
);
pIndexBuffer
=
ibuf
.
get
();
staticBinds
.
push_back
(
std
::
move
(
ibuf
)
);
}
void
SetIndexFromStatic
()
noxnd
{
assert
(
"Attempting to add index buffer a second time"
&&
pIndexBuffer
==
nullptr
);
for
(
const
auto
&
b
:
staticBinds
)
{
if
(
const
auto
p
=
dynamic_cast
<
Bind
::
IndexBuffer
*>
(
b
.
get
())
)
{
pIndexBuffer
=
p
;
return
;
}
}
assert
(
"Failed to find index buffer in static binds"
&&
pIndexBuffer
!=
nullptr
);
}
private:
const
std
::
vector
<
std
::
unique_ptr
<
Bind
::
Bindable
>>&
GetStaticBinds
()
const
noexcept
override
{
return
staticBinds
;
}
private:
static
std
::
vector
<
std
::
unique_ptr
<
Bind
::
Bindable
>>
staticBinds
;
};
template
<
class
T
>
std
::
vector
<
std
::
unique_ptr
<
Bind
::
Bindable
>>
DrawableBase
<
T
>::
staticBinds
;
\ No newline at end of file
hw3d/Mesh.cpp
View file @
c60c4cb1
...
...
@@ -32,27 +32,16 @@ const std::string& ModelException::GetNote() const noexcept
}
// Mesh
Mesh
::
Mesh
(
Graphics
&
gfx
,
std
::
vector
<
std
::
unique
_ptr
<
Bind
::
Bindable
>>
bindPtrs
)
Mesh
::
Mesh
(
Graphics
&
gfx
,
std
::
vector
<
std
::
shared
_ptr
<
Bind
::
Bindable
>>
bindPtrs
)
{
if
(
!
IsStaticInitialized
()
)
{
AddStaticBind
(
std
::
make_unique
<
Bind
::
Topology
>
(
gfx
,
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
)
);
}
AddBind
(
std
::
make_shared
<
Bind
::
Topology
>
(
gfx
,
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
)
);
for
(
auto
&
pb
:
bindPtrs
)
{
if
(
auto
pi
=
dynamic_cast
<
Bind
::
IndexBuffer
*>
(
pb
.
get
())
)
{
AddIndexBuffer
(
std
::
unique_ptr
<
Bind
::
IndexBuffer
>
{
pi
}
);
pb
.
release
();
}
else
{
AddBind
(
std
::
move
(
pb
)
);
}
}
AddBind
(
std
::
make_
unique
<
Bind
::
TransformCbuf
>
(
gfx
,
*
this
)
);
AddBind
(
std
::
make_
shared
<
Bind
::
TransformCbuf
>
(
gfx
,
*
this
)
);
}
void
Mesh
::
Draw
(
Graphics
&
gfx
,
DirectX
::
FXMMATRIX
accumulatedTransform
)
const
noxnd
{
...
...
@@ -268,7 +257,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
indices
.
push_back
(
face
.
mIndices
[
2
]
);
}
std
::
vector
<
std
::
unique
_ptr
<
Bind
::
Bindable
>>
bindablePtrs
;
std
::
vector
<
std
::
shared
_ptr
<
Bind
::
Bindable
>>
bindablePtrs
;
bool
hasSpecularMap
=
false
;
float
shininess
=
35.0
f
;
...
...
@@ -281,11 +270,11 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
aiString
texFileName
;
material
.
GetTexture
(
aiTextureType_DIFFUSE
,
0
,
&
texFileName
);
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
Texture
>
(
gfx
,
Surface
::
FromFile
(
base
+
texFileName
.
C_Str
()
)
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
Texture
>
(
gfx
,
Surface
::
FromFile
(
base
+
texFileName
.
C_Str
()
)
)
);
if
(
material
.
GetTexture
(
aiTextureType_SPECULAR
,
0
,
&
texFileName
)
==
aiReturn_SUCCESS
)
{
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
Texture
>
(
gfx
,
Surface
::
FromFile
(
base
+
texFileName
.
C_Str
()
),
1
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
Texture
>
(
gfx
,
Surface
::
FromFile
(
base
+
texFileName
.
C_Str
()
),
1
)
);
hasSpecularMap
=
true
;
}
else
...
...
@@ -293,26 +282,26 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
material
.
Get
(
AI_MATKEY_SHININESS
,
shininess
);
}
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
Sampler
>
(
gfx
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
Sampler
>
(
gfx
)
);
}
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
VertexBuffer
>
(
gfx
,
vbuf
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
VertexBuffer
>
(
gfx
,
vbuf
)
);
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
IndexBuffer
>
(
gfx
,
indices
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
IndexBuffer
>
(
gfx
,
indices
)
);
auto
pvs
=
std
::
make_
unique
<
Bind
::
VertexShader
>
(
gfx
,
L"PhongVS.cso"
);
auto
pvs
=
std
::
make_
shared
<
Bind
::
VertexShader
>
(
gfx
,
L"PhongVS.cso"
);
auto
pvsbc
=
pvs
->
GetBytecode
();
bindablePtrs
.
push_back
(
std
::
move
(
pvs
)
);
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
InputLayout
>
(
gfx
,
vbuf
.
GetLayout
().
GetD3DLayout
(),
pvsbc
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
InputLayout
>
(
gfx
,
vbuf
.
GetLayout
().
GetD3DLayout
(),
pvsbc
)
);
if
(
hasSpecularMap
)
{
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
PixelShader
>
(
gfx
,
L"PhongPSSpecMap.cso"
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
PixelShader
>
(
gfx
,
L"PhongPSSpecMap.cso"
)
);
}
else
{
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
PixelShader
>
(
gfx
,
L"PhongPS.cso"
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
PixelShader
>
(
gfx
,
L"PhongPS.cso"
)
);
struct
PSMaterialConstant
{
...
...
@@ -321,7 +310,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
float
padding
[
2
];
}
pmc
;
pmc
.
specularPower
=
shininess
;
bindablePtrs
.
push_back
(
std
::
make_
unique
<
Bind
::
PixelConstantBuffer
<
PSMaterialConstant
>>
(
gfx
,
pmc
,
1u
)
);
bindablePtrs
.
push_back
(
std
::
make_
shared
<
Bind
::
PixelConstantBuffer
<
PSMaterialConstant
>>
(
gfx
,
pmc
,
1u
)
);
}
return
std
::
make_unique
<
Mesh
>
(
gfx
,
std
::
move
(
bindablePtrs
)
);
...
...
hw3d/Mesh.h
View file @
c60c4cb1
#pragma once
#include "Drawable
Base
.h"
#include "Drawable.h"
#include "BindableCommon.h"
#include "Vertex.h"
#include <optional>
...
...
@@ -20,10 +20,10 @@ private:
std
::
string
note
;
};
class
Mesh
:
public
Drawable
Base
<
Mesh
>
class
Mesh
:
public
Drawable
{
public:
Mesh
(
Graphics
&
gfx
,
std
::
vector
<
std
::
unique
_ptr
<
Bind
::
Bindable
>>
bindPtrs
);
Mesh
(
Graphics
&
gfx
,
std
::
vector
<
std
::
shared
_ptr
<
Bind
::
Bindable
>>
bindPtrs
);
void
Draw
(
Graphics
&
gfx
,
DirectX
::
FXMMATRIX
accumulatedTransform
)
const
noxnd
;
DirectX
::
XMMATRIX
GetTransformXM
()
const
noexcept
override
;
private:
...
...
hw3d/SolidSphere.cpp
View file @
c60c4cb1
...
...
@@ -9,48 +9,39 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
using
namespace
Bind
;
namespace
dx
=
DirectX
;
if
(
!
IsStaticInitialized
()
)
{
struct
Vertex
{
dx
::
XMFLOAT3
pos
;
};
auto
model
=
Sphere
::
Make
<
Vertex
>
();
model
.
Transform
(
dx
::
XMMatrixScaling
(
radius
,
radius
,
radius
)
);
AddBind
(
std
::
make_
unique
<
VertexBuffer
>
(
gfx
,
model
.
vertices
)
);
Add
IndexBuffer
(
std
::
make_
unique
<
IndexBuffer
>
(
gfx
,
model
.
indices
)
);
AddBind
(
std
::
make_
shared
<
VertexBuffer
>
(
gfx
,
model
.
vertices
)
);
Add
Bind
(
std
::
make_
shared
<
IndexBuffer
>
(
gfx
,
model
.
indices
)
);
auto
pvs
=
std
::
make_
unique
<
VertexShader
>
(
gfx
,
L"SolidVS.cso"
);
auto
pvs
=
std
::
make_
shared
<
VertexShader
>
(
gfx
,
L"SolidVS.cso"
);
auto
pvsbc
=
pvs
->
GetBytecode
();
Add
Static
Bind
(
std
::
move
(
pvs
)
);
AddBind
(
std
::
move
(
pvs
)
);
Add
Static
Bind
(
std
::
make_
unique
<
PixelShader
>
(
gfx
,
L"SolidPS.cso"
)
);
AddBind
(
std
::
make_
shared
<
PixelShader
>
(
gfx
,
L"SolidPS.cso"
)
);
struct
PSColorConstant
{
dx
::
XMFLOAT3
color
=
{
1.0
f
,
1.0
f
,
1.0
f
};
float
padding
;
}
colorConst
;
Add
Static
Bind
(
std
::
make_
unique
<
PixelConstantBuffer
<
PSColorConstant
>>
(
gfx
,
colorConst
)
);
AddBind
(
std
::
make_
shared
<
PixelConstantBuffer
<
PSColorConstant
>>
(
gfx
,
colorConst
)
);
const
std
::
vector
<
D3D11_INPUT_ELEMENT_DESC
>
ied
=
{
{
"Position"
,
0
,
DXGI_FORMAT_R32G32B32_FLOAT
,
0
,
0
,
D3D11_INPUT_PER_VERTEX_DATA
,
0
},
};
Add
Static
Bind
(
std
::
make_
unique
<
InputLayout
>
(
gfx
,
ied
,
pvsbc
)
);
AddBind
(
std
::
make_
shared
<
InputLayout
>
(
gfx
,
ied
,
pvsbc
)
);
AddStaticBind
(
std
::
make_unique
<
Topology
>
(
gfx
,
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
)
);
}
else
{
SetIndexFromStatic
();
}
AddBind
(
std
::
make_shared
<
Topology
>
(
gfx
,
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST
)
);
AddBind
(
std
::
make_
unique
<
TransformCbuf
>
(
gfx
,
*
this
)
);
AddBind
(
std
::
make_
shared
<
TransformCbuf
>
(
gfx
,
*
this
)
);
}
void
SolidSphere
::
Update
(
float
dt
)
noexcept
{}
void
SolidSphere
::
SetPos
(
DirectX
::
XMFLOAT3
pos
)
noexcept
{
this
->
pos
=
pos
;
...
...
hw3d/SolidSphere.h
View file @
c60c4cb1
#pragma once
#include "Drawable
Base
.h"
#include "Drawable.h"
class
SolidSphere
:
public
Drawable
Base
<
SolidSphere
>
class
SolidSphere
:
public
Drawable
{
public:
SolidSphere
(
Graphics
&
gfx
,
float
radius
);
void
Update
(
float
dt
)
noexcept
override
;
void
SetPos
(
DirectX
::
XMFLOAT3
pos
)
noexcept
;
DirectX
::
XMMATRIX
GetTransformXM
()
const
noexcept
override
;
private:
...
...
hw3d/hw3d.vcxproj
View file @
c60c4cb1
...
...
@@ -141,7 +141,6 @@
<ClInclude
Include=
"ConditionalNoexcept.h"
/>
<ClInclude
Include=
"ConstantBuffers.h"
/>
<ClInclude
Include=
"Drawable.h"
/>
<ClInclude
Include=
"DrawableBase.h"
/>
<ClInclude
Include=
"dxerr.h"
/>
<ClInclude
Include=
"DxgiInfoManager.h"
/>
<ClInclude
Include=
"GDIPlusManager.h"
/>
...
...
hw3d/hw3d.vcxproj.filters
View file @
c60c4cb1
...
...
@@ -221,9 +221,6 @@
<ClInclude
Include=
"Drawable.h"
>
<Filter>
Header Files\Drawable
</Filter>
</ClInclude>
<ClInclude
Include=
"DrawableBase.h"
>
<Filter>
Header Files\Drawable
</Filter>
</ClInclude>
<ClInclude
Include=
"WindowsThrowMacros.h"
>
<Filter>
Header Files\Macros
</Filter>
</ClInclude>
...
...
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