Skip to content

Instantly share code, notes, and snippets.

@companje
Created October 6, 2019 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/b29da420d6e01545c36ed229bfc66c2a to your computer and use it in GitHub Desktop.
Save companje/b29da420d6e01545c36ed229bfc66c2a to your computer and use it in GitHub Desktop.
Distributed Points in Dome
import org.processing.wiki.triangulate.*;
PShape globe, sphere;
void settings() {
fullScreen(P3D);
}
void setup() {
surface.setSize(1200, 1200);
surface.setLocation(360, 0);
initCubeMap();
//colorMode(HSB);
//noStroke();
sphereDetail(128);
globe = createShape(SPHERE, height/2);
globe.setStroke(false);
globe.setTexture(loadImage("earth.jpg"));
sphereDetail(10);
ArrayList<PVector> points = new ArrayList();
float R[] = { 1, .93, .8, .5, 0 };
float A[] = { 12, 12, 12, 6, 1 };
for (int i=0; i<R.length; i++) {
float r = R[i];
for (int j=0; j<A[i]; j++) {
float a = j/A[i] * TWO_PI;
PVector p = toSphere(sin(a)*r, cos(a)*r);
p.mult(height/2);
println(p);
points.add(p);
}
}
ArrayList<Triangle> triangles = Triangulate.triangulate(points);
sphere = createShape();
sphere.setStrokeWeight(3);
sphere.setFill(false);
sphere.setStroke(color(255));
sphere.beginShape(TRIANGLES);
for (Triangle t : triangles) {
sphere.vertex(t.p1.x, t.p1.y, t.p1.z);
sphere.vertex(t.p2.x, t.p2.y, t.p2.z);
sphere.vertex(t.p3.x, t.p3.y, t.p3.z);
}
sphere.endShape();
}
void draw() {
background(0);
drawCubeMap();
}
void drawScene(int face) {
background(face/6.*255, 255, 255);
translate(width/2, height/2);
//rotateX(-mouseY/100.);
pushMatrix();
scale(-1, 1);
rotateY(-HALF_PI);
shape(globe);
popMatrix();
scale(-1, -1, -1);
shape(sphere);
for (int i = 0; i < sphere.getVertexCount(); i++) {
PVector v = sphere.getVertex(i);
pushMatrix();
translate(v.x, v.y, v.z);
fill(255, 0, 0);
noStroke();
sphere(20);
popMatrix();
}
}
PVector toSphere(float x, float y) { //-0.5 ... +0.5
PVector v = new PVector(x, y);
if (v.mag()>1.0f) v.normalize();
else v.z = sqrt(1.0 - v.mag());
return v;
}
PVector toSphere(float x, float y, float radius) { //-radius..+radius
PVector v = new PVector(x, y);
v.z = sqrt(radius - v.mag());
return v;
}
import java.nio.IntBuffer;
IntBuffer fbo;
IntBuffer rbo;
IntBuffer envMapTextureID;
PShader cubemapShader;
PShape domeSphere;
int envMapSize = 1000;
void initCubeMap() {
sphereDetail(50);
domeSphere = createShape(SPHERE, height/2);
domeSphere.rotateX(HALF_PI);
domeSphere.setStroke(false);
PGL pgl = beginPGL();
envMapTextureID = IntBuffer.allocate(1);
pgl.genTextures(1, envMapTextureID);
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, envMapTextureID.get(0));
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_S, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_T, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_R, PGL.CLAMP_TO_EDGE);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MIN_FILTER, PGL.NEAREST);
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MAG_FILTER, PGL.NEAREST);
for (int i = PGL.TEXTURE_CUBE_MAP_POSITIVE_X; i < PGL.TEXTURE_CUBE_MAP_POSITIVE_X + 6; i++) {
pgl.texImage2D(i, 0, PGL.RGBA8, envMapSize, envMapSize, 0, PGL.RGBA, PGL.UNSIGNED_BYTE, null);
}
// Init fbo, rbo
fbo = IntBuffer.allocate(1);
rbo = IntBuffer.allocate(1);
pgl.genFramebuffers(1, fbo);
pgl.bindFramebuffer(PGL.FRAMEBUFFER, fbo.get(0));
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, PGL.TEXTURE_CUBE_MAP_POSITIVE_X, envMapTextureID.get(0), 0);
pgl.genRenderbuffers(1, rbo);
pgl.bindRenderbuffer(PGL.RENDERBUFFER, rbo.get(0));
pgl.renderbufferStorage(PGL.RENDERBUFFER, PGL.DEPTH_COMPONENT24, envMapSize, envMapSize);
// Attach depth buffer to FBO
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT, PGL.RENDERBUFFER, rbo.get(0));
endPGL();
// Load cubemap shader.
cubemapShader = loadShader("cubemapfrag.glsl", "cubemapvert.glsl");
cubemapShader.set("cubemap", 1);
}
void drawCubeMap() {
PGL pgl = beginPGL();
pgl.activeTexture(PGL.TEXTURE1);
pgl.enable(PGL.TEXTURE_CUBE_MAP);
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, envMapTextureID.get(0));
regenerateEnvMap(pgl);
endPGL();
drawDomeMaster();
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, 0);
}
void drawDomeMaster() {
camera();
ortho();
resetMatrix();
shader(cubemapShader);
shape(domeSphere);
resetShader();
}
// Called to regenerate the envmap
void regenerateEnvMap(PGL pgl) {
// bind fbo
pgl.bindFramebuffer(PGL.FRAMEBUFFER, fbo.get(0));
int i=0;
// generate 6 views from origin(0, 0, 0)
pgl.viewport(0, 0, envMapSize, envMapSize);
perspective(90.0f * DEG_TO_RAD, 1.0f, 1.0f, 1025.0f);
for (int face = PGL.TEXTURE_CUBE_MAP_POSITIVE_X; face <
PGL.TEXTURE_CUBE_MAP_NEGATIVE_Z; face++) {
resetMatrix();
if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_X) {
camera(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f);
} else if (face == PGL.TEXTURE_CUBE_MAP_NEGATIVE_X) {
camera(0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f);
} else if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_Y) {
camera(0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f);
} else if (face == PGL.TEXTURE_CUBE_MAP_NEGATIVE_Y) {
camera(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
} else if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_Z) {
camera(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f);
}
scale(-1, 1, -1);
translate(-width/2, -height/2, -168); //was -192?
//println(-mouseX);
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, face, envMapTextureID.get(0), 0);
drawScene(i++); // Draw objects in the scene
flush(); // Make sure that the geometry in the scene is pushed to the GPU
noLights(); // Disabling lights to avoid adding many times
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, face, 0, 0);
}
}
@companje
Copy link
Author

companje commented Oct 6, 2019

points-dome

@companje
Copy link
Author

companje commented Oct 6, 2019

let op: de vertices zijn dubbel omdat bij iedere nieuwe triangle 3x vertex() wordt aangeroepen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment