Commits

Philipp Tiefenbacher  committed 6f5ee61 Draft

basic particle rendering works :)

  • Participants
  • Parent commits f774be5

Comments (0)

Files changed (4)

File src/game/CellSpace.js

 	var context = document.getElementById('c');
 	var reactor = new  EC.Reactor(context, gameW, gameH);
 	reactor.setRenderSize(gameW*zoom, gameH*zoom);
+	var gl = reactor.gl;		
+
 
 	var loader = new EC.ResLoader();
 
 	loader.load("shipExplosionRule", "rules/ship_avg4_nice", "ecfile");
 	loader.load("shipRule", "rules/ship_avg4_nice", "ecfile");
 
+	loader.load("vertexPoints", "src/game/shaders/vertexPoints.vshader", "text");
+	loader.load("drawAll", "src/game/shaders/drawAll.shader", "text");
+
 	loader.load("clear", "src/game/shaders/clear.shader", "text");
-	loader.load("mixPalette", "src/game/shaders/mixPalette.shader", "text");
+	loader.load("mixPalette", "src/game/shaders/mixPalette.shader", "text");	
 
 	loader.load("drawRect", "src/game/shaders/drawRect.shader", "text");
 	loader.load("drawCircle", "src/game/shaders/drawCircle.shader", "text");
 		var bufferDish = reactor.compileDish(64, 64);
 		var renderDish = reactor.compileDish();
 
+
+
+			
+		var pointCoordinates = new Float32Array([-0.5,-0.5,  0.5,0.4]);
+		var pointsBuffer = gl.createBuffer();
+		gl.bindBuffer(gl.ARRAY_BUFFER, pointsBuffer);
+		gl.bufferData(gl.ARRAY_BUFFER, pointCoordinates.byteLength, gl.STATIC_DRAW);
+		gl.bufferSubData(gl.ARRAY_BUFFER, 0, pointCoordinates);
+
+		var drawPointsShader = reactor.compileShader(data.vertexPoints, data.drawAll);
+
+
+
 		var clearShader = reactor.compileShader(data.clear);
 		var paintShader = reactor.compileShader(data.painter);
 
 		
 		var cnt = 0;
 		var gameLoop = new utils.AnimationLoop(function() {
-			
 			// USER INPUT Poll Keyboard //////////////////////////////////////////////////
 			var stepSize = 1.5;
 			if (keyboard.isPressed(keyboard.UP)) shipY += stepSize;
 					}); 
 
 				var pixelValues = new Uint8Array(10*10*4);
-				var gl = reactor.gl;
 
 				gl.bindFramebuffer(gl.FRAMEBUFFER, bufferDish.getCurrentFramebuffer());
 				gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, bufferDish.getCurrentTexture(), 0);
 			//reactor.mixDish(drawRectShader, shipDish, {rectPos: [shipX, shipY], rectSize: [6, 6], state: (shipRule.nrStates-1)/255});
 			reactor.mixDish(drawCircleShader, shipDish, {center: [shipX, shipY], radius: 3.5, state: (shipRule.nrStates-1)/255});
 			//reactor.mixDish(drawRectShader, enemyDish, {rectPos: [shipX+1, shipY+1], rectSize: [3, 3], state: 0});
+			
+
+			if (keyboard.isPressed(65+	1))
+			{
+				reactor.applyShader(drawPointsShader, shipDish.getCurrentFramebuffer(), false, function(gl, shader) {
+
+					gl.bindBuffer(gl.ARRAY_BUFFER, pointsBuffer);
+					var pointPosLoc = gl.getAttribLocation(shader, "pointPos");
+					gl.enableVertexAttribArray(pointPosLoc);
+					gl.vertexAttribPointer(pointPosLoc, 2, gl.FLOAT, gl.FALSE, 0, 0);
+
+					gl.uniform1f(gl.getUniformLocation(shader, "state"), 3./255.);
+
+					gl.drawArrays(gl.POINTS,0, pointCoordinates.length/2);
+				});
+				//shipDish.flip();
+			}
+			
+
 
 			// Dish INTERACTION ///////////////////////////////////
 			reactor.mixDish(intersectSpawnShader, shipExplosionDish, {tex1: shipDish, tex2: enemyDish, state: (shipExplosionRule.nrStates-1)/255.});

File src/game/gl/Reactor.js

 							"void main(void) {" +
 							"	gl_Position = vec4(aPos, 1.);" +
 							"	vTexCoord = aTexCoord;" +
+							"  gl_PointSize = 2.0;" +
 							"}";
 		}
 
 	// GL level
 
 
-	Reactor.prototype.applyShader = function(shader, framebuffer, bindCallback)
+	Reactor.prototype.applyShader = function(shader, framebuffer, bindCallback, renderCallbac)
 	{
 		var gl = this.gl;
-		
-		// is it needed??
-		//this.canvas.width = framebuffer.width;
-		//this.canvas.height = framebuffer.height;
-		//gl.viewport(0,0, framebuffer.width, framebuffer.height);
 	
 		gl.useProgram(shader);
 
 		else
 			gl.bindFramebuffer(gl.FRAMEBUFFER, null);
 
-		// default arguments for all 3D shader (convention)
-		gl.bindBuffer(gl.ARRAY_BUFFER, this.posBuffer);
-		var aPosLoc = gl.getAttribLocation(shader, "aPos");
-		gl.enableVertexAttribArray( aPosLoc );
-		var aTexLoc = gl.getAttribLocation(shader, "aTexCoord");
-		gl.enableVertexAttribArray( aTexLoc );
-		gl.vertexAttribPointer(aPosLoc, 3, gl.FLOAT, gl.FALSE, 0, 0);
-		gl.vertexAttribPointer(aTexLoc, 2, gl.FLOAT, gl.FALSE, 0, this.texCoordOffset);
-	
 		// other arguments
-		bindCallback(gl, shader);
+		
+		if (bindCallback) bindCallback(gl, shader);
 
-		gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+		if (!renderCallbac) {
+			// default arguments for all 3D shader (convention)
+			gl.bindBuffer(gl.ARRAY_BUFFER, this.posBuffer);
+			var aPosLoc = gl.getAttribLocation(shader, "aPos");
+			gl.enableVertexAttribArray( aPosLoc );
+			var aTexLoc = gl.getAttribLocation(shader, "aTexCoord");
+			gl.enableVertexAttribArray( aTexLoc );
+			gl.vertexAttribPointer(aPosLoc, 3, gl.FLOAT, gl.FALSE, 0, 0);
+			gl.vertexAttribPointer(aTexLoc, 2, gl.FLOAT, gl.FALSE, 0, this.texCoordOffset);
+			gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+		}
+		else 
+			renderCallbac(gl, shader);
 	}
 
 	// helpers

File src/game/shaders/drawAll.shader

+#ifdef GL_ES
+	precision highp float;
+#endif
+uniform sampler2D texFrame;
+uniform float state;
+
+varying vec2 vTexCoord;
+void main(void) {
+	gl_FragColor = vec4(0., 0., 0., state);
+}

File src/game/shaders/vertexPoints.vshader

+attribute vec2 pointPos;
+//uniform float pointSize;
+
+void main() {
+  gl_Position = vec4(pointPos, 0.0, 1.0);
+  gl_PointSize = 2.;
+}