Browse Source

cube with texture ok

Nicolas Cannasse 12 years ago
parent
commit
b605c461a5
6 changed files with 86 additions and 12 deletions
  1. 11 1
      engine.hxml
  2. 1 1
      engine.hxproj
  3. 29 4
      h3d/impl/WebglDriver.hx
  4. 7 4
      h3d/mat/MeshMaterial.hx
  5. 37 1
      hxd/Resource.hx
  6. 1 1
      samples/basic/Test.hx

+ 11 - 1
engine.hxml

@@ -1,8 +1,18 @@
 -cp samples/basic
 -js engine.js
 -main Test
+--macro include('h3d')
+--macro include('h2d')
+--macro include('hxd')
+-lib format
+--next
+-swf engine.swf
+-swf-header 800:600:60:000000
+-swf-version 11.6
 -lib hxsl
 -D h3d
--swf-version 11.6
+-cp samples/basic
+-main Test
 --macro include('h3d')
 --macro include('h2d')
+--macro include('hxd')

+ 1 - 1
engine.hxproj

@@ -23,7 +23,7 @@
     <option flashStrict="True" />
     <option mainClass="Test" />
     <option enabledebug="False" />
-    <option additional="-lib hxsl&#xA;-D h3d&#xA;-swf-version 11.6&#xA;--macro include('h3d')&#xA;--macro include('h2d')" />
+    <option additional="--macro include('h3d')&#xA;--macro include('h2d')&#xA;--macro include('hxd')&#xA;-lib format&#xA;&#xA;--next&#xA;&#xA;-swf engine.swf&#xA;-swf-header 800:600:60:000000&#xA;-swf-version 11.6&#xA;-lib hxsl&#xA;-D h3d&#xA;-cp samples/basic&#xA;-main Test&#xA;--macro include('h3d')&#xA;--macro include('h2d')&#xA;--macro include('hxd')" />
   </build>
   <!-- haxelib libraries -->
   <haxelib>

+ 29 - 4
h3d/impl/WebglDriver.hx

@@ -1,6 +1,8 @@
 package h3d.impl;
 import h3d.impl.Driver;
 
+#if js
+
 private typedef GL = js.html.webgl.GL;
 
 @:access(h3d.impl.Shader)
@@ -18,11 +20,17 @@ class WebglDriver extends Driver {
 		if( gl == null ) throw "Could not acquire GL context";
 		// debug if webgl_debug.js is included
 		untyped if( __js__('typeof')(WebGLDebugUtils) != "undefined" ) gl = untyped WebGLDebugUtils.makeDebugContext(gl);
+		gl.enable(GL.DEPTH_TEST);
+	}
+	
+	override function selectMaterial( mbits : Int ) {
+		gl.depthFunc(GL.LESS);
+		gl.cullFace(GL.BACK);
 	}
 	
 	override function clear( r : Float, g : Float, b : Float, a : Float ) {
 		gl.clearColor(r, g, b, a);
-		gl.clearDepth(0);
+		gl.clearDepth(1);
 		gl.clear(GL.COLOR_BUFFER_BIT|GL.DEPTH_BUFFER_BIT);
 	}
 	
@@ -157,14 +165,22 @@ class WebglDriver extends Driver {
 		var mat : Matrix = shader.mproj;
 		gl.uniformMatrix4fv(mproj, false, new js.html.Float32Array(mat.getFloats()));
 		
+		var tex : h3d.mat.Texture = shader.tex;
+		gl.activeTexture(GL.TEXTURE0);
+		gl.bindTexture(GL.TEXTURE_2D, tex.t);
+		var flags = TFILTERS[Type.enumIndex(tex.mipMap)][Type.enumIndex(tex.filter)];
+		gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, flags[0]);
+		gl.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, flags[1]);
+		gl.uniform1i(gl.getUniformLocation(shader.program, "tex"), 0);
+		
 		return true;
 	}
 	
 	override function selectBuffer( v : VertexBuffer ) {
 		var stride : Int = untyped v.stride;
 		gl.bindBuffer(GL.ARRAY_BUFFER, v);
-		gl.vertexAttribPointer(0, 3, GL.FLOAT, false, stride * 4, 0);
-		//gl.vertexAttribPointer(1, 2, GL.FLOAT, false, stride * 4, 3 * 4);
+		gl.vertexAttribPointer(1, 3, GL.FLOAT, false, stride * 4, 0);
+		gl.vertexAttribPointer(0, 2, GL.FLOAT, false, stride * 4, 3 * 4);
 	}
 	
 	override function draw( ibuf : IndexBuffer, startIndex : Int, ntriangles : Int ) {
@@ -184,4 +200,13 @@ class WebglDriver extends Driver {
 	override function init( onCreate : Bool -> Void, forceSoftware = false ) {
 		haxe.Timer.delay(onCreate.bind(false), 1);
 	}
-}
+	
+	static var TFILTERS = [
+		[[GL.NEAREST,GL.NEAREST],[GL.LINEAR,GL.LINEAR]],
+		[[GL.NEAREST,GL.NEAREST_MIPMAP_NEAREST],[GL.LINEAR,GL.LINEAR_MIPMAP_NEAREST]],
+		[[GL.NEAREST,GL.NEAREST_MIPMAP_LINEAR],[GL.LINEAR,GL.LINEAR_MIPMAP_LINEAR]],
+	];
+	
+}
+
+#end

+ 7 - 4
h3d/mat/MeshMaterial.hx

@@ -154,24 +154,27 @@ private class MeshShader extends h3d.impl.Shader {
 	static var VERTEX = "
 	
 		attribute vec3 pos;
-		//attribute vec2 uv;
+		attribute vec2 uv;
 
 		uniform mat4 mpos;
 		uniform mat4 mproj;
 		
-		//varying lowp vec2 tuv;
+		varying lowp vec2 tuv;
 
 		void main(void) {
 			gl_Position = mproj * mpos * vec4(pos, 1.0);
-			//tuv = uv;
+			tuv = uv;
 		}
 
 	";
 	
 	static var FRAGMENT = "
 	
+		varying lowp vec2 tuv;
+		uniform sampler2D tex;
+	
 		void main(void) {
-			gl_FragColor = vec4(1.0, 0, 0, 1.0);
+			gl_FragColor = texture2D(tex, tuv);
 		}
 			
 	";

+ 37 - 1
hxd/Resource.hx

@@ -17,6 +17,15 @@ abstract BitmapRes(String) {
 			var png = new format.png.Reader(new haxe.io.BytesInput(res)).read();
 			var pngBytes = format.png.Tools.extract32(png);
 			var pngHeader = format.png.Tools.getHeader(png);
+			// converts BGRA to RGBA
+			var pos = 0;
+			for( i in 0...pngHeader.width * pngHeader.height ) {
+				var b = pngBytes.get(pos);
+				var r = pngBytes.get(pos + 2);
+				pngBytes.set(pos, r);
+				pngBytes.set(pos + 2, b);
+				pos += 4;
+			}
 			var tex = engine.mem.allocTexture(pngHeader.width, pngHeader.height);
 			tex.uploadBytes(pngBytes);
 			return tex;
@@ -25,6 +34,21 @@ abstract BitmapRes(String) {
 		}
 	}
 }
+
+#elseif flash
+
+abstract BitmapRes(Class<flash.display.BitmapData>) {
+	public function new( cl ) {
+		this = cl;
+	}
+	public function toTexture() {
+		var bmp = Type.createInstance(this, [0, 0]);
+		var tex = h3d.mat.Texture.fromBitmap(hxd.BitmapData.fromNative(bmp));
+		bmp.dispose();
+		return tex;
+	}
+}
+		
 #end
 
 
@@ -37,7 +61,19 @@ class Resource {
 			switch( ext ) {
 			case "png", "jpg":
 				if( Context.defined("flash") ) {
-					throw "TODO";
+					var className = "I_" + StringTools.urlEncode(path.split(".").join("_")).split("%").join("_").split(" ").join("_");
+					var pos = Context.currentPos();
+					Context.defineType( {
+						pos : pos,
+						params : [],
+						pack : ["res"],
+						name : className,
+						meta : [ { name : ":bitmap", params : [ { expr : EConst(CString(path)), pos : pos } ], pos : pos } ],
+						kind : TDClass({ pack : ["flash","display"], name : "BitmapData", params : [] }),
+						isExtern : false,
+						fields : [],
+					});
+					return macro new hxd.Resource.BitmapRes(res.$className);
 				} else if( Context.defined("js") ) {
 					Context.addResource(fileName, sys.io.File.getBytes(path));
 					return macro new hxd.Resource.BitmapRes($v{fileName});

+ 1 - 1
samples/basic/Test.hx

@@ -12,7 +12,7 @@ class Test {
 		time = 0;
 		engine = new h3d.Engine();
 		engine.debug = true;
-		engine.backgroundColor = 0x202020;
+		engine.backgroundColor = 0xFF202020;
 		engine.onReady = start;
 		engine.init();
 	}