Skip to content

Commit

Permalink
#5584: Remove GL display list code from MD5Surface.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 26, 2022
1 parent b623534 commit 84d0f54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 156 deletions.
144 changes: 20 additions & 124 deletions radiantcore/model/md5/MD5Surface.cpp
@@ -1,7 +1,6 @@
#include "MD5Surface.h"

#include "ivolumetest.h"
#include "GLProgramAttributes.h"
#include "string/convert.h"
#include "MD5Model.h"
#include "math/Ray.h"
Expand All @@ -17,144 +16,43 @@ inline VertexPointer vertexpointer_arbitrarymeshvertex(const ArbitraryMeshVertex
// Constructor
MD5Surface::MD5Surface() :
_originalShaderName(""),
_mesh(new MD5Mesh),
_normalList(0),
_lightingList(0)
_mesh(new MD5Mesh)
{}

MD5Surface::MD5Surface(const MD5Surface& other) :
_aabb_local(other._aabb_local),
_originalShaderName(other._originalShaderName),
_mesh(other._mesh),
_normalList(0),
_lightingList(0)
_mesh(other._mesh)
{}

// Destructor
MD5Surface::~MD5Surface()
{
releaseDisplayLists();
}

// Update geometry
void MD5Surface::updateGeometry()
{
_aabb_local = AABB();

for (Vertices::const_iterator i = _vertices.begin(); i != _vertices.end(); ++i)
for (const auto& vertex : _vertices)
{
_aabb_local.includePoint(i->vertex);
_aabb_local.includePoint(vertex.vertex);
}

for (Indices::iterator i = _indices.begin();
i != _indices.end();
i += 3)
{
ArbitraryMeshVertex& a = _vertices[*(i + 0)];
ArbitraryMeshVertex& b = _vertices[*(i + 1)];
ArbitraryMeshVertex& c = _vertices[*(i + 2)];
auto& a = _vertices[*(i + 0)];
auto& b = _vertices[*(i + 1)];
auto& c = _vertices[*(i + 2)];

ArbitraryMeshTriangle_sumTangents(a, b, c);
}

for (Vertices::iterator i = _vertices.begin();
i != _vertices.end();
++i)
for (auto& vertex : _vertices)
{
i->tangent.normalise();
i->bitangent.normalise();
vertex.tangent.normalise();
vertex.bitangent.normalise();
}

// Build the display lists
createDisplayLists();
}

// Back-end render
void MD5Surface::render(const RenderInfo& info) const
{
if (info.checkFlag(RENDER_BUMP))
{
glCallList(_lightingList);
}
else
{
glCallList(_normalList);
}
}

// Construct the display lists
void MD5Surface::createDisplayLists()
{
// Release old display lists first
releaseDisplayLists();

// Create the list for lighting mode
_lightingList = glGenLists(1);
assert(_lightingList != 0);
glNewList(_lightingList, 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) {
// Submit the vertex attributes and coordinate
glVertexAttrib2dvARB(ATTR_TEXCOORD, v.texcoord);
glVertexAttrib3dvARB(ATTR_TANGENT, v.tangent);
glVertexAttrib3dvARB(ATTR_BITANGENT, v.bitangent);
glVertexAttrib3dvARB(ATTR_NORMAL, v.normal);
}
glVertex3dv(v.vertex);
}
glEnd();

glEndList();

// Generate the list for flat-shaded (unlit) mode
_normalList = glGenLists(1);
assert(_normalList != 0);
glNewList(_normalList, 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();
}

void MD5Surface::releaseDisplayLists()
{
// Release GL display lists if applicable
if (_normalList != 0)
{
glDeleteLists(_normalList, 1);
_normalList = 0;
}

if (_lightingList != 0)
{
glDeleteLists(_lightingList, 1);
_lightingList = 0;
}
}

// Selection test
void MD5Surface::testSelect(Selector& selector,
SelectionTest& test,
const Matrix4& localToWorld)
Expand Down Expand Up @@ -183,9 +81,9 @@ bool MD5Surface::getIntersection(const Ray& ray, Vector3& intersection, const Ma
i += 3)
{
// Get the vertices for this triangle
const ArbitraryMeshVertex& p1 = _vertices[*(i)];
const ArbitraryMeshVertex& p2 = _vertices[*(i+1)];
const ArbitraryMeshVertex& p3 = _vertices[*(i+2)];
const auto& p1 = _vertices[*(i)];
const auto& p2 = _vertices[*(i+1)];
const auto& p3 = _vertices[*(i+2)];

if (ray.intersectTriangle(localToWorld.transformPoint(p1.vertex),
localToWorld.transformPoint(p2.vertex), localToWorld.transformPoint(p3.vertex), triIntersection))
Expand Down Expand Up @@ -365,9 +263,9 @@ void MD5Surface::buildVertexNormals()
{
for (Indices::iterator j = _indices.begin(); j != _indices.end(); j += 3)
{
ArbitraryMeshVertex& a = _vertices[*(j + 0)];
ArbitraryMeshVertex& b = _vertices[*(j + 1)];
ArbitraryMeshVertex& c = _vertices[*(j + 2)];
auto& a = _vertices[*(j + 0)];
auto& b = _vertices[*(j + 1)];
auto& c = _vertices[*(j + 2)];

Vector3 weightedNormal((c.vertex - a.vertex).cross(b.vertex - a.vertex));

Expand All @@ -377,9 +275,9 @@ void MD5Surface::buildVertexNormals()
}

// Normalise all normal vectors
for (Vertices::iterator j = _vertices.begin(); j != _vertices.end(); ++j)
for (auto& vertex : _vertices)
{
j->normal = Normal3f(j->normal.getNormalised());
vertex.normal.normalise();
}
}

Expand All @@ -388,10 +286,8 @@ void MD5Surface::buildIndexArray()
_indices.clear();

// Build the indices based on the triangle information
for (MD5Tris::const_iterator j = _mesh->triangles.begin(); j != _mesh->triangles.end(); ++j)
for (const auto& tri : _mesh->triangles)
{
const MD5Tri& tri = (*j);

_indices.push_back(static_cast<RenderIndex>(tri.a));
_indices.push_back(static_cast<RenderIndex>(tri.b));
_indices.push_back(static_cast<RenderIndex>(tri.c));
Expand Down Expand Up @@ -494,4 +390,4 @@ void MD5Surface::parseFromTokens(parser::DefTokeniser& tok)
tok.assertNextToken("}");
}

} // namespace md5
} // namespace
38 changes: 6 additions & 32 deletions radiantcore/model/md5/MD5Surface.h
Expand Up @@ -2,7 +2,6 @@

#include "irender.h"
#include "render.h"
#include "irenderable.h"
#include "math/AABB.h"
#include "math/Frustum.h"
#include "iselectiontest.h"
Expand All @@ -20,8 +19,7 @@ namespace md5
class MD5Skeleton;

class MD5Surface final :
public model::IIndexedModelSurface,
public OpenGLRenderable
public model::IIndexedModelSurface
{
public:
typedef std::vector<ArbitraryMeshVertex> Vertices;
Expand All @@ -44,38 +42,13 @@ class MD5Surface final :
Vertices _vertices;
Indices _indices;

// The GL display lists for this surface's geometry
GLuint _normalList;
GLuint _lightingList;

private:

// Create the display lists
void createDisplayLists();

// Frees any display list in use
void releaseDisplayLists();

// Re-calculate the normal vectors
void buildVertexNormals();

public:

/**
* Constructor.
*/
MD5Surface();

/**
* Copy constructor, re-uses the MD5Mesh of <other>.
*/
// Copy constructor, re-uses the MD5Mesh of <other>.
MD5Surface(const MD5Surface& other);

/**
* Destructor.
*/
~MD5Surface();

// Set/get the shader name
void setDefaultMaterial(const std::string& name);

Expand All @@ -91,9 +64,6 @@ class MD5Surface final :
// Updates this mesh to the state of the given skeleton
void updateToSkeleton(const MD5Skeleton& skeleton);

// Back-end render function
void render(const RenderInfo& info) const;

const AABB& localAABB() const;

// Test for selection
Expand Down Expand Up @@ -123,6 +93,10 @@ class MD5Surface final :

// Rebuild the render index array - usually needs to be called only once
void buildIndexArray();

private:
// Re-calculate the normal vectors
void buildVertexNormals();
};
typedef std::shared_ptr<MD5Surface> MD5SurfacePtr;

Expand Down

0 comments on commit 84d0f54

Please sign in to comment.