Пример #1
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            this.RenderUnit.Initialize();

            {
                var bitmap = this.bitmap;
                bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                var texImageBitmap = new TexImageBitmap(bitmap);
                var texture        = new Texture(texImageBitmap);
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

                texture.Initialize();

                if (this.autoDispose)
                {
                    bitmap.Dispose();
                }
                this.bitmap = null;

                RenderMethod method = this.RenderUnit.Methods[0];
                method.Program.SetUniform("tex", texture);
            }
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            base.DoInitialize();

            this.RenderUnit.Initialize();

            {
                var bitmap = this.bitmap;
                bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                var texture = new Texture(TextureTarget.Texture2D,
                                          new TexImage2D(TexImage2D.Target.Texture2D, 0, (int)GL.GL_RGBA, bitmap.Width, bitmap.Height, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, new ImageDataProvider(bitmap)));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapS, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapT, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureWrapR, (int)GL.GL_CLAMP_TO_EDGE));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMinFilter, (int)GL.GL_LINEAR));
                texture.BuiltInSampler.Add(new TexParameteri(TexParameter.PropertyName.TextureMagFilter, (int)GL.GL_LINEAR));

                texture.Initialize();

                if (this.autoDispose)
                {
                    bitmap.Dispose();
                }
                this.bitmap = null;

                RenderMethod method = this.RenderUnit.Methods[0];
                method.Program.SetUniform("tex", texture);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="transformFeedbackObj"></param>
        /// <param name="unit"></param>
        public static void Draw(this TransformFeedbackObject transformFeedbackObj, RenderMethod unit)
        {
            if (transformFeedbackObj == null || unit == null)
            {
                throw new ArgumentNullException();
            }

            transformFeedbackObj.Draw(unit.Program, unit.VertexArrayObjects, unit.StateList);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        public override void RenderBeforeChildren(GUIRenderEventArgs arg)
        {
            this.Scissor();
            this.Viewport();

            RenderMethod method = this.RenderUnit.Methods[0];

            method.Render();
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        public override void RenderGUIBeforeChildren(GUIRenderEventArgs arg)
        {
            base.RenderGUIBeforeChildren(arg);

            ModernRenderUnit unit   = this.RenderUnit;
            RenderMethod     method = unit.Methods[0];

            method.Render(IndexAccessMode.Random);
        }
Пример #6
0
        /// <summary>
        /// This method should only be invoked once.
        /// </summary>
        private void DoInitialize()
        {
            IBufferSource model = this.Model;

            for (int i = 0; i < this.builders.Length; i++)
            {
                RenderMethod method = this.builders[i].ToRenderMethod(model);
                this.Methods[i] = method;
            }
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        public override void RenderGUIBeforeChildren(GUIRenderEventArgs arg)
        {
            base.RenderGUIBeforeChildren(arg);

            ModernRenderUnit  unit        = this.RenderUnit;
            RenderMethod      method      = unit.Methods[0];
            VertexArrayObject vao         = method.VertexArrayObject;
            IndexBuffer       indexBuffer = vao.IndexBuffer;

            method.Render();
        }
Пример #8
0
        public void RenderBeforeChildren(RenderEventArgs arg)
        {
            ICamera camera     = arg.Camera;
            mat4    projection = camera.GetProjectionMatrix();
            mat4    view       = camera.GetViewMatrix();
            mat4    model      = this.GetModelMatrix();

            RenderMethod  method  = this.RenderUnit.Methods[0];
            ShaderProgram program = method.Program;

            program.SetUniform("mvpMat", projection * view * model);

            method.Render();
        }
Пример #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Render(GLControl control)
        {
            var ctrl = control as CtrlImage;

            if (ctrl != null)
            {
                ctrl.Scissor();
                ctrl.Viewport();

                RenderMethod method = this.RenderUnit.Methods[0];

                method.Render();
            }
        }
Пример #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        public override void RenderGUIBeforeChildren(GUIRenderEventArgs arg)
        {
            base.RenderGUIBeforeChildren(arg);

            ModernRenderUnit unit    = this.RenderUnit;
            RenderMethod     method  = unit.Methods[0];
            Texture          texture = this.textureSource.BindingTexture;

            if (texture != null)
            {
                method.Program.SetUniform("tex", texture);
            }
            method.Render();
        }
Пример #11
0
        public void RenderAmbientColor(BlinnPhongAmbientEventArgs arg)
        {
            ICamera camera     = arg.Camera;
            mat4    projection = camera.GetProjectionMatrix();
            mat4    view       = camera.GetViewMatrix();
            mat4    model      = this.GetModelMatrix();

            RenderMethod  method  = this.RenderUnit.Methods[0];
            ShaderProgram program = method.Program;

            program.SetUniform("mvpMat", projection * view * model);
            program.SetUniform("ambientColor", arg.Ambient);

            method.Render();
        }
Пример #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        public void RenderBeforeChildren(RenderEventArgs arg)
        {
            GL.Instance.ClearStencil(0x0);
            GL.Instance.Clear(GL.GL_STENCIL_BUFFER_BIT); // this seems not working. I don't know why.(2017-12-13)
            GL.Instance.Enable(GL.GL_STENCIL_TEST);
            GL.Instance.StencilFunc(GL.GL_ALWAYS, 0, 0xFF);
            GL.Instance.StencilOp(GL.GL_REPLACE, GL.GL_REPLACE, GL.GL_REPLACE);
            GL.Instance.StencilMask(0xFF);
            //GL.Instance.DepthMask(false);
            //GL.Instance.ColorMask(false, false, false, false);

            RenderMethod method = this.RenderUnit.Methods[0];

            method.Render();

            //GL.Instance.ColorMask(true, true, true, true);
            //GL.Instance.DepthMask(true);
        }
Пример #13
0
        /// <summary>
        /// 
        /// </summary>
        protected override void DoInitialize()
        {
            base.DoInitialize();

            // make sure textModel only returns once.
            this.positionBuffer = (from item in this.textModel.GetVertexAttribute(GlyphsModel.position) select item).First();
            this.strBuffer = (from item in this.textModel.GetVertexAttribute(GlyphsModel.STR) select item).First();
            this.drawCmd = (from item in this.textModel.GetDrawCommand() select item).First() as DrawArraysCmd;

            GlyphServer server = this.glyphServer;
            Texture texture = server.GlyphTexture;
            RenderMethod method = this.RenderUnit.Methods[0]; // the only render unit in this node.
            ShaderProgram program = method.Program;
            program.SetUniform(glyphTexture, texture);
            program.SetUniform(width, this._width);
            program.SetUniform(height, this._height);
            program.SetUniform(textColor, this._color);
        }
Пример #14
0
        /// <summary>
        ///
        /// </summary>
        protected override void DoInitialize()
        {
            base.DoInitialize();

            this.positionBuffer = this.textModel.GetVertexAttributeBuffer(GlyphsModel.position);
            this.strBuffer      = this.textModel.GetVertexAttributeBuffer(GlyphsModel.STR);
            this.indexBuffer    = this.textModel.GetIndexBuffer() as ZeroIndexBuffer;

            GlyphServer   server  = this.glyphServer;
            Texture       texture = server.GlyphTexture;
            RenderMethod  method  = this.RenderUnit.Methods[0]; // the only render unit in this node.
            ShaderProgram program = method.Program;

            program.SetUniform(glyphTexture, texture);
            program.SetUniform(width, this._width);
            program.SetUniform(height, this._height);
            program.SetUniform(textColor, this._color);
        }
Пример #15
0
        public void RenderBeforeChildren(RenderEventArgs arg)
        {
            // gets mvpMat.
            ICamera camera        = arg.Camera;
            mat4    projectionMat = camera.GetProjectionMatrix();
            mat4    viewMat       = camera.GetViewMatrix();
            mat4    modelMat      = this.GetModelMatrix();
            mat4    mvpMat        = projectionMat * viewMat * modelMat;
            // a render uint wraps everything(model data, shaders, glswitches, etc.) for rendering.
            ModernRenderUnit unit = this.RenderUnit;
            // gets render method.
            // There could be more than 1 method(vertex shader + fragment shader) to render the same model data. Thus we need an method array.
            RenderMethod method = unit.Methods[0];
            // shader program wraps vertex shader and fragment shader.
            ShaderProgram program = method.Program;

            //set value for 'uniform mat4 mvpMat'; in shader.
            program.SetUniform("mvpMat", mvpMat);
            // render the cube model via OpenGL.
            method.Render();
        }
Пример #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RenderMethod ToRenderMethod(IBufferSource model)
        {
            // init shader program.
            ShaderProgram program = this.programProvider.GetShaderProgram();

            // init vertex attribute buffer objects.
            VertexShaderAttribute[] vertexAttributeBuffers;
            {
                var list = new List <VertexShaderAttribute>();
                foreach (AttributeMap.NamePair item in this.map)
                {
                    VertexBuffer buffer = model.GetVertexAttributeBuffer(item.NameInIBufferSource);
                    if (buffer == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }
                    list.Add(new VertexShaderAttribute(buffer, item.VarNameInShader));
                }
                vertexAttributeBuffers = list.ToArray();
            }

            // init index buffer.
            IndexBuffer indexBuffer = model.GetIndexBuffer();

            // init VAO.
            var vertexArrayObject = new VertexArrayObject(indexBuffer, program, vertexAttributeBuffers);

            var result = new RenderMethod(program, vertexArrayObject, this.states);

            // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care of this rule when designing a model's index buffer.
            var ptr = indexBuffer as OneIndexBuffer;

            if (ptr != null)
            {
                GLState glState = new PrimitiveRestartState(ptr.ElementType);
                result.StateList.Add(glState);
            }

            return(result);
        }
Пример #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RenderMethod ToRenderMethod(IBufferSource model)
        {
            // init shader program.
            ShaderProgram program = this.programProvider.GetShaderProgram();

            // init vertex attribute buffer objects.
            var attrCount  = this.map.Count(); // how many kinds of vertex attributes?
            var allBlocks  = new List <VertexBuffer> [attrCount];
            var allNames   = new string[attrCount];
            int blockCount = 0; // how many blocks an attribute is divided into?
            int index      = 0;

            foreach (AttributeMap.NamePair item in this.map)
            {
                blockCount       = 0;
                allBlocks[index] = new List <VertexBuffer>();
                foreach (var buffer in model.GetVertexAttributeBuffer(item.NameInIBufferSource))
                {
                    if (buffer == null)
                    {
                        throw new Exception(string.Format("[{0}] returns null buffer pointer!", model));
                    }
                    allBlocks[index].Add(buffer);
                    allNames[index] = item.VarNameInShader;
                    blockCount++;
                }
                if (blockCount != allBlocks[0].Count)
                {
                    throw new Exception("Not all vertex attribute buffers' block count are the same!");
                }

                index++;
            }

            // init draw commands.
            var drawCmds = (from item in model.GetDrawCommand()
                            where (item != null)
                            select item).ToArray();
            int cmdCount = drawCmds.Length;

            if (attrCount > 0 && cmdCount != blockCount)
            {
                throw new Exception("Draw Commands count != vertex buffer block count.");
            }

            // init VAOs.
            var vaos = new VertexArrayObject[cmdCount];

            for (int c = 0; c < cmdCount; c++)
            {
                var vertexShaderAttributes = new VertexShaderAttribute[attrCount];
                for (int a = 0; a < attrCount; a++)
                {
                    List <VertexBuffer> vertexAttribute = allBlocks[a];
                    string varNameInShader = allNames[a];
                    vertexShaderAttributes[a] = new VertexShaderAttribute(vertexAttribute[c], varNameInShader);
                }
                vaos[c] = new VertexArrayObject(drawCmds[c], program, vertexShaderAttributes);
            }

            var renderUnit = new RenderMethod(program, vaos, this.states);

            // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care of this rule when designing a model's index buffer.
            foreach (var cmd in drawCmds)
            {
                var ptr = cmd as IHasIndexBuffer;
                if (ptr != null)
                {
                    GLState glState = new PrimitiveRestartState(ptr.IndexBufferObject.ElementType);
                    renderUnit.StateList.Add(glState);
                    break;
                }
            }

            return(renderUnit);
        }