|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectprocessing.core.PImage
processing.core.PGraphics
processing.core.PGraphics3D
processing.opengl.PGraphicsOpenGL
public class PGraphicsOpenGL
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 |
|---|
line, shape_index, 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 |
| Fields inherited from class processing.core.PImage |
|---|
cache, format, height, imageMode, modified, mx1, mx2, my1, my2, parent, pixels, smooth, width |
| Constructor Summary | |
|---|---|
PGraphicsOpenGL()
|
|
| Method Summary | |
|---|---|
void |
ambient(float gray)
|
void |
ambient(float x,
float y,
float z)
|
void |
ambient(int rgb)
|
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 |
background(PImage bgimage)
Takes an RGB or RGBA image and sets it as the background. |
void |
beginDraw()
Prepares the PGraphics for drawing. |
javax.media.opengl.GL |
beginGL()
|
void |
bindTexture(PImage texture)
|
void |
blend(int sx,
int sy,
int dx,
int dy,
int mode)
|
void |
blend(int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2,
int mode)
TODO - extremely slow and not optimized. |
void |
blend(PImage src,
int sx,
int sy,
int dx,
int dy,
int mode)
|
void |
blend(PImage src,
int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2,
int mode)
TODO - extremely slow and not optimized. |
boolean |
canDraw()
OpenGL cannot draw until a proper native peer is available, so this returns the value of PApplet.isDisplayable() (inherited from Component). |
void |
clear()
Clear pixel buffer. |
void |
copy(int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2)
Extremely slow and not optimized, should use glCopyPixels instead. |
void |
copy(PImage src,
int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2)
TODO - extremely slow and not optimized. |
void |
directionalLight(float r,
float g,
float b,
float nx,
float ny,
float nz)
|
void |
emissive(float gray)
|
void |
emissive(float x,
float y,
float z)
|
void |
emissive(int rgb)
|
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. |
PImage |
get(int x,
int y,
int w,
int h)
Grab a subsection of a PImage, and copy it into a fresh PImage. |
javax.media.opengl.GLContext |
getContext()
Get the current context, for use by libraries that need to talk to it. |
void |
glLightSpotAngle(int num)
|
void |
glLightSpotConcentration(int num)
|
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 |
lights()
Sets up an ambient and directional light. |
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 |
noSmooth()
Disable smoothing. |
void |
pointLight(float r,
float g,
float b,
float x,
float y,
float z)
|
void |
renderLines()
|
void |
report(java.lang.String where)
Report on anything from glError(). |
void |
save(java.lang.String filename)
Save this image to disk. |
void |
set(int x,
int y,
int argb)
Silently ignores if the coordinate is outside the image. |
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 |
specular(float gray)
|
void |
specular(float x,
float y,
float z)
|
void |
specular(int rgb)
|
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)
|
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 |
textMode(int mode)
Sets the text rendering/placement to be either SCREEN (direct to the screen, exact coordinates, only use the font's original size) or MODEL (the default, where text is manipulated by translate() and can have a textSize). |
void |
textSize(float size)
Same as parent, but override for native version of the font. |
void |
updatePixels()
Call this when finished messing with the pixels[] array. |
float |
uscale(PImage texture)
|
float |
vscale(PImage texture)
|
| Methods inherited from class processing.core.PGraphics3D |
|---|
applyMatrix, applyMatrix, beginCamera, beginShape, bezierVertex, bezierVertex, box, box, camera, camera, defaultSettings, endCamera, endShape, frustum, loadMatrix, modelX, modelY, modelZ, noLights, normal, ortho, ortho, perspective, perspective, point, point, popMatrix, printCamera, printMatrix, printProjection, pushMatrix, quad, resetMatrix, rotate, rotate, rotateX, rotateY, rotateZ, scale, scale, scale, screenX, screenX, screenY, screenY, screenZ, setSize, sphere, sphereDetail, sphereDetail, texture, translate, translate, triangle, vertex, vertex, vertex, vertex |
| Methods inherited from class processing.core.PGraphics |
|---|
alpha, arc, background, background, background, background, background, background, beginRaw, beginShape, bezier, bezier, bezierDetail, bezierPoint, bezierTangent, blue, 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, ellipse, ellipseMode, endRaw, endShape, fill, fill, fill, fill, fill, fill, green, hue, image, image, image, lerpColor, lerpColor, line, line, mask, mask, noFill, noStroke, noTint, rect, rectMode, red, saturation, setParent, setPath, setPrimary, stroke, stroke, stroke, stroke, stroke, stroke, strokeWeight, text, text, text, text, text, text, text, text, text, text, text, text, textAlign, textAlign, textFont, textFont, textLeading, textureMode, textWidth, textWidth, tint, tint, tint, tint, tint, tint |
| Methods inherited from class processing.core.PImage |
|---|
blendColor, clone, getImage, imageMode, init, resize, updatePixels |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public javax.media.opengl.GL gl
public javax.media.opengl.glu.GLU glu
public static boolean BIG_ENDIAN
| Constructor Detail |
|---|
public PGraphicsOpenGL()
| Method Detail |
|---|
public void hint(int which)
PGraphicsFor 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:
hint in class PGraphics3Dpublic javax.media.opengl.GLContext getContext()
public boolean canDraw()
canDraw in class PGraphicspublic void beginDraw()
PGraphics
beginDraw in class PGraphics3Dpublic void endDraw()
PGraphics3D
endDraw in class PGraphics3Dpublic javax.media.opengl.GL beginGL()
public void endGL()
public float uscale(PImage texture)
public float vscale(PImage texture)
public void bindTexture(PImage texture)
public void renderLines()
public float textAscent()
PGraphics
textAscent in class PGraphicspublic float textDescent()
PGraphics
textDescent in class PGraphicspublic void textSize(float size)
textSize in class PGraphicspublic void textMode(int mode)
PGraphics
textMode in class PGraphicspublic void lights()
PGraphics3D
The Lighting Skinny:
The way lighting works is complicated enough that it's worth
producing a document to describe it. Lighting calculations proceed
pretty much exactly as described in the OpenGL red book.
Light-affecting material properties:
AMBIENT COLOR
- multiplies by light's ambient component
- for believability this should match diffuse color
DIFFUSE COLOR
- multiplies by light's diffuse component
SPECULAR COLOR
- multiplies by light's specular component
- usually less colored than diffuse/ambient
SHININESS
- the concentration of specular effect
- this should be set pretty high (20-50) to see really
noticeable specularity
EMISSIVE COLOR
- constant additive color effect
Light types:
AMBIENT
- one color
- no specular color
- no direction
- may have falloff (constant, linear, and quadratic)
- may have position (which matters in non-constant falloff case)
- multiplies by a material's ambient reflection
DIRECTIONAL
- has diffuse color
- has specular color
- has direction
- no position
- no falloff
- multiplies by a material's diffuse and specular reflections
POINT
- has diffuse color
- has specular color
- has position
- no direction
- may have falloff (constant, linear, and quadratic)
- multiplies by a material's diffuse and specular reflections
SPOT
- has diffuse color
- has specular color
- has position
- has direction
- has cone angle (set to half the total cone angle)
- has concentration value
- may have falloff (constant, linear, and quadratic)
- multiplies by a material's diffuse and specular reflections
Normal modes:
All of the primitives (rect, box, sphere, etc.) have their normals
set nicely. During beginShape/endShape normals can be set by the user.
AUTO-NORMAL
- if no normal is set during the shape, we are in auto-normal mode
- auto-normal calculates one normal per triangle (face-normal mode)
SHAPE-NORMAL
- if one normal is set during the shape, it will be used for
all vertices
VERTEX-NORMAL
- if multiple normals are set, each normal applies to
subsequent vertices
- (except for the first one, which applies to previous
and subsequent vertices)
Efficiency consequences:
There is a major efficiency consequence of position-dependent
lighting calculations per vertex. (See below for determining
whether lighting is vertex position-dependent.) If there is no
position dependency then the only factors that affect the lighting
contribution per vertex are its colors and its normal.
There is a major efficiency win if
1) lighting is not position dependent
2) we are in AUTO-NORMAL or SHAPE-NORMAL mode
because then we can calculate one lighting contribution per shape
(SHAPE-NORMAL) or per triangle (AUTO-NORMAL) and simply multiply it
into the vertex colors. The converse is our worst-case performance when
1) lighting is position dependent
2) we are in AUTO-NORMAL mode
because then we must calculate lighting per-face * per-vertex.
Each vertex has a different lighting contribution per face in
which it appears. Yuck.
Determining vertex position dependency:
If any of the following factors are TRUE then lighting is
vertex position dependent:
1) Any lights uses non-constant falloff
2) There are any point or spot lights
3) There is a light with specular color AND there is a
material with specular color
So worth noting is that default lighting (a no-falloff ambient
and a directional without specularity) is not position-dependent.
We should capitalize.
Simon Greenwold, April 2005
lights in class PGraphics3D
public void ambientLight(float r,
float g,
float b)
PGraphics3D
ambientLight in class PGraphics3D
public void ambientLight(float r,
float g,
float b,
float x,
float y,
float z)
PGraphics3D
ambientLight in class PGraphics3D
public void directionalLight(float r,
float g,
float b,
float nx,
float ny,
float nz)
directionalLight in class PGraphics3D
public void pointLight(float r,
float g,
float b,
float x,
float y,
float z)
pointLight in class PGraphics3D
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)
spotLight in class PGraphics3D
public void lightFalloff(float constant,
float linear,
float quadratic)
PGraphics3D
lightFalloff in class PGraphics3D
public void lightSpecular(float x,
float y,
float z)
PGraphics3D
lightSpecular in class PGraphics3Dpublic void glLightSpotAngle(int num)
public void glLightSpotConcentration(int num)
public void strokeJoin(int join)
strokeJoin in class PGraphics3Dpublic void strokeCap(int cap)
strokeCap in class PGraphics3Dpublic void ambient(int rgb)
ambient in class PGraphics3Dpublic void ambient(float gray)
ambient in class PGraphics3D
public void ambient(float x,
float y,
float z)
ambient in class PGraphics3Dpublic void specular(int rgb)
specular in class PGraphics3Dpublic void specular(float gray)
specular in class PGraphics3D
public void specular(float x,
float y,
float z)
specular in class PGraphics3Dpublic void emissive(int rgb)
emissive in class PGraphics3Dpublic void emissive(float gray)
emissive in class PGraphics3D
public void emissive(float x,
float y,
float z)
emissive in class PGraphics3Dpublic void shininess(float shine)
shininess in class PGraphics3Dpublic void background(PImage bgimage)
PGraphics3DNote that even if the image is set as RGB, the high 8 bits of each pixel must be set (0xFF000000), because the image data will be copied directly to the screen.
Also clears out the zbuffer and stencil buffer if they exist.
background in class PGraphics3Dpublic void clear()
PGraphics3D
public void smooth()
PImage
smooth in class PGraphics3Dpublic void noSmooth()
PImage
noSmooth in class PGraphics3Dpublic void loadPixels()
PImage
loadPixels in class PImagepublic void updatePixels()
PImage
updatePixels in class PImage
public int get(int x,
int y)
PImageIf 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.
get in class PImage
public PImage get(int x,
int y,
int w,
int h)
PImage
get in class PImagepublic PImage get()
PImage
get in class PImage
public void set(int x,
int y,
int argb)
PImage
set in class PImage
public void set(int x,
int y,
PImage source)
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.
set in class PImagepublic void filter(int kind)
filter in class PImage
public void filter(int kind,
float param)
filter in class PImage
public void copy(int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2)
copy in class PImage
public void copy(PImage src,
int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2)
copy in class PImage
public void blend(int sx,
int sy,
int dx,
int dy,
int mode)
public void blend(PImage src,
int sx,
int sy,
int dx,
int dy,
int mode)
public void blend(int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2,
int mode)
blend in class PImagePImage.blendColor(int,int,int)
public void blend(PImage src,
int sx1,
int sy1,
int sx2,
int sy2,
int dx1,
int dy1,
int dx2,
int dy2,
int mode)
blend in class PImagePImage.blendColor(int,int,int)public void save(java.lang.String filename)
PImageAs of revision 0100, this function requires an absolute path, in order to avoid confusion. To save inside the sketch folder, use the function savePath() from PApplet, or use saveFrame() instead. As of revision 0116, savePath() is not needed if this object has been created (as recommended) via createImage() or createGraphics() or one of its neighbors.
As of revision 0115, when using Java 1.4 and later, you can write
to several formats besides tga and tiff. If Java 1.4 is installed
and the extension used is supported (usually png, jpg, jpeg, bmp,
and tiff), then those methods will be used to write the image.
To get a list of the supported formats for writing, use:
println(javax.imageio.ImageIO.getReaderFormatNames())
To use the original built-in image writers, use .tga or .tif as the extension, or don't include an extension. When no extension is used, the extension .tif will be added to the file name.
The ImageIO API claims to support wbmp files, however they probably require a black and white image. Basic testing produced a zero-length file with no error.
save in class PImagepublic void report(java.lang.String where)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||