Skip to content

Commit

Permalink
#5584: Remove GL display list handling code from StaticModelSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 16, 2022
1 parent a11dd8e commit b2029ce
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 152 deletions.
6 changes: 6 additions & 0 deletions radiantcore/model/StaticModel.cpp
Expand Up @@ -84,26 +84,30 @@ void StaticModel::renderSolid(IRenderableCollector& rend,
const IRenderEntity& entity,
const LitObject& litObject) const
{
#if 0
// Submit renderables from each surface
foreachVisibleSurface([&](const Surface& s)
{
// Submit the ordinary shader for material-based rendering
rend.addRenderable(*s.shader, *s.surface, localToWorld,
&litObject, &entity);
});
#endif
}

void StaticModel::renderWireframe(IRenderableCollector& rend,
const Matrix4& localToWorld,
const IRenderEntity& entity) const
{
#if 0
// Submit renderables from each surface
foreachVisibleSurface([&](const Surface& s)
{
// Submit the wireframe shader for non-shaded renderers
rend.addRenderable(*entity.getWireShader(), *s.surface, localToWorld,
nullptr, &entity);
});
#endif
}

void StaticModel::setRenderSystem(const RenderSystemPtr& renderSystem)
Expand All @@ -130,6 +134,7 @@ void StaticModel::render(const RenderInfo& info) const
}
#endif

#if 0
// Iterate over the surfaces, calling the render function on each one
for (SurfaceList::const_iterator i = _surfVec.begin();
i != _surfVec.end();
Expand All @@ -151,6 +156,7 @@ void StaticModel::render(const RenderInfo& info) const
i->surface->render(info.getFlags());
#endif
}
#endif
}

std::string StaticModel::getFilename() const
Expand Down
141 changes: 12 additions & 129 deletions radiantcore/model/StaticModelSurface.cpp
Expand Up @@ -15,10 +15,7 @@ namespace model

StaticModelSurface::StaticModelSurface(std::vector<ArbitraryMeshVertex>&& vertices, std::vector<unsigned int>&& indices) :
_vertices(vertices),
_indices(indices),
_dlRegular(0),
_dlProgramVcol(0),
_dlProgramNoVCol(0)
_indices(indices)
{
// Expand the local AABB to include all vertices
for (const auto& vertex : _vertices)
Expand All @@ -27,30 +24,15 @@ StaticModelSurface::StaticModelSurface(std::vector<ArbitraryMeshVertex>&& vertic
}

calculateTangents();
createDisplayLists();
}

StaticModelSurface::StaticModelSurface(const StaticModelSurface& other) :
_defaultMaterial(other._defaultMaterial),
_vertices(other._vertices),
_indices(other._indices),
_localAABB(other._localAABB),
_dlRegular(0),
_dlProgramVcol(0),
_dlProgramNoVCol(0)
{
createDisplayLists();
}

// Destructor. Release the GL display lists.
StaticModelSurface::~StaticModelSurface()
{
glDeleteLists(_dlRegular, 1);
glDeleteLists(_dlProgramNoVCol, 1);
glDeleteLists(_dlProgramVcol, 1);
}
_defaultMaterial(other._defaultMaterial),
_vertices(other._vertices),
_indices(other._indices),
_localAABB(other._localAABB)
{}

// Tangent calculation
void StaticModelSurface::calculateTangents()
{
// Calculate the tangents and bitangents using the indices into the vertex
Expand All @@ -59,115 +41,22 @@ void StaticModelSurface::calculateTangents()
i != _indices.end();
i += 3)
{
ArbitraryMeshVertex& a = _vertices[*i];
ArbitraryMeshVertex& b = _vertices[*(i + 1)];
ArbitraryMeshVertex& c = _vertices[*(i + 2)];
auto& a = _vertices[*i];
auto& b = _vertices[*(i + 1)];
auto& c = _vertices[*(i + 2)];

// Call the tangent calculation function
ArbitraryMeshTriangle_sumTangents(a, b, c);
}

// Normalise all of the tangent and bitangent vectors
for (VertexVector::iterator j = _vertices.begin();
j != _vertices.end();
++j)
for (auto& vertex : _vertices)
{
j->tangent.normalise();
j->bitangent.normalise();
}
}

// Back-end render function
void StaticModelSurface::render(const RenderInfo& info) const
{
// Invoke appropriate display list
if (info.checkFlag(RENDER_PROGRAM))
{
if (info.checkFlag(RENDER_VERTEX_COLOUR))
{
glCallList(_dlProgramVcol);
}
else
{
glCallList(_dlProgramNoVCol);
}
}
else
{
glCallList(_dlRegular);
vertex.tangent.normalise();
vertex.bitangent.normalise();
}
}

// Construct a list for GLProgram mode, either with or without vertex colour
GLuint StaticModelSurface::compileProgramList(bool includeColour)
{
GLuint list = glGenLists(1);
assert(list != 0); // check if we run out of display lists
glNewList(list, GL_COMPILE);

glBegin(GL_TRIANGLES);
for (Indices::const_iterator i = _indices.begin();
i != _indices.end();
++i)
{
// Get the vertex for this index
ArbitraryMeshVertex& v = _vertices[*i];

// Submit the vertex attributes and coordinate
if (GLEW_ARB_vertex_program)
{
glVertexAttrib2dvARB(ATTR_TEXCOORD, v.texcoord);
glVertexAttrib3dvARB(ATTR_TANGENT, v.tangent);
glVertexAttrib3dvARB(ATTR_BITANGENT, v.bitangent);
glVertexAttrib3dvARB(ATTR_NORMAL, v.normal);
}

// Optional vertex colour
if (includeColour)
{
glColor4dv(v.colour);
}

// Submit the vertex itself
glVertex3dv(v.vertex);
}
glEnd();

glEndList();

return list;
}

// Construct the two display lists
void StaticModelSurface::createDisplayLists()
{
// Generate the lists for lighting mode
_dlProgramNoVCol = compileProgramList(false);
_dlProgramVcol = compileProgramList(true);

// Generate the list for flat-shaded (unlit) mode
_dlRegular = glGenLists(1);
assert(_dlRegular != 0); // check if we run out of display lists
glNewList(_dlRegular, GL_COMPILE);

glBegin(GL_TRIANGLES);
for (Indices::const_iterator i = _indices.begin();
i != _indices.end();
++i)
{
// Get the vertex for this index
ArbitraryMeshVertex& v = _vertices[*i];

// Submit attributes
glNormal3dv(v.normal);
glTexCoord2dv(v.texcoord);
glVertex3dv(v.vertex);
}
glEnd();

glEndList();
}

// Perform selection test for this surface
void StaticModelSurface::testSelect(Selector& selector, SelectionTest& test,
const Matrix4& localToWorld, bool twoSided) const
Expand Down Expand Up @@ -326,12 +215,6 @@ void StaticModelSurface::applyScale(const Vector3& scale, const StaticModelSurfa
}

calculateTangents();

glDeleteLists(_dlRegular, 1);
glDeleteLists(_dlProgramNoVCol, 1);
glDeleteLists(_dlProgramVcol, 1);

createDisplayLists();
}

} // namespace model
27 changes: 4 additions & 23 deletions radiantcore/model/StaticModelSurface.h
@@ -1,11 +1,9 @@
#pragma once

#include "GLProgramAttributes.h"
#include "render.h"
#include "math/AABB.h"

#include "ishaders.h"
#include "imodelsurface.h"
#include "ishaders.h"

#include "math/AABB.h"

/* FORWARD DECLS */
class ModelSkin;
Expand All @@ -27,8 +25,7 @@ namespace model
* create a renderable static mesh.
*/
class StaticModelSurface final :
public IIndexedModelSurface,
public OpenGLRenderable
public IIndexedModelSurface
{
private:
// Name of the material this surface is using by default (without any skins)
Expand All @@ -50,33 +47,17 @@ class StaticModelSurface final :
// The AABB containing this surface, in local object space.
AABB _localAABB;

// The GL display lists for this surface's geometry
GLuint _dlRegular;
GLuint _dlProgramVcol;
GLuint _dlProgramNoVCol;

private:
// Calculate tangent and bitangent vectors for all vertices.
void calculateTangents();

// Create the display lists
GLuint compileProgramList(bool includeColour);
void createDisplayLists();

public:
// Move-construct this static model surface from the given vertex- and index array
StaticModelSurface(std::vector<ArbitraryMeshVertex>&& vertices, std::vector<unsigned int>&& indices);

// Copy-constructor. All vertices and indices will be copied from 'other'.
StaticModelSurface(const StaticModelSurface& other);

~StaticModelSurface();

/**
* Render function from OpenGLRenderable
*/
void render(const RenderInfo& info) const;

/** Get the containing AABB for this surface.
*/
const AABB& getAABB() const {
Expand Down
1 change: 1 addition & 0 deletions radiantcore/model/import/AseModel.cpp
Expand Up @@ -7,6 +7,7 @@
#include "string/case_conv.h"
#include "string/trim.h"
#include "string/convert.h"
#include "render.h"

#include "render/VertexHashing.h"

Expand Down

0 comments on commit b2029ce

Please sign in to comment.