OpenGL (1.2 - 2.0) Code Samples - Page 13

Solid Node BSP Tree Compiler
 

One method of speeding up a game engine is to use a BSP-Tree (Binary-Space Partitioning Tree) to subdivide and sort your game's world geometry.

This is done by dividing up, sorting, and storing polys in a BSP Tree in a such as way as to denote their relationship to one another (i.e. being in front of or in back of another poly). Once the BSP tree is compiled or built, the engine can quickly decide whether or not it should render certain polys based on the view’s current location within the world.

For example, the BSP compiler in this sample demonstrates how early game programmers, who didn't have hardware accelerated z-buffers, used a simple solid-node style BSP tree to sort their scene polys into a back-to-front order for proper rendering. The sample also demonstrates how a BSP Tree can be used for collision detection by identifying whether or not a unobstructed line-of sight can be established between two 3D points.

Unfortunately, the code doesn't go into a lot of BSP Tree theory or teach the basics, so if you're new to the concept of a BSP Tree, check out this cool little Java Applet for a real-time demonstration of how they work.

This sample is based on one of the BSP demos that came with the "Advanced 3D BSP, PVS and CSG Techniques" course offered at GameInstitute.com. If you're truly interested in learning about BSP trees and how they're used in games, this course is the best by far and I highly recommend it.

Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

 

Simple Introduction to the NovodeX Physics SDK
 

This sample demonstrates how to use the NovodeX Physics SDK by simply creating and dropping boxes on to a plane that represents the ground. Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

Relevant Keywords: NxPhysicsSDK, NxScene, NxVec3, ErrorStream, NxCreatePhysicsSDK, setMaterialAtIndex, createScene, shapes.pushBack, createActor, NX_BROADPHASE_COHERENT, release, releaseScene, NxBodyDesc, NxBoxShapeDesc, NxActorDesc, createActor, userData, getNbActors, getActors, getGlobalPose, getColumnMajor44, flushStream, and fetchResults.

 

How to use the Tokamak SDK to Create a Rope or Chain using Ball-Joints
 

This sample demonstrates how to use the Tokamak Physics SDK to create a chain or rope like object by using small collision cubes joined by ball-joints. For demonstration purposes, the chain is dropped onto a platform, which doesn’t quite catch all of it. This allows the chain to hang off and swing about. For fun, you can use the F1 key to lift up on the chain's last segment by applying some velocity to it. Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

Relevant Keywords: neSimulator, neRigidBody, neAnimatedBody, neSimulatorSizeInfo, neBoxInertiaTensor,  neJoint, neGeometry, CreateSimulator, CreateRigidBody, AddGeometry, SetBoxSize, CollideConnected, UpdateBoundingInfo, SetInertiaTensor, SetMass, SetJointFrameWorld, SetType, SetEpsilon, SetIteration, Enable, and GetTransform.

 

How to use the Tokamak SDK to Create a Stack of Blocks

 

This sample demonstrates how to use the Tokamak Physics SDK to create a stack of blocks which can be knocked over. For demonstration purposes, you can use the F1 key to lift up and drop the bottom block, which will obviously disturb the stack. You can also use the F2 key to tilt the blue platform and dump the blocks over the side. Check out the DirectX9 source code page for the Direct3D equivalent of this sample.

Relevant Keywords: neSimulator, neRigidBody, neAnimatedBody, neSimulatorSizeInfo, neBoxInertiaTensor,  neJoint, neGeometry, CreateSimulator, CreateRigidBody, AddGeometry, SetBoxSize, UpdateBoundingInfo, SetInertiaTensor, SetMass, Enable, SetRotation and GetTransform.

 

Multi-Texture Blending With Cg
 

This sample demonstrates how to use a Cg fragment shader to blend three textures together by passing the desired contribution of each texture into the shader via the vertex's color.


Techniques like this are becoming very popular in terrain rendering engines which need to blend dramatically different textures such as rock and grass together with out creating a noticeable edge. For example, with three textures consisting of stone, grass, and sand you can render a mountain that blends in patches of grass and sand at its base.

Relevant Keywords: glActiveTextureARB, glMultiTexCoord2fARB, glClientActiveTextureARB, cgGLIsProfileSupported, cgCreateContext, cgCreateProgramFromFile, cgGLLoadProgram, cgGetNamedParameter, cgGLSetStateMatrixParameter, cgGLSetParameter4fv, cgGLBindProgram, cgGLEnableProfile, cgGLDisableProfile, cgDestroyProgram, cgDestroyContext, cgGLSetTextureParameter, cgGLDisableTextureParameter, CGprofile, CGcontext, CGprogram, CGparameter, CG_PROFILE_ARBFP1, CG_PROFILE_FP30, CG_PROFILE_FP20, CG_SOURCE, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY, GL_TEXTURE0_ARB, GL_TEXTURE1_ARB, GL_TEXTURE2_ARB, POSITION, COLOR0, main, float4, float3, uniform, float4x4, mul, in, and out.