public class Block {
private PVector position;
private float rotation;
private float rotationSpeed;
private float speed;
private float sizeX;
private float sizeY;
private float sizeZ;
private float transparency;
public Block() {
this.reset();
}
public void update() {
pushMatrix();
translate(position.x, position.y, position.z);
rotateX(rotation);
fill(0, 255, 0, this.transparency);
noStroke();
box(sizeX, sizeY, sizeZ);
position.x -= this.speed;
if(position.x < -800 - this.sizeX/2)
this.reset();
rotation += rotationSpeed;
if(rotation > TWO_PI)
rotation = 0;
popMatrix();
}
private void reset() {
this.position = new PVector(6000.0, random(-400, 1), random(-1200, 1201));
this.rotationSpeed = random(0.01, 0.1);
this.rotation = random(0, TWO_PI);
this.speed = random(20, 100);
this.sizeX = random(500, 1500);
this.sizeY = this.sizeZ = random(1, 10);
this.transparency = random(50, 100);
}
}
public class Ground {
private PImage groundTexture;
private float textureOffset;
private float textureOffset2;
private float groundSpeed;
private PVector position;
public Ground(float speed, float x, float y, float z) {
this.groundSpeed = speed;
this.textureOffset = 0.5;
this.textureOffset2 = 1;
this.position = new PVector(x, y, z);
generateTexture();
}
public void update() {
textureMode(NORMALIZED);
beginShape();
texture(this.groundTexture);
vertex(position.x - 800, position.y, position.z + 800, 1, textureOffset);
vertex(position.x - 800, position.y, position.z - 800, 0, textureOffset);
vertex(position.x + 800, position.y, position.z - 800, 0, textureOffset2);
vertex(position.x + 800, position.y, position.z + 800, 1, textureOffset2);
endShape();
textureOffset += groundSpeed;
textureOffset2 += groundSpeed;
if(textureOffset >= 0.5)
textureOffset = 0;
if(textureOffset2 >= 1)
textureOffset2 = 0.5;
}
private void generateTexture() {
PGraphics buffer = createGraphics(256, 256, P2D);
boolean switchColor = false;
buffer.beginDraw();
buffer.noStroke();
for(int i = 0; i < buffer.width; i += sqrt(buffer.width)) {
for(int j = 0; j < buffer.height; j += sqrt(buffer.height)) {
switchColor = !switchColor;
if(switchColor)
buffer.fill(220, 145, 0);
else
buffer.fill(146, 72, 0);
buffer.rect(i, j, sqrt(buffer.width), sqrt(buffer.height));
}
switchColor = !switchColor;
}
buffer.endDraw();
this.groundTexture = buffer.get(0, 0, buffer.width, buffer.height);
}
}
Ground ground;
Block[] blocks;
void setup() {
size(640, 480, P3D);
ground = new Ground(0.005, 0.0, 0.0, 0.0);
blocks = new Block[100];
for(int i = 0; i < blocks.length; i++) {
blocks[i] = new Block();
}
textFont(loadFont("Monospaced.bold-48.vlw"));
textAlign(CENTER);
smooth();
}
void draw() {
lights();
background(0);
camera(-450, -50, 0, 0, 0, 0, 0.0, 1.0, 0.0);
ground.update();
for(int i = 0; i < blocks.length; i++)
blocks[i].update();
fill(255);
rotateY(PI + HALF_PI);
text("1010011010", 0, -10, 200);
}
Quite simple scenery with moving ground. Because OpenGL is not working, I had to use P3D and smooth() method to render texture on the ground correctly. This makes sketch really slow on my computer, but that must be because my computer is as old as earth itself!