From 47030c69ced0505ecd565bfb303c6d9a0ce48905 Mon Sep 17 00:00:00 2001 From: Casey Reas Date: Tue, 6 Sep 2011 00:12:41 +0000 Subject: [PATCH] Moved texture examples to Topics --- .../Textures/TextureCube/TextureCube.pde | 91 +++++++++++++++++++ .../TextureCylinder/TextureCylinder.pde | 46 ++++++++++ .../Textures/TextureQuad/TextureQuad.pde | 28 ++++++ .../TextureTriangle/TextureTriangle.pde | 25 +++++ 4 files changed, 190 insertions(+) create mode 100644 java/examples/Topics/Textures/TextureCube/TextureCube.pde create mode 100644 java/examples/Topics/Textures/TextureCylinder/TextureCylinder.pde create mode 100644 java/examples/Topics/Textures/TextureQuad/TextureQuad.pde create mode 100644 java/examples/Topics/Textures/TextureTriangle/TextureTriangle.pde diff --git a/java/examples/Topics/Textures/TextureCube/TextureCube.pde b/java/examples/Topics/Textures/TextureCube/TextureCube.pde new file mode 100644 index 000000000..38df04997 --- /dev/null +++ b/java/examples/Topics/Textures/TextureCube/TextureCube.pde @@ -0,0 +1,91 @@ +/** + * TexturedCube + * by Dave Bollinger. + * + * Drag mouse to rotate cube. Demonstrates use of u/v coords in + * vertex() and effect on texture(). The textures get distorted using + * the P3D renderer as you can see, but they look great using OPENGL. +*/ + + +PImage tex; +float rotx = PI/4; +float roty = PI/4; + +void setup() +{ + size(640, 360, P3D); + tex = loadImage("berlin-1.jpg"); + textureMode(NORMALIZED); + fill(255); + stroke(color(44,48,32)); +} + +void draw() +{ + background(0); + noStroke(); + translate(width/2.0, height/2.0, -100); + rotateX(rotx); + rotateY(roty); + scale(90); + TexturedCube(tex); +} + +void TexturedCube(PImage tex) { + beginShape(QUADS); + texture(tex); + + // Given one texture and six faces, we can easily set up the uv coordinates + // such that four of the faces tile "perfectly" along either u or v, but the other + // two faces cannot be so aligned. This code tiles "along" u, "around" the X/Z faces + // and fudges the Y faces - the Y faces are arbitrarily aligned such that a + // rotation along the X axis will put the "top" of either texture at the "top" + // of the screen, but is not otherwised aligned with the X/Z faces. (This + // just affects what type of symmetry is required if you need seamless + // tiling all the way around the cube) + + // +Z "front" face + vertex(-1, -1, 1, 0, 0); + vertex( 1, -1, 1, 1, 0); + vertex( 1, 1, 1, 1, 1); + vertex(-1, 1, 1, 0, 1); + + // -Z "back" face + vertex( 1, -1, -1, 0, 0); + vertex(-1, -1, -1, 1, 0); + vertex(-1, 1, -1, 1, 1); + vertex( 1, 1, -1, 0, 1); + + // +Y "bottom" face + vertex(-1, 1, 1, 0, 0); + vertex( 1, 1, 1, 1, 0); + vertex( 1, 1, -1, 1, 1); + vertex(-1, 1, -1, 0, 1); + + // -Y "top" face + vertex(-1, -1, -1, 0, 0); + vertex( 1, -1, -1, 1, 0); + vertex( 1, -1, 1, 1, 1); + vertex(-1, -1, 1, 0, 1); + + // +X "right" face + vertex( 1, -1, 1, 0, 0); + vertex( 1, -1, -1, 1, 0); + vertex( 1, 1, -1, 1, 1); + vertex( 1, 1, 1, 0, 1); + + // -X "left" face + vertex(-1, -1, -1, 0, 0); + vertex(-1, -1, 1, 1, 0); + vertex(-1, 1, 1, 1, 1); + vertex(-1, 1, -1, 0, 1); + + endShape(); +} + +void mouseDragged() { + float rate = 0.01; + rotx += (pmouseY-mouseY) * rate; + roty += (mouseX-pmouseX) * rate; +} diff --git a/java/examples/Topics/Textures/TextureCylinder/TextureCylinder.pde b/java/examples/Topics/Textures/TextureCylinder/TextureCylinder.pde new file mode 100644 index 000000000..66130f1e5 --- /dev/null +++ b/java/examples/Topics/Textures/TextureCylinder/TextureCylinder.pde @@ -0,0 +1,46 @@ +/** + * Texture 3. + * + * Load an image and draw it onto a cylinder and a quad. + */ + + +int tubeRes = 32; +float[] tubeX = new float[tubeRes]; +float[] tubeY = new float[tubeRes]; +PImage img; + +void setup() { + size(640, 360, P3D); + img = loadImage("berlin-1.jpg"); + float angle = 270.0 / tubeRes; + for (int i = 0; i < tubeRes; i++) { + tubeX[i] = cos(radians(i * angle)); + tubeY[i] = sin(radians(i * angle)); + } + noStroke(); +} + +void draw() { + background(0); + translate(width / 2, height / 2); + rotateX(map(mouseY, 0, height, -PI, PI)); + rotateY(map(mouseX, 0, width, -PI, PI)); + beginShape(QUAD_STRIP); + texture(img); + for (int i = 0; i < tubeRes; i++) { + float x = tubeX[i] * 100; + float z = tubeY[i] * 100; + float u = img.width / tubeRes * i; + vertex(x, -100, z, u, 0); + vertex(x, 100, z, u, img.height); + } + endShape(); + beginShape(QUADS); + texture(img); + vertex(0, -100, 0, 0, 0); + vertex(100, -100, 0, 100, 0); + vertex(100, 100, 0, 100, 100); + vertex(0, 100, 0, 0, 100); + endShape(); +} diff --git a/java/examples/Topics/Textures/TextureQuad/TextureQuad.pde b/java/examples/Topics/Textures/TextureQuad/TextureQuad.pde new file mode 100644 index 000000000..7eb36310a --- /dev/null +++ b/java/examples/Topics/Textures/TextureQuad/TextureQuad.pde @@ -0,0 +1,28 @@ +/** + * Texture 1. + * + * Load an image and draw it onto a quad. The texture() function sets + * the texture image. The vertex() function maps the image to the geometry. + */ + +PImage img; + +void setup() { + size(640, 360, P3D); + img = loadImage("berlin-1.jpg"); + noStroke(); +} + +void draw() { + background(0); + translate(width / 2, height / 2); + rotateY(map(mouseX, 0, width, -PI, PI)); + rotateZ(PI/6); + beginShape(); + texture(img); + vertex(-100, -100, 0, 0, 0); + vertex(100, -100, 0, 400, 0); + vertex(100, 100, 0, 400, 400); + vertex(-100, 100, 0, 0, 400); + endShape(); +} diff --git a/java/examples/Topics/Textures/TextureTriangle/TextureTriangle.pde b/java/examples/Topics/Textures/TextureTriangle/TextureTriangle.pde new file mode 100644 index 000000000..1398fd3e5 --- /dev/null +++ b/java/examples/Topics/Textures/TextureTriangle/TextureTriangle.pde @@ -0,0 +1,25 @@ +/** + * Texture 2. + * + * Using a rectangular image to map a texture onto a triangle. + */ + +PImage img; + +void setup() { + size(640, 360, P3D); + img = loadImage("berlin-1.jpg"); + noStroke(); +} + +void draw() { + background(0); + translate(width / 2, height / 2); + rotateY(map(mouseX, 0, width, -PI, PI)); + beginShape(); + texture(img); + vertex(-100, -100, 0, 0, 0); + vertex(100, -40, 0, 400, 120); + vertex(0, 100, 0, 200, 400); + endShape(); +}