mirror of
https://github.com/openglobus/openglobus.git
synced 2025-12-08 19:25:27 +00:00
Directional lighting embeded.
This commit is contained in:
parent
dc215088dd
commit
d7f0e897d5
@ -2,12 +2,14 @@ goog.provide('og.light.PointLight');
|
|||||||
|
|
||||||
goog.require('og.math.Vector3');
|
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._name = name || ("p" + og.light.PointLight._counter++);
|
||||||
this._renderNode = null;
|
this._renderNode = null;
|
||||||
|
|
||||||
this._position = position || new og.math.Vector3();
|
this._position = position || new og.math.Vector3();
|
||||||
|
|
||||||
|
this.directional = true;// = directional || false;
|
||||||
|
|
||||||
this._ambient = ambient || new og.math.Vector3();
|
this._ambient = ambient || new og.math.Vector3();
|
||||||
this._diffuse = diffuse || new og.math.Vector3(0.8, 0.8, 0.8);
|
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);
|
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) {
|
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;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -44,4 +44,37 @@ og.math.Matrix3.prototype.setIdentity = function () {
|
|||||||
this._m[3] = 0; this._m[4] = 1; this._m[5] = 0;
|
this._m[3] = 0; this._m[4] = 1; this._m[5] = 0;
|
||||||
this._m[6] = 0; this._m[7] = 0; this._m[8] = 1;
|
this._m[6] = 0; this._m[7] = 0; this._m[8] = 1;
|
||||||
return this;
|
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;
|
||||||
};
|
};
|
||||||
@ -820,7 +820,7 @@ og.node.Planet.prototype._renderNodesPASS = function () {
|
|||||||
sh = h.shaderPrograms.overlays_wl._program,
|
sh = h.shaderPrograms.overlays_wl._program,
|
||||||
shu = sh.uniforms;
|
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.uniform3fv(shu.pointLightsParamsv._pName, this._pointLightsParamsv);
|
||||||
gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf);
|
gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf);
|
||||||
|
|
||||||
@ -864,7 +864,7 @@ og.node.Planet.prototype._renderNodesPASS = function () {
|
|||||||
sh = h.shaderPrograms.single_wl._program,
|
sh = h.shaderPrograms.single_wl._program,
|
||||||
shu = sh.uniforms;
|
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.uniform3fv(shu.pointLightsParamsv._pName, this._pointLightsParamsv);
|
||||||
gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf);
|
gl.uniform1fv(shu.pointLightsParamsf._pName, this._pointLightsParamsf);
|
||||||
|
|
||||||
|
|||||||
@ -254,8 +254,15 @@ og.node.RenderNode.prototype.setDrawMode = function (mode) {
|
|||||||
og.node.RenderNode.prototype.transformLights = function () {
|
og.node.RenderNode.prototype.transformLights = function () {
|
||||||
var r = this.renderer;
|
var r = this.renderer;
|
||||||
for (var i = 0; i < this._pointLights.length; i++) {
|
for (var i = 0; i < this._pointLights.length; i++) {
|
||||||
var ii = i * 3;
|
var ii = i * 4;
|
||||||
var tp = r.activeCamera._mvMatrix.mulVec3(this._pointLights[i]._position);
|
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] = tp.x;
|
||||||
this._pointLightsTransformedPositions[ii + 1] = tp.y;
|
this._pointLightsTransformedPositions[ii + 1] = tp.y;
|
||||||
this._pointLightsTransformedPositions[ii + 2] = tp.z;
|
this._pointLightsTransformedPositions[ii + 2] = tp.z;
|
||||||
|
|||||||
@ -14,7 +14,7 @@ og.shaderProgram.shape_wl = function () {
|
|||||||
uTRSMatrix: { type: og.shaderProgram.types.MAT4 },
|
uTRSMatrix: { type: og.shaderProgram.types.MAT4 },
|
||||||
uNMatrix: { 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 },
|
pointLightsParamsv: { type: og.shaderProgram.types.VEC3 },
|
||||||
pointLightsParamsf: { type: og.shaderProgram.types.FLOAT },
|
pointLightsParamsf: { type: og.shaderProgram.types.FLOAT },
|
||||||
|
|
||||||
|
|||||||
@ -54,7 +54,7 @@ og.shaderProgram.single_wl = function () {
|
|||||||
fScaleDepth: { type: og.shaderProgram.types.FLOAT },
|
fScaleDepth: { type: og.shaderProgram.types.FLOAT },
|
||||||
fScaleOverScaleDepth: { 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 },
|
pointLightsParamsv: { type: og.shaderProgram.types.VEC3 },
|
||||||
pointLightsParamsf: { type: og.shaderProgram.types.FLOAT }
|
pointLightsParamsf: { type: og.shaderProgram.types.FLOAT }
|
||||||
},
|
},
|
||||||
|
|||||||
@ -16,10 +16,9 @@ const float far = 149.6e+9;
|
|||||||
float logc = 2.0 / log( C * far + 1.0 );
|
float logc = 2.0 / log( C * far + 1.0 );
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
vec4 n = uTRSMatrix * vec4(aVertexNormal, 1.0);
|
|
||||||
vTransformedNormal = uNMatrix * n.xyz;
|
|
||||||
vTextureCoord = aTextureCoord;
|
vTextureCoord = aTextureCoord;
|
||||||
|
|
||||||
|
vTransformedNormal = uNMatrix * (uTRSMatrix * vec4(aVertexNormal, 1.0)).xyz;
|
||||||
vPosition = uMVMatrix * uTRSMatrix * vec4(aVertexPosition, 1.0);
|
vPosition = uMVMatrix * uTRSMatrix * vec4(aVertexPosition, 1.0);
|
||||||
gl_Position = uPMatrix * vPosition;
|
gl_Position = uPMatrix * vPosition;
|
||||||
gl_Position.z = ( log( C * gl_Position.w + 1.0 ) * logc - 1.0 ) * gl_Position.w;
|
gl_Position.z = ( log( C * gl_Position.w + 1.0 ) * logc - 1.0 ) * gl_Position.w;
|
||||||
|
|||||||
@ -5,7 +5,7 @@ uniform sampler2D uNormalMap;
|
|||||||
|
|
||||||
#define MAX_POINT_LIGHTS 1
|
#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 vec3 pointLightsParamsv[MAX_POINT_LIGHTS * 3];
|
||||||
uniform float pointLightsParamsf[MAX_POINT_LIGHTS];
|
uniform float pointLightsParamsf[MAX_POINT_LIGHTS];
|
||||||
uniform mat3 uNMatrix;
|
uniform mat3 uNMatrix;
|
||||||
@ -24,7 +24,8 @@ void main(void) {
|
|||||||
|
|
||||||
vec3 normal = normalize(uNMatrix * ((texture2D(uNormalMap, vTextureCoord.zw).rgb - 0.5) * 2.0));
|
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 eyeDirection = normalize(-vPosition.xyz);
|
||||||
vec3 reflectionDirection = reflect(-lightDirection, normal);
|
vec3 reflectionDirection = reflect(-lightDirection, normal);
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ void main(void) {
|
|||||||
float specularLightWeighting = pow( max( dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]);
|
float specularLightWeighting = pow( max( dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]);
|
||||||
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
|
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
|
||||||
vec3 spec = pointLightsParamsv[2] * specularLightWeighting * shininess;
|
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;
|
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, 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(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);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -186,7 +186,7 @@ og.shape.BaseShape.prototype.draw = function () {
|
|||||||
|
|
||||||
sh.activate();
|
sh.activate();
|
||||||
|
|
||||||
gl.uniform3fv(shu.pointLightsPositions._pName, rn._pointLightsTransformedPositions);
|
gl.uniform4fv(shu.pointLightsPositions._pName, rn._pointLightsTransformedPositions);
|
||||||
gl.uniform3fv(shu.pointLightsParamsv._pName, rn._pointLightsParamsv);
|
gl.uniform3fv(shu.pointLightsParamsv._pName, rn._pointLightsParamsv);
|
||||||
gl.uniform1fv(shu.pointLightsParamsf._pName, rn._pointLightsParamsf);
|
gl.uniform1fv(shu.pointLightsParamsf._pName, rn._pointLightsParamsf);
|
||||||
gl.uniformMatrix4fv(shu.uPMatrix._pName, false, r.activeCamera._pMatrix._m);
|
gl.uniformMatrix4fv(shu.uPMatrix._pName, false, r.activeCamera._pMatrix._m);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user