Skip to content

Commit df75d3f

Browse files
authored
fix vbo instance example to run with emscripten (#7809)
1 parent 8edf3f5 commit df75d3f

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

examples/gl/vboMeshDrawInstancedExample/bin/data/shaders/instanced.frag

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
OF_GLSL_SHADER_HEADER
2+
13
//
24
// _____ ___
35
// / / / / vboMeshDrawInstancedExample
@@ -24,7 +26,7 @@
2426
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2527
// THE SOFTWARE.
2628

27-
#version 150
29+
//#version 150
2830

2931
in vec4 colorVarying;
3032
in vec2 texCoordVarying;
@@ -35,6 +37,6 @@ out vec4 fragColor;
3537

3638
void main(){
3739

38-
fragColor = colorVarying;
40+
fragColor = vec4(colorVarying.rgb, 1.0);
3941

40-
}
42+
}

examples/gl/vboMeshDrawInstancedExample/bin/data/shaders/instanced.vert

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
OF_GLSL_SHADER_HEADER
12
//
23
// _____ ___
34
// / / / / vboMeshDrawInstancedExample
@@ -24,14 +25,16 @@
2425
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2526
// THE SOFTWARE.
2627

27-
#version 150
28+
29+
//#version 150
30+
2831

2932
uniform mat4 projectionMatrix;
3033
uniform mat4 modelViewMatrix;
3134
uniform mat4 modelViewProjectionMatrix;
32-
uniform vec4 globalColor = vec4(1.0);
35+
uniform vec4 globalColor;// = vec4(1.0);
3336
uniform sampler2D tex0; // we use this to sample depth data for our boxes.
34-
uniform float timeValue=0.0; // we get this from our ofApp, it's a time value moving from 0 to 1 every 30 seconds
37+
uniform float timeValue;//=0.0; // we get this from our ofApp, it's a time value moving from 0 to 1 every 30 seconds
3538

3639

3740
in vec4 position;
@@ -134,13 +137,18 @@ void main()
134137
// we would like to spread our primitives out evenly along the x and an y coordinates
135138
// we calculate an x and an y coordinate value based on the current instance ID
136139

137-
float instanceX = float(gl_InstanceID%(iCount) - iCount/2) / 128.0;
138-
float instanceY = float(gl_InstanceID/(iCount) - iCount/2) / 128.0;
140+
float instanceX = float(gl_InstanceID%(iCount) - (iCount/2)) / 128.0;
141+
float instanceY = float(gl_InstanceID/(iCount) - (iCount/2)) / 128.0;
142+
143+
// float instanceX = float(gl_InstanceID)/128.0-0.5;
144+
// float instanceY = float(gl_InstanceID)/128.0-0.5;
145+
146+
texCoordVarying = vec2(0.5,0.5);
139147

140148
// next we get a mix-value, based on the current instance x coordinate and time, which
141149
// will help us to achieve some animation happiness.
142150

143-
float timeDependentInstanceXValue = mod(instanceX * 0.25 + timeValue *2, 1.0);
151+
float timeDependentInstanceXValue = mod(instanceX * 0.25 + timeValue * 2.0, 1.0);
144152

145153

146154
// get pixel depth by sampling from our depth texture.
@@ -153,6 +161,7 @@ void main()
153161

154162
// set the color for this primitive based on the current pixelDepth modified by the time dependent instance x value.
155163
colorVarying = vec4(globalColor.rgb * pixelDepth, globalColor.a) * vec4(1.0,1.0-timeDependentInstanceXValue,(instanceX + 0.5) ,1.0);
164+
// colorVarying = vec4(1.0);
156165

157166
vec4 vPos = position;
158167

@@ -164,9 +173,9 @@ void main()
164173
vPos.x = vPos.x * 4.0 + snoise(vec2(instanceX,instanceY)) * 20.0;
165174
vPos.y = vPos.y * 4.0 + snoise(vec2(1.0-instanceX,instanceY)) * 300.0;
166175
// this will distribute our boxes in space,
167-
vPos = vPos + vec4(instanceX*20*128,instanceY*10*128, 0,0);
176+
vPos = vPos + vec4(instanceX*20.0*128.0,instanceY*10.0*128.0, 0.0,0.0);
168177
vPos.z = vPos.z + snoise(vec2(instanceX,instanceY)) * 100.0;
169178

170179

171-
gl_Position = projectionMatrix * modelViewMatrix * vPos ;
172-
}
180+
gl_Position = projectionMatrix * modelViewMatrix * vPos;
181+
}

examples/gl/vboMeshDrawInstancedExample/src/ofApp.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
// tig: uncomment the following line to use programmable GL , and GLSL 150
44
// otherwise this example will run using OpenGL 2.0 / GLSL 1.20
5-
6-
// #define USE_PROGRAMMABLE_GL
5+
#define USE_PROGRAMMABLE_GL
76

87
// note that if you use programmable GL, a different set of shaders will be loaded.
98
// see ofApp.cpp

0 commit comments

Comments
 (0)