Last active
February 23, 2024 21:04
-
-
Save KrabCode/c465362fe801e1e5ba1ff90c90ab93c3 to your computer and use it in GitHub Desktop.
Recursive textured box. Runs on android using APDE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int bgTop = 20; | |
int bgBot = 230; | |
PImage tex; | |
float sx, sy; | |
PMatrix3D mouseRotation = new PMatrix3D(); | |
void mouseRotate() { | |
float x = mouseX - pmouseX; | |
float y = mouseY - pmouseY; | |
float drag = 0.98; | |
float damp = 0.1; | |
sx = sx*drag+x*damp; | |
sy = sy*drag+y*damp; | |
float angle = 0.0025*mag(sx,sy); | |
PMatrix3D temp = new PMatrix3D(); | |
temp.rotate(angle, -sy, sx, 0); | |
mouseRotation.preApply(temp); | |
applyMatrix(mouseRotation); | |
} | |
void settings() { | |
fullScreen(P3D); | |
noSmooth(); | |
} | |
void draw() { | |
int w = width; | |
int h = height; | |
orientation(PORTRAIT); | |
background(bgTop); | |
noStroke(); | |
hint(DISABLE_DEPTH_TEST); | |
pushMatrix(); | |
translate(0,h/2-w/2); | |
beginShape(); | |
fill(bgTop); | |
vertex(0, 0); | |
vertex(w, 0); | |
fill(bgBot); | |
vertex(w, w); | |
vertex(0, w); | |
endShape(); | |
popMatrix(); | |
fill(bgBot); | |
rect(0,h/2+w/2,w,w); | |
hint(ENABLE_DEPTH_TEST); | |
int off = 100; | |
pushMatrix(); | |
translate(w/2, h/2, 0); | |
mouseRotate(); | |
if (tex != null) { | |
box(tex, 500); | |
} | |
popMatrix(); | |
int gx = off; | |
int gy = h/2-w/2+off; | |
int gw = w-off*2; | |
int gh = gw; | |
tex = get(gx,gy,gw,gh); | |
} | |
void box(PImage tex, float size) { | |
float n = size * 0.5f; | |
beginShape(QUADS); | |
texture(tex); | |
textureMode(NORMAL); | |
// +Z "front" face | |
vertex(-n, -n, +n, 0, 0); | |
vertex(+n, -n, +n, 1, 0); | |
vertex(+n, +n, +n, 1, 1); | |
vertex(-n, +n, +n, 0, 1); | |
// -Z "back" face | |
vertex(+n, -n, -n, 0, 0); | |
vertex(-n, -n, -n, 1, 0); | |
vertex(-n, +n, -n, 1, 1); | |
vertex(+n, +n, -n, 0, 1); | |
// +Y "bottom" face | |
vertex(-n, +n, +n, 0, 0); | |
vertex(+n, +n, +n, 1, 0); | |
vertex(+n, +n, -n, 1, 1); | |
vertex(-n, +n, -n, 0, 1); | |
// -Y "top" face | |
vertex(-n, -n, -n, 0, 0); | |
vertex(+n, -n, -n, 1, 0); | |
vertex(+n, -n, +n, 1, 1); | |
vertex(-n, -n, +n, 0, 1); | |
// +X "right" face | |
vertex(+n, -n, +n, 0, 0); | |
vertex(+n, -n, -n, 1, 0); | |
vertex(+n, +n, -n, 1, 1); | |
vertex(+n, +n, +n, 0, 1); | |
// -X "left" face | |
vertex(-n, -n, -n, 0, 0); | |
vertex(-n, -n, +n, 1, 0); | |
vertex(-n, +n, +n, 1, 1); | |
vertex(-n, +n, -n, 0, 1); | |
endShape(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment