Browse Source

Basic support for RTT.

Szymon Nowak 14 years ago
parent
commit
1d706dd789
3 changed files with 67 additions and 2 deletions
  1. 17 0
      src/materials/RenderTexture.js
  2. 5 1
      src/materials/Texture.js
  3. 45 1
      src/renderers/WebGLRenderer.js

+ 17 - 0
src/materials/RenderTexture.js

@@ -0,0 +1,17 @@
+THREE.RenderTexture = function ( width, height, options ) {
+
+	this.width = width;
+	this.height = height;
+
+	options = options || {};
+
+	this.wrap_s = options.wrap_s !== undefined ? options.wrap_s : THREE.ClampToEdgeWrapping;
+	this.wrap_t = options.wrap_t !== undefined ? options.wrap_t : THREE.ClampToEdgeWrapping;
+
+	this.mag_filter = options.mag_filter !== undefined ? options.mag_filter : THREE.LinearFilter;
+	this.min_filter = options.min_filter !== undefined ? options.min_filter : THREE.LinearFilter;
+
+	this.format = options.format !== undefined ? options.format : THREE.RGBFormat;
+	this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType;
+
+};

+ 5 - 1
src/materials/Texture.js

@@ -24,7 +24,7 @@ THREE.Texture.prototype = {
 		return new THREE.Texture( this.image, this.mapping, this.wrap_s, this.wrap_t, this.mag_filter, this.min_filter );
 
 	},
-	
+
 	toString: function () {
 
 		return 'THREE.Texture (<br/>' +
@@ -52,3 +52,7 @@ THREE.NearestMipMapLinearFilter = 5;
 THREE.LinearFilter = 6;
 THREE.LinearMipMapNearestFilter = 7;
 THREE.LinearMipMapLinearFilter = 8;
+
+THREE.RGBFormat = 9;
+
+THREE.UnsignedByteType = 10;

+ 45 - 1
src/renderers/WebGLRenderer.js

@@ -658,7 +658,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	};
 
-	this.render = function( scene, camera ) {
+	this.render = function( scene, camera, renderTarget ) {
 
 		var o, ol, webGLObject, object, buffer,
 			lights = scene.lights,
@@ -666,6 +666,8 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 		this.initWebGLObjects( scene );
 
+		setRenderTarget( renderTarget );
+
 		if ( this.autoClear ) {
 
 			this.clear();
@@ -1159,6 +1161,44 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 	};
 
+	function setRenderTarget( renderTexture ) {
+
+		var framebuffer;
+
+		if ( renderTexture && !renderTexture.__webGLFramebuffer ) {
+			renderTexture.__webGLFramebuffer = _gl.createFramebuffer();
+			renderTexture.__webGLRenderbuffer = _gl.createRenderbuffer();
+			renderTexture.__webGLTexture = _gl.createTexture();
+
+			// Setup renderbuffer
+			_gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer );
+			_gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTexture.width, renderTexture.height );
+
+			// Setup texture
+			_gl.bindTexture( _gl.TEXTURE_2D, renderTexture.__webGLTexture );
+			_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( renderTexture.wrap_s ) );
+			_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( renderTexture.wrap_t ) );
+			_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( renderTexture.mag_filter ) );
+			_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( renderTexture.min_filter ) );
+			_gl.generateMipmap(_gl.TEXTURE_2D);
+			_gl.texImage2D( _gl.TEXTURE_2D, 0, paramThreeToGL( renderTexture.format ), renderTexture.width, renderTexture.height, 0, paramThreeToGL( renderTexture.format ), paramThreeToGL( renderTexture.type ), null);
+
+			// Setup framebuffer
+			_gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTexture.__webGLFramebuffer );
+			_gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, renderTexture.__webGLTexture, 0 );
+			_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer);
+
+			// Release everything
+			_gl.bindTexture( _gl.TEXTURE_2D, null );
+			_gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
+			_gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
+		}
+
+		framebuffer = renderTexture ? renderTexture.__webGLFramebuffer : null;
+		_gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
+
+	}
+
 	function cacheUniformLocations( program, identifiers ) {
 
 		var i, l, id;
@@ -1229,6 +1269,10 @@ THREE.WebGLRenderer = function ( parameters ) {
 			case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
 			case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
 
+			case THREE.RGBFormat: return _gl.RGB; break;
+
+			case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
+
 		}
 
 		return 0;