Esempio n. 1
0
        public void Render(CameraOrbit camera)
        {
            if (ReferenceEquals(null, shaderObject))
            {
                return;
            }
            if (ReferenceEquals(null, shaderShadow))
            {
                return;
            }

            // calculate value for candle light flickering
            timeSpan = stopWatch.Elapsed;
            double elapsedTime = timeSpan.TotalMilliseconds;
            var    factor      = 1;

            if (smokeState)
            {
                factor = 4;
            }
            float candleFlickering = (float)Math.Sin((elapsedTime / 1000 / factor) + (.2 + random.NextDouble())) / 30 * factor;

            // setup light for shadow
            cameraLight.Distance = this.lightPosition[1] + candleFlickering;
            var light = cameraLight.CalcMatrix().ToOpenTK();

            shaderShadow.Activate();
            fboShadowMap.Activate();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.UniformMatrix4(shaderShadow.GetUniformLocation("camera"), true, ref light);
            this.DrawScene(shaderShadow, cameraLight, candleFlickering);
            fboShadowMap.Deactivate();
            shaderShadow.Deactivate();

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            shaderObject.Activate();
            fboShadowMap.Texture.Activate();
            var cam = camera.CalcMatrix().ToOpenTK();

            GL.UniformMatrix4(shaderObject.GetUniformLocation("camera"), true, ref cam);
            GL.UniformMatrix4(shaderObject.GetUniformLocation("light"), true, ref light);
            this.DrawScene(shaderObject, camera, candleFlickering);
            fboShadowMap.Texture.Deactivate();
            shaderObject.Deactivate();

            // draw environment
            shaderObject.Activate();
            this.DrawEnvironment(shaderObject, camera);
            shaderObject.Deactivate();
        }
Esempio n. 2
0
        public override void Initialize(HostInterface CurrentHost, BaseOptions CurrentOptions)
        {
            base.Initialize(CurrentHost, CurrentOptions);

            try
            {
                if (pickingShader == null)
                {
                    pickingShader = new Shader(this, "default", "picking", true);
                }
                pickingShader.Activate();
                pickingShader.Deactivate();
            }
            catch
            {
                Interface.AddMessage(MessageType.Error, false, "Initializing the touch shader failed- Falling back to legacy openGL.");
                Interface.CurrentOptions.IsUseNewRenderer = false;
                ForceLegacyOpenGL = true;
            }

            events               = new Events(this);
            overlays             = new Overlays(this);
            Touch                = new Touch(this);
            ObjectsSortedByStart = new int[] { };
            ObjectsSortedByEnd   = new int[] { };
            Program.FileSystem.AppendToLogFile("Renderer initialised successfully.");
        }
Esempio n. 3
0
        internal Touch(NewRenderer renderer)
        {
            this.renderer = renderer;

            objectStates = new List <ObjectState>();
            touchFaces   = new List <FaceState>();
            try
            {
                pickingShader = new Shader("default", "picking", true);
                pickingShader.Activate();
                pickingShader.Deactivate();
            }
            catch
            {
                Interface.AddMessage(MessageType.Error, false, "Initialising the touch shader failed- Falling back to legacy openGL.");
                Interface.CurrentOptions.IsUseNewRenderer = false;
            }


            fbo = new FrameBufferObject();
            fbo.Bind();
            fbo.SetTextureBuffer(FrameBufferObject.TargetBuffer.Color, PixelInternalFormat.R32f, PixelFormat.Red, PixelType.Float, renderer.Screen.Width, renderer.Screen.Height);
            fbo.DrawBuffers(new[] { DrawBuffersEnum.ColorAttachment0 });
            fbo.UnBind();
        }
Esempio n. 4
0
        private void DrawWithShader(Texture texture, Vector2 point, Vector2 size, Color128?color, Vector2 coordinates)
        {
            Shader.Activate();
            if (texture != null && renderer.currentHost.LoadTexture(ref texture, OpenGlTextureWrapMode.ClampClamp))
            {
                GL.BindTexture(TextureTarget.Texture2D, texture.OpenGlTextures[(int)OpenGlTextureWrapMode.ClampClamp].Name);
                renderer.LastBoundTexture = texture.OpenGlTextures[(int)OpenGlTextureWrapMode.ClampClamp];
            }
            else
            {
                Shader.DisableTexturing();
            }

            Shader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
            Shader.SetCurrentModelViewMatrix(renderer.CurrentViewMatrix);
            Shader.SetColor(color == null ? Color128.White : color.Value);
            Shader.SetPoint(point);
            Shader.SetSize(size);
            Shader.SetCoordinates(coordinates);

            /*
             * In order to call GL.DrawArrays with procedural data within the shader,
             * we first need to bind a dummy VAO
             * If this is not done, it will generate an InvalidOperation error code
             */
            renderer.dummyVao.Bind();
            GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 6);
        }
Esempio n. 5
0
        public void Render()
        {
            var cameraTranslation = CameraComponent.Active.Position;

            GL.Translate(cameraTranslation.X, cameraTranslation.Y, 0);

            // Draw the normal stuff into the fbo
            fbo.Activate();
            player.GetComponent <RenderTextureComponent>().Render();
//			var v = new RenderTextureComponent(Resource.heart, Player.PLAYER_SIZE * 10f, Player.PLAYER_SIZE * 10f);
//			v.Render();
            fbo.Deactivate();

            GL.Translate(-cameraTranslation.X, -cameraTranslation.Y, 0);


            // Draw the fbo
            GL.Enable(EnableCap.Texture2D);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Enable(EnableCap.Blend);
            fbo.Texture.Activate();
            shader.Activate();
            DrawWindowFillingQuad();
            shader.Deactivate();
            fbo.Texture.Deactivate();
            GL.Disable(EnableCap.Blend);
            GL.Disable(EnableCap.Texture2D);
        }
        public void Render(KBuild build, KAnimBank bank, int frameNumber)
        {
            Matrix4 modelMat     = Matrix4.Identity;
            Matrix4 pivotMat     = Matrix4.Identity;
            Matrix4 transformMat = Matrix4.Identity;


            geoShader.Activate();
            // Draw Origin
            geoShader.SetUniform("Model", ref modelMat);
            origin.Draw();

            if (frameNumber < 0 || frameNumber >= bank.FrameCount)
            {
                frameNumber = 0;
            }

            KAnimFrame frame = bank.Frames[frameNumber];

            Matrix4 frameMat = Matrix4.CreateTranslation(frame.X, frame.Y, 0);

            spriteShader.Activate();
            texture.Bind();

            for (int i = frame.ElementCount - 1; i >= 0; i--)
            {
                KAnimElement element = frame.Elements[i];
                string       symbol  = build.GetSymbolName(element.SymbolHash);
                if (symbol == null)
                {
                    continue;
                }
                KFrame sprite = build.GetFrame(symbol, element.FrameNumber);
                if (sprite == null)
                {
                    continue;
                }

                pivotMat = Matrix4.CreateTranslation(sprite.PivotX, sprite.PivotY, 0);

                transformMat = new Matrix4(
                    element.M00, -element.M10, 0, 0,
                    element.M01, -element.M11, 0, 0,
                    0, 0, -1, 0,
                    element.M02, -element.M12, 0, 1
                    );

                modelMat = halfScaleMat * pivotMat * transformMat;

                spriteShader.SetUniform("Model", ref modelMat);

                MakeQuadVBO(sprite.PivotWidth, sprite.PivotHeight, element.Red, element.Green, element.Blue, element.Alpha, sprite.GetUVRectangle(), out Vertex[] vertices, out int[] elements);
                quad.SetData(OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, vertices, elements);

                quad.Draw();
            }
        }
Esempio n. 7
0
 public void Render()
 {
     GL.Disable(EnableCap.CullFace);
     _shader.Activate();
     _texture.Activate();
     _geometry.Draw();
     _texture.Deactivate();
     _shader.Deactivate();
     GL.Enable(EnableCap.CullFace);
 }
Esempio n. 8
0
 private void Render()
 {
     if (ReferenceEquals(shader, null))
     {
         return;
     }
     GL.Clear(ClearBufferMask.ColorBufferBit);
     shader.Activate();
     GL.DrawArrays(PrimitiveType.Quads, 0, 4);
     shader.Deactivate();
 }
Esempio n. 9
0
 private void Render()
 {
     glTimerRender.Activate(QueryTarget.TimeElapsed);
     GL.Clear(ClearBufferMask.ColorBufferBit);
     shader.Activate();
     GL.DrawArrays(PrimitiveType.Quads, 0, 4);
     shader.Deactivate();
     glTimerRender.Deactivate();
     Console.Write("Rendertime:");
     Console.Write(glTimerRender.ResultLong / 1e6);
     Console.WriteLine("msec");
 }
Esempio n. 10
0
        public void Render()
        {
            if (shader != null)
            {
                shader.Activate();
            }

            GL.BindVertexArray(vaoHandle);

            // TODO: handle untextured materials properly
            if (material != null && lastTextureBound != material.Texture.Handle)
            {
                material.Texture.Activate();

                if (shader != null)
                {
                    if (shader.IsUniformNameSet(ShaderCommonUniform.MaterialAmbientColor))
                    {
                        shader.SetUniform(ShaderCommonUniform.MaterialAmbientColor, material.Ambient);
                    }

                    if (shader.IsUniformNameSet(ShaderCommonUniform.MaterialDiffuseColor))
                    {
                        shader.SetUniform(ShaderCommonUniform.MaterialDiffuseColor, material.Diffuse);
                    }

                    if (shader.IsUniformNameSet(ShaderCommonUniform.MaterialSpecularColor))
                    {
                        shader.SetUniform(ShaderCommonUniform.MaterialSpecularColor, material.Specular);
                    }
                }

                lastTextureBound = material.Texture.Handle;
            }

            if (elementBufferHandle != -1)
            {
                if (lastElementBufferBound != elementBufferHandle)
                {
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBufferHandle);
                    lastElementBufferBound = elementBufferHandle;
                }

                GL.DrawElements(primitiveType, numElementsToDraw, drawElementsType, 0);
            }
            else
            {
                GL.DrawArrays(primitiveType, 0, numElementsToDraw);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Call this once to initialise the renderer
        /// </summary>
        public virtual void Initialize(HostInterface CurrentHost, BaseOptions CurrentOptions)
        {
            currentHost    = CurrentHost;
            currentOptions = CurrentOptions;

            Background     = new Background(this);
            Fog            = new Fog();
            OpenGlString   = new OpenGlString(this);
            TextureManager = new TextureManager(currentHost);
            Cube           = new Cube(this);
            Rectangle      = new Rectangle(this);
            Loading        = new Loading(this);
            Keys           = new Keys(this);
            MotionBlur     = new MotionBlur(this);

            StaticObjectStates  = new List <ObjectState>();
            DynamicObjectStates = new List <ObjectState>();
            VisibleObjects      = new VisibleObjectLibrary(currentHost, Camera, currentOptions, this);
            try
            {
                DefaultShader = new Shader("default", "default", true);
                DefaultShader.Activate();
                DefaultShader.Deactivate();
            }
            catch
            {
                CurrentHost.AddMessage(MessageType.Error, false, "Initialising the default shaders failed- Falling back to legacy openGL.");
                CurrentOptions.IsUseNewRenderer = false;
            }

            GL.ClearColor(0.67f, 0.67f, 0.67f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            SetBlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            GL.Hint(HintTarget.FogHint, HintMode.Fastest);
            GL.Hint(HintTarget.LineSmoothHint, HintMode.Fastest);
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Fastest);
            GL.Hint(HintTarget.PointSmoothHint, HintMode.Fastest);
            GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Fastest);
            GL.Hint(HintTarget.GenerateMipmapHint, HintMode.Nicest);
            GL.Enable(EnableCap.CullFace);
            GL.CullFace(CullFaceMode.Front);
            GL.Disable(EnableCap.Texture2D);
            GL.Disable(EnableCap.Dither);
            GL.Disable(EnableCap.Lighting);
            GL.Disable(EnableCap.Fog);
            GL.Fog(FogParameter.FogMode, (int)FogMode.Linear);
        }
Esempio n. 12
0
 /// <summary>
 /// Performs a reset of OpenGL to the default state
 /// </summary>
 public virtual void ResetOpenGlState()
 {
     GL.Enable(EnableCap.CullFace);
     GL.Disable(EnableCap.Lighting);
     GL.Disable(EnableCap.Fog);
     GL.Disable(EnableCap.Texture2D);
     SetBlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
     UnsetBlendFunc();
     GL.Enable(EnableCap.DepthTest);
     GL.DepthMask(true);
     SetAlphaFunc(AlphaFunction.Greater, 0.9f);
     DefaultShader.Activate();
     ResetShader(DefaultShader);
     DefaultShader.Deactivate();
 }
Esempio n. 13
0
        private void DrawWithShader(string text, OpenGlFont font, double left, double top, Color128 color)
        {
            Shader.Activate();
            renderer.CurrentShader = Shader;
            Shader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
            Shader.SetCurrentModelViewMatrix(renderer.CurrentViewMatrix);

            for (int i = 0; i < text.Length; i++)
            {
                Texture        texture;
                OpenGlFontChar data;
                i += font.GetCharacterData(text, i, out texture, out data) - 1;
                if (renderer.currentHost.LoadTexture(texture, OpenGlTextureWrapMode.ClampClamp))
                {
                    GL.BindTexture(TextureTarget.Texture2D, texture.OpenGlTextures[(int)OpenGlTextureWrapMode.ClampClamp].Name);
                    Shader.SetAtlasLocation(data.TextureCoordinates);
                    double x = left - (data.PhysicalSize.X - data.TypographicSize.X) / 2;
                    double y = top - (data.PhysicalSize.Y - data.TypographicSize.Y) / 2;

                    /*
                     * In the first pass, mask off the background with pure black.
                     */
                    GL.BlendFunc(BlendingFactor.Zero, BlendingFactor.OneMinusSrcColor);
                    Shader.SetColor(new Color128(color.A, color.A, color.A, 1.0f));
                    Shader.SetPoint(new Vector2(x, y));
                    Shader.SetSize(data.PhysicalSize);

                    /*
                     * In order to call GL.DrawArrays with procedural data within the shader,
                     * we first need to bind a dummy VAO
                     * If this is not done, it will generate an InvalidOperation error code
                     */
                    renderer.dummyVao.Bind();
                    GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 6);
                    GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One);
                    Shader.SetColor(color);
                    GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 6);
                }
                left += data.TypographicSize.X;
            }
            renderer.RestoreBlendFunc();
        }
Esempio n. 14
0
        internal void RenderScene()
        {
            PreRender();
            if (touchFaces.Count == 0)
            {
                //Drop out early if the pre-render process reveals no available touch faces for a minor boost
                return;
            }
            renderer.ResetOpenGlState();

            fbo.Bind();

            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            pickingShader.Activate();
            pickingShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
            foreach (FaceState face in touchFaces)
            {
                pickingShader.SetObjectIndex(objectStates.IndexOf(face.Object) + 1);
                renderer.RenderFace(pickingShader, face);
            }
            //Must deactivate and unbind here
            pickingShader.Deactivate();
            fbo.UnBind();

            // for debug
            if (renderer.DebugTouchMode)
            {
                GL.DepthMask(false);
                GL.Disable(EnableCap.DepthTest);
                renderer.DefaultShader.Activate();
                renderer.ResetShader(renderer.DefaultShader);
                renderer.DefaultShader.SetCurrentProjectionMatrix(renderer.CurrentProjectionMatrix);
                foreach (FaceState face in touchFaces)
                {
                    renderer.RenderFace(renderer.DefaultShader, face, true);
                }
                renderer.DefaultShader.Deactivate();
            }
        }
Esempio n. 15
0
        internal void RenderScene()
        {
            PreRender();

            renderer.ResetOpenGlState();

            fbo.Bind();

            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            foreach (FaceState face in touchFaces)
            {
                pickingShader.Activate();
                renderer.ResetShader(pickingShader);
                pickingShader.SetObjectIndex(objectStates.IndexOf(face.Object) + 1);
                renderer.RenderFace(pickingShader, face);
                pickingShader.Deactivate();
            }

            fbo.UnBind();

            // for debug
            if (renderer.DebugTouchMode)
            {
                GL.DepthMask(false);
                GL.Disable(EnableCap.DepthTest);

                foreach (FaceState face in touchFaces)
                {
                    renderer.DefaultShader.Activate();
                    renderer.ResetShader(renderer.DefaultShader);
                    renderer.RenderFace(renderer.DefaultShader, face, true);
                    renderer.DefaultShader.Deactivate();
                }
            }
        }
 private void Init()
 {
     Shader.Activate(m_activeShader);
     Texture.Bind(m_activeTexture);
 }
        private void RenderAll(Material overrideMaterial)
        {
            currentMesh   = null;
            currentShader = null;

            foreach (var shaderPair in renderers)
            {
                if (!engine.shaderManager.GetShaderByHandle(shaderPair.Key).WriteMask&& overrideMaterial != null)
                {
                    continue;
                }

                if (overrideMaterial != null)
                {
                    currentShader = engine.shaderManager.GetShaderByHandle(overrideMaterial.ShaderHandle);
                }
                else
                {
                    currentShader = engine.shaderManager.GetShaderByHandle(shaderPair.Key);
                }
                currentShader.Activate();

                foreach (var materialPair in shaderPair.Value)
                {
                    SetZWrite(materialPair.Key.Shader.ZWrite);

                    foreach (var texturePair in materialPair.Key.textures)
                    {
                        texturePair.Value.Activate(texturePair.Key);
                    }

                    if (overrideMaterial == null)
                    {
                        foreach (var bufferKeyPair in materialPair.Key.structuredBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }

                        foreach (var bufferKeyPair in materialPair.Key.structuredPixelsBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }

                        foreach (var bufferKeyPair in materialPair.Key.structuredVertexBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }
                    }

                    foreach (var meshPair in materialPair.Value)
                    {
                        if (currentMesh != meshPair.Key)
                        {
                            meshPair.Key.VerticesBuffer.Activate(0);
                            meshPair.Key.IndicesBuffer.Activate(0);
                            currentMesh = meshPair.Key;
                        }

                        if (currentShader.Instancing)
                        {
                            if (instancesArray.Length < meshPair.Value.Count)
                            {
                                instancesArray = new Matrix[meshPair.Value.Count];
                            }

                            for (int i = 0; i < meshPair.Value.Count; ++i)
                            {
                                instancesArray[i] = meshPair.Value[i].LocalToWorldMatrix;
                            }
                            instancesBuffer.UpdateBuffer(instancesArray);
                            instancesBuffer.Activate(1);

                            engine.Device.DrawIndexedInstanced(meshPair.Key.IndexCount, meshPair.Value.Count, 0, 0, 0);
                        }
                        else
                        {
                            foreach (var instance in meshPair.Value)
                            {
                                objectData.WorldMatrix = instance.LocalToWorldMatrix;
                                objectBuffer.UpdateBuffer(ref objectData);
                                engine.Device.DrawIndexed(meshPair.Key.IndexCount, 0, 0);
                            }
                        }
                    }
                }
            }
        }