processing.opengl
Class PGraphicsOpenGL

java.lang.Object
  extended by processing.core.PImage
      extended by processing.core.PGraphics
          extended by processing.core.PGraphics3D
              extended by processing.opengl.PGraphicsOpenGL
All Implemented Interfaces:
java.lang.Cloneable, PConstants

public class PGraphicsOpenGL
extends PGraphics3D

Implementation of the PGraphics API that employs OpenGL rendering via JOGL.

JOGL requires Java 1.4 or higher, so there are no restrictions on this code to be compatible with Java 1.1 or Java 1.3.

This code relies on PGraphics3D for all lighting and transformations. Meaning that translate(), rotate(), and any lighting will be done in PGraphics3D, and OpenGL is only used to blit lines and triangles as fast as it possibly can.

For this reason, OpenGL may not be accelerated as far as it could be, but I don't have the time to maintain two separate versions of the renderer. My development time must always be focused on implementation and covering features first, and optimizing later.

Further, the difference may be negligible, as the primary slowdown in Java is moving pixels (i.e. a large frame buffer is nearly impossible because Java just can't do a MemoryImageSource at screen resolution) and the overhead from JNI tends to be significant. In the latter case, we may even save time in some cases where a large number of calls to OpenGL would otherwise be used, but that's probably a stretch.

The code is also very messy, while features are being added and removed rapidly as we head towards 1.0. Things got particularly ugly as we approached beta while both Simon and I were working on it. Relax, we'll get it fixed up later.

When exporting applets, the JOGL Applet Launcher is used. More information about the launcher can be found at its documentation page.


Nested Class Summary
 class PGraphicsOpenGL.TessCallback
          There must be a better way to do this, but I'm having a brain fart with all the inner class crap.
 
Field Summary
static boolean BIG_ENDIAN
          Set to true if the host system is big endian (PowerPC, MIPS, SPARC), false if little endian (x86 Intel for Mac or PC).
 javax.media.opengl.GL gl
           
 javax.media.opengl.glu.GLU glu
           
 
Fields inherited from class processing.core.PGraphics3D
camera, cameraAspect, cameraFar, cameraFOV, cameraNear, cameraX, cameraY, cameraZ, currentLightFalloffConstant, currentLightFalloffLinear, currentLightFalloffQuadratic, currentLightSpecular, lightCount, lightDiffuse, lightFalloffConstant, lightFalloffLinear, lightFalloffQuadratic, lightNormal, lightPosition, lightSpecular, lightSpotAngle, lightSpotAngleCos, lightSpotConcentration, lightType, line, MAX_LIGHTS, modelview, modelviewInv, projection, smoothTriangle, TRI_COLOR_COUNT, TRI_DIFFUSE_A, TRI_DIFFUSE_B, TRI_DIFFUSE_G, TRI_DIFFUSE_R, TRI_SPECULAR_B, TRI_SPECULAR_G, TRI_SPECULAR_R, triangle, zbuffer
 
Fields inherited from class processing.core.PGraphics
ambientB, ambientG, ambientR, backgroundColor, bezierDetail, colorMode, colorModeA, colorModeX, colorModeY, colorModeZ, curveTightness, edge, ellipseMode, emissiveB, emissiveG, emissiveR, fill, fillColor, image, imageMode, normalX, normalY, normalZ, pixelCount, rectMode, shapeMode, shininess, smooth, specularB, specularG, specularR, sphereDetailU, sphereDetailV, stroke, strokeCap, strokeColor, strokeJoin, strokeWeight, textAlign, textAlignY, textFont, textLeading, textMode, textSize, textureImage, textureMode, textureU, textureV, tint, tintColor
 
Fields inherited from class processing.core.PImage
format, height, parent, pixels, width
 
Fields inherited from interface processing.core.PConstants
A, AB, ADD, AG, ALPHA, ALPHA_MASK, ALT, AMBIENT, AR, ARC, ARGB, ARROW, B, BACKSPACE, BASELINE, BEEN_LIT, BEVEL, BLEND, BLUE_MASK, BLUR, BOTTOM, BOX, BURN, CENTER, CENTER_DIAMETER, CENTER_RADIUS, CHATTER, CLOSE, CMYK, CODED, COMPLAINT, CONTROL, CORNER, CORNERS, CROSS, CUSTOM, DA, DARKEST, DB, DEG_TO_RAD, DELETE, DG, DIAMETER, DIFFERENCE, DILATE, DIRECTIONAL, DISABLE_ACCURATE_TEXTURES, DISABLE_DEPTH_SORT, DISABLE_DEPTH_TEST, DISABLE_OPENGL_2X_SMOOTH, DISABLE_OPENGL_ERROR_REPORT, DODGE, DOWN, DR, DXF, EB, EDGE, EG, ELLIPSE, ENABLE_ACCURATE_TEXTURES, ENABLE_DEPTH_SORT, ENABLE_DEPTH_TEST, ENABLE_NATIVE_FONTS, ENABLE_OPENGL_2X_SMOOTH, ENABLE_OPENGL_4X_SMOOTH, ENABLE_OPENGL_ERROR_REPORT, ENTER, EPSILON, ER, ERODE, ERROR_BACKGROUND_IMAGE_FORMAT, ERROR_BACKGROUND_IMAGE_SIZE, ERROR_PUSHMATRIX_OVERFLOW, ERROR_PUSHMATRIX_UNDERFLOW, ERROR_TEXTFONT_NULL_PFONT, ESC, EXCLUSION, G, GIF, GRAY, GREEN_MASK, HALF_PI, HAND, HARD_LIGHT, HINT_COUNT, HSB, IMAGE, INVERT, JAVA2D, JPEG, LEFT, LIGHTEST, LINE, LINES, LINUX, MACOSX, MAX_FLOAT, MAX_INT, MIN_FLOAT, MIN_INT, MITER, MODEL, MOVE, MULTIPLY, NORMAL, NORMALIZED, NX, NY, NZ, OPAQUE, OPEN, OPENGL, ORTHOGRAPHIC, OTHER, OVERLAY, P2D, P3D, PATH, PDF, PERSPECTIVE, PI, platformNames, POINT, POINTS, POLYGON, POSTERIZE, PROBLEM, PROJECT, QUAD, QUAD_STRIP, QUADS, QUARTER_PI, R, RAD_TO_DEG, RADIUS, RECT, RED_MASK, REPLACE, RETURN, RGB, RIGHT, ROUND, SA, SB, SCREEN, SG, SHAPE, SHIFT, SHINE, SOFT_LIGHT, SPB, SPG, SPHERE, SPOT, SPR, SQUARE, SR, SUBTRACT, SW, TAB, TARGA, TEXT, THIRD_PI, THRESHOLD, TIFF, TOP, TRIANGLE, TRIANGLE_FAN, TRIANGLE_STRIP, TRIANGLES, TWO_PI, TX, TY, TZ, U, UP, V, VERTEX_FIELD_COUNT, VW, VX, VY, VZ, WAIT, WHITESPACE, WINDOWS, X, Y, Z
 
Constructor Summary
PGraphicsOpenGL()
           
 
Method Summary
 void ambientLight(float r, float g, float b)
          Add an ambient light based on the current color mode.
 void ambientLight(float r, float g, float b, float x, float y, float z)
          Add an ambient light based on the current color mode.
 void beginDraw()
          Prepares the PGraphics for drawing.
 javax.media.opengl.GL beginGL()
           
 boolean canDraw()
          OpenGL cannot draw until a proper native peer is available, so this returns the value of PApplet.isDisplayable() (inherited from Component).
 void directionalLight(float r, float g, float b, float nx, float ny, float nz)
           
 void endDraw()
          See notes in PGraphics.
 void endGL()
           
 void filter(int kind)
          This is really inefficient and not a good idea in OpenGL.
 void filter(int kind, float param)
          This is really inefficient and not a good idea in OpenGL.
 PImage get()
          Returns a copy of this PImage.
 int get(int x, int y)
          Returns an ARGB "color" type (a packed 32 bit int with the color.
 javax.media.opengl.GLContext getContext()
          Get the current context, for use by libraries that need to talk to it.
 void hint(int which)
          Enable a hint option.
 void lightFalloff(float constant, float linear, float quadratic)
          Set the light falloff rates for the last light that was created.
 void lightSpecular(float x, float y, float z)
          Set the specular color of the last light created.
 void loadPixels()
          Call this when you want to mess with the pixels[] array.
 void mask(int[] alpha)
          Set alpha channel for an image.
 void mask(PImage alpha)
          Set alpha channel for an image using another image as the source.
 void noSmooth()
          Disable smoothing.
 void pointLight(float r, float g, float b, float x, float y, float z)
           
 void report(java.lang.String where)
          Report on anything from glError().
 void resize(int wide, int high)
          Resize this image to a new width and height.
 void set(int x, int y, int argb)
          Set a single pixel to the specified color.
 void set(int x, int y, PImage source)
          Set an image directly to the screen.
 void shininess(float shine)
           
 void smooth()
          If true in PImage, use bilinear interpolation for copy() operations.
 void spotLight(float r, float g, float b, float x, float y, float z, float nx, float ny, float nz, float angle, float concentration)
           
 void strokeCap(int cap)
           
 void strokeJoin(int join)
           
 void strokeWeight(float weight)
           
 float textAscent()
          Returns the ascent of the current font at the current size.
 float textDescent()
          Returns the descent of the current font at the current size.
 void textFont(PFont which)
          Sets the current font.
 void updatePixels()
          Call this when finished messing with the pixels[] array.
 
Methods inherited from class processing.core.PGraphics3D
applyMatrix, applyMatrix, applyMatrix, applyMatrix, beginCamera, beginShape, box, camera, camera, endCamera, endShape, flush, frustum, getMatrix, getMatrix, is2D, is3D, lights, modelX, modelY, modelZ, noLights, ortho, ortho, perspective, perspective, popMatrix, printCamera, printMatrix, printProjection, pushMatrix, resetMatrix, rotate, rotate, rotateX, rotateY, rotateZ, scale, scale, scale, screenX, screenX, screenY, screenY, screenZ, setMatrix, setMatrix, setSize, sphere, texture, translate, translate, vertex, vertex
 
Methods inherited from class processing.core.PGraphics
alpha, ambient, ambient, ambient, applyMatrix, arc, background, background, background, background, background, background, background, beginRaw, beginShape, bezier, bezier, bezierDetail, bezierPoint, bezierTangent, bezierVertex, bezierVertex, blue, box, breakShape, brightness, color, color, color, color, color, color, color, color, color, colorMode, colorMode, colorMode, colorMode, curve, curve, curveDetail, curvePoint, curveTangent, curveTightness, curveVertex, curveVertex, displayable, dispose, edge, ellipse, ellipseMode, emissive, emissive, emissive, endRaw, endShape, fill, fill, fill, fill, fill, fill, getMatrix, getStyle, getStyle, green, hue, image, image, image, imageMode, lerpColor, lerpColor, line, line, noFill, normal, noStroke, noTint, point, point, popStyle, pushStyle, quad, rect, rectMode, red, saturation, setMatrix, setParent, setPath, setPrimary, shape, shape, shape, shapeMode, showException, showWarning, specular, specular, specular, sphereDetail, sphereDetail, stroke, stroke, stroke, stroke, stroke, stroke, style, text, text, text, text, text, text, text, text, text, text, text, text, text, text, textAlign, textAlign, textFont, textLeading, textMode, textSize, textureMode, textWidth, textWidth, tint, tint, tint, tint, tint, tint, triangle, vertex, vertex, vertex
 
Methods inherited from class processing.core.PImage
blend, blend, blendColor, clone, copy, copy, get, getCache, getImage, init, isModified, removeCache, save, setCache, setModified, setModified, updatePixels
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

gl

public javax.media.opengl.GL gl

glu

public javax.media.opengl.glu.GLU glu

BIG_ENDIAN

public static boolean BIG_ENDIAN
Set to true if the host system is big endian (PowerPC, MIPS, SPARC), false if little endian (x86 Intel for Mac or PC).

Constructor Detail

PGraphicsOpenGL

public PGraphicsOpenGL()
Method Detail

getContext

public javax.media.opengl.GLContext getContext()
Get the current context, for use by libraries that need to talk to it.


canDraw

public boolean canDraw()
OpenGL cannot draw until a proper native peer is available, so this returns the value of PApplet.isDisplayable() (inherited from Component).

Overrides:
canDraw in class PGraphics

beginDraw

public void beginDraw()
Description copied from class: PGraphics
Prepares the PGraphics for drawing.

When creating your own PGraphics, you should call this before drawing anything.

Overrides:
beginDraw in class PGraphics3D

endDraw

public void endDraw()
Description copied from class: PGraphics3D
See notes in PGraphics. If z-sorting has been turned on, then the triangles will all be quicksorted here (to make alpha work more properly) and then blit to the screen.

Overrides:
endDraw in class PGraphics3D

beginGL

public javax.media.opengl.GL beginGL()

endGL

public void endGL()

hint

public void hint(int which)
Description copied from class: PGraphics
Enable a hint option.

For the most part, hints are temporary api quirks, for which a proper api hasn't been properly worked out. for instance SMOOTH_IMAGES existed because smooth() wasn't yet implemented, but it will soon go away.

They also exist for obscure features in the graphics engine, like enabling/disabling single pixel lines that ignore the zbuffer, the way they do in alphabot.

Current hint options:

Overrides:
hint in class PGraphics3D

smooth

public void smooth()
Description copied from class: PGraphics
If true in PImage, use bilinear interpolation for copy() operations. When inherited by PGraphics, also controls shapes.

Overrides:
smooth in class PGraphics3D

noSmooth

public void noSmooth()
Description copied from class: PGraphics
Disable smoothing. See smooth().

Overrides:
noSmooth in class PGraphics3D

textAscent

public float textAscent()
Description copied from class: PGraphics
Returns the ascent of the current font at the current size. This is a method, rather than a variable inside the PGraphics object because it requires calculation.

Overrides:
textAscent in class PGraphics

textDescent

public float textDescent()
Description copied from class: PGraphics
Returns the descent of the current font at the current size. This is a method, rather than a variable inside the PGraphics object because it requires calculation.

Overrides:
textDescent in class PGraphics

textFont

public void textFont(PFont which)
Description copied from class: PGraphics
Sets the current font. The font's size will be the "natural" size of this font (the size that was set when using "Create Font"). The leading will also be reset.

Overrides:
textFont in class PGraphics

strokeWeight

public void strokeWeight(float weight)
Overrides:
strokeWeight in class PGraphics

strokeJoin

public void strokeJoin(int join)
Overrides:
strokeJoin in class PGraphics3D

strokeCap

public void strokeCap(int cap)
Overrides:
strokeCap in class PGraphics3D

shininess

public void shininess(float shine)
Overrides:
shininess in class PGraphics

ambientLight

public void ambientLight(float r,
                         float g,
                         float b)
Description copied from class: PGraphics3D
Add an ambient light based on the current color mode.

Overrides:
ambientLight in class PGraphics3D

ambientLight

public void ambientLight(float r,
                         float g,
                         float b,
                         float x,
                         float y,
                         float z)
Description copied from class: PGraphics3D
Add an ambient light based on the current color mode. This version includes an (x, y, z) position for situations where the falloff distance is used.

Overrides:
ambientLight in class PGraphics3D

directionalLight

public void directionalLight(float r,
                             float g,
                             float b,
                             float nx,
                             float ny,
                             float nz)
Overrides:
directionalLight in class PGraphics3D

pointLight

public void pointLight(float r,
                       float g,
                       float b,
                       float x,
                       float y,
                       float z)
Overrides:
pointLight in class PGraphics3D

spotLight

public void spotLight(float r,
                      float g,
                      float b,
                      float x,
                      float y,
                      float z,
                      float nx,
                      float ny,
                      float nz,
                      float angle,
                      float concentration)
Overrides:
spotLight in class PGraphics3D

lightFalloff

public void lightFalloff(float constant,
                         float linear,
                         float quadratic)
Description copied from class: PGraphics3D
Set the light falloff rates for the last light that was created. Default is lightFalloff(1, 0, 0).

Overrides:
lightFalloff in class PGraphics3D

lightSpecular

public void lightSpecular(float x,
                          float y,
                          float z)
Description copied from class: PGraphics3D
Set the specular color of the last light created.

Overrides:
lightSpecular in class PGraphics3D

report

public void report(java.lang.String where)
Report on anything from glError(). Don't use this inside glBegin/glEnd otherwise it'll throw an GL_INVALID_OPERATION error.


loadPixels

public void loadPixels()
Description copied from class: PImage
Call this when you want to mess with the pixels[] array.

For subclasses where the pixels[] buffer isn't set by default, this should copy all data into the pixels[] array

Overrides:
loadPixels in class PImage

updatePixels

public void updatePixels()
Description copied from class: PImage
Call this when finished messing with the pixels[] array.

Mark all pixels as needing update.

Overrides:
updatePixels in class PImage

resize

public void resize(int wide,
                   int high)
Description copied from class: PImage
Resize this image to a new width and height. Use 0 for wide or high to make that dimension scale proportionally.

Overrides:
resize in class PImage

get

public int get(int x,
               int y)
Description copied from class: PImage
Returns an ARGB "color" type (a packed 32 bit int with the color. If the coordinate is outside the image, zero is returned (black, but completely transparent).

If the image is in RGB format (i.e. on a PVideo object), the value will get its high bits set, just to avoid cases where they haven't been set already.

If the image is in ALPHA format, this returns a white with its alpha value set.

This function is included primarily for beginners. It is quite slow because it has to check to see if the x, y that was provided is inside the bounds, and then has to check to see what image type it is. If you want things to be more efficient, access the pixels[] array directly.

Overrides:
get in class PImage

get

public PImage get()
Description copied from class: PImage
Returns a copy of this PImage. Equivalent to get(0, 0, width, height).

Overrides:
get in class PImage

set

public void set(int x,
                int y,
                int argb)
Description copied from class: PImage
Set a single pixel to the specified color.

Overrides:
set in class PImage

set

public void set(int x,
                int y,
                PImage source)
Set an image directly to the screen.

TODO not optimized properly, creates multiple temporary buffers the size of the image. Needs to instead use image cache, but that requires two types of image cache. One for power of 2 textures and another for glReadPixels/glDrawPixels data that's flipped vertically. Both have their components all swapped to native.

Overrides:
set in class PImage

mask

public void mask(int[] alpha)
Description copied from class: PImage
Set alpha channel for an image. Black colors in the source image will make the destination image completely transparent, and white will make things fully opaque. Gray values will be in-between steps.

Strictly speaking the "blue" value from the source image is used as the alpha color. For a fully grayscale image, this is correct, but for a color image it's not 100% accurate. For a more accurate conversion, first use filter(GRAY) which will make the image into a "correct" grayscake by performing a proper luminance-based conversion.

Overrides:
mask in class PImage

mask

public void mask(PImage alpha)
Description copied from class: PImage
Set alpha channel for an image using another image as the source.

Overrides:
mask in class PImage

filter

public void filter(int kind)
This is really inefficient and not a good idea in OpenGL. Use get() and set() with a smaller image area, or call the filter on an image instead, and then draw that.

Overrides:
filter in class PImage

filter

public void filter(int kind,
                   float param)
This is really inefficient and not a good idea in OpenGL. Use get() and set() with a smaller image area, or call the filter on an image instead, and then draw that.

Overrides:
filter in class PImage