mirror of
https://github.com/processing/processing4.git
synced 2026-02-02 21:29:17 +01:00
adding the old examples to the new opengl lib
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<project name="Processing OpenGL2 Library" default="build">
|
||||
<project name="Processing OpenGL Library" default="build">
|
||||
|
||||
<target name="clean" description="Clean the build directories">
|
||||
<delete dir="bin" />
|
||||
<delete file="library/opengl2.jar" />
|
||||
<delete file="library/opengl.jar" />
|
||||
</target>
|
||||
|
||||
<target name="compile" description="Compile sources">
|
||||
@@ -20,7 +20,7 @@
|
||||
classpath="../../../core/core.jar; library/jogl.all.jar; library/nativewindow.all.jar;" />
|
||||
</target>
|
||||
|
||||
<target name="build" depends="compile" description="Build OpenGL2 library">
|
||||
<jar basedir="bin" destfile="library/opengl2.jar" />
|
||||
<target name="build" depends="compile" description="Build OpenGL library">
|
||||
<jar basedir="bin" destfile="library/opengl.jar" />
|
||||
</target>
|
||||
</project>
|
||||
|
||||
87
java/libraries/opengl/examples/Esfera/Esfera.pde
Normal file
87
java/libraries/opengl/examples/Esfera/Esfera.pde
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Esfera
|
||||
* by David Pena.
|
||||
*
|
||||
* Distribucion aleatoria uniforme sobre la superficie de una esfera.
|
||||
*/
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
int cuantos = 8000;
|
||||
pelo[] lista ;
|
||||
float[] z = new float[cuantos];
|
||||
float[] phi = new float[cuantos];
|
||||
float[] largos = new float[cuantos];
|
||||
float radio = 200;
|
||||
float rx = 0;
|
||||
float ry =0;
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
radio = height/3.5;
|
||||
|
||||
lista = new pelo[cuantos];
|
||||
for (int i=0; i<cuantos; i++){
|
||||
lista[i] = new pelo();
|
||||
}
|
||||
noiseDetail(3);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
translate(width/2,height/2);
|
||||
|
||||
float rxp = ((mouseX-(width/2))*0.005);
|
||||
float ryp = ((mouseY-(height/2))*0.005);
|
||||
rx = (rx*0.9)+(rxp*0.1);
|
||||
ry = (ry*0.9)+(ryp*0.1);
|
||||
rotateY(rx);
|
||||
rotateX(ry);
|
||||
fill(0);
|
||||
noStroke();
|
||||
sphere(radio);
|
||||
|
||||
for (int i=0;i<cuantos;i++){
|
||||
lista[i].dibujar();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class pelo
|
||||
{
|
||||
float z = random(-radio,radio);
|
||||
float phi = random(TWO_PI);
|
||||
float largo = random(1.15,1.2);
|
||||
float theta = asin(z/radio);
|
||||
|
||||
void dibujar(){
|
||||
|
||||
float off = (noise(millis() * 0.0005,sin(phi))-0.5) * 0.3;
|
||||
float offb = (noise(millis() * 0.0007,sin(z) * 0.01)-0.5) * 0.3;
|
||||
|
||||
float thetaff = theta+off;
|
||||
float phff = phi+offb;
|
||||
float x = radio * cos(theta) * cos(phi);
|
||||
float y = radio * cos(theta) * sin(phi);
|
||||
float z = radio * sin(theta);
|
||||
float msx= screenX(x,y,z);
|
||||
float msy= screenY(x,y,z);
|
||||
|
||||
float xo = radio * cos(thetaff) * cos(phff);
|
||||
float yo = radio * cos(thetaff) * sin(phff);
|
||||
float zo = radio * sin(thetaff);
|
||||
|
||||
float xb = xo * largo;
|
||||
float yb = yo * largo;
|
||||
float zb = zo * largo;
|
||||
|
||||
beginShape(LINES);
|
||||
stroke(0);
|
||||
vertex(x,y,z);
|
||||
stroke(200,150);
|
||||
vertex(xb,yb,zb);
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
52
java/libraries/opengl/examples/ExtrusionGL/ExtrusionGL.pde
Normal file
52
java/libraries/opengl/examples/ExtrusionGL/ExtrusionGL.pde
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Extrusion.
|
||||
*
|
||||
* Converts a flat image into spatial data points and rotates the points
|
||||
* around the center.
|
||||
*/
|
||||
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
PImage a;
|
||||
boolean onetime = true;
|
||||
int[][] aPixels;
|
||||
int[][] values;
|
||||
float angle;
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
|
||||
aPixels = new int[width][height];
|
||||
values = new int[width][height];
|
||||
noFill();
|
||||
|
||||
// Load the image into a new array
|
||||
// Extract the values and store in an array
|
||||
a = loadImage("ystone08.jpg");
|
||||
a.loadPixels();
|
||||
for (int i = 0; i < a.height; i++) {
|
||||
for (int j = 0; j < a.width; j++) {
|
||||
aPixels[j][i] = a.pixels[i*a.width + j];
|
||||
values[j][i] = int(blue(aPixels[j][i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
translate(width/2, height/2, 0);
|
||||
scale(2.0);
|
||||
|
||||
// Update and constrain the angle
|
||||
angle += 0.005;
|
||||
rotateY(angle);
|
||||
|
||||
// Display the image mass
|
||||
for (int i = 0; i < a.height; i += 2) {
|
||||
for (int j = 0; j < a.width; j += 2) {
|
||||
stroke(values[j][i], 153);
|
||||
line(j-a.width/2, i-a.height/2, -values[j][i], j-a.width/2, i-a.height/2, -values[j][i]-10);
|
||||
}
|
||||
}
|
||||
}
|
||||
165
java/libraries/opengl/examples/Geometry/Geometry.pde
Normal file
165
java/libraries/opengl/examples/Geometry/Geometry.pde
Normal file
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Geometry
|
||||
* by Marius Watz.
|
||||
*
|
||||
* Using sin/cos lookup tables, blends colors, and draws a series of
|
||||
* rotating arcs on the screen.
|
||||
*/
|
||||
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
// Trig lookup tables borrowed from Toxi; cryptic but effective.
|
||||
float sinLUT[];
|
||||
float cosLUT[];
|
||||
float SINCOS_PRECISION=1.0;
|
||||
int SINCOS_LENGTH= int((360.0/SINCOS_PRECISION));
|
||||
|
||||
// System data
|
||||
boolean dosave=false;
|
||||
int num;
|
||||
float pt[];
|
||||
int style[];
|
||||
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
background(255);
|
||||
|
||||
// Fill the tables
|
||||
sinLUT=new float[SINCOS_LENGTH];
|
||||
cosLUT=new float[SINCOS_LENGTH];
|
||||
for (int i = 0; i < SINCOS_LENGTH; i++) {
|
||||
sinLUT[i]= (float)Math.sin(i*DEG_TO_RAD*SINCOS_PRECISION);
|
||||
cosLUT[i]= (float)Math.cos(i*DEG_TO_RAD*SINCOS_PRECISION);
|
||||
}
|
||||
|
||||
num = 150;
|
||||
pt = new float[6*num]; // rotx, roty, deg, rad, w, speed
|
||||
style = new int[2*num]; // color, render style
|
||||
|
||||
// Set up arc shapes
|
||||
int index=0;
|
||||
float prob;
|
||||
for (int i=0; i<num; i++) {
|
||||
pt[index++] = random(PI*2); // Random X axis rotation
|
||||
pt[index++] = random(PI*2); // Random Y axis rotation
|
||||
|
||||
pt[index++] = random(60,80); // Short to quarter-circle arcs
|
||||
if(random(100)>90) pt[index]=(int)random(8,27)*10;
|
||||
|
||||
pt[index++] = int(random(2,50)*5); // Radius. Space them out nicely
|
||||
|
||||
pt[index++] = random(4,32); // Width of band
|
||||
if(random(100)>90) pt[index]=random(40,60); // Width of band
|
||||
|
||||
pt[index++] = radians(random(5,30))/5; // Speed of rotation
|
||||
|
||||
// get colors
|
||||
prob = random(100);
|
||||
if(prob<30) style[i*2]=colorBlended(random(1), 255,0,100, 255,0,0, 210);
|
||||
else if(prob<70) style[i*2]=colorBlended(random(1), 0,153,255, 170,225,255, 210);
|
||||
else if(prob<90) style[i*2]=colorBlended(random(1), 200,255,0, 150,255,0, 210);
|
||||
else style[i*2]=color(255,255,255, 220);
|
||||
|
||||
if(prob<50) style[i*2]=colorBlended(random(1), 200,255,0, 50,120,0, 210);
|
||||
else if(prob<90) style[i*2]=colorBlended(random(1), 255,100,0, 255,255,0, 210);
|
||||
else style[i*2]=color(255,255,255, 220);
|
||||
|
||||
style[i*2+1]=(int)(random(100))%3;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
background(0);
|
||||
|
||||
int index=0;
|
||||
translate(width/2, height/2, 0);
|
||||
rotateX(PI/6);
|
||||
rotateY(PI/6);
|
||||
|
||||
for (int i = 0; i < num; i++) {
|
||||
pushMatrix();
|
||||
|
||||
rotateX(pt[index++]);
|
||||
rotateY(pt[index++]);
|
||||
|
||||
if(style[i*2+1]==0) {
|
||||
stroke(style[i*2]);
|
||||
noFill();
|
||||
strokeWeight(1);
|
||||
arcLine(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
else if(style[i*2+1]==1) {
|
||||
fill(style[i*2]);
|
||||
noStroke();
|
||||
arcLineBars(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
else {
|
||||
fill(style[i*2]);
|
||||
noStroke();
|
||||
arc(0,0, pt[index++],pt[index++],pt[index++]);
|
||||
}
|
||||
|
||||
// increase rotation
|
||||
pt[index-5]+=pt[index]/10;
|
||||
pt[index-4]+=pt[index++]/20;
|
||||
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get blend of two colors
|
||||
int colorBlended(float fract,
|
||||
float r, float g, float b,
|
||||
float r2, float g2, float b2, float a) {
|
||||
|
||||
r2 = (r2 - r);
|
||||
g2 = (g2 - g);
|
||||
b2 = (b2 - b);
|
||||
return color(r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
|
||||
}
|
||||
|
||||
|
||||
// Draw arc line
|
||||
void arcLine(float x,float y,float deg,float rad,float w) {
|
||||
int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
|
||||
int numlines=(int)(w/2);
|
||||
|
||||
for (int j=0; j<numlines; j++) {
|
||||
beginShape();
|
||||
for (int i=0; i<a; i++) {
|
||||
vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
|
||||
}
|
||||
endShape();
|
||||
rad += 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw arc line with bars
|
||||
void arcLineBars(float x,float y,float deg,float rad,float w) {
|
||||
int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
|
||||
a /= 4;
|
||||
|
||||
beginShape(QUADS);
|
||||
for (int i=0; i<a; i+=4) {
|
||||
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
|
||||
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
|
||||
vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
|
||||
vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
|
||||
// Draw solid arc
|
||||
void arc(float x,float y,float deg,float rad,float w) {
|
||||
int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
|
||||
beginShape(QUAD_STRIP);
|
||||
for (int i = 0; i < a; i++) {
|
||||
vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
|
||||
vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
48
java/libraries/opengl/examples/LightsGL/LightsGL.pde
Normal file
48
java/libraries/opengl/examples/LightsGL/LightsGL.pde
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* LightsGL.
|
||||
* Modified from an example by Simon Greenwold.
|
||||
*
|
||||
* Display a box with three different kinds of lights.
|
||||
*/
|
||||
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(1024, 768, OPENGL);
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
defineLights();
|
||||
background(0);
|
||||
|
||||
for (int x = 0; x <= width; x += 100) {
|
||||
for (int y = 0; y <= height; y += 100) {
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
rotateY(map(mouseX, 0, width, 0, PI));
|
||||
rotateX(map(mouseY, 0, height, 0, PI));
|
||||
box(90);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void defineLights() {
|
||||
// Orange point light on the right
|
||||
pointLight(150, 100, 0, // Color
|
||||
200, -150, 0); // Position
|
||||
|
||||
// Blue directional light from the left
|
||||
directionalLight(0, 102, 255, // Color
|
||||
1, 0, 0); // The x-, y-, z-axis direction
|
||||
|
||||
// Yellow spotlight from the front
|
||||
spotLight(255, 255, 109, // Color
|
||||
0, 40, 200, // Position
|
||||
0, -0.5, -0.5, // Direction
|
||||
PI / 2, 2); // Angle, concentration
|
||||
}
|
||||
67
java/libraries/opengl/examples/SpaceJunk/Cube.pde
Normal file
67
java/libraries/opengl/examples/SpaceJunk/Cube.pde
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
class Cube {
|
||||
|
||||
// Properties
|
||||
int w, h, d;
|
||||
int shiftX, shiftY, shiftZ;
|
||||
|
||||
// Constructor
|
||||
Cube(int w, int h, int d, int shiftX, int shiftY, int shiftZ){
|
||||
this.w = w;
|
||||
this.h = h;
|
||||
this.d = d;
|
||||
this.shiftX = shiftX;
|
||||
this.shiftY = shiftY;
|
||||
this.shiftZ = shiftZ;
|
||||
}
|
||||
|
||||
// Main cube drawing method, which looks
|
||||
// more confusing than it really is. It's
|
||||
// just a bunch of rectangles drawn for
|
||||
// each cube face
|
||||
void drawCube(){
|
||||
beginShape(QUADS);
|
||||
// Front face
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Back face
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
|
||||
// Left face
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Right face
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
|
||||
// Top face
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, -h/2 + shiftY, d + shiftZ);
|
||||
|
||||
// Bottom face
|
||||
vertex(-w/2 + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, -d/2 + shiftZ);
|
||||
vertex(w + shiftX, h + shiftY, d + shiftZ);
|
||||
vertex(-w/2 + shiftX, h + shiftY, d + shiftZ);
|
||||
|
||||
endShape();
|
||||
|
||||
// Add some rotation to each box for pizazz.
|
||||
rotateY(radians(1));
|
||||
rotateX(radians(1));
|
||||
rotateZ(radians(1));
|
||||
}
|
||||
}
|
||||
65
java/libraries/opengl/examples/SpaceJunk/SpaceJunk.pde
Normal file
65
java/libraries/opengl/examples/SpaceJunk/SpaceJunk.pde
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Space Junk
|
||||
* by Ira Greenberg.
|
||||
* Zoom suggestion
|
||||
* by Danny Greenberg.
|
||||
*
|
||||
* Rotating cubes in space using a custom Cube class.
|
||||
* Color controlled by light sources. Move the mouse left
|
||||
* and right to zoom.
|
||||
*/
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
// Used for oveall rotation
|
||||
float ang;
|
||||
|
||||
// Cube count-lower/raise to test P3D/OPENGL performance
|
||||
int limit = 500;
|
||||
|
||||
// Array for all cubes
|
||||
Cube[]cubes = new Cube[limit];
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
background(0);
|
||||
noStroke();
|
||||
|
||||
// Instantiate cubes, passing in random vals for size and postion
|
||||
for (int i = 0; i< cubes.length; i++){
|
||||
cubes[i] = new Cube(int(random(-10, 10)), int(random(-10, 10)),
|
||||
int(random(-10, 10)), int(random(-140, 140)), int(random(-140, 140)),
|
||||
int(random(-140, 140)));
|
||||
}
|
||||
}
|
||||
|
||||
void draw(){
|
||||
background(0);
|
||||
fill(200);
|
||||
|
||||
// Set up some different colored lights
|
||||
pointLight(51, 102, 255, 65, 60, 100);
|
||||
pointLight(200, 40, 60, -65, -60, -150);
|
||||
|
||||
// Raise overall light in scene
|
||||
ambientLight(70, 70, 10);
|
||||
|
||||
// Center geometry in display windwow.
|
||||
// you can change 3rd argument ('0')
|
||||
// to move block group closer(+)/further(-)
|
||||
translate(width/2, height/2, -200 + mouseX * 0.65);
|
||||
|
||||
// Rotate around y and x axes
|
||||
rotateY(radians(ang));
|
||||
rotateX(radians(ang));
|
||||
|
||||
// Draw cubes
|
||||
for (int i = 0; i < cubes.length; i++){
|
||||
cubes[i].drawCube();
|
||||
}
|
||||
|
||||
// Used in rotate function calls above
|
||||
ang++;
|
||||
}
|
||||
|
||||
|
||||
174
java/libraries/opengl/examples/TexturedSphere/TexturedSphere.pde
Normal file
174
java/libraries/opengl/examples/TexturedSphere/TexturedSphere.pde
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Textured Sphere
|
||||
* by Mike 'Flux' Chang (cleaned up by Aaron Koblin).
|
||||
* Based on code by Toxi.
|
||||
*
|
||||
* A 3D textured sphere with simple rotation control.
|
||||
* Note: Controls will be inverted when sphere is upside down.
|
||||
* Use an "arc ball" to deal with this appropriately.
|
||||
*/
|
||||
|
||||
import processing.opengl.*;
|
||||
|
||||
PImage bg;
|
||||
PImage texmap;
|
||||
|
||||
int sDetail = 35; // Sphere detail setting
|
||||
float rotationX = 0;
|
||||
float rotationY = 0;
|
||||
float velocityX = 0;
|
||||
float velocityY = 0;
|
||||
float globeRadius = 450;
|
||||
float pushBack = 0;
|
||||
|
||||
float[] cx, cz, sphereX, sphereY, sphereZ;
|
||||
float sinLUT[];
|
||||
float cosLUT[];
|
||||
float SINCOS_PRECISION = 0.5;
|
||||
int SINCOS_LENGTH = int(360.0 / SINCOS_PRECISION);
|
||||
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
texmap = loadImage("world32k.jpg");
|
||||
initializeSphere(sDetail);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
renderGlobe();
|
||||
}
|
||||
|
||||
void renderGlobe() {
|
||||
pushMatrix();
|
||||
translate(width/2.0, height/2.0, pushBack);
|
||||
pushMatrix();
|
||||
noFill();
|
||||
stroke(255,200);
|
||||
strokeWeight(2);
|
||||
smooth();
|
||||
popMatrix();
|
||||
lights();
|
||||
pushMatrix();
|
||||
rotateX( radians(-rotationX) );
|
||||
rotateY( radians(270 - rotationY) );
|
||||
fill(200);
|
||||
noStroke();
|
||||
textureMode(IMAGE);
|
||||
texturedSphere(globeRadius, texmap);
|
||||
popMatrix();
|
||||
popMatrix();
|
||||
rotationX += velocityX;
|
||||
rotationY += velocityY;
|
||||
velocityX *= 0.95;
|
||||
velocityY *= 0.95;
|
||||
|
||||
// Implements mouse control (interaction will be inverse when sphere is upside down)
|
||||
if(mousePressed){
|
||||
velocityX += (mouseY-pmouseY) * 0.01;
|
||||
velocityY -= (mouseX-pmouseX) * 0.01;
|
||||
}
|
||||
}
|
||||
|
||||
void initializeSphere(int res)
|
||||
{
|
||||
sinLUT = new float[SINCOS_LENGTH];
|
||||
cosLUT = new float[SINCOS_LENGTH];
|
||||
|
||||
for (int i = 0; i < SINCOS_LENGTH; i++) {
|
||||
sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
|
||||
cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
|
||||
}
|
||||
|
||||
float delta = (float)SINCOS_LENGTH/res;
|
||||
float[] cx = new float[res];
|
||||
float[] cz = new float[res];
|
||||
|
||||
// Calc unit circle in XZ plane
|
||||
for (int i = 0; i < res; i++) {
|
||||
cx[i] = -cosLUT[(int) (i*delta) % SINCOS_LENGTH];
|
||||
cz[i] = sinLUT[(int) (i*delta) % SINCOS_LENGTH];
|
||||
}
|
||||
|
||||
// Computing vertexlist vertexlist starts at south pole
|
||||
int vertCount = res * (res-1) + 2;
|
||||
int currVert = 0;
|
||||
|
||||
// Re-init arrays to store vertices
|
||||
sphereX = new float[vertCount];
|
||||
sphereY = new float[vertCount];
|
||||
sphereZ = new float[vertCount];
|
||||
float angle_step = (SINCOS_LENGTH*0.5f)/res;
|
||||
float angle = angle_step;
|
||||
|
||||
// Step along Y axis
|
||||
for (int i = 1; i < res; i++) {
|
||||
float curradius = sinLUT[(int) angle % SINCOS_LENGTH];
|
||||
float currY = -cosLUT[(int) angle % SINCOS_LENGTH];
|
||||
for (int j = 0; j < res; j++) {
|
||||
sphereX[currVert] = cx[j] * curradius;
|
||||
sphereY[currVert] = currY;
|
||||
sphereZ[currVert++] = cz[j] * curradius;
|
||||
}
|
||||
angle += angle_step;
|
||||
}
|
||||
sDetail = res;
|
||||
}
|
||||
|
||||
// Generic routine to draw textured sphere
|
||||
void texturedSphere(float r, PImage t)
|
||||
{
|
||||
int v1,v11,v2;
|
||||
r = (r + 240 ) * 0.33;
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
float iu=(float)(t.width-1)/(sDetail);
|
||||
float iv=(float)(t.height-1)/(sDetail);
|
||||
float u=0,v=iv;
|
||||
for (int i = 0; i < sDetail; i++) {
|
||||
vertex(0, -r, 0,u,0);
|
||||
vertex(sphereX[i]*r, sphereY[i]*r, sphereZ[i]*r, u, v);
|
||||
u+=iu;
|
||||
}
|
||||
vertex(0, -r, 0,u,0);
|
||||
vertex(sphereX[0]*r, sphereY[0]*r, sphereZ[0]*r, u, v);
|
||||
endShape();
|
||||
|
||||
// Middle rings
|
||||
int voff = 0;
|
||||
for(int i = 2; i < sDetail; i++) {
|
||||
v1=v11=voff;
|
||||
voff += sDetail;
|
||||
v2=voff;
|
||||
u=0;
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
for (int j = 0; j < sDetail; j++) {
|
||||
vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1++]*r, u, v);
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2++]*r, u, v+iv);
|
||||
u+=iu;
|
||||
}
|
||||
|
||||
// Close each ring
|
||||
v1=v11;
|
||||
v2=voff;
|
||||
vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1]*r, u, v);
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v+iv);
|
||||
endShape();
|
||||
v+=iv;
|
||||
}
|
||||
u=0;
|
||||
|
||||
// Add the northern cap
|
||||
beginShape(TRIANGLE_STRIP);
|
||||
texture(t);
|
||||
for (int i = 0; i < sDetail; i++) {
|
||||
v2 = voff + i;
|
||||
vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v);
|
||||
vertex(0, r, 0,u,v+iv);
|
||||
u+=iu;
|
||||
}
|
||||
vertex(sphereX[voff]*r, sphereY[voff]*r, sphereZ[voff]*r, u, v);
|
||||
endShape();
|
||||
|
||||
}
|
||||
256
java/libraries/opengl/examples/Yellowtail/Gesture.pde
Normal file
256
java/libraries/opengl/examples/Yellowtail/Gesture.pde
Normal file
@@ -0,0 +1,256 @@
|
||||
class Gesture {
|
||||
|
||||
float damp = 5.0;
|
||||
float dampInv = 1.0 / damp;
|
||||
float damp1 = damp - 1;
|
||||
|
||||
int w;
|
||||
int h;
|
||||
int capacity;
|
||||
|
||||
Vec3f path[];
|
||||
int crosses[];
|
||||
Polygon polygons[];
|
||||
int nPoints;
|
||||
int nPolys;
|
||||
|
||||
float jumpDx, jumpDy;
|
||||
boolean exists;
|
||||
float INIT_TH = 14;
|
||||
float thickness = INIT_TH;
|
||||
|
||||
Gesture(int mw, int mh) {
|
||||
w = mw;
|
||||
h = mh;
|
||||
capacity = 600;
|
||||
path = new Vec3f[capacity];
|
||||
polygons = new Polygon[capacity];
|
||||
crosses = new int[capacity];
|
||||
for (int i=0;i<capacity;i++) {
|
||||
polygons[i] = new Polygon();
|
||||
polygons[i].npoints = 4;
|
||||
path[i] = new Vec3f();
|
||||
crosses[i] = 0;
|
||||
}
|
||||
nPoints = 0;
|
||||
nPolys = 0;
|
||||
|
||||
exists = false;
|
||||
jumpDx = 0;
|
||||
jumpDy = 0;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
nPoints = 0;
|
||||
exists = false;
|
||||
thickness = INIT_TH;
|
||||
}
|
||||
|
||||
void clearPolys() {
|
||||
nPolys = 0;
|
||||
}
|
||||
|
||||
void addPoint(float x, float y) {
|
||||
|
||||
if (nPoints >= capacity) {
|
||||
// there are all sorts of possible solutions here,
|
||||
// but for abject simplicity, I don't do anything.
|
||||
}
|
||||
else {
|
||||
float v = distToLast(x, y);
|
||||
float p = getPressureFromVelocity(v);
|
||||
path[nPoints++].set(x,y,p);
|
||||
|
||||
if (nPoints > 1) {
|
||||
exists = true;
|
||||
jumpDx = path[nPoints-1].x - path[0].x;
|
||||
jumpDy = path[nPoints-1].y - path[0].y;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float getPressureFromVelocity(float v) {
|
||||
final float scale = 18;
|
||||
final float minP = 0.02;
|
||||
final float oldP = (nPoints > 0) ? path[nPoints-1].p : 0;
|
||||
return ((minP + max(0, 1.0 - v/scale)) + (damp1*oldP))*dampInv;
|
||||
}
|
||||
|
||||
void setPressures() {
|
||||
// pressures vary from 0...1
|
||||
float pressure;
|
||||
Vec3f tmp;
|
||||
float t = 0;
|
||||
float u = 1.0 / (nPoints - 1)*TWO_PI;
|
||||
for (int i = 0; i < nPoints; i++) {
|
||||
pressure = sqrt((1.0 - cos(t))*0.5);
|
||||
path[i].p = pressure;
|
||||
t += u;
|
||||
}
|
||||
}
|
||||
|
||||
float distToLast(float ix, float iy) {
|
||||
if (nPoints > 0) {
|
||||
Vec3f v = path[nPoints-1];
|
||||
float dx = v.x - ix;
|
||||
float dy = v.y - iy;
|
||||
return mag(dx, dy);
|
||||
}
|
||||
else {
|
||||
return 30;
|
||||
}
|
||||
}
|
||||
|
||||
void compile() {
|
||||
// compute the polygons from the path of Vec3f's
|
||||
if (exists) {
|
||||
clearPolys();
|
||||
|
||||
Vec3f p0, p1, p2;
|
||||
float radius0, radius1;
|
||||
float ax, bx, cx, dx;
|
||||
float ay, by, cy, dy;
|
||||
int axi, bxi, cxi, dxi, axip, axid;
|
||||
int ayi, byi, cyi, dyi, ayip, ayid;
|
||||
float p1x, p1y;
|
||||
float dx01, dy01, hp01, si01, co01;
|
||||
float dx02, dy02, hp02, si02, co02;
|
||||
float dx13, dy13, hp13, si13, co13;
|
||||
float taper = 1.0;
|
||||
|
||||
int nPathPoints = nPoints - 1;
|
||||
int lastPolyIndex = nPathPoints - 1;
|
||||
float npm1finv = 1.0 / max(1, nPathPoints - 1);
|
||||
|
||||
// handle the first point
|
||||
p0 = path[0];
|
||||
p1 = path[1];
|
||||
radius0 = p0.p * thickness;
|
||||
dx01 = p1.x - p0.x;
|
||||
dy01 = p1.y - p0.y;
|
||||
hp01 = sqrt(dx01*dx01 + dy01*dy01);
|
||||
if (hp01 == 0) {
|
||||
hp02 = 0.0001;
|
||||
}
|
||||
co01 = radius0 * dx01 / hp01;
|
||||
si01 = radius0 * dy01 / hp01;
|
||||
ax = p0.x - si01;
|
||||
ay = p0.y + co01;
|
||||
bx = p0.x + si01;
|
||||
by = p0.y - co01;
|
||||
|
||||
int xpts[];
|
||||
int ypts[];
|
||||
|
||||
int LC = 20;
|
||||
int RC = w-LC;
|
||||
int TC = 20;
|
||||
int BC = h-TC;
|
||||
float mint = 0.618;
|
||||
float tapow = 0.4;
|
||||
|
||||
// handle the middle points
|
||||
int i = 1;
|
||||
Polygon apoly;
|
||||
for (i = 1; i < nPathPoints; i++) {
|
||||
taper = pow((lastPolyIndex-i)*npm1finv,tapow);
|
||||
|
||||
p0 = path[i-1];
|
||||
p1 = path[i ];
|
||||
p2 = path[i+1];
|
||||
p1x = p1.x;
|
||||
p1y = p1.y;
|
||||
radius1 = Math.max(mint,taper*p1.p*thickness);
|
||||
|
||||
// assumes all segments are roughly the same length...
|
||||
dx02 = p2.x - p0.x;
|
||||
dy02 = p2.y - p0.y;
|
||||
hp02 = (float) Math.sqrt(dx02*dx02 + dy02*dy02);
|
||||
if (hp02 != 0) {
|
||||
hp02 = radius1/hp02;
|
||||
}
|
||||
co02 = dx02 * hp02;
|
||||
si02 = dy02 * hp02;
|
||||
|
||||
// translate the integer coordinates to the viewing rectangle
|
||||
axi = axip = (int)ax;
|
||||
ayi = ayip = (int)ay;
|
||||
axi=(axi<0)?(w-((-axi)%w)):axi%w;
|
||||
axid = axi-axip;
|
||||
ayi=(ayi<0)?(h-((-ayi)%h)):ayi%h;
|
||||
ayid = ayi-ayip;
|
||||
|
||||
// set the vertices of the polygon
|
||||
apoly = polygons[nPolys++];
|
||||
xpts = apoly.xpoints;
|
||||
ypts = apoly.ypoints;
|
||||
xpts[0] = axi = axid + axip;
|
||||
xpts[1] = bxi = axid + (int) bx;
|
||||
xpts[2] = cxi = axid + (int)(cx = p1x + si02);
|
||||
xpts[3] = dxi = axid + (int)(dx = p1x - si02);
|
||||
ypts[0] = ayi = ayid + ayip;
|
||||
ypts[1] = byi = ayid + (int) by;
|
||||
ypts[2] = cyi = ayid + (int)(cy = p1y - co02);
|
||||
ypts[3] = dyi = ayid + (int)(dy = p1y + co02);
|
||||
|
||||
// keep a record of where we cross the edge of the screen
|
||||
crosses[i] = 0;
|
||||
if ((axi<=LC)||(bxi<=LC)||(cxi<=LC)||(dxi<=LC)) {
|
||||
crosses[i]|=1;
|
||||
}
|
||||
if ((axi>=RC)||(bxi>=RC)||(cxi>=RC)||(dxi>=RC)) {
|
||||
crosses[i]|=2;
|
||||
}
|
||||
if ((ayi<=TC)||(byi<=TC)||(cyi<=TC)||(dyi<=TC)) {
|
||||
crosses[i]|=4;
|
||||
}
|
||||
if ((ayi>=BC)||(byi>=BC)||(cyi>=BC)||(dyi>=BC)) {
|
||||
crosses[i]|=8;
|
||||
}
|
||||
|
||||
//swap data for next time
|
||||
ax = dx;
|
||||
ay = dy;
|
||||
bx = cx;
|
||||
by = cy;
|
||||
}
|
||||
|
||||
// handle the last point
|
||||
p2 = path[nPathPoints];
|
||||
apoly = polygons[nPolys++];
|
||||
xpts = apoly.xpoints;
|
||||
ypts = apoly.ypoints;
|
||||
|
||||
xpts[0] = (int)ax;
|
||||
xpts[1] = (int)bx;
|
||||
xpts[2] = (int)(p2.x);
|
||||
xpts[3] = (int)(p2.x);
|
||||
|
||||
ypts[0] = (int)ay;
|
||||
ypts[1] = (int)by;
|
||||
ypts[2] = (int)(p2.y);
|
||||
ypts[3] = (int)(p2.y);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void smooth() {
|
||||
// average neighboring points
|
||||
|
||||
final float weight = 18;
|
||||
final float scale = 1.0 / (weight + 2);
|
||||
int nPointsMinusTwo = nPoints - 2;
|
||||
Vec3f lower, upper, center;
|
||||
|
||||
for (int i = 1; i < nPointsMinusTwo; i++) {
|
||||
lower = path[i-1];
|
||||
center = path[i];
|
||||
upper = path[i+1];
|
||||
|
||||
center.x = (lower.x + weight*center.x + upper.x)*scale;
|
||||
center.y = (lower.y + weight*center.y + upper.y)*scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
java/libraries/opengl/examples/Yellowtail/Vec3f.pde
Normal file
19
java/libraries/opengl/examples/Yellowtail/Vec3f.pde
Normal file
@@ -0,0 +1,19 @@
|
||||
class Vec3f {
|
||||
float x;
|
||||
float y;
|
||||
float p; // Pressure
|
||||
|
||||
Vec3f() {
|
||||
set(0, 0, 0);
|
||||
}
|
||||
|
||||
Vec3f(float ix, float iy, float ip) {
|
||||
set(ix, iy, ip);
|
||||
}
|
||||
|
||||
void set(float ix, float iy, float ip) {
|
||||
x = ix;
|
||||
y = iy;
|
||||
p = ip;
|
||||
}
|
||||
}
|
||||
190
java/libraries/opengl/examples/Yellowtail/Yellowtail.pde
Normal file
190
java/libraries/opengl/examples/Yellowtail/Yellowtail.pde
Normal file
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* Yellowtail
|
||||
* by Golan Levin (www.flong.com).
|
||||
*
|
||||
* Click, drag, and release to create a kinetic gesture.
|
||||
*
|
||||
* Yellowtail (1998-2000) is an interactive software system for the gestural
|
||||
* creation and performance of real-time abstract animation. Yellowtail repeats
|
||||
* a user's strokes end-over-end, enabling simultaneous specification of a
|
||||
* line's shape and quality of movement. Each line repeats according to its
|
||||
* own period, producing an ever-changing and responsive display of lively,
|
||||
* worm-like textures.
|
||||
*/
|
||||
|
||||
|
||||
import processing.opengl.*;
|
||||
import java.awt.Polygon;
|
||||
|
||||
Gesture gestureArray[];
|
||||
final int nGestures = 36; // Number of gestures
|
||||
final int minMove = 3; // Minimum travel for a new point
|
||||
int currentGestureID;
|
||||
|
||||
Polygon tempP;
|
||||
int tmpXp[];
|
||||
int tmpYp[];
|
||||
|
||||
|
||||
void setup() {
|
||||
size(1024, 768, OPENGL);
|
||||
background(0, 0, 0);
|
||||
noStroke();
|
||||
|
||||
currentGestureID = -1;
|
||||
gestureArray = new Gesture[nGestures];
|
||||
for (int i = 0; i < nGestures; i++) {
|
||||
gestureArray[i] = new Gesture(width, height);
|
||||
}
|
||||
clearGestures();
|
||||
}
|
||||
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
|
||||
updateGeometry();
|
||||
fill(255, 255, 245);
|
||||
for (int i = 0; i < nGestures; i++) {
|
||||
renderGesture(gestureArray[i], width, height);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
currentGestureID = (currentGestureID+1) % nGestures;
|
||||
Gesture G = gestureArray[currentGestureID];
|
||||
G.clear();
|
||||
G.clearPolys();
|
||||
G.addPoint(mouseX, mouseY);
|
||||
}
|
||||
|
||||
|
||||
void mouseDragged() {
|
||||
if (currentGestureID >= 0) {
|
||||
Gesture G = gestureArray[currentGestureID];
|
||||
if (G.distToLast(mouseX, mouseY) > minMove) {
|
||||
G.addPoint(mouseX, mouseY);
|
||||
G.smooth();
|
||||
G.compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void keyPressed() {
|
||||
if (key == '+' || key == '=') {
|
||||
if (currentGestureID >= 0) {
|
||||
float th = gestureArray[currentGestureID].thickness;
|
||||
gestureArray[currentGestureID].thickness = min(96, th+1);
|
||||
gestureArray[currentGestureID].compile();
|
||||
}
|
||||
} else if (key == '-') {
|
||||
if (currentGestureID >= 0) {
|
||||
float th = gestureArray[currentGestureID].thickness;
|
||||
gestureArray[currentGestureID].thickness = max(2, th-1);
|
||||
gestureArray[currentGestureID].compile();
|
||||
}
|
||||
} else if (key == ' ') {
|
||||
clearGestures();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void renderGesture(Gesture gesture, int w, int h) {
|
||||
if (gesture.exists) {
|
||||
if (gesture.nPolys > 0) {
|
||||
Polygon polygons[] = gesture.polygons;
|
||||
int crosses[] = gesture.crosses;
|
||||
|
||||
int xpts[];
|
||||
int ypts[];
|
||||
Polygon p;
|
||||
int cr;
|
||||
|
||||
beginShape(QUADS);
|
||||
int gnp = gesture.nPolys;
|
||||
for (int i=0; i<gnp; i++) {
|
||||
|
||||
p = polygons[i];
|
||||
xpts = p.xpoints;
|
||||
ypts = p.ypoints;
|
||||
|
||||
vertex(xpts[0], ypts[0]);
|
||||
vertex(xpts[1], ypts[1]);
|
||||
vertex(xpts[2], ypts[2]);
|
||||
vertex(xpts[3], ypts[3]);
|
||||
|
||||
if ((cr = crosses[i]) > 0) {
|
||||
if ((cr & 3)>0) {
|
||||
vertex(xpts[0]+w, ypts[0]);
|
||||
vertex(xpts[1]+w, ypts[1]);
|
||||
vertex(xpts[2]+w, ypts[2]);
|
||||
vertex(xpts[3]+w, ypts[3]);
|
||||
|
||||
vertex(xpts[0]-w, ypts[0]);
|
||||
vertex(xpts[1]-w, ypts[1]);
|
||||
vertex(xpts[2]-w, ypts[2]);
|
||||
vertex(xpts[3]-w, ypts[3]);
|
||||
}
|
||||
if ((cr & 12)>0) {
|
||||
vertex(xpts[0], ypts[0]+h);
|
||||
vertex(xpts[1], ypts[1]+h);
|
||||
vertex(xpts[2], ypts[2]+h);
|
||||
vertex(xpts[3], ypts[3]+h);
|
||||
|
||||
vertex(xpts[0], ypts[0]-h);
|
||||
vertex(xpts[1], ypts[1]-h);
|
||||
vertex(xpts[2], ypts[2]-h);
|
||||
vertex(xpts[3], ypts[3]-h);
|
||||
}
|
||||
|
||||
// I have knowingly retained the small flaw of not
|
||||
// completely dealing with the corner conditions
|
||||
// (the case in which both of the above are true).
|
||||
}
|
||||
}
|
||||
endShape();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateGeometry() {
|
||||
Gesture J;
|
||||
for (int g=0; g<nGestures; g++) {
|
||||
if ((J=gestureArray[g]).exists) {
|
||||
if (g!=currentGestureID) {
|
||||
advanceGesture(J);
|
||||
} else if (!mousePressed) {
|
||||
advanceGesture(J);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void advanceGesture(Gesture gesture) {
|
||||
// Move a Gesture one step
|
||||
if (gesture.exists) { // check
|
||||
int nPts = gesture.nPoints;
|
||||
int nPts1 = nPts-1;
|
||||
Vec3f path[];
|
||||
float jx = gesture.jumpDx;
|
||||
float jy = gesture.jumpDy;
|
||||
|
||||
if (nPts > 0) {
|
||||
path = gesture.path;
|
||||
for (int i = nPts1; i > 0; i--) {
|
||||
path[i].x = path[i-1].x;
|
||||
path[i].y = path[i-1].y;
|
||||
}
|
||||
path[0].x = path[nPts1].x - jx;
|
||||
path[0].y = path[nPts1].y - jy;
|
||||
gesture.compile();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clearGestures() {
|
||||
for (int i = 0; i < nGestures; i++) {
|
||||
gestureArray[i].clear();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
# If you want to support more platforms, see the jogl.dev.java.net to get the
|
||||
# natives libraries for the platform in question (i.e. Solaris).
|
||||
|
||||
name = OpenGL2
|
||||
name = OpenGL
|
||||
|
||||
# In releases later than (but not including) 1.0.9, the applet JAR files
|
||||
# are downloaded directly from Sun, so that a single version is cached
|
||||
# on the user's computer, rather than increasing the download size with
|
||||
# the versions for each platform.
|
||||
applet = opengl2.jar
|
||||
applet = opengl.jar
|
||||
|
||||
Reference in New Issue
Block a user