|
||||||||
| 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
public class PGraphics3D
Subclass of PGraphics that handles 3D rendering for Java 1.1. It can render 3D inside a browser window and requires no plug-ins.
The renderer is mostly set up based on the structure of the OpenGL API, if you have questions about specifics that aren't covered here, look for reference on the OpenGL implementation of a similar feature. Lighting and camera implementation by Simon Greenwold.
| Field Summary | |
|---|---|
PLine |
line
|
int |
shape_index
|
PTriangle |
triangle
|
| Fields inherited from class processing.core.PImage |
|---|
cache, format, height, imageMode, modified, mx1, mx2, my1, my2, parent, pixels, smooth, width |
| Constructor Summary | |
|---|---|
PGraphics3D(int iwidth,
int iheight,
PApplet parent)
Constructor for the PGraphics3 object. |
|
| 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 |
applyMatrix(float n00,
float n01,
float n02,
float n10,
float n11,
float n12)
Apply a 3x2 affine transformation matrix. |
void |
applyMatrix(float n00,
float n01,
float n02,
float n03,
float n10,
float n11,
float n12,
float n13,
float n20,
float n21,
float n22,
float n23,
float n30,
float n31,
float n32,
float n33)
Apply a 4x4 transformation matrix. |
void |
background(PImage image)
Takes an RGB or RGBA image and sets it as the background. |
void |
beginCamera()
Set matrix mode to the camera matrix (instead of the current transformation matrix). |
void |
beginDraw()
Prepares the PGraphics for drawing. |
void |
beginShape(int kind)
Start a new shape. |
void |
bezierVertex(float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
See notes with the bezier() function. |
void |
bezierVertex(float x2,
float y2,
float z2,
float x3,
float y3,
float z3,
float x4,
float y4,
float z4)
See notes with the bezier() function. |
void |
box(float size)
|
void |
box(float w,
float h,
float d)
|
void |
camera()
Set camera to the default settings. |
void |
camera(float eyeX,
float eyeY,
float eyeZ,
float centerX,
float centerY,
float centerZ,
float upX,
float upY,
float upZ)
More flexible method for dealing with camera(). |
void |
defaultSettings()
Set engine's default values. |
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 |
endCamera()
Record the current settings into the camera matrix, and set the matrix mode back to the current transformation matrix. |
void |
endDraw()
See notes in PGraphics. |
void |
endShape(int mode)
|
void |
frustum(float left,
float right,
float bottom,
float top,
float znear,
float zfar)
Same as glFrustum(), except that it wipes out (rather than multiplies against) the current perspective matrix. |
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 |
loadMatrix()
Load the modelview into m00, m01, et al so that it can be used. |
float |
modelX(float x,
float y,
float z)
Returns the model space x value for an x, y, z coordinate. |
float |
modelY(float x,
float y,
float z)
Returns the model space y value for an x, y, z coordinate. |
float |
modelZ(float x,
float y,
float z)
Returns the model space z value for an x, y, z coordinate. |
void |
noLights()
Turn off all lights. |
void |
normal(float nx,
float ny,
float nz)
Sets the current normal vector. |
void |
noSmooth()
Disable smoothing. |
void |
ortho()
Calls ortho() with the proper parameters for Processing's standard orthographic projection. |
void |
ortho(float left,
float right,
float bottom,
float top,
float near,
float far)
Similar to gluOrtho(), but wipes out the current projection matrix. |
void |
perspective()
Calls perspective() with Processing's standard coordinate projection. |
void |
perspective(float fov,
float aspect,
float zNear,
float zFar)
Similar to gluPerspective(). |
void |
point(float x,
float y)
End a path. |
void |
point(float x,
float y,
float z)
|
void |
pointLight(float r,
float g,
float b,
float x,
float y,
float z)
|
void |
popMatrix()
|
void |
printCamera()
Print the current camera matrix. |
void |
printMatrix()
Print the current model (or "transformation") matrix. |
void |
printProjection()
Print the current projection matrix. |
void |
pushMatrix()
|
void |
quad(float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
Compared to the implementation in PGraphics, this adds normal(). |
void |
resetMatrix()
Load identity as the transform/model matrix. |
void |
resize(int iwidth,
int iheight)
Called in repsonse to a resize event, handles setting the new width and height internally, as well as re-allocating the pixel buffer for the new size. |
void |
rotate(float angle)
Two dimensional rotation. |
void |
rotate(float angle,
float v0,
float v1,
float v2)
Rotate around an arbitrary vector, similar to glRotate(), except that it takes radians (instead of degrees). |
void |
rotateX(float angle)
|
void |
rotateY(float angle)
|
void |
rotateZ(float angle)
Rotate in the XY plane by an angle. |
void |
scale(float s)
Same as scale(s, s, s); |
void |
scale(float sx,
float sy)
Not recommended for use in 3D, because the z-dimension is just scaled by 1, since there's no way to know what else to scale it by. |
void |
scale(float x,
float y,
float z)
Scale in three dimensions. |
float |
screenX(float x,
float y)
Given an x and y coordinate, returns the x position of where that point would be placed on screen, once affected by translate(), scale(), or any other transformations. |
float |
screenX(float x,
float y,
float z)
Maps a three dimensional point to its placement on-screen. |
float |
screenY(float x,
float y)
Given an x and y coordinate, returns the y position of where that point would be placed on screen, once affected by translate(), scale(), or any other transformations. |
float |
screenY(float x,
float y,
float z)
Maps a three dimensional point to its placement on-screen. |
float |
screenZ(float x,
float y,
float z)
Maps a three dimensional point to its placement on-screen. |
void |
shininess(float shine)
|
void |
smooth()
If true in PImage, use bilinear interpolation for copy() operations. |
void |
specular(float gray)
|
void |
specular(float gray,
float alpha)
|
void |
specular(float x,
float y,
float z)
|
void |
specular(float x,
float y,
float z,
float a)
|
void |
specular(int rgb)
|
void |
sphere(float r)
Draw a sphere with radius r centered at coordinate 0, 0, 0. |
void |
sphereDetail(int res)
|
void |
sphereDetail(int ures,
int vres)
Set the detail level for approximating a sphere. |
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 |
texture(PImage image)
Set texture image for current shape. |
void |
translate(float tx,
float ty)
|
void |
translate(float tx,
float ty,
float tz)
|
void |
triangle(float x1,
float y1,
float x2,
float y2,
float x3,
float y3)
Compared to the implementation in PGraphics, this adds normal(). |
void |
vertex(float x,
float y)
|
void |
vertex(float x,
float y,
float z)
|
void |
vertex(float x,
float y,
float u,
float v)
|
void |
vertex(float x,
float y,
float z,
float u,
float v)
|
| Methods inherited from class processing.core.PImage |
|---|
blend, blend, blendColor, clone, copy, copy, filter, filter, get, get, get, imageMode, init, loadPixels, save, set, set, updatePixels, updatePixels |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public PLine line
public PTriangle triangle
public int shape_index
| Constructor Detail |
|---|
public PGraphics3D(int iwidth,
int iheight,
PApplet parent)
iwidth - viewport widthiheight - viewport height| Method Detail |
|---|
public void resize(int iwidth,
int iheight)
resize in class PGraphicspublic void beginDraw()
PGraphics
beginDraw in class PGraphicspublic void endDraw()
endDraw in class PGraphicspublic void defaultSettings()
PGraphics
public void beginShape(int kind)
PGraphicsDifferences between beginShape() and line() and point() methods.
beginShape() is intended to be more flexible at the expense of being a little more complicated to use. it handles more complicated shapes that can consist of many connected lines (so you get joins) or lines mixed with curves.
The line() and point() command are for the far more common cases (particularly for our audience) that simply need to draw a line or a point on the screen.
From the code side of things, line() may or may not call beginShape() to do the drawing. In the beta code, they do, but in the alpha code, they did not. they might be implemented one way or the other depending on tradeoffs of runtime efficiency vs. implementation efficiency &mdash meaning the speed that things run at vs. the speed it takes me to write the code and maintain it. for beta, the latter is most important so that's how things are implemented.
beginShape in class PGraphics
public void normal(float nx,
float ny,
float nz)
normal in class PGraphicspublic void texture(PImage image)
PGraphics
texture in class PGraphicsimage - reference to a PImage object
public void vertex(float x,
float y)
vertex in class PGraphics
public void vertex(float x,
float y,
float u,
float v)
vertex in class PGraphics
public void vertex(float x,
float y,
float z)
vertex in class PGraphics
public void vertex(float x,
float y,
float z,
float u,
float v)
vertex in class PGraphics
public void bezierVertex(float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
bezierVertex in class PGraphics
public void bezierVertex(float x2,
float y2,
float z2,
float x3,
float y3,
float z3,
float x4,
float y4,
float z4)
bezierVertex in class PGraphicspublic void endShape(int mode)
endShape in class PGraphics
public void point(float x,
float y)
PGraphics
point in class PGraphics
public void point(float x,
float y,
float z)
point in class PGraphics
public void triangle(float x1,
float y1,
float x2,
float y2,
float x3,
float y3)
triangle in class PGraphics
public void quad(float x1,
float y1,
float x2,
float y2,
float x3,
float y3,
float x4,
float y4)
quad in class PGraphicspublic void box(float size)
box in class PGraphics
public void box(float w,
float h,
float d)
box in class PGraphicspublic void sphereDetail(int res)
sphereDetail in class PGraphics
public void sphereDetail(int ures,
int vres)
sphereDetail in class PGraphicspublic void sphere(float r)
Implementation notes:
cache all the points of the sphere in a static array top and bottom are just a bunch of triangles that land in the center point
sphere is a series of concentric circles who radii vary along the shape, based on, er.. cos or something
[toxi 031031] new sphere code. removed all multiplies with radius, as scale() will take care of that anyway [toxi 031223] updated sphere code (removed modulos) and introduced sphereAt(x,y,z,r) to avoid additional translate()'s on the user/sketch side [davbol 080801] now using separate sphereDetailU/V
sphere in class PGraphics
public void translate(float tx,
float ty)
translate in class PGraphics
public void translate(float tx,
float ty,
float tz)
translate in class PGraphicspublic void rotate(float angle)
rotate in class PGraphicspublic void rotateX(float angle)
rotateX in class PGraphicspublic void rotateY(float angle)
rotateY in class PGraphicspublic void rotateZ(float angle)
rotateZ in class PGraphics
public void rotate(float angle,
float v0,
float v1,
float v2)
rotate in class PGraphicspublic void scale(float s)
scale in class PGraphics
public void scale(float sx,
float sy)
scale in class PGraphics
public void scale(float x,
float y,
float z)
scale in class PGraphicspublic void pushMatrix()
pushMatrix in class PGraphicspublic void popMatrix()
popMatrix in class PGraphicspublic void resetMatrix()
resetMatrix in class PGraphics
public void applyMatrix(float n00,
float n01,
float n02,
float n10,
float n11,
float n12)
PGraphics
applyMatrix in class PGraphics
public void applyMatrix(float n00,
float n01,
float n02,
float n03,
float n10,
float n11,
float n12,
float n13,
float n20,
float n21,
float n22,
float n23,
float n30,
float n31,
float n32,
float n33)
applyMatrix in class PGraphicspublic void loadMatrix()
loadMatrix in class PGraphicspublic void printMatrix()
printMatrix in class PGraphicspublic void beginCamera()
Note that the camera matrix is *not* the perspective matrix, it is in front of the modelview matrix (hence the name "model" and "view" for that matrix).
beginCamera() specifies that all coordinate transforms until endCamera() should be pre-applied in inverse to the camera transform matrix. Note that this is only challenging when a user specifies an arbitrary matrix with applyMatrix(). Then that matrix will need to be inverted, which may not be possible. But take heart, if a user is applying a non-invertible matrix to the camera transform, then he is clearly up to no good, and we can wash our hands of those bad intentions.
begin/endCamera clauses do not automatically reset the camera transform matrix. That's because we set up a nice default camera transform int setup(), and we expect it to hold through draw(). So we don't reset the camera transform matrix at the top of draw(). That means that an innocuous-looking clause like
beginCamera(); translate(0, 0, 10); endCamera();at the top of draw(), will result in a runaway camera that shoots infinitely out of the screen over time. In order to prevent this, it is necessary to call some function that does a hard reset of the camera transform matrix inside of begin/endCamera. Two options are
camera(); // sets up the nice default camera transform resetMatrix(); // sets up the identity camera transformSo to rotate a camera a constant amount, you might try
beginCamera(); camera(); rotateY(PI/8); endCamera();
beginCamera in class PGraphicspublic void endCamera()
Note that this will destroy any settings to scale(), translate(), or whatever, because the final camera matrix will be copied (not multiplied) into the modelview.
endCamera in class PGraphicspublic void camera()
Processing camera behavior:
Camera behavior can be split into two separate components, camera transformation, and projection. The transformation corresponds to the physical location, orientation, and scale of the camera. In a physical camera metaphor, this is what can manipulated by handling the camera body (with the exception of scale, which doesn't really have a physcial analog). The projection corresponds to what can be changed by manipulating the lens.
We maintain separate matrices to represent the camera transform and projection. An important distinction between the two is that the camera transform should be invertible, where the projection matrix should not, since it serves to map three dimensions to two. It is possible to bake the two matrices into a single one just by multiplying them together, but it isn't a good idea, since lighting, z-ordering, and z-buffering all demand a true camera z coordinate after modelview and camera transforms have been applied but before projection. If the camera transform and projection are combined there is no way to recover a good camera-space z-coordinate from a model coordinate.
Fortunately, there are no functions that manipulate both camera transformation and projection.
camera() sets the camera position, orientation, and center of the scene. It replaces the camera transform with a new one. This is different from gluLookAt(), but I think the only reason that GLU's lookat doesn't fully replace the camera matrix with the new one, but instead multiplies it, is that GL doesn't enforce the separation of camera transform and projection, so it wouldn't be safe (you'd probably stomp your projection).
The transformation functions are the same ones used to manipulate the modelview matrix (scale, translate, rotate, etc.). But they are bracketed with beginCamera(), endCamera() to indicate that they should apply (in inverse), to the camera transformation matrix.
This differs considerably from camera transformation in OpenGL. OpenGL only lets you say, apply everything from here out to the projection or modelview matrix. This makes it very hard to treat camera manipulation as if it were a physical camera. Imagine that you want to move your camera 100 units forward. In OpenGL, you need to apply the inverse of that transformation or else you'll move your scene 100 units forward--whether or not you've specified modelview or projection matrix. Remember they're just multiplied by model coods one after another. So in order to treat a camera like a physical camera, it is necessary to pre-apply inverse transforms to a matrix that will be applied to model coordinates. OpenGL provides nothing of this sort, but Processing does! This is the camera transform matrix.
camera in class PGraphics
public void camera(float eyeX,
float eyeY,
float eyeZ,
float centerX,
float centerY,
float centerZ,
float upX,
float upY,
float upZ)
The actual call is like gluLookat. Here's the real skinny on what does what:
camera(); or camera(ex, ey, ez, cx, cy, cz, ux, uy, uz);do not need to be called from with beginCamera();/endCamera(); That's because they always apply to the camera transformation, and they always totally replace it. That means that any coordinate transforms done before camera(); in draw() will be wiped out. It also means that camera() always operates in untransformed world coordinates. Therefore it is always redundant to call resetMatrix(); before camera(); This isn't technically true of gluLookat, but it's pretty much how it's used.
Now, beginCamera(); and endCamera(); are useful if you want to move the camera around using transforms like translate(), etc. They will wipe out any coordinate system transforms that occur before them in draw(), but they will not automatically wipe out the camera transform. This means that they should be at the top of draw(). It also means that the following:
beginCamera(); rotateY(PI/8); endCamera();will result in a camera that spins without stopping. If you want to just rotate a small constant amount, try this:
beginCamera(); camera(); // sets up the default view rotateY(PI/8); endCamera();That will rotate a little off of the default view. Note that this is entirely equivalent to
camera(); // sets up the default view beginCamera(); rotateY(PI/8); endCamera();because camera() doesn't care whether or not it's inside a begin/end clause. Basically it's safe to use camera() or camera(ex, ey, ez, cx, cy, cz, ux, uy, uz) as naked calls because they do all the matrix resetting automatically.
camera in class PGraphicspublic void printCamera()
printCamera in class PGraphicspublic void ortho()
ortho in class PGraphics
public void ortho(float left,
float right,
float bottom,
float top,
float near,
float far)
Implementation partially based on Mesa's matrix.c.
ortho in class PGraphicspublic void perspective()
Projection functions:
This behavior is pretty much familiar from OpenGL, except where functions replace matrices, rather than multiplying against the previous.
perspective in class PGraphics
public void perspective(float fov,
float aspect,
float zNear,
float zFar)
perspective in class PGraphics
public void frustum(float left,
float right,
float bottom,
float top,
float znear,
float zfar)
Implementation based on the explanation in the OpenGL blue book.
frustum in class PGraphicspublic void printProjection()
printProjection in class PGraphics
public float screenX(float x,
float y)
PGraphics
screenX in class PGraphics
public float screenY(float x,
float y)
PGraphics
screenY in class PGraphics
public float screenX(float x,
float y,
float z)
PGraphicsGiven an (x, y, z) coordinate, returns the x position of where that point would be placed on screen, once affected by translate(), scale(), or any other transformations.
screenX in class PGraphics
public float screenY(float x,
float y,
float z)
PGraphicsGiven an (x, y, z) coordinate, returns the y position of where that point would be placed on screen, once affected by translate(), scale(), or any other transformations.
screenY in class PGraphics
public float screenZ(float x,
float y,
float z)
PGraphicsGiven an (x, y, z) coordinate, returns its z value. This value can be used to determine if an (x, y, z) coordinate is in front or in back of another (x, y, z) coordinate. The units are based on how the zbuffer is set up, and don't relate to anything "real". They're only useful for in comparison to another value obtained from screenZ(), or directly out of the zbuffer[].
screenZ in class PGraphics
public float modelX(float x,
float y,
float z)
PGraphicsThis will give you a coordinate after it has been transformed by translate(), rotate(), and camera(), but not yet transformed by the projection matrix. For instance, his can be useful for figuring out how points in 3D space relate to the edge coordinates of a shape.
modelX in class PGraphics
public float modelY(float x,
float y,
float z)
PGraphics
modelY in class PGraphics
public float modelZ(float x,
float y,
float z)
PGraphics
modelZ in class PGraphicspublic void strokeJoin(int join)
strokeJoin in class PGraphicspublic void strokeCap(int cap)
strokeCap in class PGraphicspublic void ambient(int rgb)
ambient in class PGraphicspublic void ambient(float gray)
ambient in class PGraphics
public void ambient(float x,
float y,
float z)
ambient in class PGraphicspublic void specular(int rgb)
specular in class PGraphicspublic void specular(float gray)
specular in class PGraphics
public void specular(float gray,
float alpha)
specular in class PGraphics
public void specular(float x,
float y,
float z)
specular in class PGraphics
public void specular(float x,
float y,
float z,
float a)
specular in class PGraphicspublic void shininess(float shine)
shininess in class PGraphics