ncannasse / hxsl

Haxe Shader Language, a high level 3D shader language for Haxe
86 stars 10 forks source link

Matrix multiplication (+ small Camera problem) #21

Open Lemage74 opened 11 years ago

Lemage74 commented 11 years ago

Hi, Here a big and a small problem. The big one is about matrix multiplication. To make an application targeting AS3, I took the example 2_texture as a base. As I wanted the projection matrix to affect the direction of the light (It was a mistake, but it doesn't matter here), so I replaced: function vertex( mpos : M44, mproj : M44, light : Float3 ) { out = input.pos.xyzw * mpos * mproj; var tnorm = (input.norm * mpos).normalize(); lpow = light.dot(tnorm).max(0); tuv = input.uv; } by function vertex( mpos : M44, mproj : M44, light : Float3 ) { out = input.pos.xyzw * mpos * mproj; var tnorm = (input.norm * mpos * mproj).normalize(); lpow = light.dot(tnorm).max(0); tuv = input.uv; } then I wanted to optimize by putting mpos_mproj in an variable, so I changed it for: function vertex( mpos : M44, mproj : M44, light : Float3 ) { var mposproj:M44 = mpos_mproj; out = input.pos.xyzw * mposproj; var tnorm = (input.norm * mposproj).normalize(); lpow = light.dot(tnorm).max(0); tuv = input.uv; } and got an error on mpos_mproj: M44 should be M44 I changed mposproj declaration for: var mposproj = mpos_mproj; and then got an error on input.pos.xyzw * mposproj: M44 should be Float4

The small problem come with the Camera class that is given with the examples. As the X,Y,Z vectors in Stage3D form a left-hand frame (see http://www.adobe.com/devnet/flashplayer/articles/3d-cameras.html) with X axis to right, Y to up and Z to front, the resulting projection matrix of Camera class comes reversing bottom and up. In my case, I solved the problem replacing line 44: var ay = az.cross(ax); by var ay = ax.cross(az); It solved the problem with AS3 but it could cause it for other platforms. Maybe a better solution could be fine using precompilation instructions.