FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added new DomeProjection example · ddf/processing@28e7351 · GitHub

Repository navigation

Commit 28e7351

Browse files
committed
Added new DomeProjection example
1 parent ab35b31 commit 28e7351

5 files changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
void initCubeMap() {
2+
gridTex = loadImage("grid.png");
3+
sphereDetail(50);
4+
domeSphere = createShape(SPHERE, height/2.0f);
5+
domeSphere.setTexture(gridTex);
6+
domeSphere.rotateX(HALF_PI);
7+
domeSphere.setStroke(false);
8+
9+
PGL pgl = beginPGL();
10+
11+
envMapTextureID = IntBuffer.allocate(1);
12+
pgl.genTextures(1, envMapTextureID);
13+
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, envMapTextureID.get(0));
14+
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_S, PGL.CLAMP_TO_EDGE);
15+
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_T, PGL.CLAMP_TO_EDGE);
16+
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_WRAP_R, PGL.CLAMP_TO_EDGE);
17+
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MIN_FILTER, PGL.NEAREST);
18+
pgl.texParameteri(PGL.TEXTURE_CUBE_MAP, PGL.TEXTURE_MAG_FILTER, PGL.NEAREST);
19+
for (int i = PGL.TEXTURE_CUBE_MAP_POSITIVE_X; i < PGL.TEXTURE_CUBE_MAP_POSITIVE_X + 6; i++) {
20+
pgl.texImage2D(i, 0, PGL.RGBA8, envMapSize, envMapSize, 0, PGL.RGBA, PGL.UNSIGNED_BYTE, null);
21+
}
22+
23+
// Init fbo, rbo
24+
fbo = IntBuffer.allocate(1);
25+
rbo = IntBuffer.allocate(1);
26+
pgl.genFramebuffers(1, fbo);
27+
pgl.bindFramebuffer(PGL.FRAMEBUFFER, fbo.get(0));
28+
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, PGL.TEXTURE_CUBE_MAP_POSITIVE_X, envMapTextureID.get(0), 0);
29+
30+
pgl.genRenderbuffers(1, rbo);
31+
pgl.bindRenderbuffer(PGL.RENDERBUFFER, rbo.get(0));
32+
pgl.renderbufferStorage(PGL.RENDERBUFFER, PGL.DEPTH_COMPONENT24, envMapSize, envMapSize);
33+
34+
// Attach depth buffer to FBO
35+
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT, PGL.RENDERBUFFER, rbo.get(0));
36+
37+
pgl.enable(PGL.TEXTURE_CUBE_MAP);
38+
pgl.activeTexture(PGL.TEXTURE1);
39+
pgl.bindTexture(PGL.TEXTURE_CUBE_MAP, envMapTextureID.get(0));
40+
41+
endPGL();
42+
43+
// Load cubemap shader.
44+
cubemapShader = loadShader("cubemapfrag.glsl", "cubemapvert.glsl");
45+
cubemapShader.set("EnvMap", 1);
46+
}
47+
48+
void drawCubeMap() {
49+
regenerateEnvMap();
50+
drawDomeMaster();
51+
}
52+
53+
void drawDomeMaster() {
54+
ortho();
55+
resetMatrix();
56+
shader(cubemapShader);
57+
shape(domeSphere);
58+
resetShader();
59+
}
60+
61+
// Called to regenerate the envmap
62+
void regenerateEnvMap() {
63+
PGL pgl = beginPGL();
64+
65+
// bind fbo
66+
pgl.bindFramebuffer(PGL.FRAMEBUFFER, fbo.get(0));
67+
68+
// generate 6 views from origin(0, 0, 0)
69+
pgl.viewport(0, 0, envMapSize, envMapSize);
70+
perspective(90.0f * DEG_TO_RAD, 1.0f, 1.0f, 1025.0f);
71+
for (int face = PGL.TEXTURE_CUBE_MAP_POSITIVE_X; face <
72+
PGL.TEXTURE_CUBE_MAP_NEGATIVE_Z; face++) {
73+
resetMatrix();
74+
75+
if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_X) {
76+
camera(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f);
77+
} else if (face == PGL.TEXTURE_CUBE_MAP_NEGATIVE_X) {
78+
camera(0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f);
79+
} else if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_Y) {
80+
camera(0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f);
81+
} else if (face == PGL.TEXTURE_CUBE_MAP_NEGATIVE_Y) {
82+
camera(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
83+
} else if (face == PGL.TEXTURE_CUBE_MAP_POSITIVE_Z) {
84+
camera(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f);
85+
}
86+
87+
scale(-1, 1, -1);
88+
translate(-width * 0.5f, -height * 0.5f, -500);
89+
90+
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, face, envMapTextureID.get(0), 0);
91+
92+
drawScene(); // Draw objects in the scene
93+
flush(); // Make sure that the geometry in the scene is pushed to the GPU
94+
noLights(); // Disabling lights to avoid adding many times
95+
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0, face, 0, 0);
96+
}
97+
98+
endPGL();
99+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* DomeProjection
3+
*
4+
* This sketch uses use environmental mapping to render the output
5+
* on a full spherical come.
6+
*
7+
* Based on the FullDomeTemplate code from Christopher Warnow:
8+
* https://github.com/mphasize/FullDome
9+
*
10+
*/
11+
12+
import java.nio.IntBuffer;
13+
14+
PShader cubemapShader;
15+
PShape domeSphere;
16+
PImage gridTex;
17+
18+
IntBuffer fbo;
19+
IntBuffer rbo;
20+
IntBuffer envMapTextureID;
21+
22+
int envMapSize = 1024;
23+
24+
void setup() {
25+
size(640, 640, P3D);
26+
initCubeMap();
27+
}
28+
29+
void draw() {
30+
background(0);
31+
drawCubeMap();
32+
}
33+
34+
void drawScene() {
35+
background(0);
36+
stroke(255, 0, 0);
37+
strokeWeight(2);
38+
for (int i = -width; i < 2 * width; i += 50) {
39+
line(i, -height, -100, i, 2 *height, -100);
40+
}
41+
for (int i = -height; i < 2 * height; i += 50) {
42+
line(-width, i, -100, 2 * width, i, -100);
43+
}
44+
45+
lights();
46+
noStroke();
47+
translate(mouseX, mouseY, 200);
48+
rotateX(frameCount * 0.01);
49+
rotateY(frameCount * 0.01);
50+
box(100);
51+
}
52+

‎java/examples/Topics/Shaders/DomeProjection/data/cubemapfrag.glsl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uniform sampler2D texture;uniform samplerCube EnvMap;varying vec3 reflectDir;uniform vec2 texOffset;varying vec4 vertColor;varying vec4 vertTexCoord;void main() {// gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor; vec3 envColor = vec3(textureCube(EnvMap, reflectDir)); gl_FragColor = vec4(envColor, 1.0); }/*void main() { // Look up environment map value in cube map vec3 envColor = vec3(textureCube(EnvMap, ReflectDir)); gl_FragColor = vec4(envColor, 1.0); }*/

‎java/examples/Topics/Shaders/DomeProjection/data/cubemapvert.glsl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define PROCESSING_TEXTURE_SHADERuniform mat4 transform;uniform mat4 modelview;uniform mat4 texMatrix;attribute vec4 vertex;attribute vec4 color;attribute vec2 texCoord;attribute vec3 normal;varying vec4 vertColor;varying vec4 vertTexCoord;uniform mat3 normalMatrix;varying vec3 reflectDir;void main() { gl_Position = transform * vertex; vec3 ecNormal = normalize(normalMatrix * normal); // Vertex in eye coordinates vec3 ecVertex = vec3(modelview * vertex); // Normal vector in eye coordinates vec3 eyeDir = ecVertex.xyz; reflectDir = reflect(eyeDir, ecNormal); vertColor = color; vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);}/*varying vec3 ReflectDir;void main() { gl_Position = ftransform(); vec3 normal = normalize(gl_NormalMatrix * gl_Normal); vec4 pos = gl_ModelViewMatrix * gl_Vertex; vec3 eyeDir = pos.xyz; ReflectDir = reflect(eyeDir, normal);}*/
13.9 KB
LoadingViewer requires iframe.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL