Java Code Examples for processing.core.PShape#normal()

The following examples show how to use processing.core.PShape#normal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Icosahedron.java    From haxademic with MIT License 6 votes vote down vote up
public static PShape createIcosahedron(PGraphics pg, int level, PImage img) {
	// the icosahedron is created with positions, normals and texture coordinates in the above class
	Icosahedron ico = new Icosahedron(level);
	pg.textureMode(P.NORMAL); // set textureMode to normalized (range 0 to 1);
	PShape mesh = pg.createShape(); // create the initial PShape
	mesh.beginShape(P.TRIANGLES); // define the PShape type: TRIANGLES
	mesh.noStroke();
	if(img != null) mesh.texture(img);

	// put all the vertices, uv texture coordinates and normals into the PShape
	for (int i=0; i<ico.positions.size(); i++) {
		PVector pos = ico.positions.get(i);
		PVector t = ico.texCoords.get(i);
		PVector n = ico.normals.get(i);
		mesh.normal(n.x, n.y, n.z);
		mesh.vertex(pos.x, pos.y, pos.z, t.x, t.y);
	}
	mesh.endShape();
	return mesh;
}
 
Example 2
Source File: Shapes.java    From haxademic with MIT License 6 votes vote down vote up
public static PShape createSheetPoints(int detail, float width, float height) {
	PShape sh = P.p.createShape();
	sh.beginShape(PConstants.POINTS);
	sh.stroke(255);
	sh.strokeWeight(1);
	sh.noFill();
	float cellW = width / detail;
	float cellH = height / detail;
	// int numVertices = 0;
	for (int col = 0; col < detail; col++) {
		for (int row = 0; row < detail; row++) {
			float xU = col * cellW;
			float yV = row * cellH;
			float x = -width/2f + col * cellW;
			float y = -height/2f + row * cellH;
			float z = 0;
			sh.normal(x, y, z);
			sh.vertex(x, y, z, P.map(xU, 0, width, 0, 1), P.map(yV, 0, height, 0, 1));
			// numVertices += 1;
		}
	}
	// P.println("createSheet() vertices:", numVertices);
	sh.endShape(); 
	return sh;
}
 
Example 3
Source File: Shapes.java    From haxademic with MIT License 6 votes vote down vote up
public static PShape createCan(float radius, float height, int detail) {
	P.p.textureMode(P.NORMAL); 
	PShape sh = P.p.createShape();
	sh.beginShape(P.QUAD_STRIP);
	sh.noStroke();
	for (int i = 0; i <= detail; i++) {
		float angle = P.TWO_PI / detail;
		float x = P.sin(i * angle);
		float z = P.cos(i * angle);
		float u = (float)i / detail;
		sh.normal(x, 0, z);
		sh.vertex(x * radius, -height/2, z * radius, u, 0);
		sh.vertex(x * radius, +height/2, z * radius, u, 1);
	}
	sh.endShape();
	P.p.textureMode(P.IMAGE); 	// reset 
	return sh;
}
 
Example 4
Source File: DwSoftGrid3D.java    From PixelFlow with MIT License 6 votes vote down vote up
private final void vertex(PShape pg, DwParticle3D p, float[] n, float tu, float tv){
  if(p.all_springs_deactivated){
    degenerated = true;
    if(lastp != null){
      pg.vertex(lastp.cx,lastp.cy,lastp.cz, 0, 0);
    }
  } else {
    if(degenerated){
      pg.vertex(p.cx,p.cy,p.cz, 0, 0);
      pg.vertex(p.cx,p.cy,p.cz, 0, 0);
      degenerated = false;
    }
    pg.normal(n[0], n[1], n[2]); 
    pg.vertex(p.cx, p.cy, p.cz, tu, tv);
    lastp = p;
  }
}
 
Example 5
Source File: TextureSphere.java    From haxademic with MIT License 6 votes vote down vote up
PShape createIcosahedron(int level, String imagePath) {
	// the icosahedron is created with positions, normals and texture coordinates in the above class
	Icosahedron ico = new Icosahedron(level);

	textureMode(NORMAL); // set textureMode to normalized (range 0 to 1);
	PImage tex = loadImage(imagePath); // load the texture

	PShape mesh = createShape(); // create the initial PShape
	mesh.beginShape(TRIANGLES); // define the PShape type: TRIANGLES
	mesh.noStroke();
	mesh.texture(tex); // set the texture
	// put all the vertices, uv texture coordinates and normals into the PShape
	for (int i=0; i<ico.positions.size(); i++) {
		PVector p = ico.positions.get(i);
		PVector t = ico.texCoords.get(i);
		PVector n = ico.normals.get(i);
		mesh.normal(n.x, n.y, n.z);
		mesh.vertex(p.x, p.y, p.z, t.x, t.y);
	}
	mesh.endShape();

	return mesh; // our work is done here, return DA MESH! ;-)
}
 
Example 6
Source File: ShaderVertexTrigDeform.java    From haxademic with MIT License 6 votes vote down vote up
PShape createCylinder(float r, float h, int detail, PImage tex) {
	textureMode(NORMAL);
	PShape sh = createShape();
	sh.beginShape(QUAD_STRIP);
	sh.noStroke();
	sh.texture(tex);
	for (int i = 0; i <= detail; i++) {
		float angle = TWO_PI / detail;
		float x = sin(i * angle);
		float z = cos(i * angle);
		float u = (float)i / detail;
		sh.normal(x, 0, z);
		sh.vertex(x * r, -h/2, z * r, u, 0);
		sh.vertex(x * r, +h/2, z * r, u, 1);    
	}
	sh.endShape(); 
	return sh;
}
 
Example 7
Source File: Shapes.java    From haxademic with MIT License 5 votes vote down vote up
public static PShape createSheet(int detail, PImage tex) {
	P.p.textureMode(P.NORMAL); 
	// P.println("Shapes.createSheet() setting textureMode is weird to do here... Maybe should be PAppletHax default?");
	PShape sh = P.p.createShape();
	sh.beginShape(P.QUADS);
	sh.noStroke();
	sh.texture(tex);
	float cellW = tex.width / detail;
	float cellH = tex.height / detail;
	// int numVertices = 0;
	for (int col = 0; col < detail; col++) {
		for (int row = 0; row < detail; row++) {
			float xU = col * cellW;
			float yV = row * cellH;
			float x = -tex.width/2f + col * cellW;
			float y = -tex.height/2f + row * cellH;
			float z = 0;
			sh.normal(x, y, z);
			sh.vertex(x, y, z, P.map(xU, 0, tex.width, 0, 1), P.map(yV, 0, tex.height, 0, 1));
			sh.vertex(x, y + cellH, z, P.map(xU, 0, tex.width, 0, 1), P.map(yV + cellH, 0, tex.height, 0, 1));    
			sh.vertex(x + cellW, y + cellH, z, P.map(xU + cellW, 0, tex.width, 0, 1), P.map(yV + cellH, 0, tex.height, 0, 1));    
			sh.vertex(x + cellW, y, z, P.map(xU + cellW, 0, tex.width, 0, 1), P.map(yV, 0, tex.height, 0, 1));
			// numVertices++;
		}
	}
	// P.println("createSheet() vertices:", numVertices);
	sh.endShape(); 
	P.p.textureMode(P.IMAGE); 	// reset 
	return sh;
}
 
Example 8
Source File: Shapes.java    From haxademic with MIT License 5 votes vote down vote up
public static PShape createSheet(int cols, int rows, PImage tex) {
	P.p.textureMode(P.NORMAL); 
	// P.println("Shapes.createSheet() setting textureMode is weird to do here... Maybe should be PAppletHax default?");
	PShape sh = P.p.createShape();
	sh.beginShape(P.QUADS);
	sh.noStroke();
	sh.texture(tex);
	float cellW = (float) tex.width / (float) cols;
	float cellH = (float) tex.height / (float) rows;
	// int numVertices = 0;
	for (int col = 0; col < cols; col++) {
		for (int row = 0; row < rows; row++) {
			float xU = col * cellW;
			float yV = row * cellH;
			float x = -tex.width/2f + col * cellW;
			float y = -tex.height/2f + row * cellH;
			float z = 0;
			sh.normal(x, y, z);
			sh.vertex(x, y, z, 					P.map(xU, 0, tex.width, 0, 1), 			P.map(yV, 0, tex.height, 0, 1));
			sh.vertex(x, y + cellH, z, 			P.map(xU, 0, tex.width, 0, 1), 			P.map(yV + cellH, 0, tex.height, 0, 1));    
			sh.vertex(x + cellW, y + cellH, z, 	P.map(xU + cellW, 0, tex.width, 0, 1), 	P.map(yV + cellH, 0, tex.height, 0, 1));    
			sh.vertex(x + cellW, y, z, 			P.map(xU + cellW, 0, tex.width, 0, 1), 	P.map(yV, 0, tex.height, 0, 1));
			// numVertices++;
		}
	}
	// P.println("createSheet() vertices:", numVertices);
	sh.endShape(); 
	P.p.textureMode(P.IMAGE); 	// reset 
	return sh;
}
 
Example 9
Source File: ParticleSystem.java    From PixelFlow with MIT License 5 votes vote down vote up
public PShape createParticleShape(DwParticle2D particle, PImage pimg_sprite){
  
  final float rad = 2;

  PShape shp_sprite = papplet.createShape();
  shp_sprite.beginShape(PConstants.QUADS);
  shp_sprite.noStroke();
  shp_sprite.noFill();
  shp_sprite.tint(255,10,10);
  if(particle.idx == IDX_MOUSE_PARTICLE){
    shp_sprite.tint(200,100,100);
  } else {
    float r = 0 + papplet.random(-30, 30);
    float g = 100;
    float b = 100;
    shp_sprite.tint(r,g,b);
  }
  shp_sprite.textureMode(PConstants.NORMAL);
  shp_sprite.texture(pimg_sprite);
  shp_sprite.normal(0, 0, 1);
  shp_sprite.vertex(-rad, -rad, 0, 0);
  shp_sprite.vertex(+rad, -rad, 1, 0);
  shp_sprite.vertex(+rad, +rad, 1, 1);
  shp_sprite.vertex(-rad, +rad, 0, 1);
  shp_sprite.endShape();
  
  return shp_sprite;
}
 
Example 10
Source File: Icosahedron.java    From haxademic with MIT License 5 votes vote down vote up
public static PShape createIcosahedronGrouped(PApplet p, int level, PImage img, int fillColor, int strokeColor, float strokeWeight) {
	// the icosahedron is created with positions, normals and texture coordinates in the above class
	Icosahedron ico = new Icosahedron(level);
	p.textureMode(P.NORMAL); // set textureMode to normalized (range 0 to 1);
	PShape mesh = p.createShape(P.GROUP); // create the initial PShape
	
	
	// put all the vertices, uv texture coordinates and normals into the PShape
	PShape triangle = null;
	for (int i=0; i<ico.positions.size(); i++) {
		if(i % 3 == 0) {
			triangle = p.createShape();
			triangle.beginShape(P.TRIANGLE); // define the PShape type: TRIANGLES
			if(fillColor != -1) {
				triangle.strokeWeight(strokeWeight);
				triangle.stroke(strokeColor);
				triangle.fill(fillColor);
			}
			if(img != null) mesh.texture(img);
		}
		PVector pos = ico.positions.get(i);
		PVector t = ico.texCoords.get(i);
		PVector n = ico.normals.get(i);
		triangle.normal(n.x, n.y, n.z);
		triangle.vertex(pos.x, pos.y, pos.z, t.x, t.y);
		
		if(i % 3 == 2) {
			triangle.endShape();
			mesh.addChild(triangle);
		}
	}
	
	return mesh;
}
 
Example 11
Source File: DwSoftBall3D.java    From PixelFlow with MIT License 5 votes vote down vote up
private PShape createShape(PGraphics pg){
  int     faces_count = mesh.ifs.getFacesCount();
  int[][] faces       = mesh.ifs.getFaces();
  
  float[] n = new float[3]; // normal buffer
  
  PShape shp = pg.createShape();
  shp.beginShape(PConstants.TRIANGLES);
  shp.noStroke();
  shp.fill(material_color);
  for(int i = 0; i < faces_count; i++){
    int v0 = faces[i][0];
    int v1 = faces[i][1];
    int v2 = faces[i][2];
    DwParticle3D p0 = particles[v0]; if(p0.all_springs_deactivated) continue;
    DwParticle3D p1 = particles[v1]; if(p1.all_springs_deactivated) continue;
    DwParticle3D p2 = particles[v2]; if(p2.all_springs_deactivated) continue;
    
    if(FLAT_SHADING){
      n[0] = n[1] = n[2] = 0;
      DwParticle3D.crossAccum(p0, p1, p2, n);
      shp.normal(n[0], n[1], n[2]); 
      shp.vertex(p0.cx, p0.cy, p0.cz);
      shp.vertex(p1.cx, p1.cy, p1.cz);
      shp.vertex(p2.cx, p2.cy, p2.cz);
    } else {
      n = normals[v0];  shp.normal(n[0], n[1], n[2]);  shp.vertex(p0.cx, p0.cy, p0.cz);
      n = normals[v1];  shp.normal(n[0], n[1], n[2]);  shp.vertex(p1.cx, p1.cy, p1.cz);
      n = normals[v2];  shp.normal(n[0], n[1], n[2]);  shp.vertex(p2.cx, p2.cy, p2.cz);
    }
  }
  shp.endShape();
  return shp;
}
 
Example 12
Source File: KinectShaderVertexDeform.java    From haxademic with MIT License 5 votes vote down vote up
PShape createSheet(int detail, PImage tex) {
	p.textureMode(NORMAL);
	PShape sh = p.createShape();
	sh.beginShape(QUADS);
	sh.noStroke();
	sh.noFill();
	sh.texture(tex);
	float cellW = tex.width / detail;
	float cellH = tex.height / detail;
	int numVertices = 0;
	for (int col = 0; col < tex.width; col += cellW) {
		for (int row = 0; row < tex.height; row += cellH) {
			float xU = col;
			float yV = row;
			float x = -tex.width/2f + xU;
			float y = -tex.height/2f + yV;
			float z = 0;
			sh.normal(x, y, z);
			sh.vertex(x, y, z, 					P.map(xU, 0, tex.width, 0, 1f), P.map(yV, 0, tex.height, 0, 1f));
			sh.vertex(x, y + cellH, z, 			P.map(xU, 0, tex.width, 0, 1f), P.map(yV + cellH, 0, tex.height, 0, 1f));    
			sh.vertex(x + cellW, y + cellH, z,	P.map(xU + cellW, 0, tex.width, 0, 1f), P.map(yV + cellH, 0, tex.height, 0, 1f));    
			sh.vertex(x + cellW, y, z, 			P.map(xU + cellW, 0, tex.width, 0, 1f), P.map(yV, 0, tex.height, 0, 1f));
			numVertices++;
		}
	}
	P.println(numVertices, "vertices");
	sh.endShape(); 
	return sh;
}
 
Example 13
Source File: DwMeshUtils.java    From PixelFlow with MIT License 4 votes vote down vote up
static public void createPolyhedronShape(PShape shape, DwIndexedFaceSetAble ifs, float scale, int verts_per_face, boolean smooth){
    
    int type = -1;
    
    switch(verts_per_face){
      case 3: type = PConstants.TRIANGLES; break;
      case 4: type = PConstants.QUADS; break;
      default: return;
    }

    shape.setStroke(false);
    shape.beginShape(type);
//    shape.noStroke();
    
    int  [][] faces = ifs.getFaces();
    float[][] verts = ifs.getVerts();
    
    for(int[] face : faces){
      float nx = 0, ny = 0, nz = 0;

      int num_verts = face.length;
      
      // compute face normal
      if(!smooth){
        for(int i = 0; i < num_verts; i++){
          int vi = face[i];
          nx += verts[vi][0];
          ny += verts[vi][1];
          nz += verts[vi][2];
        }
        nx /= num_verts;
        ny /= num_verts;
        nz /= num_verts;
//        if(type == PConstants.QUADS){
          shape.normal(-nx, -ny, -nz);  // TODO: processing bug i guess
//        } else {
          shape.normal(nx, ny, nz); 
//        }
      }
      
      for(int i = 0; i < num_verts; i++){
//      for(int i = num_verts-1; i >= 0; i--){
        float[] v = verts[face[i]];
        if(smooth){
//          if(type == PConstants.QUADS){
//            shape.normal(-v[0], -v[1], -v[2]);  // TODO: processing bug i guess
//          } else {
            shape.normal(v[0], v[1], v[2]); 
//          }
        }
        shape.vertex(v[0]*scale, v[1]*scale, v[2]*scale);
      }
    }
    shape.endShape();
  }
 
Example 14
Source File: ParticleSystem.java    From PixelFlow with MIT License 4 votes vote down vote up
public PShape createParticleShape(DwParticle2D particle, PImage sprite_img){
    
    final float rad = particle.rad;

    PShape shp_particle = papplet.createShape(PShape.GROUP);
    
    if( PARTICLE_SHAPE_IDX >= 0 && PARTICLE_SHAPE_IDX < 4){
      
      PShape sprite = papplet.createShape(PShape.GEOMETRY);
      sprite.beginShape(PConstants.QUAD);
      sprite.noStroke();
      sprite.noFill();
      sprite.textureMode(PConstants.NORMAL);
      sprite.texture(sprite_img);
      sprite.normal(0, 0, 1);
      sprite.vertex(-rad, -rad, 0, 0);
      sprite.vertex(+rad, -rad, 1, 0);
      sprite.vertex(+rad, +rad, 1, 1);
      sprite.vertex(-rad, +rad, 0, 1);
      sprite.endShape();
      
      shp_particle.addChild(sprite);
    }
    else if( PARTICLE_SHAPE_IDX == 4){   
      
      float threshold1 = 1;   // radius shortening for arc segments
      float threshold2 = 140; // arc between segments
      
      double arc1 = Math.acos(Math.max((rad-threshold1), 0) / rad);
      double arc2 = (180 - threshold2) * Math.PI / 180;
      double arc = Math.min(arc1, arc2);
      
      int num_vtx = (int)Math.ceil(2*Math.PI/arc);
      
//      System.out.println(num_vtx);

      PShape circle = papplet.createShape(PShape.GEOMETRY);
      circle.beginShape();
      circle.noStroke();
      circle.fill(200,100);
      for(int i = 0; i < num_vtx; i++){
        float vx = (float) Math.cos(i * 2*Math.PI/num_vtx) * rad;
        float vy = (float) Math.sin(i * 2*Math.PI/num_vtx) * rad;
        circle.vertex(vx, vy);
      }
      circle.endShape(PConstants.CLOSE);

      PShape line = papplet.createShape(PShape.GEOMETRY);
      line.beginShape(PConstants.LINES);
      line.stroke(0, 100);
      line.strokeWeight(1);
      line.vertex(0, 0);
      line.vertex(-(rad-1), 0);
      line.endShape();
      
//      PShape circle = papplet.createShape(PConstants.ELLIPSE, 0, 0, rad*2, rad*2);
//      circle.setStroke(false);
//      circle.setFill(papplet.color(200,100));
//
//      PShape line = papplet.createShape(PConstants.LINE, 0, 0, -(rad-1), 0);
//      line.setStroke(papplet.color(0,200));
//      line.setStrokeWeight(1); 
      
      shp_particle.addChild(circle);
      shp_particle.addChild(line);
    }
    
    return shp_particle;
    
  }
 
Example 15
Source File: Skylight_ClothSimulation.java    From PixelFlow with MIT License 4 votes vote down vote up
public void displayAABB(PGraphics3D canvas, float[] aabb){
  if(shp_aabb == null){
    float xmin = aabb[0], xmax = aabb[3];
    float ymin = aabb[1], ymax = aabb[4];
    float zmin = aabb[2], zmax = aabb[5];
    
    shp_aabb = createShape(GROUP);
    
    PShape plane_zmin = createShape();
    plane_zmin.beginShape(QUAD);
    plane_zmin.stroke(0);
    plane_zmin.strokeWeight(1);
    plane_zmin.fill(16,96,192);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymax, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymax, zmin);
    plane_zmin.endShape(CLOSE);
    shp_aabb.addChild(plane_zmin);
    
    PShape plane_zmax = createShape();
    plane_zmax.beginShape(QUAD);
    plane_zmax.noFill();
    plane_zmax.stroke(0);
    plane_zmax.strokeWeight(1);
    plane_zmax.vertex(xmin, ymin, zmax);
    plane_zmax.vertex(xmax, ymin, zmax);
    plane_zmax.vertex(xmax, ymax, zmax);
    plane_zmax.vertex(xmin, ymax, zmax);
    plane_zmax.endShape(CLOSE);
    shp_aabb.addChild(plane_zmax);
    
    PShape vert_lines = createShape();
    vert_lines.beginShape(LINES);
    vert_lines.stroke(0);
    vert_lines.strokeWeight(1);
    vert_lines.vertex(xmin, ymin, zmin);  vert_lines.vertex(xmin, ymin, zmax);
    vert_lines.vertex(xmax, ymin, zmin);  vert_lines.vertex(xmax, ymin, zmax);
    vert_lines.vertex(xmax, ymax, zmin);  vert_lines.vertex(xmax, ymax, zmax);
    vert_lines.vertex(xmin, ymax, zmin);  vert_lines.vertex(xmin, ymax, zmax);
    vert_lines.endShape();
    shp_aabb.addChild(vert_lines);
    
    PShape corners = createShape();
    corners.beginShape(POINTS);
    corners.stroke(0);
    corners.strokeWeight(7);
    corners.vertex(xmin, ymin, zmin);  corners.vertex(xmin, ymin, zmax);
    corners.vertex(xmax, ymin, zmin);  corners.vertex(xmax, ymin, zmax);
    corners.vertex(xmax, ymax, zmin);  corners.vertex(xmax, ymax, zmax);
    corners.vertex(xmin, ymax, zmin);  corners.vertex(xmin, ymax, zmax);
    corners.endShape();
    shp_aabb.addChild(corners);
  }
  canvas.shape(shp_aabb);
}
 
Example 16
Source File: Skylight_BulletPhysics_CellFracture.java    From PixelFlow with MIT License 4 votes vote down vote up
public PShape createVoronoiCellShape(WB_VoronoiCell3D cell, Vector3f center_of_mass){
    
 
    boolean[] on_bounds = cell.getVerticesOnBoundary();
    
    PShape voronoi_cell = createShape(GROUP);
    
    WB_Mesh mesh = cell.getMesh();
    int[][] faces = mesh.getFacesAsInt();

    int on_boundary = 0;
    for(int j = 0; j < faces.length; j++){
      int[] face = faces[j];
      WB_Vector normal = mesh.getFaceNormal(j);
      
      PShape polygon = createShape();

      polygon.beginShape(POLYGON);
      polygon.normal(normal.xf(), normal.yf(), normal.zf());
      
      for(int k = 0; k < face.length; k++){
        WB_Coord vtx = mesh.getVertex(face[k]);
        
        float x = vtx.xf() - center_of_mass.x;
        float y = vtx.yf() - center_of_mass.y;
        float z = vtx.zf() - center_of_mass.z;
        polygon.vertex(x,y,z);
        
        if(on_bounds[face[k]]){
          on_boundary++;
        }
      }
      
      polygon.endShape(CLOSE);
//      polygon.setFill(true);
//      if(on_boundary == face.length){
//      if(on_boundary > 0){
//        polygon.setFill(color(8));
//      } else {
//        polygon.setFill(color(255,0,0));
//      }
//    
//      polygon.setStroke(false);
      
      
      voronoi_cell.addChild(polygon);
    }

    String wire = "";
    voronoi_cell.setFill(true);
//    if(on_boundary == face.length){
    if(on_boundary > 0){
      voronoi_cell.setFill(color(32));
      wire = "[wire]";
    } else {
      voronoi_cell.setFill(color(255,8,0));
    }
  
    voronoi_cell.setStroke(false);
    voronoi_cell.setStroke(color(64,4,0));
    voronoi_cell.setName("[cvh] "+wire);

    return voronoi_cell;
  }
 
Example 17
Source File: Softbody3D_Cloth.java    From PixelFlow with MIT License 4 votes vote down vote up
public void displayAABB(float[] aabb){
  if(shp_aabb == null){
    float xmin = aabb[0], xmax = aabb[3];
    float ymin = aabb[1], ymax = aabb[4];
    float zmin = aabb[2], zmax = aabb[5];
    
    shp_aabb = createShape(GROUP);
    
    PShape plane_zmin = createShape();
    plane_zmin.beginShape(QUAD);
    plane_zmin.stroke(0);
    plane_zmin.strokeWeight(1);
    plane_zmin.fill(64);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymax, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymax, zmin);
    plane_zmin.endShape(CLOSE);
    shp_aabb.addChild(plane_zmin);
    
    PShape plane_zmax = createShape();
    plane_zmax.beginShape(QUAD);
    plane_zmax.noFill();
    plane_zmax.stroke(0);
    plane_zmax.strokeWeight(1);
    plane_zmax.vertex(xmin, ymin, zmax);
    plane_zmax.vertex(xmax, ymin, zmax);
    plane_zmax.vertex(xmax, ymax, zmax);
    plane_zmax.vertex(xmin, ymax, zmax);
    plane_zmax.endShape(CLOSE);
    shp_aabb.addChild(plane_zmax);
    
    PShape vert_lines = createShape();
    vert_lines.beginShape(LINES);
    vert_lines.stroke(0);
    vert_lines.strokeWeight(1);
    vert_lines.vertex(xmin, ymin, zmin);  vert_lines.vertex(xmin, ymin, zmax);
    vert_lines.vertex(xmax, ymin, zmin);  vert_lines.vertex(xmax, ymin, zmax);
    vert_lines.vertex(xmax, ymax, zmin);  vert_lines.vertex(xmax, ymax, zmax);
    vert_lines.vertex(xmin, ymax, zmin);  vert_lines.vertex(xmin, ymax, zmax);
    vert_lines.endShape();
    shp_aabb.addChild(vert_lines);
    
    PShape corners = createShape();
    corners.beginShape(POINTS);
    corners.stroke(0);
    corners.strokeWeight(7);
    corners.vertex(xmin, ymin, zmin);  corners.vertex(xmin, ymin, zmax);
    corners.vertex(xmax, ymin, zmin);  corners.vertex(xmax, ymin, zmax);
    corners.vertex(xmax, ymax, zmin);  corners.vertex(xmax, ymax, zmax);
    corners.vertex(xmin, ymax, zmin);  corners.vertex(xmin, ymax, zmax);
    corners.endShape();
    shp_aabb.addChild(corners);
  }
  shape(shp_aabb);
}
 
Example 18
Source File: Softbody3D_Playground.java    From PixelFlow with MIT License 4 votes vote down vote up
public void displayAABB(float[] aabb){
  if(shp_aabb == null){
    float xmin = aabb[0], xmax = aabb[3];
    float ymin = aabb[1], ymax = aabb[4];
    float zmin = aabb[2], zmax = aabb[5];
    
    shp_aabb = createShape(GROUP);
    
    PShape plane_zmin = createShape();
    plane_zmin.beginShape(QUAD);
    plane_zmin.stroke(0);
    plane_zmin.strokeWeight(1);
    plane_zmin.fill(192);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymax, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymax, zmin);
    plane_zmin.endShape(CLOSE);
    shp_aabb.addChild(plane_zmin);
    
    PShape plane_zmax = createShape();
    plane_zmax.beginShape(QUAD);
    plane_zmax.noFill();
    plane_zmax.stroke(0);
    plane_zmax.strokeWeight(1);
    plane_zmax.vertex(xmin, ymin, zmax);
    plane_zmax.vertex(xmax, ymin, zmax);
    plane_zmax.vertex(xmax, ymax, zmax);
    plane_zmax.vertex(xmin, ymax, zmax);
    plane_zmax.endShape(CLOSE);
    shp_aabb.addChild(plane_zmax);
    
    PShape vert_lines = createShape();
    vert_lines.beginShape(LINES);
    vert_lines.stroke(0);
    vert_lines.strokeWeight(1);
    vert_lines.vertex(xmin, ymin, zmin);  vert_lines.vertex(xmin, ymin, zmax);
    vert_lines.vertex(xmax, ymin, zmin);  vert_lines.vertex(xmax, ymin, zmax);
    vert_lines.vertex(xmax, ymax, zmin);  vert_lines.vertex(xmax, ymax, zmax);
    vert_lines.vertex(xmin, ymax, zmin);  vert_lines.vertex(xmin, ymax, zmax);
    vert_lines.endShape();
    shp_aabb.addChild(vert_lines);
    
    PShape corners = createShape();
    corners.beginShape(POINTS);
    corners.stroke(0);
    corners.strokeWeight(7);
    corners.vertex(xmin, ymin, zmin);  corners.vertex(xmin, ymin, zmax);
    corners.vertex(xmax, ymin, zmin);  corners.vertex(xmax, ymin, zmax);
    corners.vertex(xmax, ymax, zmin);  corners.vertex(xmax, ymax, zmax);
    corners.vertex(xmin, ymax, zmin);  corners.vertex(xmin, ymax, zmax);
    corners.endShape();
    shp_aabb.addChild(corners);
  }
  shape(shp_aabb);
}
 
Example 19
Source File: Softbody3D_ParticleCollisionSystem.java    From PixelFlow with MIT License 4 votes vote down vote up
public void displayAABB(float[] aabb){
  if(shp_aabb == null){
    float xmin = aabb[0], xmax = aabb[3];
    float ymin = aabb[1], ymax = aabb[4];
    float zmin = aabb[2], zmax = aabb[5];
    
    shp_aabb = createShape(GROUP);
    
    PShape plane_zmin = createShape();
    plane_zmin.beginShape(QUAD);
    plane_zmin.stroke(0);
    plane_zmin.strokeWeight(1);
    plane_zmin.fill(192);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymin, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmax, ymax, zmin);
    plane_zmin.normal(0, 0, 1); plane_zmin.vertex(xmin, ymax, zmin);
    plane_zmin.endShape(CLOSE);
    shp_aabb.addChild(plane_zmin);
    
    PShape plane_zmax = createShape();
    plane_zmax.beginShape(QUAD);
    plane_zmax.noFill();
    plane_zmax.stroke(0);
    plane_zmax.strokeWeight(1);
    plane_zmax.vertex(xmin, ymin, zmax);
    plane_zmax.vertex(xmax, ymin, zmax);
    plane_zmax.vertex(xmax, ymax, zmax);
    plane_zmax.vertex(xmin, ymax, zmax);
    plane_zmax.endShape(CLOSE);
    shp_aabb.addChild(plane_zmax);
    
    PShape vert_lines = createShape();
    vert_lines.beginShape(LINES);
    vert_lines.stroke(0);
    vert_lines.strokeWeight(1);
    vert_lines.vertex(xmin, ymin, zmin);  vert_lines.vertex(xmin, ymin, zmax);
    vert_lines.vertex(xmax, ymin, zmin);  vert_lines.vertex(xmax, ymin, zmax);
    vert_lines.vertex(xmax, ymax, zmin);  vert_lines.vertex(xmax, ymax, zmax);
    vert_lines.vertex(xmin, ymax, zmin);  vert_lines.vertex(xmin, ymax, zmax);
    vert_lines.endShape();
    shp_aabb.addChild(vert_lines);
    
    PShape corners = createShape();
    corners.beginShape(POINTS);
    corners.stroke(0);
    corners.strokeWeight(7);
    corners.vertex(xmin, ymin, zmin);  corners.vertex(xmin, ymin, zmax);
    corners.vertex(xmax, ymin, zmin);  corners.vertex(xmax, ymin, zmax);
    corners.vertex(xmax, ymax, zmin);  corners.vertex(xmax, ymax, zmax);
    corners.vertex(xmin, ymax, zmin);  corners.vertex(xmin, ymax, zmax);
    corners.endShape();
    shp_aabb.addChild(corners);
  }
  shape(shp_aabb);
}
 
Example 20
Source File: ParticleSystem.java    From PixelFlow with MIT License 4 votes vote down vote up
public PShape createParticleShape(DwParticle2D particle, PImage sprite_img){
    
    final float rad = particle.rad;

    PShape shp_particle = papplet.createShape(PShape.GROUP);
    
    if( PARTICLE_SHAPE_IDX >= 0 && PARTICLE_SHAPE_IDX < 4){
      
      PShape sprite = papplet.createShape(PShape.GEOMETRY);
      sprite.beginShape(PConstants.QUAD);
      sprite.noStroke();
      sprite.noFill();
      sprite.textureMode(PConstants.NORMAL);
      sprite.texture(sprite_img);
      sprite.normal(0, 0, 1);
      sprite.vertex(-rad, -rad, 0, 0);
      sprite.vertex(+rad, -rad, 1, 0);
      sprite.vertex(+rad, +rad, 1, 1);
      sprite.vertex(-rad, +rad, 0, 1);
      sprite.endShape();
      
      shp_particle.addChild(sprite);
    }
    else if( PARTICLE_SHAPE_IDX == 4){   
      
      float threshold1 = 1;   // radius shortening for arc segments
      float threshold2 = 140; // arc between segments
      
      double arc1 = Math.acos(Math.max((rad-threshold1), 0) / rad);
      double arc2 = (180 - threshold2) * Math.PI / 180;
      double arc = Math.min(arc1, arc2);
      
      int num_vtx = (int)Math.ceil(2*Math.PI/arc);
      
//      System.out.println(num_vtx);

      PShape circle = papplet.createShape(PShape.GEOMETRY);
      circle.beginShape();
      circle.noStroke();
      circle.fill(200,100);
      for(int i = 0; i < num_vtx; i++){
        float vx = (float) Math.cos(i * 2*Math.PI/num_vtx) * rad;
        float vy = (float) Math.sin(i * 2*Math.PI/num_vtx) * rad;
        circle.vertex(vx, vy);
      }
      circle.endShape(PConstants.CLOSE);

      PShape line = papplet.createShape(PShape.GEOMETRY);
      line.beginShape(PConstants.LINES);
      line.stroke(0, 100);
      line.strokeWeight(1);
      line.vertex(0, 0);
      line.vertex(-(rad-1), 0);
      line.endShape();
      
//      PShape circle = papplet.createShape(PConstants.ELLIPSE, 0, 0, rad*2, rad*2);
//      circle.setStroke(false);
//      circle.setFill(papplet.color(200,100));
//
//      PShape line = papplet.createShape(PConstants.LINE, 0, 0, -(rad-1), 0);
//      line.setStroke(papplet.color(0,200));
//      line.setStrokeWeight(1); 
      
      shp_particle.addChild(circle);
      shp_particle.addChild(line);
    }
    
    return shp_particle;
    
  }