Generating terrain entirely on the GPU is cool, however as pretty as it looks, in the end you need to interact with it. Bullet Physics engine provides very neat ways to define your own collider shapes. So I decided to port GPU code to C++ and use the output for bullet shape.
Porting noise was easy since GLM library has been designed to be as close as to shader code. You can checkout it on Github
Now the fun part: Full code
virtual void processAllTriangles(btTriangleCallback* callback,
const btVector3& aabbMin,
const btVector3& aabbMax) const
{
reVec3 expand(1.5,1.5,1.5);
reVec3 min = fromBullet(aabbMin)-expand, max = fromBullet(aabbMax)+expand;
btVector3 c = toBullet(calculateVertex(reVec3((min.x+max.x)*0.5, 0, (min.z+max.z)*0.5)));
btVector3 bl = toBullet(calculateVertex(reVec3(min.x, 0, min.z)));
btVector3 br = toBullet(calculateVertex(reVec3(min.x, 0, max.z)));
btVector3 tl = toBullet(calculateVertex(reVec3(max.x, 0, min.z)));
btVector3 tr = toBullet(calculateVertex(reVec3(max.x, 0, max.z)));
btVector3 vertices[3];
vertices[0] = bl;
vertices[1] = br;
vertices[2] = c;
callback->processTriangle(vertices, 1, 0);
vertices[0] = tr;
vertices[1] = tl;
vertices[2] = c;
callback->processTriangle(vertices, 2, 0);
vertices[0] = br;
vertices[1] = tr;
vertices[2] = c;
callback->processTriangle(vertices, 3, 0);
vertices[0] = tl;
vertices[1] = bl;
vertices[2] = c;
callback->processTriangle(vertices, 4, 0);
}
Each time bullet queries out collision shape, the noise functions are used to get the vertices necessary and return triangles. One of the other cool things about this is, bullet actually queries with AABB(axis aligned bounding box) of interest, what we return depends on location of that AABB, we generate the surface inside of it, so the virtual resolution of our terrain is very very high, smoother simulation is a bonus.
Conclusion
Putting together a procedural terrain collider was easier than I thought, and bullet played with it pretty nicely. This still seems more and more like a very viable option for mobile games.
Comments
comments powered by Disqus