Skip to content

Commit

Permalink
Merge pull request #77 from mlavik1/memory-fix
Browse files Browse the repository at this point in the history
Fixed some issues caused by dereferencing pointers to elements of an …
  • Loading branch information
jklimke authored May 11, 2023
2 parents 9b72e39 + e32cc52 commit a22e75d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
13 changes: 12 additions & 1 deletion sources/include/citygml/tesselator.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class LIBCITYGML_EXPORT Tesselator: public TesselatorBase
void compute() override;

private:
void processContours();

typedef void (APIENTRY *GLU_TESS_CALLBACK)();
static void CALLBACK beginCallback( GLenum, void* );
static void CALLBACK vertexDataCallback( GLvoid*, void* );
Expand All @@ -65,9 +67,18 @@ class LIBCITYGML_EXPORT Tesselator: public TesselatorBase
static void CALLBACK errorCallback(GLenum, void*);

private:
//
struct ContourRef {
ContourRef(unsigned int index, unsigned int length) : index(index), length(length) {}
unsigned int index;
unsigned int length;
};

GLUtesselator *_tobj;
GLenum _curMode;
GLenum _curMode;
GLenum _windingRule;
std::vector<TVec3d> _originalVertices;
std::vector<ContourRef> _contourQueue;
};

#endif // __TESSELATOR_H__
36 changes: 26 additions & 10 deletions sources/src/citygml/tesselator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Tesselator::~Tesselator()

void Tesselator::compute()
{
processContours();
gluTessEndPolygon( _tobj );
}

Expand All @@ -69,15 +70,30 @@ void Tesselator::addContour(const std::vector<TVec3d>& pts, std::vector<std::vec
unsigned int pos = _vertices.size();
TesselatorBase::addContour(pts, textureCoordinatesLists);

gluTessBeginContour( _tobj );

unsigned int len = pts.size();
for ( unsigned int i = 0; i < len; i++ )
// Add contour to queue, and process later.
ContourRef contour(pos, len);
_contourQueue.push_back(contour);
}

void Tesselator::processContours()
{
_originalVertices = _vertices;

for (const ContourRef& contour : _contourQueue)
{
gluTessVertex( _tobj, &(_vertices[pos + i][0]), &_indices[pos + i] );
}
gluTessBeginContour( _tobj );

gluTessEndContour( _tobj );
for ( unsigned int i = 0; i < contour.length; i++ )
{
void* data = reinterpret_cast<void*>(static_cast<uintptr_t>(_indices[contour.index + i]));
gluTessVertex( _tobj, &(_originalVertices[contour.index + i][0]), data );
}

gluTessEndContour( _tobj );
}
_contourQueue.clear();
_originalVertices.clear();
}

void CALLBACK Tesselator::beginCallback( GLenum which, void* userData )
Expand All @@ -89,7 +105,7 @@ void CALLBACK Tesselator::beginCallback( GLenum which, void* userData )
void CALLBACK Tesselator::vertexDataCallback( GLvoid *data, void* userData )
{
Tesselator *tess = static_cast<Tesselator*>(userData);
unsigned int index = *reinterpret_cast<unsigned int*>(data);
unsigned int index = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(data));

assert(index < tess->_vertices.size());

Expand All @@ -113,8 +129,8 @@ void CALLBACK Tesselator::combineCallback( GLdouble coords[3], void* vertex_data
TVec2f newTexCoord(0,0);

for (int i = 0; i < 4; i++) {
if (vertex_data[i] != nullptr) {
unsigned int vertexIndex = *reinterpret_cast<unsigned int*>(vertex_data[i]);
if (weight[i] > 0.0f) {
unsigned int vertexIndex = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(vertex_data[i]));
newTexCoord = newTexCoord + weight[i] * texcords.at(vertexIndex);
}
}
Expand All @@ -126,7 +142,7 @@ void CALLBACK Tesselator::combineCallback( GLdouble coords[3], void* vertex_data
}
}

*outData = &tess->_indices.back();
*outData = reinterpret_cast<void*>(static_cast<uintptr_t>(tess->_indices.back()));
}

void CALLBACK Tesselator::endCallback( void* userData )
Expand Down

0 comments on commit a22e75d

Please sign in to comment.