Giandomenico's page
home
ClsOpenGL class

by Giandomenico De Sanctis - gidesay@yahoo.com
version 2.1 - 2000


The ClsOpenGL class is a Visual Basic 5-compliant class for interfacing with the OpenGL / GLU library.
OpenGL is a professional 3D objects library, originary produced by Silicon Graphics for his machines, and now ported on many systems: Unix, Macintosh and Windows NT/95/98.
The Microsoft version is OpenGL 1.1, with two libraries: OPENGL32.DLL and GLU32.DLL. These are shipped with Windows NT and Windows 98 most recent versions, or are downloadable from Microsoft site.
OpenGL has many and many functions, covering all types of 3D manipulation: objects drawings, solid and wireframed; colour shading, with Phong method and others; special effects as fog; primitives as sphere, cone, cylinder, prysm, torus, NURBS (Non Uniform Rational Spline Surfaces); ecc. ecc.
A complete reference guide to OpenGL functions is available at Digital.
This class is aimed to simplify the use of standard OpenGL: when many calls to OpenGL functions are requested to obtain a simple result, the class encapsulate all in a single method, easy to use. This is the case, for example, in initialisation of a OpenGL rendering context (the OpenGL drawing ambient), where over 20 calls to different functions are necessary to define all the parameters: storage, lights, textures, perspective parameters, viewport, ecc.
Not all OpenGL functions are wrapped by corresponding methods: for example GLpushmatrix and GLpopmatrix, to save and restore the projection and modelview matrix. But all main functions are exposed via the class methods. Nevertheless, it is always possible to call directly a OpenGL function (as showed in the examples), bypassing the class, to obtain special effects.
It's also included the BMPTEX.DLL: this is used to quickly load BMP images as textures, and to save OpenGL drawings, always as a BMP image.
To use the class, you must load in VB project the reference libraries to OpenGL functions:
- OpenGL 1.1 - version 1.2 (OPENGL32.TLB)
- GLUT 3.5 - version 1.0 (GLUT32.TLB)
by Patrice Scribe - @1997 - (visit his great site dedicated to VB and OpenGL)
and load the class CLSOPENGL.CLS, and the BMPTEXDLL.BAS module which refer to BMPTEX.DLL.
In the subsequent tables are listed the properties and methods exposed by the class.

/P/ = Property /S/ = Method/Sub /F/ = Method/Function /E/ = Event

Properties


/P/ hGLRC As Long
pointer to OpenGL Rendering Context

/P/ hPalette As Long
pointer to current palette used

/P/ colorbits As Integer
number of color bits used by OpenGL context: 2 = 2 colors; 4 = 16 colors; 8 = 256 colors;
16 = 65535 colors; 24 = true colors

/P/ shaded As Boolean
set to true if faces of 3D objects/surfaces are drawn with color shading

/P/ texBitmap As New Collection
collection of BMP textures OpenGL indexes, loaded with the method LoadBMPImage.
Any texture have an unique identifying index

/P/ GLlist As New Collection
collection of indexes to the OpenGL lists created with various methods

/P/ nurb As GLUnurbs
pointer to the NURB current rendering context (for drawing interpolated surfaces)

/P/ filter_enabled As Boolean
set to true if a viewing filter is enabled

/P/ background_enabled As Boolean
set to true if a fixed background is enabled

/P/ textured As Boolean
set to true if faces of 3D objects/surfaces are textured

/P/ wireframed As Boolean
set to true if faces of 3D objects/surfaces are drawn only with borders (wireframe)

/P/ selected As Long
index of object selected with mouse on scene
Methods


/F/ drawterrain ( y ( ) As GLfloat, nx As Integer, nz As Integer, lx As Single, lz As Single, x0 As Integer, z0 As Integer, lista As Integer, scal As Single, tex1d ( ) As Byte, flag_triangle As Boolean, alpha As Single ) As Integer
DRAWTERRAIN - store in a OpenGL list a regular mesh of heightfield points, approximated with a series
of quads or triangles. The mesh is shaded using a 1D texture, to assign a color to every height,
digitized in the range 0-255.
y(): a matrix with 'nx' rows and 'nz' columns, any value y(ix, iz) is the Y height of the mesh at the
point in X-Z plane having coordinates: x=x0 + lx * ix; z=z0 + lz * iz (ix and iz >= 1)
nx = rows of the mesh
nz = columns of the mesh
lx = interval between two consecutive points along X axis
lz = interval between two consecutive points along Z axis
x0 = X coordinate of the origin point of mesh
z0 = Z coordinate of the origin point
lista = index of Gllist element that identifies the mesh for any subsequent drawing operation; if
lista=0, drawmesh return the index associated with the mesh, else return a value equal to lista
scal = reference scale of heights: any Y value is divided into scal, then the result (>=0 and <=255)
is used to assign the texture 1D coordinate
tex1d(0 to 2, 0 to 255) = a 256 values vector, containing the RGB components for every 1D texture colors to assign to every Y height.
flag_triangle = if true, any rectangular cell of the mesh is drawn as 4 triangles, having the vertexes
in the 4 vertexes of cell and in the middle point; if false, the cell is drawn as a OpenGL QUADS
alpha = transparency parameter of object, 0 = totally transparent; 1 = totally opaque

/F/ createbackground ( baseradius As Single, heigh As Single, precision As Integer, tex ( ) As String, skytex As String, collist ( ) As Single, lista As Integer ) As Integer
CREATEBACKGROUND - Define a scene background as an octagonal cylinder, with the faces
textured in the inside part. Each face may have different texture or color. The background is
completed with a top "sky" plane, with his texture or color. The background by default is centered
in the origin (x = 0, y = 0, z = 0).
baseradius = radius of base circle
heigh = height of background along Y axis
precision = parameter measuring the number of subpoints that have the faces, minimum value = 1.
tex(1 to 8) = names of a previously loaded texture for every face; if some faces are not textured,
set tex() = "" for that faces
skytex = name of previously loaded texture for top of background. In skytex = "", then to the top
is applied a "skyblue" color
collist(1 to 8, 0 to 2) = RGB colors of every face, used only if tex() = ""
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/F/ createcylinder ( baseradius As Single, topradius As Single, heigh As Single, precision As Integer, tex As String, colore ( ) As Single, lista As Integer ) As Integer
CREATECYLINDER - create a new generalized cylinder object, with base at coordinates origin (0, 0, 0)
and axis parallel to Z coordinate. The cylinder is open at extremities.
baseradius, topradius = radius of base and top of cylinder; if not equal, the object become
a truncated cone. If either is zero, the object is a cone.
heigh = height of cylinder along Z axis
precision = parameter measuring the number of faces that have the cylinder, minimum 3. With
3 = a prysm with triangular base, 4 = a prysm with square base, and so on.
With precision >= 15, the object is a real cylinder (or cone).
tex = name of a previously loaded texture; if the cylinder is not textured, set tex = ""
colore(0 to 2) = RGB color of cylinder, used only if tex = ""
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/F/ createsphere ( radius As Single, precision As Integer, tex As String, colore ( ) As Single, alpha As Single, lista As Integer ) As Integer
CREATESPHERE - create a new generalized sphere object, with center at coordinates origin (0, 0, 0).
radius = radius of sphere
precision = parameter measuring the number of faces that have the sphere, minimum 4.
With precision >= 15, the object is a real sphere.
tex = name of a previously loaded texture; if the sphere is not textured, set tex = ""
colore(0 to 2) = RGB color of sphere, used only if tex = ""
alpha = transparency parameter of object, 0 = totally transparent; 1 = totally opaque
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/F/ createprysm ( widt As Single, dept As Single, heigh1 As Single, heigh2 As Single, precision As Integer, texlist ( ) As String, collist ( ) As Single, lista As Integer ) As Integer
CREATEPRYSM - Define a prysmatic object, with inclined top if heigh1 not = heigh2. The texture or
color may be different for every face.
For every face and every axis, define a number of points equal to 'precision + 1'. If
precision = 1, then only the four corner vertexes of every faces are defined. This is useful because
OpenGL made a per-vertex lighting calculation, then having only the four corner vertexes in some cases
may generate poor lighting results.
widt = dimension of prysm along X axis
dept = dimension of prysm along Z axis
heigh1, heigh2 = forward and backward dimensions of prysm along Y axis
precision = parameter measuring the number of subpoints that have the faces, minimum value 1.
texlist(1 to 6) = names of a previously loaded texture for every face; if some faces are not textured,
set texlist() = "" for that faces
collist(1 to 6, 0 to 2) = RGB colors of every face, used only if tex = ""
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/F/ createfloor ( widt As Single, dept As Single, heigh As Single, precision As Integer, tex As String, colore ( ) As Single, lista As Integer ) As Integer
CREATEFLOOR - Define a square floor for the scene, parallel to X-Z plane, and having a corner
in the origin.
widt = dimension along X axis
dept = dimension along Z axis
heigh = position on Y axis
precision = parameter measuring the number of subpoints that have the floor, minimum value 1.
tex = name of a previously loaded texture; if floor is not textured, set tex="". The texture is
applied to every cell defined by parametr 'precision', as if the floor is tiled. For example,
precision = 4 mean that the floor have 4 x 4 tiles, each with the entire texture applied.
colore(0 to 2) = RGB color of floor, used only if tex = ""
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/F/ createtorus ( r1 As Double, r2 As Double, precision As Integer, tex As String, colore ( ) As Single, lista As Integer ) As Integer
CREATETORUS - create a new torus object, with circular section, around the coordinates origin (0, 0, 0).
r1 = radius of circular axis of torus
r2 = radius of circular section of torus
precision = parameter measuring the number of "slices" that have the torus, greater precision
mean more approximation to a perfectly smooth torus
tex = name of a previously loaded texture; if tex = "" the torus is not textured
colore(0 to 2) = RGB color of torus, used only if tex = ""
lista = index of Gllist element that identifies the object for any subsequent drawing operation; if
lista=0, the function return the index associated with the new object, else return a value equal
to lista

/S/ deleteNURBS ( )
DELETENURBS - delete the NURBS rendering context

/F/ drawNURBS ( ctrlpoint ( ) As GLfloat, precision As Single, matcolor ( ) As GLfloat, lista As Integer ) As Integer
DRAWNURBS - store in a OpenGL list a NURBS surface of the 3° order: this require 16 control points, in the
ctrlpoint matrix. This matrix has three dimension: ctrlpoint(0 to 2, 0 to 3, 0 to 3). First index
identifies the point coordinates x, y, z; second and third indexes identifies rows and columns for
control points.
precision = value to control the accuracy of NURBS surfaces: greater values equal minor accuracy
matcolor(0 to 3) = a four value vector containing RGB values (from 0 to 1.0) of the material color; the
fourth value is the alpha parameter, normally set to 1.0
lista = index of Gllist element that identifies the surface for any subsequent drawing operation; if
lista=0, drawNURBS return the index associated with the surface, else return a value equal to lista

/S/ setmaterial ( matprop ( ) As Single )
SETMATERIAL - set the light reflection caratheristics for the current material (AMBIENT and DIFFUSE)
matprop() = a four value vector containing RGB values (from 0 to 1.0) of the light color; the
fourth value is the alpha parameter, normally set to 1.0

/F/ LoadDXFobj ( nomedxf As String, dxftex As String, scalex As Single, scaley As Single, scalez As Single, col_r As Single, col_g As Single, col_b As Single ) As Integer
LOADDXFOBJ - load a 3D object defined in a DXF format file, as a group of 3DFACE labels; the object
is stored in a list, whose index is returned by function.
nomedxf = file name of the DXF object
dxftex = key-name of a texture, previously loaded with LoadBMPImage, this texture is applied to the
object; if dxftex is an empty string, no texture is applied
scalex, scaley, scalez = scale factors to multiply each coordinate; if no scale transformation
is needed, set the factor to 1.0
col_r, col_g, col_b = RGB component of the object color

/S/ LoadBMPImage ( nome As String, nomebitmap As String )
LOADBMPIMAGE - load a BMP image from the file 'nome', and store it in the texBitmap class collection
with the key-name 'nomebitmap', ready to use in 3D objects

/S/ GLinit ( obj )
GLINIT - initialise the OpenGL rendering context, linking it to "obj": a form or a picture box.
The drawings therefore are showed in this object. Many drawing parameters are assigned with default
values; the class properties 'textured', 'wireframed', 'shaded' are read to assign the correct
appearance to the drawn 3D objects. The 'hGLRC' property contain the pointer to the OpenGL Rendering
Context.

/S/ drawall ( )
DRAWALL - draw all the objects stored in each list collected in the GLlist collection

/S/ modrotate ( xAngle As Single, yAngle As Single, zAngle As Single )
MODROTATE - rotate the current axis (model view in OpenGL) of the angles xAngle, yAngle, zAngle, in
degrees. The previous axis orientation is not saved.

/S/ saveimage ( namefile As String )
SAVEIMAGE - save the current OpenGL drawing in the file 'namefile', in no-compressed BMP format

/S/ setlight ( lnum As Integer, lspot As Boolean, lcolor ( ) As Single, lposition ( ) As Single )
SETLIGHT - set and activate a OpenGL light of index lnum (light index may be one in constants
GL_LIGHT0, GL_LIGHT1, ......., GL_LIGHT7; GL_LIGHT0 and GL_LIGHT1 are defined as default by GLInit).
lspot = set to true if this is a spot light
lcolor(0 to 3) = a four value vector containing RGB values (from 0 to 1.0) of the light color; the fourth
value is the alpha parameter, normally set to 1.0
lposition(0 to 3) = a four value vector containing the x, y and z coordinates of light position; the fourth
value is set to 1.0 if it's a positional light, is set to 0 if it's a directional light

/S/ modtranslate ( xDelta As Single, yDelta As Single, zDelta As Single )
MODTRANSLATE - translate the current axis (model view in OpenGL) of the distances xDelta, yDelta,
zDelta, in OpenGL units. The previous axis orientation is not saved.

/S/ getcamerapos ( camerax As Single, cameray As Single, cameraz As Single )
GETCAMERAPOS - return the camera position, in MODELVIEW coordinates
camerax, cameray, cameraz = the camera coordinates

/S/ setcamerapos ( ByVal newx As Single, ByVal newy As Single, ByVal newz As Single )
SETCAMERAPOS - set the camera position, in MODELVIEW coordinates. The direction of view
is set parallel to X-Z plane, towards a point having xc = newx , yc = newy, zc = newz - 1.
newx, newy, newz = the new camera coordinates

/S/ cameragoup ( ByVal incrY As Single )
CAMERAGOUP - translate the camera and direction of view up or down.
incry = increment of Y coordinate

/S/ cameraupdown ( ByVal incrY As Single )
CAMERAUPDOWN - rotate the direction of view upward or downward.
incry = increment of view center Y coordinate

/S/ camerarolling ( ByVal angleZ As Single )
CAMERAROLLING- rotate the direction of view around MODELVIEW Z axis, rolling the view.
angleZ = rotation in degrees

/S/ camerarot ( ByVal angleY As Single )
CAMERAROT - rotate the direction of view around MODELVIEW Y axis, to right or to left.
angleY = rotation in degrees

/S/ cameraforwback ( ByVal incr As Single )
CAMERAFORBACK - translate the camera forward or backward, along the direction of view.
incr = shift of camera coordinates

/S/ cameraleftright ( ByVal incr As Single )
CAMERALEFTRIGHT - translate the camera to left or to right, in respect of the direction of view.
incr = shift of camera coordinates

/S/ initdraw ( )
INITDRAW - erase the depth and color buffer for a new draw cycle

/S/ completedraw ( )
COMPLETEDRAW - show the OpenGL canvas with all the objects drawn before

/S/ drawlist ( lista )
DRAWLIST - draw the list of index 'lista'

/S/ setobjpos ( dx As Single, dy As Single, dz As Single )
SETOBJPOS - set the current translation for MODELVIEW matrix
dx, dy, dz = coordinates translations

/S/ setobjrot ( rx As Single, ry As Single, rz As Single )
SETOBJROT - set the current rotation for MODELVIEW matrix
rx = rotation around X axis, in degrees
ry = rotation around Y axis, in degrees
rz = rotation around Z axis, in degrees

/S/ initlist ( lista As Integer, tex As String, colore ( ) As Single, ftex As Boolean, fcol As Boolean, Optional alpha As Single )
INITLIST - change the main charateristics of an object stored in a OpenGL list, without the need to
re-generate the entire object description. The properties of object that may be changed are: texture,
color, position (as previously set by a call to SETOBJPOS), rotation (as previously set by a call to SETOBJROT)
lista = index of Gllist element that identifies the object to change
tex = name of a previously loaded texture
colore(0 to 2) = RGB color of object
ftex = if true, the texture "tex" is bounded to object
fcol = if true, the color "colore()" is applied as material color to object
alpha = optional transparency parameter of object, 0 = mean no value passed, is equal to 1 = totally
opaque; values less 1 and greater 0 activate object transparency

/S/ selectobj ( mx As Double, my As Double )
SELECTOBJ - use the OpenGL special functions to find the 3D object under the point in the drawing
window, having coordinates mx and my (in window units). Useful calling with the mouse coordinates.
The sub set the class property 'selected' with the index of list, corresponding to one
of the 3D objects previously registered with others functions.

/S/ drawcursor ( )
DRAWCURSOR - draw a conic yellow cursor in the position set with a call to MOVECURSOR

/S/ movecursor ( dx As Single, dy As Single, dz As Single )
MOVECURSOR - set the position of a cursor, drawn with a call to DRAWCURSOR
dx, dy, dz = cursor coordinates

/S/ filter ( tex As String, colore ( ) As Single, alpha As Single )
FILTER - draw a visual filter in front of the camera. This may be a color filter, or
a texture filter, provided that alpha is less 1, else the filter is totally opaque!!
tex = name of a previously loaded texture; if the filter is not textured, set tex = ""
colore(0 to 2) = RGB color of filter, blended with the texture
alpha = transparency parameter of filter, 0 = totally transparent; 1 = totally opaque

/S/ background ( tex As String, colore ( ) As Single, alpha As Single )
BACKGROUND - draw a fixed background behind all other objects. The background is not subject
to perspective transformations.
tex = name of a previously loaded texture; if the background is not textured, set tex = ""
colore(0 to 2) = RGB color of background, blended with the texture
alpha = transparency parameter of background, 0 = totally transparent; 1 = totally opaque



Example programs
The example programs are:

TXOPENGL : create a clsOpenGL object and bind it to a picture box, then allow to load a DXF object, and a BMP texture; the object is drawed in the picture box, it is rotated and translated by menu or keyboard.

TXGRID : create a clsOpenGL object, bind it to a picture box, then draw a heightfield using the 'drawterrain' method; with keys it's possible to move the viewpoint over the terrain.