Height field fixes (#6)

Fix for height field flat edges
Fix for height field shape cast
Made B3_REST_OFFSET and official constant
This commit is contained in:
Erin Catto 2026-05-14 23:44:39 -07:00 • committed by GitHub
commit 758dc665dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 471 additions and 341 deletions

View file

@ -1223,180 +1223,6 @@ void ResizeScene( Scene* scene, const Camera* camera )
CheckOpenGL();
}
#if 0
UniqueMesh CreateUniqueMesh( const b3Vec3* vertexPositions, const b3Vec3* vertexNormals, int triangleCount,
const int* triangleIndices, const Edge* edges, int edgeCount, const int* materials, Arena arena )
{
UniqueMesh shape = {};
// Vertex buffers
{
// Create vertex buffers
int vertexCount = 3 * triangleCount;
VertexData* vertexData = (VertexData*)arena.Allocate( vertexCount * sizeof( VertexData ) );
int index = 0;
for ( int triangleIndex = 0; triangleIndex < triangleCount; ++triangleIndex )
{
int vertexIndex1 = triangleIndices[3 * triangleIndex + 0];
int vertexIndex2 = triangleIndices[3 * triangleIndex + 1];
int vertexIndex3 = triangleIndices[3 * triangleIndex + 2];
b3Vec3 vertexPosition1 = vertexPositions[vertexIndex1];
b3Vec3 vertexPosition2 = vertexPositions[vertexIndex2];
b3Vec3 vertexPosition3 = vertexPositions[vertexIndex3];
vertexData[index + 0].position = vertexPosition1;
vertexData[index + 1].position = vertexPosition2;
vertexData[index + 2].position = vertexPosition3;
b3Vec3 vertexNormal1, vertexNormal2, vertexNormal3;
if ( vertexNormals )
{
vertexNormal1 = vertexNormals[vertexIndex1];
vertexNormal2 = vertexNormals[vertexIndex2];
vertexNormal3 = vertexNormals[vertexIndex3];
}
else
{
b3Vec3 edge1 = vertexPosition2 - vertexPosition1;
b3Vec3 edge2 = vertexPosition3 - vertexPosition1;
b3Vec3 normal = b3Cross( edge1, edge2 );
normal = b3Normalize( normal );
vertexNormal1 = normal;
vertexNormal2 = normal;
vertexNormal3 = normal;
}
vertexData[index + 0].normal = vertexNormal1;
vertexData[index + 1].normal = vertexNormal2;
vertexData[index + 2].normal = vertexNormal3;
if ( materials != nullptr )
{
vertexData[index + 0].material = materials[triangleIndex];
vertexData[index + 1].material = materials[triangleIndex];
vertexData[index + 2].material = materials[triangleIndex];
}
else
{
vertexData[index + 0].material = -1;
vertexData[index + 1].material = -1;
vertexData[index + 2].material = -1;
}
index += 3;
}
assert( index == vertexCount );
// Fill vertex buffers
shape.triangleCount = triangleCount;
glGenVertexArrays( 1, &shape.vertexArrayId );
glGenBuffers( 1, &shape.vertexBufferId );
// Copy static vertex data
glBindVertexArray( shape.vertexArrayId );
glBindBuffer( GL_ARRAY_BUFFER, shape.vertexBufferId );
glBufferData( GL_ARRAY_BUFFER, vertexCount * sizeof( VertexData ), vertexData, GL_STATIC_DRAW );
// positions
uint32_t positionAttribute = 0;
glEnableVertexAttribArray( positionAttribute );
glVertexAttribPointer( positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof( VertexData ),
(void*)offsetof( VertexData, position ) );
// normals
uint32_t normalAttribute = 1;
glEnableVertexAttribArray( normalAttribute );
glVertexAttribPointer( normalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof( VertexData ),
(void*)offsetof( VertexData, normal ) );
// materials
uint32_t materialAttribute = 2;
glEnableVertexAttribArray( materialAttribute );
glVertexAttribIPointer( materialAttribute, 1, GL_INT, sizeof( VertexData ), (void*)offsetof( VertexData, material ) );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindVertexArray( 0 );
shape.shadowCaster = true;
}
// Edge buffer
{
// Create edge buffer
EdgeData* edgeBuffer = (EdgeData*)arena.Allocate( 2 * edgeCount * sizeof( EdgeData ) );
for ( int edgeIndex = 0; edgeIndex < edgeCount; ++edgeIndex )
{
int vertexIndex1 = edges[edgeIndex].index1;
int vertexIndex2 = edges[edgeIndex].index2;
int flags = edges[edgeIndex].flags;
const b3Vec3* vb = vertexPositions;
b3Vec3 vertexPosition1 = vb[vertexIndex1];
b3Vec3 vertexPosition2 = vb[vertexIndex2];
edgeBuffer[2 * edgeIndex + 0].position = vertexPosition1;
edgeBuffer[2 * edgeIndex + 0].flags = flags;
edgeBuffer[2 * edgeIndex + 1].position = vertexPosition2;
edgeBuffer[2 * edgeIndex + 1].flags = flags;
}
// Fill edge buffer
shape.edgeCount = edgeCount;
glGenVertexArrays( 1, &shape.edgeArrayId );
glGenBuffers( 1, &shape.edgeBufferId );
glBindVertexArray( shape.edgeArrayId );
glBindBuffer( GL_ARRAY_BUFFER, shape.edgeBufferId );
glBufferData( GL_ARRAY_BUFFER, 2 * edgeCount * sizeof( EdgeData ), edgeBuffer, GL_STATIC_DRAW );
uint32_t positionIndex = 0;
glEnableVertexAttribArray( positionIndex );
glVertexAttribPointer( positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof( EdgeData ), (void*)offsetof( EdgeData, position ) );
uint32_t flagsIndex = 1;
glEnableVertexAttribArray( flagsIndex );
glVertexAttribIPointer( flagsIndex, 1, GL_INT, sizeof( EdgeData ), (void*)offsetof( EdgeData, flags ) );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindVertexArray( 0 );
}
CheckOpenGL();
return shape;
}
void DestroyUniqueMesh( UniqueMesh* shape )
{
if ( shape->vertexBufferId != 0 )
{
glDeleteBuffers( 1, &shape->vertexBufferId );
}
if ( shape->vertexArrayId != 0 )
{
glDeleteVertexArrays( 1, &shape->vertexArrayId );
}
if ( shape->edgeBufferId != 0 )
{
glDeleteBuffers( 1, &shape->edgeBufferId );
}
if ( shape->edgeArrayId != 0 )
{
glDeleteVertexArrays( 1, &shape->edgeArrayId );
}
}
#endif
void DrawDebugCapsule( Scene* scene, DebugCapsule* capsule, b3Transform transform, b3HexColor color, float alpha )
{
float radius = capsule->radius;