Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
0
 public static vec3 cross(vec3 lhs, vec3 rhs)
 {
     return new vec3(
         lhs.y * rhs.z - rhs.y * lhs.z,
         lhs.z * rhs.x - rhs.z * lhs.x,
         lhs.x * rhs.y - rhs.x * lhs.y);
 }
Exemplo n.º 3
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;
        }
        /// <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;
        }
Exemplo n.º 5
0
 public vec4(vec3 xyz, float w)
 {
     this.x = xyz.x;
     this.y = xyz.y;
     this.z = xyz.z;
     this.w = w;
 }
Exemplo n.º 6
0
        public override string ToString()
        {
            var positions = this.positions;
            if (positions == null) { positions = new vec3[0]; }

            string strPositions = positions.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}: Pos: {1} Cube Index: {2} vertex ID:{3}/{4} ∈{5}",
                GeometryType, strPositions, CubeIndex, lastVertexID, stageVertexID, From);

            return result;
            //return base.ToString();
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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;
        }
 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);
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
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); }
        }
Exemplo n.º 12
0
        /// <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; }
        }
Exemplo n.º 13
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.VectorProduct(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.VectorProduct(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;
            }
        }
Exemplo n.º 14
0
        protected override void DoInitialize()
        {
            skybox_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.VertexShader, skybox_shader_vs);
            ShaderHelper.vglAttachShaderSource(skybox_prog, ShaderType.FragmentShader, skybox_shader_fs);
            GL.LinkProgram(skybox_prog);

            object_prog = GL.CreateProgram();
            ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.VertexShader, object_shader_vs);
            ShaderHelper.vglAttachShaderSource(object_prog, ShaderType.FragmentShader, object_shader_fs);
            GL.LinkProgram(object_prog);

            GL.GenBuffers(1, cube_vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, cube_vbo[0]);
            var cube_vertices = new UnmanagedArray<vec3>(8);
            cube_vertices[0] = new vec3(-1.0f, -1.0f, -1.0f);
            cube_vertices[1] = new vec3(-1.0f, -1.0f, 1.0f);
            cube_vertices[2] = new vec3(-1.0f, 1.0f, -1.0f);
            cube_vertices[3] = new vec3(-1.0f, 1.0f, 1.0f);
            cube_vertices[4] = new vec3(1.0f, -1.0f, -1.0f);
            cube_vertices[5] = new vec3(1.0f, -1.0f, 1.0f);
            cube_vertices[6] = new vec3(1.0f, 1.0f, -1.0f);
            cube_vertices[7] = new vec3(1.0f, 1.0f, 1.0f);

            var cube_indices = new UnmanagedArray<ushort>(16);
            // First strip
            cube_indices[0] = 0;
            cube_indices[1] = 1;
            cube_indices[2] = 2;
            cube_indices[3] = 3;
            cube_indices[4] = 6;
            cube_indices[5] = 7;
            cube_indices[6] = 4;
            cube_indices[7] = 5;
            // Second strip
            cube_indices[8] = 2;
            cube_indices[9] = 6;
            cube_indices[10] = 0;
            cube_indices[11] = 4;
            cube_indices[12] = 1;
            cube_indices[13] = 5;
            cube_indices[14] = 3;
            cube_indices[15] = 7;

            GL.BufferData(BufferTarget.ArrayBuffer, cube_vertices, BufferUsage.StaticDraw);
            cube_vertices.Dispose();

            GL.GenVertexArrays(1, vao);
            GL.BindVertexArray(vao[0]);
            GL.VertexAttribPointer(0, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
            GL.EnableVertexAttribArray(0);

            GL.GenBuffers(1, cube_element_buffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, cube_element_buffer[0]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, cube_indices, BufferUsage.StaticDraw);
            cube_indices.Dispose();

            skybox_rotate_loc = GL.GetUniformLocation(skybox_prog, "tc_rotate");
            object_mat_mvp_loc = GL.GetUniformLocation(object_prog, "mat_mvp");
            object_mat_mv_loc = GL.GetUniformLocation(object_prog, "mat_mv");
            skyboxTexLocation = GL.GetUniformLocation(skybox_prog, "tex");
            objectTexLocation = GL.GetUniformLocation(object_prog, "tex");

            //tex = new Texture2D();
            //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"media\TantolundenCube.png");
            //tex.Initialize(bmp);
            vglImageData data = new vglImageData();
            tex = vgl.vglLoadTexture(@"media\TantolundenCube.dds", 0, ref data);

            uint e = GL.GetError();

            vgl.vglUnloadImage(ref data);

            vboObject.LoadFromVBM(@"media\unit_torus.vbm", 0, 1, 2);
        }
Exemplo n.º 15
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 IOrthoViewCamera 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];

                IOrthoCamera orthoCamera = camera;
                if (width > height)
                {
                    orthoCamera.Left = -size * width / height;
                    orthoCamera.Right = size * width / height;
                    orthoCamera.Bottom = -size;
                    orthoCamera.Top = size;
                }
                else
                {
                    orthoCamera.Left = -size;
                    orthoCamera.Right = size;
                    orthoCamera.Bottom = -size * height / width;
                    orthoCamera.Top = size * height / width;
                }
                orthoCamera.Near = 0.001;
                orthoCamera.Far = size * 3 + 1;// double.MaxValue;
            }
        }
Exemplo n.º 16
0
        protected void InitializeVAO(out uint[] vao, out DrawMode primitiveMode, out int vertexCount)
        {
            primitiveMode = DrawMode.QuadStrip;// PrimitiveModes.QuadStrip;
            vertexCount = faceCount * 2;

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

            //  Create a vertex buffer for the vertex data.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                UnmanagedArray<vec3> positionArray = new UnmanagedArray<vec3>(faceCount * 2);
                for (int i = 0; i < faceCount * 2; i++)
                {
                    int face = i / 2;
                    positionArray[i] = new vec3(
                        (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)),
                        (i % 2 == 1 ? -1 : 1) * this.height / 2,
                        (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount))
                        );
                }

                uint positionLocation = shaderProgram.GetAttributeLocation(strin_Position);

                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.
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ArrayBuffer, ids[0]);
                UnmanagedArray<vec3> colorArray = new UnmanagedArray<vec3>(faceCount * 2);
                for (int i = 0; i < colorArray.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        colorArray[i] = new vec3(1, 0, 0); //new vec3((i % 3) / 3.0f, (i + 1) % 3 / 3.0f, (i + 2) % 3 / 3.0f);
                    }
                    else
                    {
                        colorArray[i] = new vec3(0, 1, 0); //new vec3((i % 3) / 3.0f, (i + 1) % 3 / 3.0f, (i + 2) % 3 / 3.0f);
                    }
                }

                uint colorLocation = shaderProgram.GetAttributeLocation(strin_Color);

                GL.BufferData(BufferTarget.ArrayBuffer, colorArray, BufferUsage.StaticDraw);
                GL.VertexAttribPointer(colorLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                GL.EnableVertexAttribArray(colorLocation);
                colorArray.Dispose();
            }
            {
                uint[] ids = new uint[1];
                GL.GenBuffers(1, ids);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                UnmanagedArray<uint> cylinderIndex = new UnmanagedArray<uint>(faceCount * 2 + 2);
                for (int i = 0; i < cylinderIndex.Length - 2; i++)
                {
                    cylinderIndex[i] = (uint)i;
                }
                cylinderIndex[cylinderIndex.Length - 2] = 0;
                cylinderIndex[cylinderIndex.Length - 1] = 1;
                GL.BufferData(BufferTarget.ElementArrayBuffer, cylinderIndex, BufferUsage.StaticDraw);
                cylinderIndex.Dispose();
            }
            //  Unbind the vertex array, we've finished specifying data for it.
            GL.BindVertexArray(0);
        }
Exemplo n.º 17
0
        ///// <summary>
        ///// 获取或设置此字符串的字体资源。
        ///// </summary>
        //public FontResource Resource
        //{
        //    get { return this.resouce; }
        //    set
        //    {
        //        if (value != this.resouce)
        //        {
        //            this.resouce = value;
        //            InitTexture(this.content, this.FontSize, this.resouce);
        //        }
        //    }
        //}
        //public void UpdateProperties(string content, GLColor color, int fontSize, FontResource fontResource)
        //{
        //}
        /// <summary>
        /// 用shader+VAO+组装的texture显示字符串
        /// </summary>
        /// <param name="content">要显示的字符串</param>
        /// <param name="position">字符串的中心位置</param>
        /// <param name="color">文字颜色,默认为黑色</param>
        /// <param name="fontSize">字体大小,默认为32</param>
        /// <param name="fontResource">字体资源。默认的字体资源只支持ASCII码。</param>
        public PointSpriteStringElement(
            string content, vec3 position, 
            GLColor color = null, int fontSize = 32, int maxRowWidth = 256, FontResource fontResource = null)
        {
            if (fontSize > 256) { throw new ArgumentException(); }

            this.content = content;
            this.position = position;

            if (color == null)
            {
                textColor = new vec3(0, 0, 0);
            }
            else
            {
                textColor = new vec3(color.R, color.G, color.B);
            }

            this.fontSize = fontSize;

            if (0 < maxRowWidth && maxRowWidth < 257)
            {
                this.maxRowWidth = maxRowWidth;
            }
            else
            {
                throw new ArgumentOutOfRangeException("max row width must between 0 and 257(not include 0 or 257)");
            }

            if (fontResource == null)
            {
                this.resource = FontResource.Default;
            }
            else
            {
                this.resource = fontResource;
            }
        }
        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 * 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);

                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();
            }
            // 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);
        }
Exemplo n.º 19
0
        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();
        }
Exemplo n.º 20
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();
        }
Exemplo n.º 21
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);
        }
Exemplo n.º 22
0
 public static vec3 normalize(vec3 v)
 {
     float sqr = v.x * v.x + v.y * v.y + v.z * v.z;
     return v * (1.0f / (float)Math.Sqrt(sqr));
 }
Exemplo n.º 23
0
        internal static void TypicalScene()
        {
            // 数组较小时可按此方法使用UnmanagedArray,数组较大时请参考UnmanagedArrayHelper。
            const int count = 100;

            // 测试float类型
            {
                var floatArray = new UnmanagedArray<float>(count);
                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(); }
                }
            }

            // 测试decimal类型
            {
                var decimalArray = new UnmanagedArray<decimal>(count);
                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(); }
                }
            }

            // 测试int类型
            {
                var intArray = new UnmanagedArray<int>(count);
                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(); }
                }
            }

            // 测试bool类型
            {
                var boolArray = new UnmanagedArray<bool>(count);
                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(); }
                }
            }

            // 测试vec3类型
            {
                var vec3Array = new UnmanagedArray<vec3>(count);
                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(); }
                }

                // 释放此数组占用的内存,这之后就不能再使用vec3Array了。
                vec3Array.Dispose();
            }

            // 速度较慢,不再使用。
            //// 测试foreach
            //foreach (var item in vec3Array.Elements())
            //{
            //    Console.WriteLine(item);
            //}

            // 立即释放所有非托管数组占用的内存,任何之前创建的UnmanagedBase数组都不再可用了。
            UnmanagedArray<int>.FreeAll();
        }
Exemplo n.º 24
0
 public vec2(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
 }
Exemplo n.º 25
0
        public static float Magnitude(this vec3 vector)
        {
            double result = Math.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);

            return((float)result);
        }
Exemplo n.º 26
0
        /// <summary>
        /// 计算内积
        /// </summary>
        /// <param name="vector"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        public static float ScalarProduct(this vec3 vector, vec3 rhs)
        {
            float result = vector.x * rhs.x + vector.y * rhs.y + vector.z * rhs.z;

            return(result);
        }
Exemplo n.º 27
0
        protected void InitializeVAO()
        {
            this.axisPrimitiveMode = DrawMode.QuadStrip;// PrimitiveModes.QuadStrip;
            this.axisVertexCount = faceCount * 2;
            this.vao = new uint[4];

            GL.GenVertexArrays(4, vao);

            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++)
            {
                GL.BindVertexArray(vao[axisIndex]);

                //  Create a vertex buffer for the vertex data.
                using (var positionArray = new UnmanagedArray<vec3>(faceCount * 2))
                {
                    for (int i = 0; i < faceCount * 2; 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]);
                    }

                    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);
                }

                //  Now do the same for the colour data.
                using (var colorArray = new UnmanagedArray<vec3>(faceCount * 2))
                {
                    for (int i = 0; i < colorArray.Length; i++)
                    {
                        colorArray[i] = colors[axisIndex];
                    }

                    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);
                }

                using (var cylinderIndex = new UnmanagedArray<uint>(faceCount * 2 + 2))
                {
                    for (int i = 0; i < cylinderIndex.Length - 2; i++)
                    {
                        cylinderIndex[i] = (uint)i;
                    }
                    cylinderIndex[cylinderIndex.Length - 2] = 0;
                    cylinderIndex[cylinderIndex.Length - 1] = 1;

                    uint[] ids = new uint[1];
                    GL.GenBuffers(1, ids);
                    GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[0]);
                    GL.BufferData(BufferTarget.ElementArrayBuffer, cylinderIndex, BufferUsage.StaticDraw);
                }
                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
            // 计算XZ平面
            {
                this.planPrimitveMode = DrawMode.LineLoop;
                this.planVertexCount = 4;

                GL.BindVertexArray(vao[3]);

                //  Create a vertex buffer for the vertex data.
                using (var plan = new UnmanagedArray<vec3>(4))
                {
                    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);

                    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, plan, BufferUsage.StaticDraw);
                    GL.VertexAttribPointer(positionLocation, 3, GL.GL_FLOAT, false, 0, IntPtr.Zero);
                    GL.EnableVertexAttribArray(positionLocation);
                }

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

                    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);
                }

                //  Unbind the vertex array, we've finished specifying data for it.
                GL.BindVertexArray(0);
            }
        }
Exemplo n.º 28
0
 public void SetBounds(vec3 min, vec3 max)
 {
     this.minPosition = min;
     this.maxPosition = max;
 }
Exemplo n.º 29
0
 public vec3(vec3 v)
 {
     this.x = v.x;
     this.y = v.y;
     this.z = v.z;
 }
Exemplo n.º 30
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);
        }
Exemplo n.º 31
0
        public static vec3 ToVec3(this float[] array, int startIndex = 0)
        {
            vec3 result = new vec3(array[startIndex], array[startIndex + 1], array[startIndex + 2]);

            return(result);
        }
Exemplo n.º 32
0
 public static float dot(vec3 x, vec3 y)
 {
     vec3 tmp = new vec3(x * y);
     return tmp.x + tmp.y + tmp.z;
 }
Exemplo n.º 33
0
        /// <summary>
        /// 用随机颜色更新当前的颜色。
        /// </summary>
        public void UpdateColorBuffer()
        {
            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;
                        }
                    }
                }
            }

            // update buffer object.
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.colorBuffer[0]);

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

            colorArray.CopyTo(destColors);

            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);
            }

            colorArray.Dispose();
        }