|
1 |
| -""" |
2 |
| - * TexturedCube |
3 |
| - * based on pde example by Dave Bollinger. |
4 |
| - * |
5 |
| - * Drag mouse to rotate cube. Demonstrates use of u/v coords in |
6 |
| - * vertex() and effect on texture(). |
7 |
| -""" |
8 |
| -tex = loadImage("data/berlin-1.jpg") |
9 |
| -rotx = PI / 4 |
10 |
| -roty = PI / 4 |
11 |
| -rate = 0.01 |
12 |
| - |
13 |
| -def setup(): |
14 |
| - size(640, 360, OPENGL) |
15 |
| - textureMode(NORMAL) |
16 |
| - fill(255) |
17 |
| - stroke(color(44, 48, 32)) |
18 |
| - |
19 |
| -def draw(): |
20 |
| - background(0) |
21 |
| - noStroke() |
22 |
| - translate(width / 2.0, height / 2.0, -100) |
23 |
| - rotateX(rotx) |
24 |
| - rotateY(roty) |
25 |
| - scale(90) |
26 |
| - TexturedCube() |
27 |
| - |
28 |
| -def TexturedCube(): |
29 |
| - beginShape(QUADS) |
30 |
| - texture(tex) |
31 |
| - # Given one texture and six faces, we can easily set up the uv coordinates |
32 |
| - # such that four of the faces tile "perfectly" along either u or v, but the other |
33 |
| - # two faces cannot be so aligned. This code tiles "along" u, "around" the X / Z faces |
34 |
| - # and fudges the Y faces - the Y faces are arbitrarily aligned such that a |
35 |
| - # rotation along the X axis will put the "top" of either texture at the "top" |
36 |
| - # of the screen, but is not otherwised aligned with the X / Z faces. (This |
37 |
| - # just affects what type of symmetry is required if you need seamless |
38 |
| - # tiling all the way around the cube) |
39 |
| - |
40 |
| - # +Z "front" face |
41 |
| - vertex(-1, -1, 1, 0, 0) |
42 |
| - vertex(1, -1, 1, 1, 0) |
43 |
| - vertex(1, 1, 1, 1, 1) |
44 |
| - vertex(-1, 1, 1, 0, 1) |
45 |
| - # -Z "back" face |
46 |
| - vertex(1, -1, -1, 0, 0) |
47 |
| - vertex(-1, -1, -1, 1, 0) |
48 |
| - vertex(-1, 1, -1, 1, 1) |
49 |
| - vertex(1, 1, -1, 0, 1) |
50 |
| - # +Y "bottom" face |
51 |
| - vertex(-1, 1, 1, 0, 0) |
52 |
| - vertex(1, 1, 1, 1, 0) |
53 |
| - vertex(1, 1, -1, 1, 1) |
54 |
| - vertex(-1, 1, -1, 0, 1) |
55 |
| - # -Y "top" face |
56 |
| - vertex(-1, -1, -1, 0, 0) |
57 |
| - vertex(1, -1, -1, 1, 0) |
58 |
| - vertex(1, -1, 1, 1, 1) |
59 |
| - vertex(-1, -1, 1, 0, 1) |
60 |
| - # +X "right" face |
61 |
| - vertex(1, -1, 1, 0, 0) |
62 |
| - vertex(1, -1, -1, 1, 0) |
63 |
| - vertex(1, 1, -1, 1, 1) |
64 |
| - vertex(1, 1, 1, 0, 1) |
65 |
| - # -X "left" face |
66 |
| - vertex(-1, -1, -1, 0, 0) |
67 |
| - vertex(-1, -1, 1, 1, 0) |
68 |
| - vertex(-1, 1, 1, 1, 1) |
69 |
| - vertex(-1, 1, -1, 0, 1) |
70 |
| - endShape() |
71 |
| - |
72 |
| -def mouseDragged(): |
73 |
| - global rotx, roty |
74 |
| - rotx += (pmouseY - mouseY) * rate |
75 |
| - roty += (mouseX - pmouseX) * rate |
| 1 | +""" |
| 2 | + * TexturedCube |
| 3 | + * based on pde example by Dave Bollinger. |
| 4 | + * |
| 5 | + * Drag mouse to rotate cube. Demonstrates use of u/v coords in |
| 6 | + * vertex() and effect on texture(). |
| 7 | +""" |
| 8 | +rotx = PI / 4 |
| 9 | +roty = PI / 4 |
| 10 | +rate = 0.01 |
| 11 | + |
| 12 | + |
| 13 | +def setup(): |
| 14 | + size(640, 360, OPENGL) |
| 15 | + textureMode(NORMAL) |
| 16 | + fill(255) |
| 17 | + stroke(color(44, 48, 32)) |
| 18 | + global tex |
| 19 | + tex = loadImage("data/berlin-1.jpg") |
| 20 | + |
| 21 | + |
| 22 | +def draw(): |
| 23 | + background(0) |
| 24 | + noStroke() |
| 25 | + translate(width / 2.0, height / 2.0, -100) |
| 26 | + rotateX(rotx) |
| 27 | + rotateY(roty) |
| 28 | + scale(90) |
| 29 | + TexturedCube() |
| 30 | + |
| 31 | + |
| 32 | +def TexturedCube(): |
| 33 | + beginShape(QUADS) |
| 34 | + texture(tex) |
| 35 | + # Given one texture and six faces, we can easily set up the uv coordinates |
| 36 | + # such that four of the faces tile "perfectly" along either u or v, but the other |
| 37 | + # two faces cannot be so aligned. This code tiles "along" u, "around" the X / Z faces |
| 38 | + # and fudges the Y faces - the Y faces are arbitrarily aligned such that a |
| 39 | + # rotation along the X axis will put the "top" of either texture at the "top" |
| 40 | + # of the screen, but is not otherwised aligned with the X / Z faces. (This |
| 41 | + # just affects what type of symmetry is required if you need seamless |
| 42 | + # tiling all the way around the cube) |
| 43 | + |
| 44 | + # +Z "front" face |
| 45 | + vertex(-1, -1, 1, 0, 0) |
| 46 | + vertex(1, -1, 1, 1, 0) |
| 47 | + vertex(1, 1, 1, 1, 1) |
| 48 | + vertex(-1, 1, 1, 0, 1) |
| 49 | + # -Z "back" face |
| 50 | + vertex(1, -1, -1, 0, 0) |
| 51 | + vertex(-1, -1, -1, 1, 0) |
| 52 | + vertex(-1, 1, -1, 1, 1) |
| 53 | + vertex(1, 1, -1, 0, 1) |
| 54 | + # +Y "bottom" face |
| 55 | + vertex(-1, 1, 1, 0, 0) |
| 56 | + vertex(1, 1, 1, 1, 0) |
| 57 | + vertex(1, 1, -1, 1, 1) |
| 58 | + vertex(-1, 1, -1, 0, 1) |
| 59 | + # -Y "top" face |
| 60 | + vertex(-1, -1, -1, 0, 0) |
| 61 | + vertex(1, -1, -1, 1, 0) |
| 62 | + vertex(1, -1, 1, 1, 1) |
| 63 | + vertex(-1, -1, 1, 0, 1) |
| 64 | + # +X "right" face |
| 65 | + vertex(1, -1, 1, 0, 0) |
| 66 | + vertex(1, -1, -1, 1, 0) |
| 67 | + vertex(1, 1, -1, 1, 1) |
| 68 | + vertex(1, 1, 1, 0, 1) |
| 69 | + # -X "left" face |
| 70 | + vertex(-1, -1, -1, 0, 0) |
| 71 | + vertex(-1, -1, 1, 1, 0) |
| 72 | + vertex(-1, 1, 1, 1, 1) |
| 73 | + vertex(-1, 1, -1, 0, 1) |
| 74 | + endShape() |
| 75 | + |
| 76 | + |
| 77 | +def mouseDragged(): |
| 78 | + global rotx, roty |
| 79 | + rotx += (pmouseY - mouseY) * rate |
| 80 | + roty += (mouseX - pmouseX) * rate |
0 commit comments