diff --git a/src/og/light/pointLight.js b/src/og/light/pointLight.js index dbf488e8..8882904e 100644 --- a/src/og/light/pointLight.js +++ b/src/og/light/pointLight.js @@ -2,12 +2,14 @@ goog.provide('og.light.PointLight'); goog.require('og.math.Vector3'); -og.light.PointLight = function (name, position, ambient, diffuse, specular, shininess) { +og.light.PointLight = function (name, position, ambient, diffuse, specular, shininess, directional) { this._name = name || ("p" + og.light.PointLight._counter++); this._renderNode = null; this._position = position || new og.math.Vector3(); + this.directional = true;// = directional || false; + this._ambient = ambient || new og.math.Vector3(); this._diffuse = diffuse || new og.math.Vector3(0.8, 0.8, 0.8); this._specular = specular || new og.math.Vector3(0.18, 0.18, 0.18); @@ -65,7 +67,9 @@ og.light.PointLight.prototype.isActive = function () { og.light.PointLight.prototype.setPosition = function (position) { - this._position = position; + this._position.x = position.x; + this._position.y = position.y; + this._position.z = position.z; return this; }; diff --git a/src/og/math/matrix3.js b/src/og/math/matrix3.js index 2a12cd39..b55e8782 100644 --- a/src/og/math/matrix3.js +++ b/src/og/math/matrix3.js @@ -44,4 +44,37 @@ og.math.Matrix3.prototype.setIdentity = function () { this._m[3] = 0; this._m[4] = 1; this._m[5] = 0; this._m[6] = 0; this._m[7] = 0; this._m[8] = 1; return this; +}; + +og.math.Matrix3.prototype.mulVec = function (p) { + var d = p.x, e = p.y, g = p.z; + var m = this._m; + return new og.math.Vector3( + m[0] * d + m[3] * e + m[6] * g, + m[1] * d + m[4] * e + m[7] * g, + m[2] * d + m[5] * e + m[8] * g + ); +}; + +og.math.Matrix3.prototype.toMatrix4 = function () { + var res = new og.math.Matrix4(); + var b = res._m; + var a = this._m; + b[0] = a[0]; + b[1] = a[1]; + b[2] = a[2]; + b[3] = 0; + b[4] = a[3]; + b[5] = a[4]; + b[6] = a[5]; + b[7] = 0; + b[8] = a[6]; + b[9] = a[7]; + b[10] = a[8]; + b[11] = 0; + b[12] = 0; + b[13] = 0; + b[14] = 0; + b[15] = 1; + return res; }; \ No newline at end of file diff --git a/src/og/node/planet.js b/src/og/node/planet.js index 336ed088..c77e7933 100644 --- a/src/og/node/planet.js +++ b/src/og/node/planet.js @@ -820,7 +820,7 @@ og.node.Planet.prototype._renderNodesPASS = function () { sh = h.shaderPrograms.overlays_wl._program, shu = sh.uniforms; - gl.uniform3fv(shu.pointLightsPositions._pName, this._pointLightsTransformedPositions); + gl.uniform4fv(shu.pointLightsPositions._pName, this._pointLightsTransformedPositions); gl.uniform3fv(shu.pointLightsParamsv._pName, this._pointLightsParamsv); gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf); @@ -864,7 +864,7 @@ og.node.Planet.prototype._renderNodesPASS = function () { sh = h.shaderPrograms.single_wl._program, shu = sh.uniforms; - gl.uniform3fv(shu.pointLightsPositions._pName, this._pointLightsTransformedPositions); + gl.uniform4fv(shu.pointLightsPositions._pName, this._pointLightsTransformedPositions); gl.uniform3fv(shu.pointLightsParamsv._pName, this._pointLightsParamsv); gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf); diff --git a/src/og/node/renderNode.js b/src/og/node/renderNode.js index 7bb38df1..0f0b40e0 100644 --- a/src/og/node/renderNode.js +++ b/src/og/node/renderNode.js @@ -254,8 +254,15 @@ og.node.RenderNode.prototype.setDrawMode = function (mode) { og.node.RenderNode.prototype.transformLights = function () { var r = this.renderer; for (var i = 0; i < this._pointLights.length; i++) { - var ii = i * 3; - var tp = r.activeCamera._mvMatrix.mulVec3(this._pointLights[i]._position); + var ii = i * 4; + var tp; + if (this._pointLights[i].directional) { + tp = r.activeCamera._nMatrix.mulVec(this._pointLights[i]._position); + this._pointLightsTransformedPositions[ii + 3] = 0; + } else { + tp = r.activeCamera._mvMatrix.mulVec3(this._pointLights[i]._position); + this._pointLightsTransformedPositions[ii + 3] = 1; + } this._pointLightsTransformedPositions[ii] = tp.x; this._pointLightsTransformedPositions[ii + 1] = tp.y; this._pointLightsTransformedPositions[ii + 2] = tp.z; diff --git a/src/og/shaderProgram/shape.js b/src/og/shaderProgram/shape.js index a8842e53..cb307a12 100644 --- a/src/og/shaderProgram/shape.js +++ b/src/og/shaderProgram/shape.js @@ -14,7 +14,7 @@ og.shaderProgram.shape_wl = function () { uTRSMatrix: { type: og.shaderProgram.types.MAT4 }, uNMatrix: { type: og.shaderProgram.types.MAT4 }, - pointLightsPositions: { type: og.shaderProgram.types.VEC3 }, + pointLightsPositions: { type: og.shaderProgram.types.VEC4 }, pointLightsParamsv: { type: og.shaderProgram.types.VEC3 }, pointLightsParamsf: { type: og.shaderProgram.types.FLOAT }, diff --git a/src/og/shaderProgram/single.js b/src/og/shaderProgram/single.js index 7fa2dee5..16c6a7e7 100644 --- a/src/og/shaderProgram/single.js +++ b/src/og/shaderProgram/single.js @@ -54,7 +54,7 @@ og.shaderProgram.single_wl = function () { fScaleDepth: { type: og.shaderProgram.types.FLOAT }, fScaleOverScaleDepth: { type: og.shaderProgram.types.FLOAT }, - pointLightsPositions: { type: og.shaderProgram.types.VEC3 }, + pointLightsPositions: { type: og.shaderProgram.types.VEC4 }, pointLightsParamsv: { type: og.shaderProgram.types.VEC3 }, pointLightsParamsf: { type: og.shaderProgram.types.FLOAT } }, diff --git a/src/og/shaders/shape_wl_vs.txt b/src/og/shaders/shape_wl_vs.txt index 2d269775..70b0ebf2 100644 --- a/src/og/shaders/shape_wl_vs.txt +++ b/src/og/shaders/shape_wl_vs.txt @@ -16,10 +16,9 @@ const float far = 149.6e+9; float logc = 2.0 / log( C * far + 1.0 ); void main(void) { - vec4 n = uTRSMatrix * vec4(aVertexNormal, 1.0); - vTransformedNormal = uNMatrix * n.xyz; vTextureCoord = aTextureCoord; + vTransformedNormal = uNMatrix * (uTRSMatrix * vec4(aVertexNormal, 1.0)).xyz; vPosition = uMVMatrix * uTRSMatrix * vec4(aVertexPosition, 1.0); gl_Position = uPMatrix * vPosition; gl_Position.z = ( log( C * gl_Position.w + 1.0 ) * logc - 1.0 ) * gl_Position.w; diff --git a/src/og/shaders/single_wl_fs.txt b/src/og/shaders/single_wl_fs.txt index ca01b481..ee838b1b 100644 --- a/src/og/shaders/single_wl_fs.txt +++ b/src/og/shaders/single_wl_fs.txt @@ -5,7 +5,7 @@ uniform sampler2D uNormalMap; #define MAX_POINT_LIGHTS 1 -uniform vec3 pointLightsPositions[MAX_POINT_LIGHTS]; +uniform vec4 pointLightsPositions[MAX_POINT_LIGHTS]; uniform vec3 pointLightsParamsv[MAX_POINT_LIGHTS * 3]; uniform float pointLightsParamsf[MAX_POINT_LIGHTS]; uniform mat3 uNMatrix; @@ -24,7 +24,8 @@ void main(void) { vec3 normal = normalize(uNMatrix * ((texture2D(uNormalMap, vTextureCoord.zw).rgb - 0.5) * 2.0)); - vec3 lightDirection = normalize(pointLightsPositions[0]/* - vPosition.xyz*/); + vec3 lightDirection = normalize(pointLightsPositions[0].xyz - vPosition.xyz * pointLightsPositions[0].w); + vec3 eyeDirection = normalize(-vPosition.xyz); vec3 reflectionDirection = reflect(-lightDirection, normal); @@ -36,7 +37,7 @@ void main(void) { float specularLightWeighting = pow( max( dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]); float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0); vec3 spec = pointLightsParamsv[2] * specularLightWeighting * shininess; - vec3 lightWeighting = pointLightsParamsv[0] + pointLightsParamsv[1] * diffuseLightWeighting*c0 + c1 + spec; + vec3 lightWeighting = pointLightsParamsv[0] + pointLightsParamsv[1] * diffuseLightWeighting * c0 + c1 + spec; vec3 night = 10.0 * vec3(0.58, 0.48, 0.25) * (0.31 - length(c0)) * nightImageColor.rgb; @@ -44,7 +45,8 @@ void main(void) { //gl_FragColor = vec4(c1, 1.0) + vec4(res.rgb * c0 + res.rgb*lightWeighting*(1.0-c0) + night * step(0.0, night) + spec, res.a); //gl_FragColor = vec4(c1*lightWeighting + res.rgb * c0 *lightWeighting + res.rgb * lightWeighting * (1.0 - c0) + night * step(0.0, night) + spec , res.a); + //gl_FragColor = vec4(res.rgb*lightWeighting + night * step(0.0, night) + spec, res.a); - gl_FragColor = vec4(res.rgb*lightWeighting + night * step(0.0, night) + spec, res.a); + gl_FragColor = vec4(res.rgb * lightWeighting + night * step(0.0, night) + spec, res.a); } \ No newline at end of file diff --git a/src/og/shapes/baseShape.js b/src/og/shapes/baseShape.js index 4d5ab322..7b22839e 100644 --- a/src/og/shapes/baseShape.js +++ b/src/og/shapes/baseShape.js @@ -186,7 +186,7 @@ og.shape.BaseShape.prototype.draw = function () { sh.activate(); - gl.uniform3fv(shu.pointLightsPositions._pName, rn._pointLightsTransformedPositions); + gl.uniform4fv(shu.pointLightsPositions._pName, rn._pointLightsTransformedPositions); gl.uniform3fv(shu.pointLightsParamsv._pName, rn._pointLightsParamsv); gl.uniform1fv(shu.pointLightsParamsf._pName, rn._pointLightsParamsf); gl.uniformMatrix4fv(shu.uPMatrix._pName, false, r.activeCamera._pMatrix._m);