Permalink
Browse files

Update PingPong example

  • Loading branch information...
1 parent 2f8736e commit 72747e75666cec05aa6eb98abdce0f7b9e150d3f @notlion committed Jun 1, 2011
@@ -1,24 +0,0 @@
-#ifdef VERTEX
-
- uniform mat4 u_mvp_matrix;
-
- attribute vec3 a_position;
-
- void main()
- {
- gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
- }
-
-#endif
-
-
-#ifdef FRAGMENT
-
- uniform vec4 u_color;
-
- void main()
- {
- gl_FragColor = u_color;
- }
-
-#endif
@@ -1,4 +1,4 @@
- var fs = require('fs')
+var fs = require('fs')
, plask = require('plask')
, em = require('../../embr-plask');
@@ -22,8 +22,23 @@ plask.simpleWindow({
// Make Shaders
em.Program.include("noise3D.glsl", fs.readFileSync("noise3D.glsl", "utf8"));
this.smear_prog = new em.Program(gl, fs.readFileSync("smear.glsl", "utf8"));
- this.tex_prog = new em.Program(gl, fs.readFileSync("texture.glsl", "utf8"));
- this.color_prog = new em.Program(gl, fs.readFileSync("color.glsl", "utf8"));
+ this.smear_prog.link();
+
+ this.color_mater = new em.ColorMaterial(gl);
+ this.color_mater.useUniforms({
+ projection: this.projection,
+ modelview: new em.Mat4()
+ });
+
+ this.tex_mater = new em.ColorMaterial(gl, {
+ flags: { use_texture: true }
+ });
+ this.tex_mater.useUniforms({
+ projection: this.projection,
+ modelview: new em.Mat4(),
+ texture: 0,
+ color: new em.Vec4(1,1,1,1)
+ });
// Make PingPong Framebuffers
this.pp = new em.PingPong(gl, 512, 512, [
@@ -33,55 +48,46 @@ plask.simpleWindow({
// Make Plane
this.plane = em.Vbo.makePlane(gl, -1, -1, 1, 1);
- this.plane.attributes.position.location = this.smear_prog.loc_a_position;
- this.plane.attributes.texcoord.location = this.smear_prog.loc_a_texcoord;
+ this.plane.attributes.position.location = this.smear_prog.locations.a_position;
+ this.plane.attributes.texcoord.location = this.smear_prog.locations.a_texcoord;
- // Make Brush (todo: somehow this is broken)
this.cube = em.Vbo.makeCube(gl, 0.1, 0.1, 0.1);
- this.cube.attributes.position.location = this.color_prog.loc_a_position;
-
- // gl.enable(gl.BLEND);
- // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
+ this.color_mater.assignLocations(this.cube);
},
draw: function()
{
var gl = this.gl;
- var prog;
-
var time = this.frametime;
// First Pass: Render to FBO
this.pp.bind();
- prog = this.smear_prog;
- prog.use();
var r = em.Noise.sn2(0, time / 10) * 0.1;
- prog.set_u_mvp_matrix(this.projection.dup().rotate(r, 0,0,1).scale(1.04, 1.04, 1.04));
- prog.set_u_time(this.frametime / 4);
- prog.set_u_scale(0.01);
- prog.set_u_tex(0);
+ this.smear_prog.useUniforms({
+ u_mvp_matrix: this.projection.dup().rotate(r, 0,0,1).scale(1.04, 1.04, 1.04),
+ u_time: this.frametime / 4,
+ u_scale: 0.01,
+ u_tex: 0
+ });
this.pp.bindTexture(0);
this.plane.draw();
this.pp.unbindTexture(0);
- prog = this.color_prog;
- prog.use();
- prog.set_u_mvp_matrix(this.projection);
- prog.set_u_color(new em.Vec4(Math.sin(time * 2), Math.sin(time * 3), Math.cos(time * 4), 0.25));
+ this.color_mater.use();
+ this.color_mater.uniforms.color(
+ new em.Vec4(Math.sin(time * 2), Math.sin(time * 3), Math.cos(time * 4), 0.25)
+ );
this.cube.draw();
this.pp.unbind();
// Second Pass: Render to Screen
- prog = this.tex_prog;
- prog.use();
- prog.set_u_mvp_matrix(this.projection);
- prog.set_u_tex(0);
+ this.tex_mater.use();
this.pp.bindTexture(0);
this.plane.draw();
@@ -1,7 +1,7 @@
varying vec2 v_texcoord;
-#ifdef VERTEX
+#ifdef EM_VERTEX
uniform mat4 u_mvp_matrix;
@@ -17,7 +17,7 @@ varying vec2 v_texcoord;
#endif
-#ifdef FRAGMENT
+#ifdef EM_FRAGMENT
#include "noise3D.glsl"
@@ -1,29 +0,0 @@
-varying vec2 v_texcoord;
-
-
-#ifdef VERTEX
-
- uniform mat4 u_mvp_matrix;
-
- attribute vec3 a_position;
- attribute vec2 a_texcoord;
-
- void main()
- {
- v_texcoord = a_texcoord;
- gl_Position = u_mvp_matrix * vec4(a_position, 1.0);
- }
-
-#endif
-
-
-#ifdef FRAGMENT
-
- uniform sampler2D u_tex;
-
- void main()
- {
- gl_FragColor = texture2D(u_tex, v_texcoord);
- }
-
-#endif
@@ -10,10 +10,18 @@ Embr.ColorMaterial = (function(){
"varying vec4 v_color;",
"#endif",
+ "#ifdef use_texture",
+ "attribute vec2 a_texcoord;",
+ "varying vec2 v_texcoord;",
+ "#endif",
+
"void main(){",
"#ifdef use_vertex_color",
"v_color = a_color;",
"#endif",
+ "#ifdef use_texture",
+ "v_texcoord = a_texcoord;",
+ "#endif",
"gl_Position = projection * modelview * vec4(a_position, 1.0);",
"}"
].join("\n");
@@ -22,26 +30,36 @@ Embr.ColorMaterial = (function(){
"uniform vec4 color;",
"#ifdef use_vertex_color",
- "varying vec4 v_color;",
+ "varying vec4 v_color;",
+ "#endif",
+
+ "#ifdef use_texture",
+ "uniform sampler2D texture;",
+ "varying vec2 v_texcoord;",
"#endif",
"void main(){",
+ "vec4 c = color;",
"#ifdef use_vertex_color",
- "gl_FragColor = v_color * color;",
- "#else",
- "gl_FragColor = color;",
+ "c *= v_color;",
+ "#endif",
+ "#ifdef use_texture",
+ "c *= texture2D(texture, v_texcoord);",
"#endif",
+ "gl_FragColor = c;",
"}"
].join("\n");
var default_options = {
attributes: {
position: "a_position",
+ texcoord: "a_texcoord",
color: "a_color"
},
flags: {
- use_vertex_color: false
+ use_vertex_color: false,
+ use_texture: false
}
};
View
@@ -1,6 +1,9 @@
Embr.Material = (function(){
function Material(gl, src_vertex, src_fragment, options){
+ if(!src_fragment)
+ src_fragment = src_vertex;
+
if(options && options.flags){
var src_prefix = "";
for(var o in options.flags){

0 comments on commit 72747e7

Please sign in to comment.