Module BGL

Module BGL

source code

The Blender.BGL submodule (the OpenGL wrapper).

New: some GLU functions: gluLookAt, etc.

The Blender.BGL submodule

This module wraps OpenGL constants and functions, making them available from within Blender Python.

The complete list can be retrieved from the module itself, by listing its contents: dir(Blender.BGL). A simple search on the net can point to more than enough material to teach OpenGL programming, from books to many collections of tutorials.

The "red book": "OpenGL Programming Guide: The Official Guide to Learning OpenGL" and the online NeHe tutorials are two of the best resources.

Example:
 import Blender
 from Blender.BGL import *
 from Blender import Draw
 R = G = B = 0
 A = 1
 title = "Testing BGL  + Draw"
 instructions = "Use mouse buttons or wheel to change the background color."
 quitting = " Press ESC or q to quit."
 len1 = Draw.GetStringWidth(title)
 len2 = Draw.GetStringWidth(instructions + quitting)
 #
 def show_win():
   glClearColor(R,G,B,A)                # define color used to clear buffers 
   glClear(GL_COLOR_BUFFER_BIT)         # use it to clear the color buffer
   glColor3f(0.35,0.18,0.92)            # define default color
   glBegin(GL_POLYGON)                  # begin a vertex data list
   glVertex2i(165, 158)
   glVertex2i(252, 55)
   glVertex2i(104, 128)
   glEnd()
   glColor3f(0.4,0.4,0.4)               # change default color
   glRecti(40, 96, 60+len1, 113)
   glColor3f(1,1,1)
   glRasterPos2i(50,100)                # move cursor to x = 50, y = 100
   Draw.Text(title)                     # draw this text there
   glRasterPos2i(350,40)                # move cursor again
   Draw.Text(instructions + quitting)   # draw another msg
   glBegin(GL_LINE_LOOP)                # begin a vertex-data list
   glVertex2i(46,92)
   glVertex2i(120,92)
   glVertex2i(120,115)
   glVertex2i(46,115)
   glEnd()                              # close this list
 #
 def ev(evt, val):                      # event callback for Draw.Register()
   global R,G,B,A                       # ... it handles input events
   if evt == Draw.ESCKEY or evt == Draw.QKEY:
     Draw.Exit()                        # this quits the script
   elif not val: return
   elif evt == Draw.LEFTMOUSE: R = 1 - R
   elif evt == Draw.MIDDLEMOUSE: G = 1 - G
   elif evt == Draw.RIGHTMOUSE: B = 1 - B
   elif evt == Draw.WHEELUPMOUSE:
     R += 0.1
     if R > 1: R = 1
   elif evt == Draw.WHEELDOWNMOUSE:
     R -= 0.1
     if R < 0: R = 0
   else:
     return                             # don't redraw if nothing changed
   Draw.Redraw(1)                       # make changes visible.
 #
 Draw.Register(show_win, ev, None)      # start the main loop



Note: you can use the Image module and Image.Image BPy object to load and set textures. See Image.Image.glLoad and Image.Image.glFree, for example.

See Also:
www.opengl.org, nehe.gamedev.net
Classes
  Buffer
The Buffer object is simply a block of memory that is delineated and initialized by the user.
Functions
 
glAccum(op, value)
Operate on the accumulation buffer
source code
 
glAlphaFunc(func, ref)
Specify the alpha test function
source code
 
glAreTexturesResident(n, textures, residences)
Determine if textures are loaded in texture memory
source code
 
glBegin(mode)
Delimit the vertices of a primitive or a group of like primatives
source code
 
glBindTexture(target, texture)
Bind a named texture to a texturing target
source code
 
glBitmap(width, height, xorig, yorig, xmove, ymove, bitmap)
Draw a bitmap
source code
 
glBlendFunc(sfactor, dfactor)
Specify pixel arithmetic
source code
 
glCallList(list)
Execute a display list
source code
 
glCallLists(n, type, lists)
Execute a list of display lists
source code
 
glClear(mask)
Clear buffers to preset values
source code
 
glClearAccum(red, green, blue, alpha)
Specify clear values for the accumulation buffer
source code
 
glClearColor(red, green, blue, alpha)
Specify clear values for the color buffers
source code
 
glClearDepth(depth)
Specify the clear value for the depth buffer
source code
 
glClearIndex(c)
Specify the clear value for the color index buffers
source code
 
glClearStencil(s)
Specify the clear value for the stencil buffer
source code
 
glClipPlane(plane, equation)
Specify a plane against which all geometry is clipped
source code
 
glColor(red, green, blue, alpha)
glColor3b, glColor3d, glColor3f, glColor3i, glColor3s, glColor3ub, glColor3ui, glColor3us, glColor4b, glColor4d, glColor4f, glColor4i, glColor4s, glColor4ub, glColor4ui, glColor4us, glColor3bv, glColor3dv, glColor3fv, glColor3iv, glColor3sv, glColor3ubv, glColor3uiv, glColor3usv, glColor4bv, glColor4dv, glColor4fv, glColor4iv, glColor4sv, glColor4ubv, glColor4uiv, glColor4usv
source code
 
glColorMask(red, green, blue, alpha)
Enable and disable writing of frame buffer color components
source code
 
glColorMaterial(face, mode)
Cause a material color to track the current color
source code
 
glCopyPixels(x, y, width, height, type)
Copy pixels in the frame buffer
source code
 
glCullFace(mode)
Specify whether front- or back-facing facets can be culled
source code
 
glDeleteLists(list, range)
Delete a contiguous group of display lists
source code
 
glDeleteTextures(n, textures)
Delete named textures
source code
 
glDepthFunc(func)
Specify the value used for depth buffer comparisons
source code
 
glDepthMask(flag)
Enable or disable writing into the depth buffer
source code
 
glDepthRange(zNear, zFar)
Specify mapping of depth values from normalized device coordinates to window coordinates
source code
 
glDisable(cap)
Disable server-side GL capabilities
source code
 
glDrawBuffer(mode)
Specify which color buffers are to be drawn into
source code
 
glDrawPixels(width, height, format, type, pixels)
Write a block of pixels to the frame buffer
source code
 
glEdgeFlag(flag)
glEdgeFlag, glEdgeFlagv
source code
 
glEnable(cap)
Enable server-side GL capabilities
source code
 
glEnd()
Delimit the vertices of a primitive or group of like primitives
source code
 
glEndList()
Create or replace a display list
source code
 
glEvalCoord(u, v)
glEvalCoord1d, glEvalCoord1f, glEvalCoord2d, glEvalCoord2f, glEvalCoord1dv, glEvalCoord1fv, glEvalCoord2dv, glEvalCoord2fv
source code
 
glEvalMesh(mode, i1, i2)
glEvalMesh1 or glEvalMesh2
source code
 
glEvalPoint(i, j)
glEvalPoint1 and glEvalPoint2
source code
 
glFeedbackBuffer(size, type, buffer)
Controls feedback mode
source code
 
glFinish()
Block until all GL execution is complete
source code
 
glFlush()
Force Execution of GL commands in finite time
source code
 
glFog(pname, param)
glFogf, glFogi, glFogfv, glFogiv
source code
 
glFrontFace(mode)
Define front- and back-facing polygons
source code
 
glFrustum(left, right, bottom, top, zNear, zFar)
Multiply the current matrix by a perspective matrix
source code
 
glGenLists(range)
Generate a contiguous set of empty display lists
source code
 
glGenTextures(n, textures)
Generate texture names
source code
 
glGet(pname, param)
glGetBooleanv, glGetfloatv, glGetFloatv, glGetIntegerv
source code
 
glGetClipPlane(plane, equation)
Return the coefficients of the specified clipping plane
source code
 
glGetError()
Return error information
source code
 
glGetLight(light, pname, params)
glGetLightfv and glGetLightiv
source code
 
glGetMap(target, query, v)
glGetMapdv, glGetMapfv, glGetMapiv
source code
 
glGetMaterial(face, pname, params)
glGetMaterialfv, glGetMaterialiv
source code
 
glGetPixelMap(map, values)
glGetPixelMapfv, glGetPixelMapuiv, glGetPixelMapusv
source code
 
glGetPolygonStipple(mask)
Return the polygon stipple pattern
source code
 
glGetString(name)
Return a string describing the current GL connection
source code
 
glGetTexEnv(target, pname, params)
glGetTexEnvfv, glGetTexEnviv
source code
 
glGetTexGen(coord, pname, params)
glGetTexGendv, glGetTexGenfv, glGetTexGeniv
source code
 
glGetTexImage(target, level, format, type, pixels)
Return a texture image
source code
 
glGetTexLevelParameter(target, level, pname, params)
glGetTexLevelParameterfv, glGetTexLevelParameteriv
source code
 
glGetTexParameter(target, pname, params)
glGetTexParameterfv, glGetTexParameteriv
source code
 
glHint(target, mode)
Specify implementation-specific hints
source code
 
glIndex(c)
glIndexd, glIndexf, glIndexi, glIndexs, glIndexdv, glIndexfv, glIndexiv, glIndexsv
source code
 
glInitNames()
Initialize the name stack
source code
 
glIsEnabled(cap)
Test whether a capability is enabled
source code
 
glIsList(list)
Determine if a name corresponds to a display-list
source code
 
glIsTexture(texture)
Determine if a name corresponds to a texture
source code
 
glLight(light, pname, param)
glLightf,glLighti, glLightfv, glLightiv
source code
 
glLightModel(pname, param)
glLightModelf, glLightModeli, glLightModelfv, glLightModeliv
source code
 
glLineStipple(factor, pattern)
Specify the line stipple pattern
source code
 
glLineWidth(width)
Specify the width of rasterized lines.
source code
 
glListBase(base)
Set the display-list base for glCallLists
source code
 
glLoadIdentity()
Replace the current matrix with the identity matrix
source code
 
glLoadMatrix(m)
glLoadMatrixd, glLoadMatixf
source code
 
glLoadName(name)
Load a name onto the name stack.
source code
 
glLogicOp(opcode)
Specify a logical pixel operation for color index rendering
source code
 
glMap1(target, u1, u2, stride, order, points)
glMap1d, glMap1f
source code
 
glMap2(target, u1, u2, ustride, uorder, v1, v2, vstride, vorder, points)
glMap2d, glMap2f
source code
 
glMapGrid(un, u1, u2, vn, v1, v2)
glMapGrid1d, glMapGrid1f, glMapGrid2d, glMapGrid2f
source code
 
glMaterial(face, pname, params)
Specify material parameters for the lighting model.
source code
 
glMatrixMode(mode)
Specify which matrix is the current matrix.
source code
 
glMultMatrix(m)
glMultMatrixd, glMultMatrixf
source code
 
glNewList(list, mode)
Create or replace a display list
source code
 
glNormal3(nx, ny, nz, v)
Normal3b, Normal3bv, Normal3d, Normal3dv, Normal3f, Normal3fv, Normal3i, Normal3iv, Normal3s, Normal3sv
source code
 
glOrtho(left, right, bottom, top, zNear, zFar)
Multiply the current matrix with an orthographic matrix
source code
 
glPassThrough(token)
Place a marker in the feedback buffer
source code
 
glPixelMap(map, mapsize, values)
glPixelMapfv, glPixelMapuiv, glPixelMapusv
source code
 
glPixelStore(pname, param)
glPixelStoref, glPixelStorei
source code
 
glPixelTransfer(pname, param)
glPixelTransferf, glPixelTransferi
source code
 
glPixelZoom(xfactor, yfactor)
Specify the pixel zoom factors
source code
 
glPointSize(size)
Specify the diameter of rasterized points
source code
 
glPolygonMode(face, mode)
Select a polygon rasterization mode
source code
 
glPolygonOffset(factor, units)
Set the scale and units used to calculate depth values
source code
 
glPolygonStipple(mask)
Set the polygon stippling pattern
source code
 
glPopAttrib()
Pop the server attribute stack
source code
 
glPopClientAttrib()
Pop the client attribute stack
source code
 
glPopMatrix()
Pop the current matrix stack
source code
 
glPopName()
Pop the name stack
source code
 
glPrioritizeTextures(n, textures, priorities)
Set texture residence priority
source code
 
glPushAttrib(mask)
Push the server attribute stack
source code
 
glPushClientAttrib(mask)
Push the client attribute stack
source code
 
glPushMatrix()
Push the current matrix stack
source code
 
glPushName(name)
Push the name stack
source code
 
glRasterPos(x, y, z, w)
glRasterPos2d, glRasterPos2f, glRasterPos2i, glRasterPos2s, glRasterPos3d, glRasterPos3f, glRasterPos3i, glRasterPos3s, glRasterPos4d, glRasterPos4f, glRasterPos4i, glRasterPos4s, glRasterPos2dv, glRasterPos2fv, glRasterPos2iv, glRasterPos2sv, glRasterPos3dv, glRasterPos3fv, glRasterPos3iv, glRasterPos3sv, glRasterPos4dv, glRasterPos4fv, glRasterPos4iv, glRasterPos4sv
source code
 
glReadBuffer(mode)
Select a color buffer source for pixels.
source code
 
glReadPixels(x, y, width, height, format, type, pixels)
Read a block of pixels from the frame buffer
source code
 
glRect(x1, y1, x2, y2, v1, v2)
glRectd, glRectf, glRecti, glRects, glRectdv, glRectfv, glRectiv, glRectsv
source code
 
glRenderMode(mode)
Set rasterization mode
source code
 
glRotate(angle, x, y, z)
glRotated, glRotatef
source code
 
glScale(x, y, z)
glScaled, glScalef
source code
 
glScissor(x, y, width, height)
Define the scissor box
source code
 
glSelectBuffer(size, buffer)
Establish a buffer for selection mode values
source code
 
glShadeModel(mode)
Select flat or smooth shading
source code
 
glStencilFuc(func, ref, mask)
Set function and reference value for stencil testing
source code
 
glStencilMask(mask)
Control the writing of individual bits in the stencil planes
source code
 
glStencilOp(fail, zfail, zpass)
Set stencil test actions
source code
 
glTexCoord(s, t, r, q, v)
glTexCoord1d, glTexCoord1f, glTexCoord1i, glTexCoord1s, glTexCoord2d, glTexCoord2f, glTexCoord2i, glTexCoord2s, glTexCoord3d, glTexCoord3f, glTexCoord3i, glTexCoord3s, glTexCoord4d, glTexCoord4f, glTexCoord4i, glTexCoord4s, glTexCoord1dv, glTexCoord1fv, glTexCoord1iv, glTexCoord1sv, glTexCoord2dv, glTexCoord2fv, glTexCoord2iv, glTexCoord2sv, glTexCoord3dv, glTexCoord3fv, glTexCoord3iv, glTexCoord3sv, glTexCoord4dv, glTexCoord4fv, glTexCoord4iv, glTexCoord4sv
source code
 
glTexEnv(target, pname, param)
glTextEnvf, glTextEnvi, glTextEnvfv, glTextEnviv
source code
 
glTexGen(coord, pname, param)
glTexGend, glTexGenf, glTexGeni, glTexGendv, glTexGenfv, glTexGeniv
source code
 
glTexImage1D(target, level, internalformat, width, border, format, type, pixels)
Specify a one-dimensional texture image
source code
 
glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels)
Specify a two-dimensional texture image
source code
 
glTexParameter(target, pname, param)
glTexParameterf, glTexParameteri, glTexParameterfv, glTexParameteriv
source code
 
glTranslate(x, y, z)
glTranslatef, glTranslated
source code
 
glVertex(x, y, z, w, v)
glVertex2d, glVertex2f, glVertex2i, glVertex2s, glVertex3d, glVertex3f, glVertex3i, glVertex3s, glVertex4d, glVertex4f, glVertex4i, glVertex4s, glVertex2dv, glVertex2fv, glVertex2iv, glVertex2sv, glVertex3dv, glVertex3fv, glVertex3iv, glVertex3sv, glVertex4dv, glVertex4fv, glVertex4iv, glVertex4sv
source code
 
glViewport(x, y, width, height)
Set the viewport
source code
 
gluPerspective(fovY, aspect, zNear, zFar)
Set up a perspective projection matrix.
source code
 
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)
Define a viewing transformation
source code
 
gluOrtho2D(left, right, bottom, top)
Define a 2-D orthographic projection matrix
source code
 
gluPickMatrix(x, y, width, height, viewport)
Define a picking region
source code
 
gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport, winx, winy, winz)
Map object coordinates to window coordinates.
source code
 
gluUnProject(winx, winy, winz, modelMatrix, projMatrix, viewport, objx, objy, objz)
Map object coordinates to window coordinates.
source code
Function Details

glAccum(op, value)

source code 
Operate on the accumulation buffer
Parameters:
  • op (Enumerated constant) - The accumulation buffer operation.
  • value (float) - a value used in the accumulation buffer operation.

glAlphaFunc(func, ref)

source code 
Specify the alpha test function
Parameters:
  • func (Enumerated constant) - Specifies the alpha comparison function.
  • ref (float) - The reference value that incoming alpha values are compared to. Clamped between 0 and 1.

glAreTexturesResident(n, textures, residences)

source code 
Determine if textures are loaded in texture memory
Parameters:
  • n (int) - Specifies the number of textures to be queried.
  • textures (Buffer object type GL_INT) - Specifies an array containing the names of the textures to be queried
  • residences (Buffer object type GL_INT(boolean)) - An array in which the texture residence status in returned.The residence status of a texture named by an element of textures is returned in the corresponding element of residences.

glBegin(mode)

source code 
Delimit the vertices of a primitive or a group of like primatives
Parameters:
  • mode (Enumerated constant) - Specifies the primitive that will be create from vertices between glBegin and glEnd.

glBindTexture(target, texture)

source code 
Bind a named texture to a texturing target
Parameters:
  • target (Enumerated constant) - Specifies the target to which the texture is bound.
  • texture (unsigned int) - Specifies the name of a texture.

glBitmap(width, height, xorig, yorig, xmove, ymove, bitmap)

source code 
Draw a bitmap
Parameters:
  • width, height - Specify the pixel width and height of the bitmap image.
  • xorig, yorig - Specify the location of the origin in the bitmap image. The origin is measured from the lower left corner of the bitmap, with right and up being the positive axes.
  • xmove, ymove - Specify the x and y offsets to be added to the current raster position after the bitmap is drawn.
  • bitmap (Buffer object type GL_BYTE) - Specifies the address of the bitmap image.

glBlendFunc(sfactor, dfactor)

source code 
Specify pixel arithmetic
Parameters:
  • sfactor (Enumerated constant) - Specifies how the red, green, blue, and alpha source blending factors are computed.
  • dfactor (Enumerated constant) - Specifies how the red, green, blue, and alpha destination blending factors are computed.

glCallList(list)

source code 
Execute a display list
Parameters:
  • list (unsigned int) - Specifies the integer name of the display list to be executed.

glCallLists(n, type, lists)

source code 
Execute a list of display lists
Parameters:
  • n (int) - Specifies the number of display lists to be executed.
  • type (Enumerated constant) - Specifies the type of values in lists.
  • lists (Buffer object) - Specifies the address of an array of name offsets in the display list. The pointer type is void because the offsets can be bytes, shorts, ints, or floats, depending on the value of type.

glClear(mask)

source code 
Clear buffers to preset values
Parameters:
  • mask (Enumerated constant(s)) - Bitwise OR of masks that indicate the buffers to be cleared.

glClearAccum(red, green, blue, alpha)

source code 
Specify clear values for the accumulation buffer
Parameters:
  • red, green, blue, alpha - Specify the red, green, blue, and alpha values used when the accumulation buffer is cleared. The initial values are all 0.

glClearColor(red, green, blue, alpha)

source code 
Specify clear values for the color buffers
Parameters:
  • red, green, blue, alpha - Specify the red, green, blue, and alpha values used when the color buffers are cleared. The initial values are all 0.

glClearDepth(depth)

source code 
Specify the clear value for the depth buffer
Parameters:
  • depth (int) - Specifies the depth value used when the depth buffer is cleared. The initial value is 1.

glClearIndex(c)

source code 
Specify the clear value for the color index buffers
Parameters:
  • c (float) - Specifies the index used when the color index buffers are cleared. The initial value is 0.

glClearStencil(s)

source code 
Specify the clear value for the stencil buffer
Parameters:
  • s (int) - Specifies the index used when the stencil buffer is cleared. The initial value is 0.

glClipPlane(plane, equation)

source code 
Specify a plane against which all geometry is clipped
Parameters:
  • plane (Enumerated constant) - Specifies which clipping plane is being positioned.
  • equation (Buffer object type GL_FLOAT(double)) - Specifies the address of an array of four double- precision floating-point values. These values are interpreted as a plane equation.

glColor(red, green, blue, alpha)

source code 

glColor3b, glColor3d, glColor3f, glColor3i, glColor3s, glColor3ub, glColor3ui, glColor3us, glColor4b, glColor4d, glColor4f, glColor4i, glColor4s, glColor4ub, glColor4ui, glColor4us, glColor3bv, glColor3dv, glColor3fv, glColor3iv, glColor3sv, glColor3ubv, glColor3uiv, glColor3usv, glColor4bv, glColor4dv, glColor4fv, glColor4iv, glColor4sv, glColor4ubv, glColor4uiv, glColor4usv

Set a new color.
Parameters:
  • red, green, blue - Specify new red, green, and blue values for the current color.
  • alpha - Specifies a new alpha value for the current color. Included only in the four-argument glColor4 commands. (With '4' colors only)