The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

OpenGL::GLM - Interface to the GLM library for loading Alias Wavefront OBJ files

SYNOPSIS

  use OpenGL::GLM;
  $model = new OpenGL::GLM($filename);
  $model->Unitize;
  $model->Draw(GLM_SMOOTH);

DESCRIPTION

This module is a perl interface to the GLM library, by Nate Robbins and Jeff Rogers, for loading Alias Wavefront 3d object files and displaying them through OpenGL.

Note that no OpenGL::GLM objects should be created until an OpenGL context has first been established, by, eg, opening a GLUT or SDL OpenGL window.

METHODS

new
 my $object = new OpenGL::GLM('camel.obj');

Reads a model description from a Wavefront .OBJ file, and returns an OpenGL::GLM object. Takes one argument, the name of the file.

Unitize
  my $scale = $object->Unitize;

"Unitizes" a model by translating it to the origin and scaling it to fit in a unit cube around the origin. Returns the scalefactor used.

Scale
  $model->Scale($scalefactor);

Scales a model by a given amount.

ReverseWinding
  $model->ReverseWinding;

Reverse the polygon winding for all polygons in this model. Default winding is counter-clockwise. Also changes the direction of the normals.

FacetNormals
 $model->FacetNormals;

Generates facet normals for a model (by taking the cross product of the two vectors derived from the sides of each triangle). Assumes a counter-clockwise winding.

VertexNormals
 $model->VertexNormals($angle,$keep_existing);

Generates smooth vertex normals for a model. First builds a list of all the triangles each vertex is in. Then loops through each vertex in the the list averaging all the facet normals of the triangles each vertex is in. Finally, sets the normal index in the triangle for the vertex to the generated smooth normal. If the dot product of a facet normal and the facet normal associated with the first triangle in the list of triangles the current vertex is in is greater than the cosine of the angle parameter to the function, that facet normal is not added into the average normal calculation and the corresponding vertex is given the facet normal. This tends to preserve hard edges. The angle to use depends on the model, but 90 degrees is usually a good start.

$angle - maximum angle (in degrees) to smooth across
$keep_existing - if GL_TRUE, do not overwrite existing normals
LinearTexture
 $model->LinearTexture;

Generates texture coordinates according to a linear projection of the texture map. It generates these by linearly mapping the vertices onto a square.

SpheremapTexture
 $model->SpheremapTexture;

Generates texture coordinates according to a spherical projection of the texture map. Sometimes referred to as spheremap, or reflection map texture coordinates. It generates these by using the normal to calculate where that vertex would map onto a sphere. Since it is impossible to map something flat perfectly onto something spherical, there is distortion at the poles. This particular implementation causes the poles along the X axis to be distorted.

WriteOBJ
 $model->WriteOBJ($filename,$mode);

Writes a model description in Wavefront .OBJ format to a file. $mode is a bitwise OR of values describing what is written to the file:

GLM_NONE - write only vertices
GLM_FLAT - write facet normals
GLM_SMOOTH - write vertex normals
GLM_TEXTURE - write texture coords

GLM_FLAT and GLM_SMOOTH should not both be specified.

Draw
 $model->Draw($mode);

Renders the model to the current OpenGL context using the mode specified. $mode is a bitwise OR of values describing what is to be rendered:

GLM_NONE - render with only vertices
GLM_FLAT - render with facet normals
GLM_SMOOTH - render with vertex normals
GLM_TEXTURE - render with texture coords

GLM_FLAT and GLM_SMOOTH should not both be specified.

List
 $model->List($mode);

Generates and returns a display list for the model using the mode specified. $mode is a bitwise OR of values describing what is to be rendered:

GLM_NONE - render with only vertices
GLM_FLAT - render with facet normals
GLM_SMOOTH - render with vertex normals
GLM_TEXTURE - render with texture coords

GLM_FLAT and GLM_SMOOTH should not both be specified.

Weld
 $model->Weld($epsilon);

Eliminate (weld) vectors that are within an epsilon of each other. $epsilon is the maximum difference between vertices ; 0.00001 is a good start for a unitized model.

Exportable constants

  GLM_2_SIDED
  GLM_COLOR
  GLM_FLAT
  GLM_MATERIAL
  GLM_MAX_SHININESS
  GLM_MAX_TEXTURE_SIZE
  GLM_NONE
  GLM_SMOOTH
  GLM_TEXTURE

SEE ALSO

http://devernay.free.fr/hacks/glm/
http://www.dcs.ed.ac.uk/home/mxr/gfx/3d/OBJ.spec
OpenGL::Simple
OpenGL::Simple::GLUT
OpenGL::Simple::Viewer

AUTHOR

Jonathan Chin, <jon-opengl-glm@earth.li>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Jonathan Chin. Released and distributed under the GPL.