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
ce1e5732
Commit
ce1e5732
authored
May 14, 2019
by
chili
Browse files
using dynamic vtx layout for suzanne asstest
parent
2b41752b
Changes
6
Show whitespace changes
Inline
Side-by-side
hw3d/App.cpp
View file @
ce1e5732
...
...
@@ -13,46 +13,16 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "Vertex.h"
namespace
dx
=
DirectX
;
GDIPlusManager
gdipm
;
void
f
()
{
VertexBuffer
vb
(
std
::
move
(
VertexLayout
{}
.
Append
<
VertexLayout
::
Position3D
>
()
.
Append
<
VertexLayout
::
Normal
>
()
.
Append
<
VertexLayout
::
Texture2D
>
()
)
);
vb
.
EmplaceBack
(
dx
::
XMFLOAT3
{
1.0
f
,
1.0
f
,
5.0
f
},
dx
::
XMFLOAT3
{
2.0
f
,
1.0
f
,
4.0
f
},
dx
::
XMFLOAT2
{
6.0
f
,
9.0
f
}
);
vb
.
EmplaceBack
(
dx
::
XMFLOAT3
{
6.0
f
,
9.0
f
,
6.0
f
},
dx
::
XMFLOAT3
{
9.0
f
,
6.0
f
,
9.0
f
},
dx
::
XMFLOAT2
{
4.2
f
,
0.0
f
}
);
auto
pos
=
vb
[
0
].
Attr
<
VertexLayout
::
Position3D
>
();
auto
nor
=
vb
[
0
].
Attr
<
VertexLayout
::
Normal
>
();
auto
tex
=
vb
[
1
].
Attr
<
VertexLayout
::
Texture2D
>
();
vb
.
Back
().
Attr
<
VertexLayout
::
Position3D
>
().
z
=
420.0
f
;
pos
=
vb
.
Back
().
Attr
<
VertexLayout
::
Position3D
>
();
const
auto
&
cvb
=
vb
;
pos
=
cvb
[
1
].
Attr
<
VertexLayout
::
Position3D
>
();
}
App
::
App
()
:
wnd
(
800
,
600
,
"The Donkey Fart Box"
),
light
(
wnd
.
Gfx
()
)
{
f
();
class
Factory
{
public:
...
...
hw3d/AssTest.cpp
View file @
ce1e5732
...
...
@@ -4,6 +4,7 @@
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "Vertex.h"
AssTest
::
AssTest
(
Graphics
&
gfx
,
std
::
mt19937
&
rng
,
std
::
uniform_real_distribution
<
float
>&
adist
,
...
...
@@ -19,11 +20,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
if
(
!
IsStaticInitialized
()
)
{
struct
Vertex
{
dx
::
XMFLOAT3
pos
;
dx
::
XMFLOAT3
n
;
};
using
hw3dexp
::
VertexLayout
;
hw3dexp
::
VertexBuffer
vbuf
(
std
::
move
(
VertexLayout
{}
.
Append
<
VertexLayout
::
Position3D
>
()
.
Append
<
VertexLayout
::
Normal
>
()
));
Assimp
::
Importer
imp
;
const
auto
pModel
=
imp
.
ReadFile
(
"models
\\
suzanne.obj"
,
...
...
@@ -32,14 +34,12 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
);
const
auto
pMesh
=
pModel
->
mMeshes
[
0
];
std
::
vector
<
Vertex
>
vertices
;
vertices
.
reserve
(
pMesh
->
mNumVertices
);
for
(
unsigned
int
i
=
0
;
i
<
pMesh
->
mNumVertices
;
i
++
)
{
v
ertices
.
push_b
ack
(
{
{
pMesh
->
mVertices
[
i
].
x
*
scale
,
pMesh
->
mVertices
[
i
].
y
*
scale
,
pMesh
->
mVertices
[
i
].
z
*
scale
},
v
buf
.
EmplaceB
ack
(
dx
::
XMFLOAT3
{
pMesh
->
mVertices
[
i
].
x
*
scale
,
pMesh
->
mVertices
[
i
].
y
*
scale
,
pMesh
->
mVertices
[
i
].
z
*
scale
},
*
reinterpret_cast
<
dx
::
XMFLOAT3
*>
(
&
pMesh
->
mNormals
[
i
])
}
);
);
}
std
::
vector
<
unsigned
short
>
indices
;
...
...
@@ -53,7 +53,7 @@ AssTest::AssTest( Graphics& gfx,std::mt19937& rng,
indices
.
push_back
(
face
.
mIndices
[
2
]
);
}
AddStaticBind
(
std
::
make_unique
<
VertexBuffer
>
(
gfx
,
v
ertices
)
);
AddStaticBind
(
std
::
make_unique
<
VertexBuffer
>
(
gfx
,
v
buf
)
);
AddStaticIndexBuffer
(
std
::
make_unique
<
IndexBuffer
>
(
gfx
,
indices
)
);
...
...
hw3d/Vertex.h
View file @
ce1e5732
...
...
@@ -3,17 +3,19 @@
#include <DirectXMath.h>
#include <type_traits>
struct
BGRAColor
namespace
hw3dexp
{
struct
BGRAColor
{
unsigned
char
a
;
unsigned
char
r
;
unsigned
char
g
;
unsigned
char
b
;
};
};
class
VertexLayout
{
public:
class
VertexLayout
{
public:
enum
ElementType
{
Position2D
,
...
...
@@ -62,7 +64,7 @@ public:
case
Float4Color
:
return
sizeof
(
XMFLOAT3
);
case
BGRAColor
:
return
sizeof
(
::
BGRAColor
);
return
sizeof
(
hw3dexp
::
BGRAColor
);
}
assert
(
"Invalid element type"
&&
false
);
return
0u
;
...
...
@@ -75,7 +77,7 @@ public:
ElementType
type
;
size_t
offset
;
};
public:
public:
template
<
ElementType
Type
>
const
Element
&
Resolve
()
const
noexcept
(
!
IS_DEBUG
)
{
...
...
@@ -107,14 +109,14 @@ public:
{
return
elements
.
size
();
}
private:
private:
std
::
vector
<
Element
>
elements
;
};
};
class
Vertex
{
class
Vertex
{
friend
class
VertexBuffer
;
public:
public:
template
<
VertexLayout
::
ElementType
Type
>
auto
&
Attr
()
noexcept
(
!
IS_DEBUG
)
{
...
...
@@ -188,7 +190,7 @@ public:
assert
(
"Bad element type"
&&
false
);
}
}
protected:
protected:
Vertex
(
char
*
pData
,
const
VertexLayout
&
layout
)
noexcept
(
!
IS_DEBUG
)
:
pData
(
pData
),
...
...
@@ -196,7 +198,7 @@ protected:
{
assert
(
pData
!=
nullptr
);
}
private:
private:
template
<
typename
First
,
typename
...
Rest
>
// enables parameter pack setting of multiple parameters by element index
void
SetAttributeByIndex
(
size_t
i
,
First
&&
first
,
Rest
&&
...
rest
)
noexcept
(
!
IS_DEBUG
)
...
...
@@ -217,14 +219,14 @@ private:
assert
(
"Parameter attribute type mismatch"
&&
false
);
}
}
private:
private:
char
*
pData
=
nullptr
;
const
VertexLayout
&
layout
;
};
};
class
ConstVertex
{
public:
class
ConstVertex
{
public:
ConstVertex
(
const
Vertex
&
v
)
noexcept
(
!
IS_DEBUG
)
:
vertex
(
v
)
...
...
@@ -234,17 +236,21 @@ public:
{
return
const_cast
<
Vertex
&>
(
vertex
).
Attr
<
Type
>
();
}
private:
private:
Vertex
vertex
;
};
};
class
VertexBuffer
{
public:
class
VertexBuffer
{
public:
VertexBuffer
(
VertexLayout
layout
)
noexcept
(
!
IS_DEBUG
)
:
layout
(
std
::
move
(
layout
)
)
{}
const
char
*
GetData
()
const
noexcept
(
!
IS_DEBUG
)
{
return
buffer
.
data
();
}
const
VertexLayout
&
GetLayout
()
const
noexcept
{
return
layout
;
...
...
@@ -287,7 +293,8 @@ public:
{
return
const_cast
<
VertexBuffer
&>
(
*
this
)[
i
];
}
private:
private:
std
::
vector
<
char
>
buffer
;
VertexLayout
layout
;
};
\ No newline at end of file
};
}
\ No newline at end of file
hw3d/VertexBuffer.h
View file @
ce1e5732
#pragma once
#include "Bindable.h"
#include "GraphicsThrowMacros.h"
#include "Vertex.h"
class
VertexBuffer
:
public
Bindable
{
...
...
@@ -23,6 +24,23 @@ public:
sd
.
pSysMem
=
vertices
.
data
();
GFX_THROW_INFO
(
GetDevice
(
gfx
)
->
CreateBuffer
(
&
bd
,
&
sd
,
&
pVertexBuffer
)
);
}
VertexBuffer
(
Graphics
&
gfx
,
const
hw3dexp
::
VertexBuffer
&
vbuf
)
:
stride
(
(
UINT
)
vbuf
.
GetLayout
().
Size
()
)
{
INFOMAN
(
gfx
);
D3D11_BUFFER_DESC
bd
=
{};
bd
.
BindFlags
=
D3D11_BIND_VERTEX_BUFFER
;
bd
.
Usage
=
D3D11_USAGE_DEFAULT
;
bd
.
CPUAccessFlags
=
0u
;
bd
.
MiscFlags
=
0u
;
bd
.
ByteWidth
=
UINT
(
vbuf
.
Size
()
);
bd
.
StructureByteStride
=
stride
;
D3D11_SUBRESOURCE_DATA
sd
=
{};
sd
.
pSysMem
=
vbuf
.
GetData
();
GFX_THROW_INFO
(
GetDevice
(
gfx
)
->
CreateBuffer
(
&
bd
,
&
sd
,
&
pVertexBuffer
)
);
}
void
Bind
(
Graphics
&
gfx
)
noexcept
override
;
protected:
UINT
stride
;
...
...
hw3d/hw3d.vcxproj
View file @
ce1e5732
...
...
@@ -182,8 +182,8 @@
<ClInclude
Include=
"Texture.h"
/>
<ClInclude
Include=
"Topology.h"
/>
<ClInclude
Include=
"TransformCbuf.h"
/>
<ClInclude
Include=
"Vertex.h"
/>
<ClInclude
Include=
"VertexBuffer.h"
/>
<ClInclude
Include=
"VertexLayout.h"
/>
<ClInclude
Include=
"VertexShader.h"
/>
<ClInclude
Include=
"Window.h"
/>
<ClInclude
Include=
"WindowsMessageMap.h"
/>
...
...
hw3d/hw3d.vcxproj.filters
View file @
ce1e5732
...
...
@@ -326,7 +326,7 @@
<ClInclude
Include=
"AssTest.h"
>
<Filter>
Header Files\Drawable
</Filter>
</ClInclude>
<ClInclude
Include=
"Vertex
Layout
.h"
>
<ClInclude
Include=
"Vertex.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
...
...
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