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:
parent
aedb0e34aa
commit
758dc665dd
8 changed files with 471 additions and 341 deletions
|
|
@ -408,20 +408,29 @@ public:
|
|||
if ( m_context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 45.0f, 30.0f, 12.0f, b3Vec3_zero );
|
||||
EnableGrid( m_scene, true );
|
||||
}
|
||||
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.position = { -10.0f, 0.0f, -10.0f };
|
||||
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
m_heightField = b3CreateGrid( 40, 40, { 0.5f, 1.0f, 0.5f }, false );
|
||||
//m_heightField = b3CreateWave( 40, 40, {1.0f, 2.0f, 1.0f}, 0.02f, 0.04f, false );
|
||||
b3CreateHeightFieldShape( groundId, &shapeDef, m_heightField );
|
||||
|
||||
m_gridMesh = b3CreateGridMesh( 40, 40, 0.5f, 1, true );
|
||||
//b3CreateMeshShape( groundId, &shapeDef, m_gridMesh, b3Vec3_one );
|
||||
}
|
||||
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId groundId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
// m_boxMesh = b3CreateBoxMesh( { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, true );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3BoxHull box = b3MakeBoxHull( 20.0f, 1.0f, 20.0f );
|
||||
b3CreateHullShape( groundId, &shapeDef, &box.base );
|
||||
|
||||
//m_boxMesh = b3CreateBoxMesh( { 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f }, true );
|
||||
m_boxMesh = b3CreatePlatformMesh( { 0.0f, 1.5f, 0.0f }, 1.0f, 2.0f, 5.0f );
|
||||
// m_scale = { -0.6f, 1.0f, 2.0f };
|
||||
m_boxMesh = b3CreatePlatformMesh( { 0.0f, 0.5f, 0.0f }, 1.0f, 2.0f, 5.0f );
|
||||
b3Vec3 scale = b3Vec3_one;
|
||||
b3CreateMeshShape( groundId, &shapeDef, m_boxMesh, scale );
|
||||
}
|
||||
|
|
@ -445,6 +454,8 @@ public:
|
|||
~SBoxMover() override
|
||||
{
|
||||
b3DestroyMesh( m_boxMesh );
|
||||
b3DestroyHeightField( m_heightField );
|
||||
b3DestroyMesh( m_gridMesh );
|
||||
}
|
||||
|
||||
void Render() override
|
||||
|
|
@ -460,6 +471,8 @@ public:
|
|||
}
|
||||
|
||||
b3MeshData* m_boxMesh;
|
||||
b3HeightField* m_heightField;
|
||||
b3MeshData* m_gridMesh;
|
||||
};
|
||||
|
||||
static int sampleBoxMesh = SampleManager::Register( "Issues", "s&box mover", SBoxMover::Create );
|
||||
|
|
|
|||
|
|
@ -792,8 +792,8 @@ public:
|
|||
m_rowCount = 400;
|
||||
m_columnCount = 400;
|
||||
#else
|
||||
m_rowCount = 100;
|
||||
m_columnCount = 100;
|
||||
m_rowCount = 10;
|
||||
m_columnCount = 10;
|
||||
#endif
|
||||
|
||||
m_groundId = {};
|
||||
|
|
@ -807,9 +807,9 @@ public:
|
|||
// m_rayOrigin = { 30.0f, 2.0f, 0.0f };
|
||||
// m_rayTranslation = { -60.0f, -4.0f, 0.0f };
|
||||
|
||||
m_rayOrigin = { 0.0f, 2.0f, 15.0f };
|
||||
m_rayTranslation = { 0.0f, -4.0f, 0.0f };
|
||||
m_radius = 0.571f;
|
||||
m_rayOrigin = { 5.5f, 4.0f, 1.01f };
|
||||
m_rayTranslation = { 0.0f, -8.0f, 0.0f };
|
||||
m_radius = 0.2f;
|
||||
|
||||
m_heightField = nullptr;
|
||||
|
||||
|
|
@ -887,11 +887,11 @@ public:
|
|||
float fontSize = ImGui::GetFontSize();
|
||||
float height = 18.0f * fontSize;
|
||||
ImGui::SetNextWindowPos( { 1.0f * fontSize, m_camera->m_height - height - 3.0f * fontSize }, ImGuiCond_Once );
|
||||
ImGui::SetNextWindowSize( { 20.0f * fontSize, height } );
|
||||
ImGui::SetNextWindowSize( { 40.0f * fontSize, height } );
|
||||
|
||||
ImGui::Begin( "Height Field", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize );
|
||||
|
||||
ImGui::PushItemWidth( 200.0f );
|
||||
ImGui::PushItemWidth( 500.0f );
|
||||
|
||||
if ( ImGui::SliderInt( "columns", &m_columnCount, 1, 500 ) )
|
||||
{
|
||||
|
|
@ -913,10 +913,10 @@ public:
|
|||
CreateScene();
|
||||
}
|
||||
|
||||
ImGui::SliderFloat( "ray x", &m_rayOrigin.x, -2.0f * m_radius - 1.0f * m_scale.x * ( m_columnCount - 1 ),
|
||||
2.0f * m_radius + 1.0f * m_scale.x * ( m_columnCount - 1 ) );
|
||||
ImGui::SliderFloat( "ray z", &m_rayOrigin.z, -2.0f * m_radius - 1.0f * m_scale.z * ( m_rowCount - 1 ),
|
||||
2.0f * m_radius + 1.0f * m_scale.z * ( m_rowCount - 1 ) );
|
||||
ImGui::SliderFloat( "ray x", &m_rayOrigin.x, -2.0f * m_radius - 0.5f * m_scale.x * ( m_columnCount - 1 ),
|
||||
2.0f * m_radius + 0.5f * m_scale.x * ( m_columnCount - 1 ) );
|
||||
ImGui::SliderFloat( "ray z", &m_rayOrigin.z, -2.0f * m_radius - 0.5f * m_scale.z * ( m_rowCount - 1 ),
|
||||
2.0f * m_radius + 0.5f * m_scale.z * ( m_rowCount - 1 ) );
|
||||
ImGui::SliderFloat( "delta x", &m_rayTranslation.x, -2.0f * m_scale.x * ( m_columnCount - 1 ),
|
||||
2.0f * m_scale.x * ( m_columnCount - 1 ) );
|
||||
ImGui::SliderFloat( "delta z", &m_rayTranslation.z, -2.0f * m_scale.z * ( m_rowCount - 1 ),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue