Java Code Examples for processing.core.PGraphics#textureMode()

The following examples show how to use processing.core.PGraphics#textureMode() . 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: Shapes.java    From haxademic with MIT License 6 votes vote down vote up
public static void drawDiscTextured(PGraphics pg, float radius, float innerRadius, int numSegments, PImage texture) {
	pg.pushMatrix();
	
	float segmentRads = P.TWO_PI / numSegments;
	for( int i = 0; i < numSegments; i++ ) {
		float curRads = i * segmentRads;
		float nextRads = (i+1) * segmentRads;
		float progress = curRads / P.TWO_PI;
		float progressNext = nextRads / P.TWO_PI;
		
		pg.beginShape(P.TRIANGLES);
		pg.textureMode(P.NORMAL);
		pg.texture(texture);
		
		pg.vertex( P.cos(curRads) * innerRadius, P.sin(curRads) * innerRadius, 0, progress, 1 );
		pg.vertex( P.cos(curRads) * radius, P.sin(curRads) * radius, 0, progress, 0 );
		pg.vertex( P.cos(nextRads) * radius, P.sin(nextRads) * radius, 0, progressNext, 0 );
		
		pg.vertex( P.cos(curRads) * innerRadius, P.sin(curRads) * innerRadius, 0, progress, 1 );
		pg.vertex( P.cos(nextRads) * innerRadius, P.sin(nextRads) * innerRadius, 0, progressNext, 1 );
		pg.vertex( P.cos(nextRads) * radius, P.sin(nextRads) * radius, 0, progressNext, 0 );
		pg.endShape();
	}
	
	pg.popMatrix();
}
 
Example 2
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 3
Source File: DwFoldingTile.java    From PixelFlow with MIT License 5 votes vote down vote up
public void displayMesh(PGraphics pg, DwIndexedFaceSet ifs){
//    pg.beginShape(PConstants.TRIANGLES);
    pg.textureMode(PConstants.NORMAL);
    pg.texture(DEF.style.texture);
    pg.noStroke();
    int     s0,s1,s2;
    int     i0,i1,i2;
    float[] t0,t1,t2;
    float[] v0,v1,v2;
    for(int i = 0; i < DEF.FACES_COUNT; i++){
      i0 = faces[i][0];  v0 = ifs.verts[i0];
      i1 = faces[i][1];  v1 = ifs.verts[i1];
      i2 = faces[i][2];  v2 = ifs.verts[i2];
      
      i0 = DEF.FACES[i][0]; s0 = DEF.HILO[i0];  t0 = DEF.TEX_COORDS[i0];
      i1 = DEF.FACES[i][1]; s1 = DEF.HILO[i1];  t1 = DEF.TEX_COORDS[i1];
      i2 = DEF.FACES[i][2]; s2 = DEF.HILO[i2];  t2 = DEF.TEX_COORDS[i2];
      
      int ci = DEF.FACES_COL[i];
      
      if(DEF.style.texture != null){
        DwDisplayUtils.vertex(pg, v0, t0); 
        DwDisplayUtils.vertex(pg, v1, t1); 
        DwDisplayUtils.vertex(pg, v2, t2); 
      } else {
//        pg.fill(DEF.style.COL[s0]); DwDisplayUtils.vertex(pg, v0);
//        pg.fill(DEF.style.COL[s1]); DwDisplayUtils.vertex(pg, v1);
//        pg.fill(DEF.style.COL[s2]); DwDisplayUtils.vertex(pg, v2);
        pg.fill(DEF.style.RGBS[ci][s0]); DwDisplayUtils.vertex(pg, v0);
        pg.fill(DEF.style.RGBS[ci][s1]); DwDisplayUtils.vertex(pg, v1);
        pg.fill(DEF.style.RGBS[ci][s2]); DwDisplayUtils.vertex(pg, v2);
      }
    }
//    pg.endShape();
  }
 
Example 4
Source File: DwFoldingTile.java    From PixelFlow with MIT License 5 votes vote down vote up
public void displayMesh(PGraphics pg, DwParticle3D[] particles){
//    pg.beginShape(PConstants.TRIANGLES);
    pg.textureMode(PConstants.NORMAL);
    pg.texture(DEF.style.texture);
    pg.noStroke();
    int          s0,s1,s2;
    int          i0,i1,i2;
    float[]      t0,t1,t2;
    DwParticle3D v0,v1,v2;
    for(int i = 0; i < DEF.FACES_COUNT; i++){
      i0 = faces[i][0];  v0 = particles[i0];  if(v0.all_springs_deactivated) continue;
      i1 = faces[i][1];  v1 = particles[i1];  if(v1.all_springs_deactivated) continue;
      i2 = faces[i][2];  v2 = particles[i2];  if(v2.all_springs_deactivated) continue;
      
      i0 = DEF.FACES[i][0]; s0 = DEF.HILO[i0];  t0 = DEF.TEX_COORDS[i0];
      i1 = DEF.FACES[i][1]; s1 = DEF.HILO[i1];  t1 = DEF.TEX_COORDS[i1];
      i2 = DEF.FACES[i][2]; s2 = DEF.HILO[i2];  t2 = DEF.TEX_COORDS[i2];
      
      int ci = DEF.FACES_COL[i];
      
      if(DEF.style.texture != null){
        DwDisplayUtils.vertex(pg, v0, t0); 
        DwDisplayUtils.vertex(pg, v1, t1); 
        DwDisplayUtils.vertex(pg, v2, t2); 
      } else {
//        pg.fill(DEF.style.COL[s0]); DwDisplayUtils.vertex(pg, v0);
//        pg.fill(DEF.style.COL[s1]); DwDisplayUtils.vertex(pg, v1);
//        pg.fill(DEF.style.COL[s2]); DwDisplayUtils.vertex(pg, v2);
 
        pg.fill(DEF.style.RGBS[ci][s0]); DwDisplayUtils.vertex(pg, v0);
        pg.fill(DEF.style.RGBS[ci][s1]); DwDisplayUtils.vertex(pg, v1);
        pg.fill(DEF.style.RGBS[ci][s2]); DwDisplayUtils.vertex(pg, v2);
      }
    }
//    pg.endShape();
  }
 
Example 5
Source File: Shapes.java    From haxademic with MIT License 5 votes vote down vote up
public static void drawTexturedRect(PGraphics pg, PImage texture) {
	pg.beginShape(P.QUAD);
	pg.textureMode(P.NORMAL);
	pg.texture(texture);
	pg.vertex(-texture.width/2, -texture.height/2, 			0, 0);
	pg.vertex( texture.width/2, -texture.height/2, 			1, 0);
	pg.vertex( texture.width/2,  texture.height/2, 			1, 1);
	pg.vertex(-texture.width/2,  texture.height/2, 			0, 1);
	pg.endShape();
}
 
Example 6
Source File: Shapes.java    From haxademic with MIT License 5 votes vote down vote up
public static void drawTexturedLine(PGraphics pg, PImage texture, float xStart, float yStart, float xEnd, float yEnd, int color, float thickness, float texOffset, float cornerRadius) {
	// calc textured rectangle rotation * distance
	float startToEndAngle = MathUtil.getRadiansToTarget(xStart, yStart, xEnd, yEnd);
	float dist = MathUtil.getDistance(xStart, yStart, xEnd, yEnd);
	float thicknessHalf = thickness/2f;
	float textureHeightHalf = texture.height/2f;
	
	// set context
	OpenGLUtil.setTextureRepeat(pg);
	pg.push();
	pg.translate(xStart, yStart);
	pg.rotate(startToEndAngle);
	
	// draw textured rect
	pg.noStroke();
	pg.beginShape();
	pg.texture(texture);
	pg.textureMode(P.IMAGE);
	pg.tint(color);
	if(cornerRadius <= 0) {
		pg.vertex(0, -thicknessHalf, 0, texOffset, 0);
		pg.vertex(dist, -thicknessHalf, 0, texOffset + dist, 0);
		pg.vertex(dist, thicknessHalf, 0, texOffset + dist, texture.height);
		pg.vertex(0, thicknessHalf, 0, texOffset, texture.height);
	} else {
		// diamond shape, left/center, clockwise to right/center
		pg.vertex(0, 0, 0, 									texOffset, textureHeightHalf);
		pg.vertex(cornerRadius, -thicknessHalf, 0, 			texOffset + cornerRadius, 0);
		pg.vertex(dist - cornerRadius, -thicknessHalf, 0, 	texOffset + dist - cornerRadius, 0);
		// right/center, clockwise to left/center
		pg.vertex(dist, 0, 0, 								texOffset + dist, textureHeightHalf);
		pg.vertex(dist - cornerRadius, thicknessHalf, 0, 	texOffset + dist - cornerRadius, texture.height);
		pg.vertex(cornerRadius, thicknessHalf, 0, 			texOffset + cornerRadius, texture.height);
	}
	pg.endShape();
	pg.noTint();
	pg.pop();
}
 
Example 7
Source File: TiledGrid.java    From haxademic with MIT License 5 votes vote down vote up
public void draw(PGraphics pg, float cols, float rows, boolean drawOutline) {
	int prevRectMode = pg.rectMode;
	PG.setTextureRepeat(pg, true);
	pg.pushMatrix();
	float drawW = cols * tileSize + strokeWeight;
	float drawH = rows * tileSize + strokeWeight;
	pg.noStroke();
	pg.beginShape();
	pg.textureMode(P.IMAGE);
	pg.texture(gridCell);
	if(prevRectMode == PConstants.CENTER) pg.translate(P.round(-drawW/2), P.round(-drawH/2));
	pg.vertex(0, 0, 0,			offsetX * tileSize + 0, 		offsetY * tileSize + 0);
	pg.vertex(drawW, 0, 0, 		offsetX * tileSize + drawW, 	offsetY * tileSize + 0);
	pg.vertex(drawW, drawH, 0, 	offsetX * tileSize + drawW, 	offsetY * tileSize + drawH);
	pg.vertex(0, drawH, 0, 		offsetX * tileSize + 0, 		offsetY * tileSize + drawH);
	pg.endShape();
	
	if(drawOutline) {
		pg.rectMode(PConstants.CORNER); // make sure rect is drawing from the same top left
		pg.fill(colorStroke);
		pg.rect(0, 0, drawW, strokeWeight);	// top
		pg.rect(0, drawH - strokeWeight, drawW, strokeWeight);	// bottom
		pg.rect(0, 0, strokeWeight, drawH);	// left
		pg.rect(drawW - strokeWeight, 0, strokeWeight, drawH);	// right
		pg.rectMode(prevRectMode);		// reset rect mode to whatever it was before
	}
	
	pg.popMatrix();
}
 
Example 8
Source File: Shapes.java    From haxademic with MIT License 4 votes vote down vote up
public static void textureQuadSubdivided(PGraphics pg, PImage texture, int subDivideSteps, 
		float x1, float y1, float z1, float u1, float v1,
		float x2, float y2, float z2, float u2, float v2,
		float x3, float y3, float z3, float u3, float v3,
		float x4, float y4, float z4, float u4, float v4
	) {
	
	pg.beginShape(P.QUAD);
	pg.textureMode(PConstants.NORMAL);
	pg.texture(texture);

	// subdivide quad for better resolution
	// vertices go top-left, clockwise to bottom left
	float stepsX = subDivideSteps;
	float segmentX = 1f / subDivideSteps;
	float stepsY = subDivideSteps;
	float segmentY = 1f / stepsY;

	// draw a subdivided grid
	for( float x=0; x < stepsX; x++ ) {
		// calculate spread of mesh grid and uv coordinates
		float xNorm = x * segmentX;
		float xNormNext = (x+1) * segmentX;

		for( float y=0; y < stepsY; y++ ) {
			// calculate grid cells' uv coordinates
			float yNorm = y * segmentY;
			float yNormNext = (y+1) * segmentY;

			// calc grid positions based on interpolating columns between corners
			// we only need the xProgress for this, since we're slicing by column
			float colTopX = P.lerp(x1, x2, xNorm);
			float colTopY = P.lerp(y1, y2, xNorm);
			float colTopZ = P.lerp(z1, z2, xNorm);
			float colTopU = P.lerp(u1, u2, xNorm);
			float colTopV = P.lerp(v1, v2, xNorm);
			float colBotX = P.lerp(x4, x3, xNorm);
			float colBotY = P.lerp(y4, y3, xNorm);
			float colBotZ = P.lerp(z4, z3, xNorm);
			float colBotU = P.lerp(u4, u3, xNorm);
			float colBotV = P.lerp(v4, v3, xNorm);

			float nextColTopX = P.lerp(x1, x2, xNormNext);
			float nextColTopY = P.lerp(y1, y2, xNormNext);
			float nextColTopZ = P.lerp(z1, z2, xNormNext);
			float nextColTopU = P.lerp(u1, u2, xNormNext);
			float nextColTopV = P.lerp(v1, v2, xNormNext);
			float nextColBotX = P.lerp(x4, x3, xNormNext);
			float nextColBotY = P.lerp(y4, y3, xNormNext);
			float nextColBotZ = P.lerp(z4, z3, xNormNext);
			float nextColBotU = P.lerp(u4, u3, xNormNext);
			float nextColBotV = P.lerp(v4, v3, xNormNext);

			// calc quad coords
			float quadTopLeftX = P.lerp(colTopX, colBotX, yNorm);
			float quadTopLeftY = P.lerp(colTopY, colBotY, yNorm);
			float quadTopLeftZ = P.lerp(colTopZ, colBotZ, yNorm);
			float quadTopLeftU = P.lerp(colTopU, colBotU, yNorm);
			float quadTopLeftV = P.lerp(colTopV, colBotV, yNorm);
			float quadTopRightX = P.lerp(nextColTopX, nextColBotX, yNorm);
			float quadTopRightY = P.lerp(nextColTopY, nextColBotY, yNorm);
			float quadTopRightZ = P.lerp(nextColTopZ, nextColBotZ, yNorm);
			float quadTopRightU = P.lerp(nextColTopU, nextColBotU, yNorm);
			float quadTopRightV = P.lerp(nextColTopV, nextColBotV, yNorm);
			float quadBotRightX = P.lerp(nextColTopX, nextColBotX, yNormNext);
			float quadBotRightY = P.lerp(nextColTopY, nextColBotY, yNormNext);
			float quadBotRightZ = P.lerp(nextColTopZ, nextColBotZ, yNormNext);
			float quadBotRightU = P.lerp(nextColTopU, nextColBotU, yNormNext);
			float quadBotRightV = P.lerp(nextColTopV, nextColBotV, yNormNext);
			float quadBotLeftX = P.lerp(colTopX, colBotX, yNormNext);
			float quadBotLeftY = P.lerp(colTopY, colBotY, yNormNext);
			float quadBotLeftZ = P.lerp(colTopZ, colBotZ, yNormNext);
			float quadBotLeftU = P.lerp(colTopU, colBotU, yNormNext);
			float quadBotLeftV = P.lerp(colTopV, colBotV, yNormNext);

			// draw subdivided quads
			pg.vertex(quadTopLeftX, quadTopLeftY, quadTopLeftZ, quadTopLeftU, quadTopLeftV);
			pg.vertex(quadTopRightX, quadTopRightY, quadTopRightZ, quadTopRightU, quadTopRightV);
			pg.vertex(quadBotRightX, quadBotRightY, quadBotRightZ, quadBotRightU, quadBotRightV);
			pg.vertex(quadBotLeftX, quadBotLeftY, quadBotLeftZ, quadBotLeftU, quadBotLeftV);
		}
	}
	
	pg.endShape(P.CLOSE);
}
 
Example 9
Source File: ImageUtil.java    From haxademic with MIT License 4 votes vote down vote up
public static void drawTextureMappedRect(PGraphics dest, PImage texture, int subdivideX, int subdivideY, float topLeftX, float topLeftY, float topRightX, float topRightY, float bottomRightX, float bottomRightY, float bottomLeftX, float bottomLeftY) {
	// draw to screen with pinned corner coords
	// generalized version ported from PGraphicsKeystone
	// inspired by: https://github.com/davidbouchard/keystone & http://marcinignac.com/blog/projectedquads-source-code/
	dest.textureMode(PConstants.IMAGE);
	dest.noStroke();
	dest.fill(255);
	dest.beginShape(PConstants.QUAD);
	dest.texture(texture);
	
	if(subdivideX > 0) {
		// subdivide quad for better resolution
		float stepsX = subdivideX;
		float stepsY = subdivideY;

		for( float x=0; x < stepsX; x += 1f ) {
			// calculate spread of mesh grid and uv coordinates
			float xPercent = x/stepsX;
			float xPercentNext = (x+1f)/stepsX;
			if( xPercentNext > 1 ) xPercentNext = 1;
			float uPercent = xPercent;
			float uPercentNext = xPercentNext;

			for( float y=0; y < stepsY; y += 1f ) {
				// calculate spread of mesh grid and uv coordinates
				float yPercent = y/stepsY;
				float yPercentNext = (y+1f)/stepsY;
				if( yPercentNext > 1 ) yPercentNext = 1;
				float vPercent = yPercent;
				float vPercentNext = yPercentNext;

				// calc grid positions based on interpolating columns between corners
				float colTopX = interp(topLeftX, topRightX, xPercent);
				float colTopY = interp(topLeftY, topRightY, xPercent);
				float colBotX = interp(bottomLeftX, bottomRightX, xPercent);
				float colBotY = interp(bottomLeftY, bottomRightY, xPercent);
				
				float nextColTopX = interp(topLeftX, topRightX, xPercentNext);
				float nextColTopY = interp(topLeftY, topRightY, xPercentNext);
				float nextColBotX = interp(bottomLeftX, bottomRightX, xPercentNext);
				float nextColBotY = interp(bottomLeftY, bottomRightY, xPercentNext);
				
				// calc quad coords
				float quadTopLeftX = interp(colTopX, colBotX, yPercent);
				float quadTopLeftY = interp(colTopY, colBotY, yPercent);
				float quadTopRightX = interp(nextColTopX, nextColBotX, yPercent);
				float quadTopRightY = interp(nextColTopY, nextColBotY, yPercent);
				float quadBotRightX = interp(nextColTopX, nextColBotX, yPercentNext);
				float quadBotRightY = interp(nextColTopY, nextColBotY, yPercentNext);
				float quadBotLeftX = interp(colTopX, colBotX, yPercentNext);
				float quadBotLeftY = interp(colTopY, colBotY, yPercentNext);
				
				// draw subdivided quads
				dest.vertex(quadTopLeftX, quadTopLeftY, 0, 	texture.width * uPercent, 		texture.height * vPercent);
				dest.vertex(quadTopRightX, quadTopRightY, 0, 	texture.width * uPercentNext, 	texture.height * vPercent);
				dest.vertex(quadBotRightX, quadBotRightY, 0, 	texture.width * uPercentNext, 	texture.height * vPercentNext);
				dest.vertex(quadBotLeftX, quadBotLeftY, 0, 	texture.width * uPercent, 		texture.height * vPercentNext);
			}
		}
	} else {
		// default single mapped quad
		dest.vertex(topLeftX, topLeftY, 0, 			0, 0);
		dest.vertex(topRightX, topRightY, 0, 			texture.width, 0);
		dest.vertex(bottomRightX, bottomRightY, 0, 	texture.width, texture.height);
		dest.vertex(bottomLeftX, bottomLeftY, 0, 	0,  texture.height);
	}

	dest.endShape();
}
 
Example 10
Source File: TiledTexture.java    From haxademic with MIT License 4 votes vote down vote up
public void draw(PGraphics pg, float drawW, float drawH, boolean drawFromCenter) {
	PG.setTextureRepeat(pg, true);
	float halfDrawW = drawW / 2f;
	float halfDrawH = drawH / 2f;
	float halfSizeX = halfDrawW * sizeX;
	float halfSizeY = halfDrawH * sizeY;
			
	pg.noStroke();
	pg.beginShape();
	pg.textureMode(P.IMAGE);
	pg.texture(texture);
	if(rotation == 0) {
		tlX = uX - halfSizeX;
		tlY = vY - halfSizeY;
		trX = uX + halfSizeX;
		trY = vY - halfSizeY;
		brX = uX + halfSizeX;
		brY = vY + halfSizeY;
		blX = uX - halfSizeX;
		blY = vY + halfSizeY;
	} else {
		float curRot = rotation + P.PI;
		float radius = MathUtil.getDistance(0, 0, halfSizeX, halfSizeY);
		float tlRads = -MathUtil.getRadiansToTarget(uX - halfSizeX, vY - halfSizeY, uX, vY) + curRot;
		float trRads = -MathUtil.getRadiansToTarget(uX + halfSizeX, vY - halfSizeY, uX, vY) + curRot;
		float brRads = -MathUtil.getRadiansToTarget(uX + halfSizeX, vY + halfSizeY, uX, vY) + curRot;
		float blRads = -MathUtil.getRadiansToTarget(uX - halfSizeX, vY + halfSizeY, uX, vY) + curRot;
		tlX = uX + radius * P.cos(tlRads);
		tlY = vY - radius * P.sin(tlRads);
		trX = uX + radius * P.cos(trRads);
		trY = vY - radius * P.sin(trRads);
		brX = uX + radius * P.cos(brRads);
		brY = vY - radius * P.sin(brRads);
		blX = uX + radius * P.cos(blRads);
		blY = vY - radius * P.sin(blRads);
	}
	if(drawFromCenter) {
		pg.vertex(-halfDrawW, -halfDrawH, tlX, tlY);
		pg.vertex( halfDrawW, -halfDrawH, trX, trY);
		pg.vertex( halfDrawW,  halfDrawH, brX, brY);
		pg.vertex(-halfDrawW,  halfDrawH, blX, blY);
	} else {
		pg.vertex(0, 0, 		tlX, tlY);
		pg.vertex(drawW, 0, 	trX, trY);
		pg.vertex(drawW, drawH, brX, brY);
		pg.vertex(0, drawH, 	blX, blY);
	}
	pg.endShape();
}