OpenGL (1.2 - 2.0) Code Samples - Page 12

Grayscale Shader

This sample demonstrates how to use Cg or GLslang to write a fragment shader for converting RGB (Red, Green, and Blue) colors into their gray-scale equivalents.


Rendering Fake Volumetric Lines

This sample demonstrates how to render fake volumetric lines Using Cg. The technique is ideal for rendering effects such as laser fire, tracer rounds, and neon signs. The sample is based on the "Cg Volume Lines" sample, which ships with the current nVIDIA SDK. See the whitepaper titled VolumeLine.pdf in the SDK for more information.



Tired of calling wglGetProcAddress or glXGetProcAddressARB to set up OpenGL extensions? Yeah, me too! So, stop it and start using GLEW: The OpenGL Extension Wrangler Library to wrangle up those pesky extensions.


View Frustum Culling

This sample demonstrates how to perform view frustum culling, which is a very clever way of using bounding volumes to skip the rending of objects which are outside the current view. It's a fairly straight forward concept: extract the clip planes that define the view's frustum from the scene's model-view-projection matrix and use them to see whether the bound spheres that represent our 3D objects are in or out of the frustum. If the object is outside the frustum, we do nothing and skip it. If it proves to be inside or intersecting the frustum, we render it. This is widely used optimization in simulations and games and every 3D graphics programmer should be aware of it.

Relevant Keywords: glMatrixMode, glLoadIdentity, glScalef, glRotatef, glTranslatef, glPushAttrib, glPopAttrib, glLoadIdentity, glRasterPos2f, glutBitmapCharacter, glGetFloatv, glutGet, GL_PROJECTION_MATRIX, GL_MODELVIEW_MATRIX, GLUT_BITMAP_HELVETICA_12, and GLUT_ELAPSED_TIME.

Basic Collision Detection using Bounding Spheres and Triangle Intersection

As you may or may not know, truly accurate collision detection between two 3D objects can only be done by performing an intersection test on every triangle in the first object against every triangle of the second object. Of course, it's too wasteful to just go around blindly performing this costly procedure on every object in the scene against every other object. To save us considerable CPU cycles, we'll calculate and assign a bounding sphere for each of our 3D objects so we can perform a quick check to see whether or not the two objects are even close to one another and require further testing. In short, if the two bounding spheres of our objects overlap, then there exists a possible collision and we need investigate further by checking triangles. If the two spheres don't overlap, a collision is not possible and we can move on.

Please note that the spheres shown below are being rendered for demonstration purposes only. The spheres that are actually used for collision detection are purely mathematical and exist only as a center point in 3D space and a radius.