protected override void DoInitialize()
        {
            // init shader program
            this.shaderProgram = PickingShaderHelper.GetPickingShaderProgram();

            // init property buffer objects
            var propertyBufferPtrs = new PropertyBufferPtr[propertyNameMap.Count()];
            int index = 0;
            foreach (var item in propertyNameMap)
            {
                PropertyBufferPtr bufferPtr = this.bufferable.GetProperty(
                    item.nameInIBufferable, item.VarNameInShader);
                if (bufferPtr == null) { throw new Exception(); }
                propertyBufferPtrs[index++] = bufferPtr;
            }

            this.propertyBufferPtrs = propertyBufferPtrs;
            this.indexBufferPtr = this.bufferable.GetIndex();

            foreach (var item in propertyNameMap)
            {
                PropertyBufferPtr bufferPtr = this.bufferable.GetProperty(
                    item.nameInIBufferable, item.VarNameInShader);
                if (bufferPtr == null) { throw new Exception(); }

                if (item.nameInIBufferable == positionNameInIBufferable)
                {
                    this.positionBufferPtr = new PropertyBufferPtr(
                        "in_Position",// in_Postion same with in the PickingShader.vert shader
                        bufferPtr.BufferId,
                        bufferPtr.DataSize,
                        bufferPtr.DataType,
                        bufferPtr.Length,
                        bufferPtr.ByteLength);
                    break;
                }
            }

            // RULE: Renderer takes uint.MaxValue, ushort.MaxValue or byte.MaxValue as PrimitiveRestartIndex. So take care this rule when designing a model's index buffer.
            var ptr = this.indexBufferPtr as OneIndexBufferPtr;
            if (ptr != null)
            {
                GLSwitch glSwitch = new PrimitiveRestartSwitch(ptr);
                this.switchList.Add(glSwitch);
            }

            // 由于picking.vert/frag只支持vec3的position buffer,所以有此硬性规定。
            if (this.positionBufferPtr.DataSize != 3 || this.positionBufferPtr.DataType != OpenGL.GL_FLOAT)
            { throw new Exception(string.Format("Position buffer must use a type composed of 3 float as PropertyBuffer<T>'s T!")); }
        }
示例#2
0
        /// <summary>
        /// 支持UI布局的渲染器
        /// </summary>
        /// <param name="modernRenderer">要渲染的对象</param>
        /// <param name="Anchor">绑定到窗口的哪些边?</param>
        /// <param name="Margin">到绑定边的距离</param>
        /// <param name="Size">UI大小</param>
        /// <param name="zNear"></param>
        /// <param name="zFar"></param>
        public UIAxisRenderer(
            System.Windows.Forms.AnchorStyles Anchor,
            System.Windows.Forms.Padding Margin,
            System.Drawing.Size Size,
            int zNear = -1000,
            int zFar  = 1000
            )
            : base(null, Anchor, Margin, Size, zNear, zFar)
        {
            ShaderCode[] shaderCodes = new ShaderCode[2];
            shaderCodes[0] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.vert"), ShaderType.VertexShader);
            shaderCodes[1] = new ShaderCode(File.ReadAllText(@"01Renderer\Simple.frag"), ShaderType.FragmentShader);
            var propertyNameMap = new PropertyNameMap();

            propertyNameMap.Add("in_Position", "position");
            propertyNameMap.Add("in_Color", "color");

            PickableRenderer pickableRenderer = PickableRendererFactory.GetRenderer(
                new Axis(), shaderCodes, propertyNameMap, "position");

            pickableRenderer.Name = string.Format("Pickable: [{0}]", "Axis");
            pickableRenderer.Initialize();
            {
                GLSwitch lineWidthSwitch = new LineWidthSwitch(10);
                pickableRenderer.SwitchList.Add(lineWidthSwitch);
                GLSwitch pointSizeSwitch = new PointSizeSwitch(10);
                pickableRenderer.SwitchList.Add(pointSizeSwitch);
                GLSwitch polygonModeSwitch = new PolygonModeSwitch(PolygonModes.Filled);
                pickableRenderer.SwitchList.Add(polygonModeSwitch);
                if (pickableRenderer is OneIndexRenderer)
                {
                    GLSwitch primitiveRestartSwitch = new PrimitiveRestartSwitch((pickableRenderer as OneIndexRenderer).IndexBufferPtr);
                    pickableRenderer.SwitchList.Add(primitiveRestartSwitch);
                }
            }
            this.renderer = pickableRenderer;

            this.textList.Add(new Tuple <vec3, string, Font, Color>(new vec3(offset, 0, 0), "X", new Font("Courier New", fontSize), Color.Red));
            this.textList.Add(new Tuple <vec3, string, Font, Color>(new vec3(0, offset, 0), "Y", new Font("Courier New", fontSize), Color.Green));
            this.textList.Add(new Tuple <vec3, string, Font, Color>(new vec3(0, 0, offset), "Z", new Font("Courier New", fontSize), Color.Blue));
        }