Example #1
0
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------
        private void RenderLight(ShaderGroup lightShader, LightComponent component, MeshItem lightVolume)
        {
            LightItem light = (LightItem) this._library.GetResource(component.LightHandle);

            if(light == null)
                return;

            float intensity = (float) light.Properties["intensity"];

            /*update sphere scale matrix*/
            this._scaleMatrix.Elements[0] = intensity * 2;
            this._scaleMatrix.Elements[5] = intensity * 2;
            this._scaleMatrix.Elements[10] = intensity * 2;
            this._scaleMatrix.Elements[15] = 1.0f;

            this._vMatrix.MultiplyM2(component.Parent.World, this._mvMatrix);
            this._mvMatrix.MultiplyM2(this._scaleMatrix, this._mvMatrix);

            Vector3 pos = this._mvMatrix.TransformV(new Vector3(new float[] {0.0f, 0.0f, 0.0f}));

            /*check view occlusion*/
            this.UpdateCullInfo(this._mvMatrix);

            /*test for view occlusion*/
            if (this._occluder != null && !this._occluder.Test(this._cullInfo, null, lightVolume))
                return;

            /*bind shader, mesh and buffers*/
            lightShader.ShaderBinder.BindLight(pos, light, this._mvMatrix, this._pMatrix);
            this._context.DrawElements(WebGLE.Triangles, this._lightSphereVolume.Indexes.Length, WebGLE.UnsignedShortT, 0);
        }
Example #2
0
 //------------------------------------------------------------------------------------------
 //------------------------------------------------------------------------------------------
 public void AddShaderGroup(ShaderGroup shaderGroup)
 {
     this._shaderGroups[shaderGroup.Name] = shaderGroup;
 }
Example #3
0
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------
        public void Initialize(float fov, float zNear, float zFar)
        {
            this.DisplayDebugInformation();
            WebGLGraphics that = this;

            /*render texture dimensions*/
            this._renderTextureDim = this.SmallestPowerOfTwo(_canvas.Width);
            this._renderViewport =
                SystemCore.Environment.CreateFloat32ArrayFromArray(new float[] { _renderTextureDim, _renderTextureDim, });

            this._shaderGroups = new Dictionary<string, ShaderGroup>();

            /*rendering shaders*/
            ShaderGroup phongShaderGroup = new ShaderGroup();
            phongShaderGroup.Name = "phong";
            phongShaderGroup.ShaderBinder = new PhongBinder(that.Library, that._context, phongShaderGroup);

            SystemCore.ResourceManager.GetResource("/Data/Shader/phong_pass_geom.shader", null).ResourceChanged.Subscribe(
                delegate(object sender, object args) {
                Resource resource = (Resource) sender;

                if (resource.Finished) {
                    phongShaderGroup.GeometryPassShader = (CompiledShader) resource.Data;
                }
            }, null);

            SystemCore.ResourceManager.GetResource("/Data/Shader/phong_pass_prepost.shader", null).ResourceChanged.Subscribe(
                delegate(object sender, object args) {
                Resource resource = (Resource)sender;

                if (resource.Finished) {
                    phongShaderGroup.PrePostProcessPassShader = (CompiledShader)resource.Data;
                }
            }, null);

            SystemCore.ResourceManager.GetResource("/Data/Shader/phong_pass_light.shader", null).ResourceChanged.Subscribe(
                delegate(object sender, object args) {
                Resource resource = (Resource)sender;

                if (resource.Finished) {
                    phongShaderGroup.LightPassShader = (CompiledShader)resource.Data;
                }
            }, null);

            SystemCore.ResourceManager.GetResource("/Data/Shader/phong_pass_final.shader", null).ResourceChanged.Subscribe(
                delegate(object sender, object args) {
                Resource resource = (Resource)sender;

                if (resource.Finished) {
                    phongShaderGroup.FinalPassShader = (CompiledShader)resource.Data;
                }
            }, null);

            this.AddShaderGroup(phongShaderGroup);
            this.ActiveShaderGroup = "phong";

            /*core library - which contain things like light bounding meshes*/
            Library.LoadLibrary("/Data/JSON/core.json").Finished.Subscribe(delegate(object sender, object args) {
                Handle sphereHandle = new Handle();
                sphereHandle.Collection = "core";
                sphereHandle.Id = "point_light_sphere-lib";

                that._lightSphereVolume = ((MeshItem)Library.GetResource(sphereHandle));
            }, true);

            /*enable webGL float texture extension*/
            SystemCore.Logger.Log(this._context.GetExtension("OES_texture_float").ToString());

            this.SetupFrameBuffers();

            /*webgl render queues*/
            this._opaqueMeshQueue = new List<List<Node>>();
            this._transparentMeshQueue = new List<List<Node>>();
            this._lightQueue = new Dictionary<int, List<Node>>();

            /*camera*/
            this._camera = new Node();
            this._camera.AddComponent(new CameraComponent());

            /*perspective*/
            this._scaleMatrix = new Matrix4X4(null);
            this._mvMatrix = new Matrix4X4(null);
            this._vMatrix = new Matrix4X4(null);
            this._nMatrix = new Matrix4X4(null);
            this._pMatrix = Matrix4X4.MakePerspective(fov, 800.0f / 600.0f, zNear, zFar);

            this._context.Viewport(0, 0, this._renderTextureDim, this._renderTextureDim);
            this._context.EnableVertexAttribArray(0);
            this._context.EnableVertexAttribArray(1);
            this._context.EnableVertexAttribArray(2);

            this._context.Enable(WebGLE.DepthTest);
            this._context.Enable(WebGLE.CullFace);
            this._context.BlendFunc(WebGLE.SrcAlpha, WebGLE.One);

            /*occlusion*/
            this._cullInfo = new CullInfo();

            /*post process effects*/
            this._effects = new List<IPostProcessEffect>();

            /*active texture targets*/
            this._activeTextureTarget = new List<int>();
            this._activeTextureTarget[0] = WebGLE.Texture0;
            this._activeTextureTarget[1] = WebGLE.Texture1;
            this._activeTextureTarget[2] = WebGLE.Texture2;
            /*I don't need any more for now*/
        }
Example #4
0
        public PhongBinder(Library library, WebGL graphics, ShaderGroup parent)
        {
            if(parent == null)
                throw new Exception("Parent cannot be null");
            if (graphics == null)
                throw new Exception("Graphics cannot be null");
            if (library == null)
                throw new Exception("Library cannot be null");

            this._library = library;
            this._graphics = graphics;
            this._parent = parent;

            this._vertexBuffer = this._graphics.CreateBuffer();
            this._graphics.BindBuffer(WebGLE.ArrayBuffer, _vertexBuffer);
            float[] vertices = SystemCore.Environment.CreateFloat32ArrayFromArray(new float[] {
                1.0f, 1.0f, 0.0f,
                -1.0f, 1.0f, 0.0f,
                1.0f, -1.0f, 0.0f,
                -1.0f, -1.0f, 0.0f
            });
            this._graphics.BufferData(WebGLE.ArrayBuffer, vertices, WebGLE.StaticDraw);
        }