Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Game Library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Onuralp SEZER
Game Library
Commits
02f30c6c
Commit
02f30c6c
authored
2 years ago
by
Joseph Walton-Rivers
Browse files
Options
Downloads
Patches
Plain Diff
cleanup static model rendering code
parent
5ded7174
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fggl/gfx/ogl4/models.cpp
+48
-66
48 additions, 66 deletions
fggl/gfx/ogl4/models.cpp
with
48 additions
and
66 deletions
fggl/gfx/ogl4/models.cpp
+
48
−
66
View file @
02f30c6c
...
...
@@ -30,6 +30,44 @@
namespace
fggl
::
gfx
::
ogl4
{
static
std
::
shared_ptr
<
ogl
::
ArrayBuffer
>
setupArrayBuffer
(
std
::
shared_ptr
<
ogl
::
VertexArray
>&
vao
,
std
::
vector
<
data
::
Vertex
>&
data
)
{
auto
buff
=
std
::
make_shared
<
ogl
::
ArrayBuffer
>
();
buff
->
write
(
data
.
size
()
*
sizeof
(
data
::
Vertex
),
data
.
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
// set up the vertex attributes
auto
posAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
posititon
));
auto
normalAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
normal
));
auto
colAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
colour
));
vao
->
setAttribute
(
*
buff
.
get
(),
0
,
posAttr
);
vao
->
setAttribute
(
*
buff
.
get
(),
1
,
normalAttr
);
vao
->
setAttribute
(
*
buff
.
get
(),
3
,
colAttr
);
return
buff
;
}
static
std
::
shared_ptr
<
ogl
::
ElementBuffer
>
setupIndexBuffer
(
std
::
shared_ptr
<
ogl
::
VertexArray
>&
vao
,
std
::
vector
<
uint32_t
>&
data
)
{
auto
elementBuffer
=
std
::
make_shared
<
ogl
::
ElementBuffer
>
();
elementBuffer
->
write
(
data
.
size
()
*
sizeof
(
uint32_t
),
data
.
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
return
elementBuffer
;
}
static
void
setupComponent
(
StaticModel
*
modelComp
,
std
::
shared_ptr
<
ogl
::
Shader
>&
shader
,
data
::
Mesh
&
mesh
)
{
auto
vao
=
std
::
make_shared
<
ogl
::
VertexArray
>
();
auto
meshBuffer
=
setupArrayBuffer
(
vao
,
mesh
.
vertexList
());
auto
elementBuffer
=
setupIndexBuffer
(
vao
,
mesh
.
indexList
());
// set up the element attributes
modelComp
->
vao
=
vao
;
modelComp
->
vertexData
=
meshBuffer
;
modelComp
->
elements
=
elementBuffer
;
modelComp
->
pipeline
=
shader
;
modelComp
->
elementCount
=
mesh
.
indexCount
();
modelComp
->
drawType
=
ogl
::
Primative
::
TRIANGLE
;
}
void
StaticModelRenderer
::
resolveModels
(
ecs3
::
World
&
world
)
{
// FIXME: this needs something reactive or performance will suck.
auto
renderables
=
world
.
findMatching
<
gfx
::
StaticMesh
>
();
...
...
@@ -40,38 +78,8 @@ namespace fggl::gfx::ogl4 {
}
auto
*
meshComp
=
world
.
get
<
gfx
::
StaticMesh
>
(
renderable
);
// create a vao to store the mesh
auto
vao
=
std
::
make_shared
<
ogl
::
VertexArray
>
();
vao
->
bind
();
// setup the mesh buffer stuff
auto
meshToUpload
=
meshComp
->
mesh
;
auto
meshBuffer
=
std
::
make_shared
<
ogl
::
ArrayBuffer
>
();
spdlog
::
info
(
"mesh data to upload: {}"
,
meshToUpload
.
vertexCount
());
meshBuffer
->
write
(
meshToUpload
.
vertexCount
()
*
sizeof
(
data
::
Vertex
),
meshToUpload
.
vertexList
().
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
// set up the vertex attributes
auto
posAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
posititon
));
auto
normalAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
normal
));
auto
colAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
colour
));
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
0
,
posAttr
);
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
1
,
normalAttr
);
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
3
,
colAttr
);
// set up the element attributes
auto
elementBuffer
=
std
::
make_shared
<
ogl
::
ElementBuffer
>
();
elementBuffer
->
write
(
meshToUpload
.
indexCount
()
*
sizeof
(
uint32_t
),
meshToUpload
.
indexList
().
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
auto
*
modelComp
=
world
.
add
<
StaticModel
>
(
renderable
);
modelComp
->
vao
=
vao
;
modelComp
->
vertexData
=
meshBuffer
;
modelComp
->
elements
=
elementBuffer
;
modelComp
->
pipeline
=
m_phong
;
modelComp
->
elementCount
=
meshToUpload
.
indexCount
();
modelComp
->
drawType
=
ogl
::
Primative
::
TRIANGLE
;
setupComponent
(
modelComp
,
m_phong
,
meshComp
->
mesh
);
// no active model, we need to resolve/load one.
spdlog
::
info
(
"looks like {} needs a static mesh"
,
renderable
);
...
...
@@ -86,40 +94,16 @@ namespace fggl::gfx::ogl4 {
}
auto
*
heightmap
=
world
.
get
<
data
::
HeightMap
>
(
renderable
);
// create a vao to store the mesh
auto
vao
=
std
::
make_shared
<
ogl
::
VertexArray
>
();
vao
->
bind
();
// setup the mesh buffer stuff
data
::
Mesh
meshToUpload
{};
data
::
generateHeightMesh
(
heightmap
,
meshToUpload
);
auto
meshBuffer
=
std
::
make_shared
<
ogl
::
ArrayBuffer
>
();
spdlog
::
info
(
"mesh data to upload: {}"
,
meshToUpload
.
vertexCount
());
meshBuffer
->
write
(
meshToUpload
.
vertexCount
()
*
sizeof
(
data
::
Vertex
),
meshToUpload
.
vertexList
().
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
// set up the vertex attributes
auto
posAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
posititon
));
auto
normalAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
normal
));
auto
colAttr
=
ogl
::
attribute
<
data
::
Vertex
,
math
::
vec3
>
(
offsetof
(
data
::
Vertex
,
colour
));
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
0
,
posAttr
);
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
1
,
normalAttr
);
vao
->
setAttribute
(
*
meshBuffer
.
get
(),
3
,
colAttr
);
// set up the element attributes
auto
elementBuffer
=
std
::
make_shared
<
ogl
::
ElementBuffer
>
();
elementBuffer
->
write
(
meshToUpload
.
indexCount
()
*
sizeof
(
uint32_t
),
meshToUpload
.
indexList
().
data
(),
ogl
::
BufUsage
::
STATIC_DRAW
);
data
::
Mesh
heightMapMesh
{};
data
::
generateHeightMesh
(
heightmap
,
heightMapMesh
);
auto
*
modelComp
=
world
.
add
<
StaticModel
>
(
renderable
);
modelComp
->
vao
=
vao
;
modelComp
->
vertexData
=
meshBuffer
;
modelComp
->
elements
=
elementBuffer
;
modelComp
->
pipeline
=
m_phong
;
modelComp
->
elementCount
=
meshToUpload
.
indexCount
();
setupComponent
(
modelComp
,
m_phong
,
heightMapMesh
);
// we know this is a triangle strip with a restart vertex...
// FIXME the model should be telling us this...
modelComp
->
drawType
=
ogl
::
Primative
::
TRIANGLE_STRIP
;
modelComp
->
restartIndex
=
meshToUpload
.
restartVertex
;
modelComp
->
restartIndex
=
heightMapMesh
.
restartVertex
;
// no active model, we need to resolve/load one.
spdlog
::
info
(
"looks like heightmap, {} needs a static mesh"
,
renderable
);
...
...
@@ -137,8 +121,6 @@ namespace fggl::gfx::ogl4 {
return
;
}
float
deltaTime
=
0.0
f
;
// perform a rendering pass for each camera (will usually only be one...)
for
(
auto
&
cameraEnt
:
cameras
){
//TODO should be cliping this to only visible objects
...
...
@@ -154,8 +136,8 @@ namespace fggl::gfx::ogl4 {
auto
*
const
camTransform
=
world
.
get
<
math
::
Transform
>
(
cameraEnt
);
auto
*
const
camComp
=
world
.
get
<
gfx
::
Camera
>
(
cameraEnt
);
math
::
mat4
projectionMatrix
=
glm
::
perspective
(
camComp
->
fov
,
camComp
->
aspectRatio
,
camComp
->
nearPlane
,
camComp
->
farPlane
);
math
::
mat4
viewMatrix
=
glm
::
lookAt
(
camTransform
->
origin
(),
camComp
->
target
,
camTransform
->
up
()
);
const
math
::
mat4
projectionMatrix
=
glm
::
perspective
(
camComp
->
fov
,
camComp
->
aspectRatio
,
camComp
->
nearPlane
,
camComp
->
farPlane
);
const
math
::
mat4
viewMatrix
=
glm
::
lookAt
(
camTransform
->
origin
(),
camComp
->
target
,
camTransform
->
up
()
);
// TODO lighting needs to not be this...
math
::
vec3
lightPos
{
20.0
f
,
20.0
f
,
15.0
f
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment