24 Miscellaneous

24.010
How can I render a wireframe scene with hidden lines removed?

The preferred method is to render your geometry in two
passes: first render it in fill mode with color set to the
background color, then render it again in line mode. Use
polygon offset so the lines over the polygons render
correctly. The polygon offset
section might be helpful to you.

Often you need to preserve a nonuniform background, such
as a gradient fill or an image. In this case, execute the
fill pass with glColorMask() set to all GL_FALSE, then
perform the line pass as usual. Again, use polygon offset to
minimize Z fighting.

24.030
My init code calls glGetString() to find information about the
OpenGL implementation, but why doesn't it return a string?

The most likely cause of this problem is that a context
hasn't been made current. An OpenGL rendering context must
exist and be made current to a window for any OpenGL calls to
function and return meaningful values.

24.039
Where can I find 3D model files?

As this has little to do with OpenGL, what follows is by
no means an exhaustive list:

You can make your own 3D models using any package you
desire, and then loading the geometry file. ModelMagic3D is
shareware and comes with source code. GLScene
is also available.

24.040
How can I load geometry files, such as 3DS, OBJ, DEM, etc. and
render them with OpenGL?

OpenGL, being a 3D graphics API, has no built-in support
for reading application-specific file formats. If you're
writing an application that needs to read a specific file
type, you'll need to add code to support a particular file
type.

Many OpenGL users already have written code to do this,
and in some cases, the code is available on the Web. A few
are listed here. If you can't find what you are looking for,
you might try doing a Web search.

24.050
How can I save my OpenGL rendering as an image file, such as GIF,
TIF, JPG, BMP, etc.? How can I read these image files and use
them as texture maps?

To save a rendering, the easiest method is to use any of a
number of image utilities that let you capture the screen or
window, and save it is a file.

To accomplish this programmatically, you read your image
with glReadPixels(), and use the image data as input to a
routine that creates image files.

Similarly, to read an image file and use it as a texture
map, you need a routine that will read the image file. Then
send the texture data to OpenGL with glTexImage2D().

OpenGL will not read or write image files for you. To read
or write image files, you can either write your own code,
include code that someone else has written, or call into an
image file library. The following links contain information
on all three strategies.

OpenGL applications typically use the depth test to
perform hidden surface removal. However, depending on your
application and the nature of your geometry database, a BSP
tree can enhance performance when used in conjunction with
the depth test or when used in place of the depth test.

BSP trees also may be used to cull non-visible geometry
from the database.

Yes. Nothing in OpenGL prevents you from using an octree.
An octree is especially helpful when used in conjunction with
occlusion culling extensions (such as HP's GL_HP_occlusion_test).

24.080
Can I do radiosity with OpenGL?

OpenGL doesn't contain any direct support for radiosity,
it doesn't prevent you from displaying a database containing
precomputed radiosity values.

An application needs to perform its own radiosity
iterations over the database to be displayed. After
sufficient color values are computed at each vertex, the
application renders the database as normal OpenGL primitives,
specifying the computed color at each vertex. glShadeModel()
should be set to GL_SMOOTH and lighting should be disabled.

24.090
Can I raytrace with OpenGL?

OpenGL contains no direct support for raytracing.

You might want to use raytracing to produce realistic
shadows and reflections. However, you can simulate in many
ways these effects in OpenGL without raytracing. See the section on shadows or the section on texture mapping for some
algorithms.

You can use OpenGL as part of the ray intersection test.
For example, a scene can be rendered with a unique color
assigned to each primitive in the scene. This color can be
read back to determine the primitive intersected by a ray at
a given pixel. If the exact geometry is used in this
algorithm, some aliasing may result. To reduce these aliasing
artifacts, you can render bounding volumes instead.

Also, by changing the viewpoint and view direction, you
can use this algorithm for intersection testing of secondary
rays.

A ray tracing application might also use OpenGL for
displaying the final image. In this case, the application is
responsible for computing the color value of each pixel. The
pixels then can be rendered as individual GL_POINTS
primitives or stored in an array and displayed via a call to
glDrawPixels().

The GLUT 3.7 distribution contains an example program
called csg.c that may be informative.

24.110
How can I perform collision detection with OpenGL?

OpenGL contains no direct support for collision detection.
Your application needs to perform this operation itself.

OpenGL can be used to evaluate potential collisions the
same way it can evaluate
ray intersections (i.e., the scene is rendered from the
object's point of view, looking in the direction of motion,
with an orthographic projection and a field-of-view
restricted to the object's bounding rectangle.) Visible
primitives are potential collision candidates. You can
examine their Z values to determine range.

24.120
I understand OpenGL might cache commands in an internal buffer.
Can I perform an abort operation, so these buffers are simply
emptied instead of executed?

No. After you issue OpenGL commands, inevitably they'll be
executed.

24.130
What's the difference between glFlush() and glFinish() and why
would I want to use these routines?

The OpenGL spec allows an implementation to store commands
and data in buffers, which are awaiting execution. glFlush()
causes these buffers to be emptied and executed. Thus, any
pending rendering commands will be executed, but glFlush()
may return before their execution is complete. glFinish()
instructs an implementation to not return until the effects
of all commands are executed and updated.

A typical use of glFlush() might be to ensure rendering
commands are exected when rendering to the front buffer.

glFinish() might be particularly useful if an app draws
using both OpenGL and the window system's drawing commands.
Such an application would first draw OpenGL, then call
glFinish() before proceeding to issue the window system's
drawing commands.

24.140
How can I print with OpenGL?

OpenGL currently provides no services for printing. The
OpenGL ARB has discussed a GLS stream protocol, which would
enable a more common interface for printing, but for now,
printing is only accomplished by system-specific means.

On a Microsoft Windows platform, ALT-PrintScreen copies
the active window to the clipboard. (To copy the entire
screen, make the desktop active by clicking on it, then use
ALT-PrintScreen.) Then you can paste the contents of the
clipboard to any 2D image processing software, such as
Microsoft Paint, and print from there.

You can capture an OpenGL rendering with any common 2D
image processing packages that provide a screen or window
capture utility, and print from there.

Also, can print programatically using any method available
on your platform. For example in Microsoft Windows, you might
use glReadPixels() to read your window, write the pixel data
to a DIB, and submit the DIB for printing.

This
tutorial contains sample source for a program that prints
OpenGL images under Microsoft Windows.

This
article contains a description of how to output to a
vector-based image file such as Postscript or WMF.

24.150
Can I capture or log the OpenGL calls an application makes?

IBM has a product called ZAPdb that does this. It ships
with many UNIX implementations, including IBM and HP. It was
available on Windows NT in the past, but its current status
is unknown. A non-IBM web page appears to have ZAPdb
available for download.

3dpipeline.com
offers a product called GLAlanyze Pro, which captures OpenGL
call traces, as well as provides other analysis features.

In theory, you could code a simple library that contains
OpenGL function entry points, and logs function calls and
parameters passed. Name this library opengl32.dll and store
it in your Windows system folder (first, be careful to save
the existing opengl32.dll). This shouldn't be a difficult
programming task, but it might be tedious and time consuming.
This solution is not limited to Microsoft Windows; using the
appropriate library name, you can code this capture utility
on any platform, provided your application is linked with a
dynamically loadable library.

24.160
How can I render red-blue stereo pairs?

The Viewing section contains a question on creating a stereo view, and
has a link to information on creating anaglyphs. The basic
idea, In OpenGL, is as follows:

glColorMask (GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE)

Assuming the red image is the left image, set the
projection and model-view matrices for the left image.