/// <summary>
        /// http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_Cube-small.jpg
        /// </summary>
        static DemoColorCodedPickingElement()
        {
            unitCubePos = new vec3[8];
            unitCubePos[0] = new vec3(1, 1, 1);
            unitCubePos[1] = new vec3(-1, 1, 1);
            unitCubePos[2] = new vec3(1, -1, 1);
            unitCubePos[3] = new vec3(-1, -1, 1);
            unitCubePos[4] = new vec3(1, 1, -1);
            unitCubePos[5] = new vec3(-1, 1, -1);
            unitCubePos[6] = new vec3(1, -1, -1);
            unitCubePos[7] = new vec3(-1, -1, -1);

            unitCubeIndex = new uint[14];
            unitCubeIndex[0] = 0;
            unitCubeIndex[1] = 2;
            unitCubeIndex[2] = 4;
            unitCubeIndex[3] = 6;
            unitCubeIndex[4] = 7;
            unitCubeIndex[5] = 2;
            unitCubeIndex[6] = 3;
            unitCubeIndex[7] = 0;
            unitCubeIndex[8] = 1;
            unitCubeIndex[9] = 4;
            unitCubeIndex[10] = 5;
            unitCubeIndex[11] = 7;
            unitCubeIndex[12] = 1;
            unitCubeIndex[13] = 3;
        }
Example #2
0
 public QuadPosition(vec3 v0,vec3 v1, vec3 v2, vec3 v3)
 {
     this.vertex0 = v0;
     this.vertex1 = v1;
     this.vertex2 = v2;
     this.vertex3 = v3;
 }
        /// <summary>
        /// http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_Cube-small.jpg
        /// </summary>
        static DemoHexahedron2Element()
        {
            vec3[] vertexes = new vec3[8];
            vertexes[0] = new vec3(1, 1, 1);
            vertexes[1] = new vec3(-1, 1, 1);
            vertexes[2] = new vec3(1, -1, 1);
            vertexes[3] = new vec3(-1, -1, 1);
            vertexes[4] = new vec3(1, 1, -1);
            vertexes[5] = new vec3(-1, 1, -1);
            vertexes[6] = new vec3(1, -1, -1);
            vertexes[7] = new vec3(-1, -1, -1);

            unitCubePos = new vec3[14];
            unitCubePos[0] = vertexes[0];
            unitCubePos[1] = vertexes[2];
            unitCubePos[2] = vertexes[4];
            unitCubePos[3] = vertexes[6];
            unitCubePos[4] = vertexes[7];
            unitCubePos[5] = vertexes[2];
            unitCubePos[6] = vertexes[3];
            unitCubePos[7] = vertexes[0];
            unitCubePos[8] = vertexes[1];
            unitCubePos[9] = vertexes[4];
            unitCubePos[10] = vertexes[5];
            unitCubePos[11] = vertexes[7];
            unitCubePos[12] = vertexes[1];
            unitCubePos[13] = vertexes[3];

        }
Example #4
0
        /// <summary>
        /// Apply specifed viewType to camera according to bounding box's size and position.
        /// <para>    +-------+    </para>
        /// <para>   /|      /|    </para>
        /// <para>  +-------+ |    </para>
        /// <para>  | |     | |    </para>
        /// <para>  | O-----|-+---X</para>
        /// <para>  |/      |/     </para>
        /// <para>  +-------+      </para>
        /// <para> /  |            </para>
        /// <para>Y   Z            </para>
        /// <para>其边长为(2 * Math.Sqrt(3)), 所在的坐标系如下</para>
        /// <para>   O---X</para>
        /// <para>  /|    </para>
        /// <para> Y |    </para>
        /// <para>   Z    </para>
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="boundingBox"></param>
        /// <param name="viewType"></param>
        public static void ApplyViewType(this IPerspectiveViewCamera camera, IBoundingBox boundingBox,
            ViewTypes viewType)
        {
            float sizeX, sizeY, sizeZ;
            boundingBox.GetBoundDimensions(out sizeX, out sizeY, out sizeZ);
            float size = Math.Max(Math.Max(sizeX, sizeY), sizeZ);

            {
                float centerX, centerY, centerZ;
                boundingBox.GetCenter(out centerX, out centerY, out centerZ);
                vec3 target = new vec3(centerX, centerY, centerZ);

                vec3 target2Position;
                vec3 upVector;
                GetBackAndUp(out target2Position, out upVector, viewType);

                vec3 position = target + target2Position * (size * 2 + 1);

                camera.Position = position;
                camera.Target = target;
                camera.UpVector = upVector;
            }

            {
                int[] viewport = new int[4];
                GL.GetInteger(GetTarget.Viewport, viewport);
                int width = viewport[2]; int height = viewport[3];

                IPerspectiveCamera perspectiveCamera = camera;
                perspectiveCamera.FieldOfView = 60;
                perspectiveCamera.AspectRatio = (double)width / (double)height;
                perspectiveCamera.Near = 0.01;
                perspectiveCamera.Far = size * 3 + 1;// double.MaxValue;
            }
        }
Example #5
0
 public vec4(vec3 xyz, float w)
 {
     this.x = xyz.x;
     this.y = xyz.y;
     this.z = xyz.z;
     this.w = w;
 }
Example #6
0
        /// <summary>
        /// http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_Cube-small.jpg
        /// </summary>
        static DemoMapBuffer()
        {
            unitCubePos = new vec3[8];
            unitCubePos[0] = new vec3(1, 1, 1);
            unitCubePos[1] = new vec3(-1, 1, 1);
            unitCubePos[2] = new vec3(1, -1, 1);
            unitCubePos[3] = new vec3(-1, -1, 1);
            unitCubePos[4] = new vec3(1, 1, -1);
            unitCubePos[5] = new vec3(-1, 1, -1);
            unitCubePos[6] = new vec3(1, -1, -1);
            unitCubePos[7] = new vec3(-1, -1, -1);

            unitCubeIndex = new uint[14];
            unitCubeIndex[0] = 0;
            unitCubeIndex[1] = 2;
            unitCubeIndex[2] = 4;
            unitCubeIndex[3] = 6;
            unitCubeIndex[4] = 7;
            unitCubeIndex[5] = 2;
            unitCubeIndex[6] = 3;
            unitCubeIndex[7] = 0;
            unitCubeIndex[8] = 1;
            unitCubeIndex[9] = 4;
            unitCubeIndex[10] = 5;
            unitCubeIndex[11] = 7;
            unitCubeIndex[12] = 1;
            unitCubeIndex[13] = 3;
        }
        public override string ToString()
        {
            var positions = this.positions;
            if (positions == null) { positions = new vec3[0]; }
            var colors = this.colors;
            if (colors == null) { colors = new vec3[0]; }

            string strPositions = positions.PrintArray();
            string strColors = colors.PrintArray();

            uint stageVertexID = this.StageVertexID;
            IColorCodedPicking picking = this.From;

            string lastVertexID = "?";
            if (picking != null)
            {
                uint tmp;
                if (picking.GetLastVertexIDOfPickedGeometry(stageVertexID, out tmp))
                {
                    lastVertexID = string.Format("{0}", tmp);
                }
            }

            string result = string.Format("{0}: P: {1} C: {2} ID:{3}/{4} ∈{5}",
                GeometryType, strPositions, strColors, lastVertexID, stageVertexID, From);

            return result;
            //return base.ToString();
        }
Example #8
0
        public PyramidModel()
        {
            var positions = new vec3[12];
            positions[0] = new vec3(0.0f, 1.0f, 0.0f);
            positions[1] = new vec3(-1.0f, -1.0f, 1.0f);
            positions[2] = new vec3(1.0f, -1.0f, 1.0f);
            positions[3] = new vec3(0.0f, 1.0f, 0.0f);
            positions[4] = new vec3(1.0f, -1.0f, 1.0f);
            positions[5] = new vec3(1.0f, -1.0f, -1.0f);
            positions[6] = new vec3(0.0f, 1.0f, 0.0f);
            positions[7] = new vec3(1.0f, -1.0f, -1.0f);
            positions[8] = new vec3(-1.0f, -1.0f, -1.0f);
            positions[9] = new vec3(0.0f, 1.0f, 0.0f);
            positions[10] = new vec3(-1.0f, -1.0f, -1.0f);
            positions[11] = new vec3(-1.0f, -1.0f, 1.0f);
            this.positions = positions;

            var colors = new vec3[12];
            colors[0] = new vec3(1.0f, 0.0f, 0.0f);
            colors[1] = new vec3(0.0f, 1.0f, 0.0f);
            colors[2] = new vec3(0.0f, 0.0f, 1.0f);
            colors[3] = new vec3(1.0f, 0.0f, 0.0f);
            colors[4] = new vec3(0.0f, 0.0f, 1.0f);
            colors[5] = new vec3(0.0f, 1.0f, 0.0f);
            colors[6] = new vec3(1.0f, 0.0f, 0.0f);
            colors[7] = new vec3(0.0f, 1.0f, 0.0f);
            colors[8] = new vec3(0.0f, 0.0f, 1.0f);
            colors[9] = new vec3(1.0f, 0.0f, 0.0f);
            colors[10] = new vec3(0.0f, 0.0f, 1.0f);
            colors[11] = new vec3(0.0f, 1.0f, 0.0f);
            this.colors = colors;
        }
 public SimpleUIPointSpriteStringElement(IUILayoutParam param,
     string content, vec3 position, 
     GLColor color = null, int fontSize = 32, int maxRowWidth = 256, FontResource fontResource = null)
 {
     IUILayout layout = this;
     layout.Param = param;
     this.element = new PointSpriteStringElement(content, position, color, fontSize, maxRowWidth, fontResource);
 }
Example #10
0
        /// <summary>
        /// 绘制三维坐标轴
        /// </summary>
        /// <param name="radius">轴(圆柱)的半径</param>
        /// <param name="axisLength">轴(圆柱)的长度</param>
        /// <param name="faceCount">轴(圆柱)的面数(越多则越圆滑)</param>
        public AxisElement(float radius = 0.3f, float axisLength = 10, int faceCount = 10)
        {
            this.radius = radius;
            this.axisLength = axisLength;
            this.faceCount = faceCount;

            this.planColor = new vec3(1, 1, 1);
        }
Example #11
0
        /// <summary>
        /// 绘制三维坐标轴
        /// </summary>
        /// <param name="planColor">XZ平面的颜色</param>
        /// <param name="radius">轴(圆柱)的半径</param>
        /// <param name="length">轴(圆柱)的长度</param>
        /// <param name="faceCount">轴(圆柱)的面数(越多则越圆滑)</param>
        public AxisElement(vec3 planColor, float radius = 0.3f, float length = 10, int faceCount = 10)
        {
            this.radius = radius;
            this.axisLength = length;
            this.faceCount = faceCount;

            this.planColor = planColor;
        }
Example #12
0
 public static vec3 GetRandomVec3(float minmag = 0.0f, float maxmag = 1.0f)
 {
     var distance = maxmag - minmag;
     vec3 result = new vec3(
         (float)(random.NextDouble() * distance + minmag),
         (float)(random.NextDouble() * distance + minmag),
         (float)(random.NextDouble() * distance + minmag)
         );
     return result;
 }
        /// <summary>
        /// Make sure the bounding box covers specifed vec3.
        /// </summary>
        /// <param name="vec3"></param>
        public void Extend(vec3 vertex)
        {
            if (vertex.x < this.minPosition.x) { this.minPosition.x = vertex.x; }
            if (vertex.y < this.minPosition.y) { this.minPosition.y = vertex.y; }
            if (vertex.z < this.minPosition.z) { this.minPosition.z = vertex.z; }

            if (vertex.x > this.maxPosition.x) { this.maxPosition.x = vertex.x; }
            if (vertex.y > this.maxPosition.y) { this.maxPosition.y = vertex.y; }
            if (vertex.z > this.maxPosition.z) { this.maxPosition.z = vertex.z; }
        }
Example #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="anchor">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent.
        /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param>
        /// <param name="margin">the space between viewport and SimpleRect.</param>
        /// <param name="size">Stores width when <see cref="OpenGLUIRect.Anchor"/>.Left &amp; <see cref="OpenGLUIRect.Anchor"/>.Right is <see cref="OpenGLUIRect.Anchor"/>.None.
        /// <para> and height when <see cref="OpenGLUIRect.Anchor"/>.Top &amp; <see cref="OpenGLUIRect.Anchor"/>.Bottom is <see cref="OpenGLUIRect.Anchor"/>.None.</para></param>
        /// <param name="zNear"></param>
        /// <param name="zFar"></param>
        /// <param name="rectColor">default color is red.</param>
        public SimpleUIRect(IUILayoutParam param, GLColor rectColor = null)
        {
            IUILayout layout = this;
            layout.Param = param;

            if (rectColor == null)
            { this.rectColor = new vec3(0, 0, 1); }
            else
            { this.rectColor = new vec3(rectColor.R, rectColor.G, rectColor.B); }
        }
        private unsafe void InitVertexBuffers()
        {
            {
                VR02PositionBuffer positionBuffer = new VR02PositionBuffer(strin_Position);
                positionBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);
                vec3* array = (vec3*)positionBuffer.FirstElement();
                int index = 0;
                for (int i = 0; i < xFrameCount; i++)
                {
                    for (int j = 0; j < yFrameCount; j++)
                    {
                        for (int k = 0; k < zFrameCount; k++)
                        {
                            array[index++] = new vec3(
                                (float)i / (float)xFrameCount - 0.5f,
                                (float)j / (float)yFrameCount - 0.5f,
                                ((float)k / (float)zFrameCount - 0.5f) * 109.0f / 256.0f
                                );
                        }
                    }
                }
                this.positionBufferRenderer = positionBuffer.GetRenderer();
                positionBuffer.Dispose();
            }

            {
                VR02UVBuffer uvBuffer = new VR02UVBuffer(strin_uv);
                uvBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);
                vec3* array = (vec3*)uvBuffer.FirstElement();
                int index = 0;
                for (int i = 0; i < xFrameCount; i++)
                {
                    for (int j = 0; j < yFrameCount; j++)
                    {
                        for (int k = 0; k < zFrameCount; k++)
                        {
                            array[index++] = new vec3(
                                (float)i / (float)xFrameCount,
                                (float)j / (float)yFrameCount,
                                (float)k / (float)zFrameCount
                                );
                        }
                    }
                }
                this.uvBufferRenderer = uvBuffer.GetRenderer();
                uvBuffer.Dispose();
            }
            {
                var indexBuffer = new ZeroIndexBuffer(DrawMode.Points, 0, xFrameCount * yFrameCount * zFrameCount);
                indexBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);// this actually does nothing.
                this.indexBufferRenderer = indexBuffer.GetRenderer();
                indexBuffer.Dispose();
            }
            this.vao = new VertexArrayObject(this.positionBufferRenderer, this.uvBufferRenderer, this.indexBufferRenderer);
        }
Example #16
0
 public override string ToString()
 {
     var builder = new System.Text.StringBuilder();
     var cols = new vec3[] { col0, col1, col2 };
     for (int i = 0; i < cols.Length; i++)
     {
         builder.Append(cols[i]);
         builder.Append(" + ");
     }
     return builder.ToString();
 }
        private void InitVAO()
        {
            this.mode = DrawMode.Quads;
            this.vertexCount = 4;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray<vec3> in_Position = new UnmanagedArray<vec3>(this.vertexCount);
            UnmanagedArray<vec2> in_TexCoord = new UnmanagedArray<vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;

            float factor = (float)this.ttfTexture.BigBitmap.Width / (float)this.ttfTexture.BigBitmap.Height;
            float x1 = -factor;
            float x2 = factor;
            float y1 = -1;
            float y2 = 1;

            in_Position[0] = new vec3(x1, y1, 0);
            in_Position[1] = new vec3(x2, y1, 0);
            in_Position[2] = new vec3(x2, y2, 0);
            in_Position[3] = new vec3(x1, y2, 0);

            in_TexCoord[0] = new vec2(0, 0);
            in_TexCoord[1] = new vec2(1, 0);
            in_TexCoord[2] = new vec2(1, 1);
            in_TexCoord[3] = new vec2(0, 1);

            if (vao[0] != 0)
            { GL.DeleteBuffers(1, vao); }
            if (vbo[0] != 0)
            { GL.DeleteBuffers(vbo.Length, vbo); }

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
Example #18
0
        public void MouseMove(int x, int y)
        {
            if (this.mouseDownFlag)
            {
                IViewCamera camera = this.Camera;
                if (camera == null) { return; }

                vec3 back = this.back;
                vec3 right = this.right;
                vec3 up = this.up;
                Size bound = this.bound;
                Point downPosition = this.downPosition;
                {
                    float deltaX = -horizontalRotationFactor * (x - downPosition.X) / bound.Width;
                    float cos = (float)Math.Cos(deltaX);
                    float sin = (float)Math.Sin(deltaX);
                    vec3 newBack = new vec3(
                        back.x * cos + right.x * sin,
                        back.y * cos + right.y * sin,
                        back.z * cos + right.z * sin);
                    back = newBack;
                    right = up.cross(back);
                    back.Normalize();
                    right.Normalize();
                }
                {
                    float deltaY = verticalRotationFactor * (y - downPosition.Y) / bound.Height;
                    float cos = (float)Math.Cos(deltaY);
                    float sin = (float)Math.Sin(deltaY);
                    vec3 newBack = new vec3(
                        back.x * cos + up.x * sin,
                        back.y * cos + up.y * sin,
                        back.z * cos + up.z * sin);
                    back = newBack;
                    up = back.cross(right);
                    back.Normalize();
                    up.Normalize();
                }

                camera.Position = camera.Target +
                    back * (float)((camera.Position - camera.Target).Magnitude());
                camera.UpVector = up;
                this.back = back;
                this.right = right;
                this.up = up;
                this.downPosition.X = x;
                this.downPosition.Y = y;
            }
        }
Example #19
0
        /// <summary>
        /// Build a look at view matrix.
        /// </summary>
        /// <param name="eye">The eye.</param>
        /// <param name="center">The center.</param>
        /// <param name="up">Up.</param>
        /// <returns></returns>
        public static mat4 lookAt(vec3 eye, vec3 center, vec3 up)
        {
            vec3 f = (center - eye); f.Normalize();
            vec3 s = f.cross(up); s.Normalize();
            vec3 u = s.cross(f);

            mat4 Result = new mat4(1);
            Result[0, 0] = s.x;
            Result[1, 0] = s.y;
            Result[2, 0] = s.z;
            Result[0, 1] = u.x;
            Result[1, 1] = u.y;
            Result[2, 1] = u.z;
            Result[0, 2] = -f.x;
            Result[1, 2] = -f.y;
            Result[2, 2] = -f.z;
            Result[3, 0] = -s.dot(eye);// dot(s, eye);
            Result[3, 1] = -u.dot(eye);// dot(u, eye);
            Result[3, 2] = f.dot(eye);// dot(f, eye);
            return Result;
        }
Example #20
0
        private static void GenNormals(TeapotModel model)
        {
            var faceNormals = new vec3[model.faces.Count];
            model.normals.AddRange(new vec3[model.positions.Count]);

            for (int i = 0; i < model.faces.Count; i++)
            {
                var face = model.faces[i];
                vec3 vertex0 = model.positions[face.Item1 - 1];
                vec3 vertex1 = model.positions[face.Item2 - 1];
                vec3 vertex2 = model.positions[face.Item3 - 1];
                vec3 v1 = vertex0 - vertex2;
                vec3 v2 = vertex2 - vertex1;
                faceNormals[i] = v1.cross(v2);
            }

            for (int i = 0; i < model.positions.Count; i++)
            {
                vec3 sum = new vec3();
                int shared = 0;
                for (int j = 0; j < model.faces.Count; j++)
                {
                    var face = model.faces[j];
                    if (face.Item1 - 1 == i || face.Item2 - 1 == i || face.Item3 - 1 == i)
                    {
                        sum = sum + faceNormals[i];
                        shared++;
                    }
                }
                if (shared > 0)
                {
                    sum = sum / shared;
                    sum.Normalize();
                }
                model.normals[i] = sum;
            }

        }
Example #21
0
 public mat3(vec3 col0, vec3 col1, vec3 col2)
 {
     this.col0 = col0;
     this.col1 = col1;
     this.col2 = col2;
 }
        private void InitVAO()
        {
            int size = this.size;

            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            // prepare positions
            {
                var positionArray = new UnmanagedArray<vec3>(size * size * size * 14);
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                    + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_PositionLocation);

                positionArray.Dispose();
            }
            // prepare colors
            {
                var colorArray = new UnmanagedArray<vec3>(size * size * size * 14);
                Random random = new Random();
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint in_ColorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_ColorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);

            // prepare firsts and counts
            {
                // todo: rename 'size'
                this.firsts = new int[size * size * size];
                for (int i = 0; i < this.firsts.Length; i++)
                {
                    this.firsts[i] = i * 14;
                }

                this.counts = new int[size * size * size];
                for (int i = 0; i < this.counts.Length; i++)
                {
                    this.counts[i] = 14;
                }
            }
        }
Example #23
0
        private void PrepareCamera()
        {
            var camera = this.Camera;
            if (camera != null)
            {
                vec3 back = camera.Position - camera.Target;
                vec3 right = Camera.UpVector.cross(back);
                vec3 up = back.cross(right);
                back.Normalize();
                right.Normalize();
                up.Normalize();

                this.back = back;
                this.right = right;
                this.up = up;

                if (this.originalCamera == null)
                { this.originalCamera = new Camera(); }
                this.originalCamera.Position = camera.Position;
                this.originalCamera.UpVector = camera.UpVector;
            }
        }
Example #24
0
 public vec3(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
     this.z = v.z;
 }
        public static void TypicalScene()
        {
            const int count = 1000000;

            long startTick = 0;
            long interval, interval2;

            // 测试float类型
            {
                var floatArray = new UnmanagedArray<float>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    floatArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = floatArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    float* header = (float*)floatArray.FirstElement();
                    float* last = (float*)floatArray.LastElement();
                    float* tailAddress = (float*)floatArray.TailAddress();
                    int value = 0;
                    for (float* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = value++;
                    }
                    int i = 0;
                    for (float* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        if (item != i)
                        { throw new Exception(); }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);
            }

            // 测试decimal类型
            {
                var decimalArray = new UnmanagedArray<decimal>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    decimalArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = decimalArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    decimal* header = (decimal*)decimalArray.FirstElement();
                    decimal* last = (decimal*)decimalArray.LastElement();
                    decimal* tailAddress = (decimal*)decimalArray.TailAddress();
                    int value = 0;
                    for (decimal* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = value++;
                    }
                    int i = 0;
                    for (decimal* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        if (item != i)
                        { throw new Exception(); }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);
            }

            // 测试int类型
            {
                var intArray = new UnmanagedArray<int>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    intArray[i] = i;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = intArray[i];
                    if (item != i)
                    { throw new Exception(); }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    int* header = (int*)intArray.FirstElement();
                    int* last = (int*)intArray.LastElement();
                    int* tailAddress = (int*)intArray.TailAddress();
                    int value = 0;
                    for (int* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = value++;
                    }
                    int i = 0;
                    for (int* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        if (item != i)
                        { throw new Exception(); }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);
            }

            // 测试bool类型
            {
                var boolArray = new UnmanagedArray<bool>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    boolArray[i] = i % 2 == 0;
                }
                for (int i = 0; i < count; i++)
                {
                    var item = boolArray[i];
                    if (item != (i % 2 == 0))
                    { throw new Exception(); }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    bool* header = (bool*)boolArray.FirstElement();
                    bool* last = (bool*)boolArray.LastElement();
                    bool* tailAddress = (bool*)boolArray.TailAddress();
                    int value = 0;
                    for (bool* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = (value % 2 == 0);
                        value++;
                    }
                    int i = 0;
                    for (bool* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        if (item != (i % 2 == 0))
                        { throw new Exception(); }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);
            }

            // 测试vec3类型
            {
                var vec3Array = new UnmanagedArray<vec3>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    vec3Array[i] = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                }
                for (int i = 0; i < count; i++)
                {
                    var item = vec3Array[i];
                    var old = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                    if (item.x != old.x || item.y != old.y || item.z != old.z)
                    { throw new Exception(); }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    vec3* header = (vec3*)vec3Array.FirstElement();
                    vec3* last = (vec3*)vec3Array.LastElement();
                    vec3* tailAddress = (vec3*)vec3Array.TailAddress();
                    int i = 0;
                    for (vec3* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                        i++;
                    }
                    i = 0;
                    for (vec3* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        var old = new vec3(i * 3 + 0, i * 3 + 1, i * 3 + 2);
                        if (item.x != old.x || item.y != old.y || item.z != old.z)
                        { throw new Exception(); }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);

            }

            // 测试mat4类型
            {
                var vec3Array = new UnmanagedArray<mat4>(count);
                startTick = DateTime.Now.Ticks;
                for (int i = 0; i < count; i++)
                {
                    vec3Array[i] = new mat4(new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3));
                }
                for (int i = 0; i < count; i++)
                {
                    var item = vec3Array[i];
                    var old = new mat4(new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3));
                    for (int col = 0; col < 4; col++)
                    {
                        for (int row = 0; row < 4; row++)
                        {
                            if (item[col][row] != old[col][row])
                            { throw new Exception(); }
                        }
                    }
                }
                interval = DateTime.Now.Ticks - startTick;

                unsafe
                {
                    startTick = DateTime.Now.Ticks;
                    mat4* header = (mat4*)vec3Array.FirstElement();
                    mat4* last = (mat4*)vec3Array.LastElement();
                    mat4* tailAddress = (mat4*)vec3Array.TailAddress();
                    int i = 0;
                    for (mat4* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++)
                    {
                        *ptr = new mat4(new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3));
                        i++;
                    }
                    i = 0;
                    for (mat4* ptr = header; ptr <= last/*or: ptr < tailAddress*/; ptr++, i++)
                    {
                        var item = *ptr;
                        var old = new mat4(new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3), new vec4(i, i + 1, i + 2, i + 3));
                        for (int col = 0; col < 4; col++)
                        {
                            for (int row = 0; row < 4; row++)
                            {
                                if (item[col][row] != old[col][row])
                                { throw new Exception(); }
                            }
                        }
                    }
                    interval2 = DateTime.Now.Ticks - startTick;
                }
                Console.WriteLine("Ticks: safe: {0} vs unsafe: {1}", interval, interval2);

            }

            // 立即释放所有非托管数组占用的内存,任何之前创建的UnmanagedBase数组都不再可用了。
            UnmanagedArray<int>.FreeAll();
        }
Example #26
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.Lines;
            this.axisVertexCount = 8 * 3;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(8 * 3);
                const float halfLength = 0.5f;
                // x axis
                positionArray[0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[1] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[3] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[4] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[6] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[7] = new vec3(halfLength, halfLength, -halfLength);
                // y axis
                positionArray[8 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[8 + 1] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[8 + 2] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[8 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[8 + 4] = new vec3(halfLength, -halfLength, halfLength);
                positionArray[8 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[8 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[8 + 7] = new vec3(halfLength, halfLength, -halfLength);
                // z axis
                positionArray[16 + 0] = new vec3(-halfLength, -halfLength, -halfLength);
                positionArray[16 + 1] = new vec3(-halfLength, -halfLength, halfLength);
                positionArray[16 + 2] = new vec3(-halfLength, halfLength, -halfLength);
                positionArray[16 + 3] = new vec3(-halfLength, halfLength, halfLength);
                positionArray[16 + 4] = new vec3(halfLength, halfLength, -halfLength);
                positionArray[16 + 5] = new vec3(halfLength, halfLength, halfLength);
                positionArray[16 + 6] = new vec3(halfLength, -halfLength, -halfLength);
                positionArray[16 + 7] = new vec3(halfLength, -halfLength, halfLength);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(8 * 3);
                vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1), };
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = colors[i / 8];
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Example #27
0
        public float dot(vec3 rhs)
        {
            var result = this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;

            return(result);
        }
Example #28
0
 public vec2(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
 }
Example #29
0
        private void InitVAO()
        {
            this.axisPrimitiveMode = DrawMode.LineLoop;
            this.axisVertexCount = 4;
            this.vao = new uint[1];

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            //  Create a vertex buffer for the vertex data.
            {
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(4);
                positionArray[0] = new vec3(-0.5f, -0.5f, 0);
                positionArray[1] = new vec3(0.5f, -0.5f, 0);
                positionArray[2] = new vec3(0.5f, 0.5f, 0);
                positionArray[3] = new vec3(-0.5f, 0.5f, 0);

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(positionLocation);

                positionArray.Dispose();
            }

            //  Now do the same for the colour data.
            {
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(4);
                vec3 color = this.rectColor;
                for (int i = 0; i < colorArray.Length; i++)
                {
                    colorArray[i] = color;
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);

                colorArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Example #30
0
 public vec2(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
 }
Example #31
0
        private void InitVAO()
        {
            int size = this.size;

            GL.GenVertexArrays(1, vao);

            GL.BindVertexArray(vao[0]);

            // prepare positions
            {
                var positionArray = new UnmanagedArray<vec3>(size * size * size * 8);
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                positionArray[index++] = unitCubePos[cubeIndex]
                                    + new vec3((i - size / 2) * unitSpace, (j - size / 2) * unitSpace, (k - size / 2) * unitSpace);
                            }
                        }
                    }
                }

                uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, positionArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_PositionLocation);

                positionArray.Dispose();
            }
            // prepare colors
            {
                var colorArray = new UnmanagedArray<vec3>(size * size * size * 8);
                Random random = new Random();
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                            for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                            {
                                vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                colorArray[index++] = color;
                            }
                        }
                    }
                }

                uint in_ColorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.GenBuffers(1, this.colorBuffer);
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0]);
                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(in_ColorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(in_ColorLocation);

                colorArray.Dispose();
            }
            // prepare index
            {
                var indexArray = new UnmanagedArray<uint>(size * size * size * (14 + 1));
                int index = 0;
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int k = 0; k < size; k++)
                        {
                            for (int cubeIndex = 0; cubeIndex < 14; cubeIndex++)
                            {
                                long posIndex = unitCubeIndex[cubeIndex] + (i * size * size + j * size + k) * 8;
                                indexArray[index++] = (uint)posIndex;
                            }

                            indexArray[index++] = uint.MaxValue;
                        }
                    }
                }

                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                GL.BufferData(BufferTarget.ElementArrayBuffer, indexArray, BufferUsage.StaticDraw);

                indexArray.Dispose();
            }

            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Example #32
0
        public override bool Equals(object obj)
        {
            vec3 p = (vec3)obj;

            return(this == p);
        }
Example #33
0
        /// <summary>
        /// 用随机颜色更新当前的颜色。
        /// </summary>
        public void UpdateColorBuffer()
        {
            {
                // update buffer object.
                GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0]);

                IntPtr destColors = GL.MapBuffer(BufferTarget.ArrayBuffer, MapBufferAccess.ReadWrite);

                //colorArray.CopyTo(destColors);
                unsafe
                {
                    vec3* array = (vec3*)destColors.ToPointer();
                    int index = 0;
                    for (int i = 0; i < size; i++)
                    {
                        for (int j = 0; j < size; j++)
                        {
                            for (int k = 0; k < size; k++)
                            {
                                //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
                                {
                                    vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                                    array[index++] = color;
                                }
                            }
                        }
                    }
                }

                GL.UnmapBuffer(BufferTarget.ArrayBuffer);
            }

            //// This do the same thing: update buffer object
            //using (var mappingBuffer = new MappingBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0], MapBufferAccess.ReadWrite))
            //{
            //    //colorArray.CopyTo(mappingBuffer.BufferPointer);
            //    unsafe
            //    {
            //        vec3* array = (vec3*)mappingBuffer.BufferPointer.ToPointer();
            //        int index = 0;
            //        for (int i = 0; i < size; i++)
            //        {
            //            for (int j = 0; j < size; j++)
            //            {
            //                for (int k = 0; k < size; k++)
            //                {
            //                    //vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
            //                    for (int cubeIndex = 0; cubeIndex < 8; cubeIndex++)
            //                    {
            //                        vec3 color = new vec3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
            //                        array[index++] = color;
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}

            //colorArray.Dispose();
        }
Example #34
0
        protected unsafe void InitializeVAO()
        {

            vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1) };
            // 计算三个坐标轴
            for (int axisIndex = 0; axisIndex < 3; axisIndex++)
            {
                var axisVertexCount = faceCount * 2;

                //  Create a vertex buffer for the vertex data.
                using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw))
                {
                    positionBuffer.Alloc(axisVertexCount);
                    vec3* positionArray = (vec3*)positionBuffer.FirstElement();
                    for (int i = 0; i < axisVertexCount; i++)
                    {
                        int face = i / 2;
                        float[] components = new float[]{
                            i % 2 == 1 ? 0 : this.axisLength,
                            (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                            (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))};
                        positionArray[i] = new vec3(
                            components[(0 + axisIndex) % 3], components[(2 + axisIndex) % 3], components[(4 + axisIndex) % 3]);
                    }
                    this.positionBufferRenderers[axisIndex] = positionBuffer.GetRenderer();
                }

                //  Now do the same for the color data.
                using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw))
                {
                    colorBuffer.Alloc(axisVertexCount);
                    vec3* colorArray = (vec3*)colorBuffer.FirstElement();
                    for (int i = 0; i < axisVertexCount; i++)
                    {
                        colorArray[i] = colors[axisIndex];
                    }
                    this.colorBufferRenderers[axisIndex] = colorBuffer.GetRenderer();
                }

                // Now for the index data.
                using (var indexBuffer = new AxisIndexBuffer())
                {
                    int indexLength = axisVertexCount + 2;
                    indexBuffer.Alloc(indexLength);
                    byte* cylinderIndex = (byte*)indexBuffer.FirstElement();
                    for (int i = 0; i < indexLength - 2; i++)
                    {
                        cylinderIndex[i] = (byte)i;
                    }
                    cylinderIndex[indexLength - 2] = 0;
                    cylinderIndex[indexLength - 1] = 1;
                    this.indexBufferRenderers[axisIndex] = indexBuffer.GetRenderer();
                }

            }
            // 计算XZ平面
            {
                int planVertexCount = 4;

                //  Create a vertex buffer for the vertex data.
                using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw))
                {
                    positionBuffer.Alloc(planVertexCount);
                    vec3* plan = (vec3*)positionBuffer.FirstElement();
                    {
                        float length = this.axisLength;
                        plan[0] = new vec3(-length, 0, -length);
                        plan[1] = new vec3(-length, 0, length);
                        plan[2] = new vec3(length, 0, length);
                        plan[3] = new vec3(length, 0, -length);
                    }
                    this.planPositionBufferRenderer = positionBuffer.GetRenderer();
                }

                //  Now do the same for the colour data.
                using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw))
                {
                    colorBuffer.Alloc(planVertexCount);
                    vec3* colorArray = (vec3*)colorBuffer.FirstElement();
                    {
                        for (int i = 0; i < planVertexCount; i++)
                        {
                            colorArray[i] = this.planColor;
                        }
                    }
                    this.planColorBufferRenderer = colorBuffer.GetRenderer();
                }

                using (var indexBuffer = new ZeroIndexBuffer(DrawMode.LineLoop, 0, planVertexCount))
                {
                    indexBuffer.Alloc(planVertexCount);//这句话实际上什么都没有做。
                    this.planIndexBufferRenderer = indexBuffer.GetRenderer();
                }
            }
        }
Example #35
0
        private void InitVAO(string value)
        {
            if (value == null) { value = string.Empty; }

            this.mode = DrawMode.Quads;
            this.vertexCount = 4 * value.Length;

            //  Create a vertex buffer for the vertex data.
            UnmanagedArray<vec3> in_Position = new UnmanagedArray<vec3>(this.vertexCount);
            UnmanagedArray<vec2> in_TexCoord = new UnmanagedArray<vec2>(this.vertexCount);
            Bitmap bigBitmap = this.ttfTexture.BigBitmap;
            // step 1: set width for each glyph
            vec3[] tmpPositions = new vec3[this.vertexCount];
            float totalLength = 0;
            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];
                CharacterInfo cInfo;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    float glyphWidth = (float)cInfo.width / (float)this.ttfTexture.FontHeight;
                    if (i == 0)
                    {
                        tmpPositions[i * 4 + 0] = new vec3(0, 0, 0);
                        tmpPositions[i * 4 + 1] = new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 2] = new vec3(glyphWidth, 1, 0);
                        tmpPositions[i * 4 + 3] = new vec3(0, 1, 0);
                    }
                    else
                    {
                        tmpPositions[i * 4 + 0] = tmpPositions[i * 4 + 0 - 4 + 1];
                        tmpPositions[i * 4 + 1] = tmpPositions[i * 4 + 0] + new vec3(glyphWidth, 0, 0);
                        tmpPositions[i * 4 + 3] = tmpPositions[i * 4 + 3 - 4 - 1];
                        tmpPositions[i * 4 + 2] = tmpPositions[i * 4 + 3] + new vec3(glyphWidth, 0, 0);
                    }
                    totalLength += glyphWidth;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
            }
            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];
                CharacterInfo cInfo;
                float x1 = 0;
                float x2 = 1;
                float y1 = 0;
                float y2 = 1;
                if (this.ttfTexture.CharInfoDict.TryGetValue(c, out cInfo))
                {
                    x1 = (float)cInfo.xoffset / (float)bigBitmap.Width;
                    x2 = (float)(cInfo.xoffset + cInfo.width) / (float)bigBitmap.Width;
                    y1 = (float)cInfo.yoffset / (float)bigBitmap.Height;
                    y2 = (float)(cInfo.yoffset + this.ttfTexture.FontHeight) / (float)bigBitmap.Height;
                }
                //else
                //{ throw new Exception(string.Format("Not support for display the char [{0}]", c)); }
                in_Position[i * 4 + 0] = tmpPositions[i * 4 + 0] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 1] = tmpPositions[i * 4 + 1] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 2] = tmpPositions[i * 4 + 2] - new vec3(totalLength / 2, 0, 0);
                in_Position[i * 4 + 3] = tmpPositions[i * 4 + 3] - new vec3(totalLength / 2, 0, 0);

                //in_TexCoord[i * 4 + 0] = new vec2(x1, y1);
                //in_TexCoord[i * 4 + 1] = new vec2(x2, y1);
                //in_TexCoord[i * 4 + 2] = new vec2(x2, y2);
                //in_TexCoord[i * 4 + 3] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 0] = new vec2(x1, y2);
                in_TexCoord[i * 4 + 1] = new vec2(x2, y2);
                in_TexCoord[i * 4 + 2] = new vec2(x2, y1);
                in_TexCoord[i * 4 + 3] = new vec2(x1, y1);
            }

            if (vao[0] != 0)
            { GL.DeleteBuffers(1, vao); }
            if (vbo[0] != 0)
            { GL.DeleteBuffers(vbo.Length, vbo); }

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);

            GL.GenBuffers(2, vbo);

            uint in_PositionLocation = shaderProgram.GetAttributeLocation(strin_Position);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_Position, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_PositionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_PositionLocation);

            uint in_TexCoordLocation = shaderProgram.GetAttributeLocation(strin_TexCoord);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[1]);
            GL.BufferData(BufferTarget.ArrayBuffer, in_TexCoord, BufferUsage.StaticDraw);
            GL.VertexAttribPointer(in_TexCoordLocation, 2, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(in_TexCoordLocation);

            GL.BindVertexArray(0);

            in_Position.Dispose();
            in_TexCoord.Dispose();
        }
Example #36
0
 //  TODO: this is actually defined as an extension, put in the right file.
 public static mat4 rotate(float angle, vec3 v)
 {
     return(rotate(mat4.identity(), angle, v));
 }