F# 图形数学基础。
这几天,在看Unity3D,很有意思,其中看到,第一人称控制器,就想看到里面的一些控制脚本是如何实现,才发现,学到的好多数据知识已经还给老师了,还好,走遍大江南北,跟着的书不多,唯一的二本高数没丢.
然后参考网上各个简洁明了的讲洁.
主要有http://my.csdn.net/cppyin 的从零实现3D图像引擎.
用此文只是用来记录一些向量,矩阵等的操作.没别的实际意义.会与我的学习来更新此文章.
1.求矩阵的逆矩阵。
type XMatrix3 = class val m11:float val m12:float val m13:float val m21:float val m22:float val m23:float val m31:float val m32:float val m33:float //new (x11,x12,x21,x22) = XMatrix(this.m11 = x11;this.m12 = x12;this.m13 = 0.0;this.m21 = x21;this.m22 = x22;this.m23 = 0.0;this.m31 = 0.0;this.m32 = 0.0;this.m33 = 1.0) new (x11,x12,x13,x21,x22,x23,x31,x32,x33) = {m11 = x11;m12 = x12;m13 = x13; m21 = x21;m22 = x22;m23 = x23; m31 = x31;m32 = x32;m33 = x33} new (x11,x12,x21,x22) = {m11 = x11;m12 = x12;m13 = 0.0; m21 = x21;m22 = x22;m23 = 0.0; m31 = 0.0;m32 = 0.0;m33 = 1.0} //求行列式 member r.Det = r.m11*(r.m22*r.m33 - r.m32*r.m23) - r.m12*(r.m21*r.m33 - r.m31*r.m23) + r.m13*(r.m21*r.m32 - r.m31*r.m22) member r.Inverse = let det = r.Det let det_inv = 1.0/det new XMatrix3((r.m22*r.m33 - r.m32*r.m23)*det_inv, -(r.m12*r.m33 - r.m32*r.m13)*det_inv, (r.m12*r.m23 - r.m22*r.m13)*det_inv, -(r.m21*r.m33 - r.m31*r.m23)*det_inv, (r.m11*r.m33 - r.m31*r.m13)*det_inv, -(r.m11*r.m23 - r.m21*r.m13)*det_inv, (r.m21*r.m32 - r.m31*r.m22)*det_inv, -(r.m11*r.m32 - r.m31*r.m12)*det_inv, (r.m11*r.m22 - r.m21*r.m12)*det_inv) end let a = XMatrix3(1.0,2.0,-1.0,3.0,1.0,0.0,-1.0,0.0,-2.0) let d = a.Det let v = XMatrix3(3.0,-4.0,1.0,-2.0) let s = v.Inverse let ss = s.m11,s.m12,s.m21,s.m22
2.求二线段有没交点。交点的位置。这个看从零系列文章的时候,开始没想通怎么到作者是如何来求t1,t2那一步的,然后用F#自己来推导,原来如此。
type XPoint2 = val x:float val y:float new (a,b) = {x = a;y = b} type XVector2 = val x:float val y:float new (a,b) = {x = a;y = b} type XSegment2 = val p0: XPoint2 val p1: XPoint2 new (ps,pe) = {p0= ps;p1=pe} member m.v with get() = new XVector2(m.p1.x-m.p0.x,m.p1.y - m.p0.y) member m.paramLine//参数化直线 with get() = fun(t) -> new XPoint2(m.p0.x + t*m.v.x,m.p0.y+t*m.v.y) static member Intersect (s1:XSegment2,s2:XSegment2) = //let x1 t1 = s1.p0.x + s1.v.x* t1 //let y1 t1 = s1.p0.y + s1.v.y* t1 //let x2 t2 = s2.p0.x + s2.v.x* t2 //let y2 t2 = s2.p0.y + s2.v.y* t2 //s1.paramLine(t1).x - s2.paramLine(t1).x //s1.paramLine(t1).y = s2.paramLine(t1).y //交点 //let x_ = fun(t1,t2) -> s1.v.x* t1 - s2.v.x* t2 + s1.p0.x - s2.p0.x = 0 //let y_ = fun(t1,t2) -> s1.v.y* t1 - s2.v.y* t2 + s2.p0.y - s2.p0.y = 0 //用矩阵来求方阵式 let xy = new XMatrix3(s1.v.x,-s2.v.x,s1.v.y,-s2.v.y) let xyI = xy.Inverse let a = s2.p0.x - s1.p0.x let b = s2.p0.y - s1.p0.y let t1 = xyI.m11 * a + xyI.m12 * b let t2 = xyI.m21 * a + xyI.m22 * b t1,t2,s1.paramLine(t1),s2.paramLine(t2) let x1 = XSegment2(XPoint2(4.0,4.0),XPoint2(2.0,6.0)) let x2 = XSegment2(XPoint2(4.0,2.0),XPoint2(6.0,4.0)) let q,w,e,r = XSegment2.Intersect(x1,x2) let show = e.x,e.y,r.x,r.y
val x1 : XMath.XSegment2
val x2 : XMath.XSegment2
val w : float = 0.5
val r : XMath.XPoint2
val q : float = -0.5
val e : XMath.XPoint2
val it : float * float * float * float = (5.0, 3.0, 5.0, 3.0)
3.向量点乘(内积)r = (a,b,c)*(x,y,z)=ax+by+cz,是一个标量,不具有方向了。有如下关系,cos@=r/(|a,b,c|*|x,y,z|).而cos@=一边的投影长度/另一边。所以用来求二个向量之间投影向量与对应的法向量。
向量叉乘(叉积)r=(a,b,c)*(x,y,z)=(bz-yc,-az+xc,ay-bx).是一个向量,有如下关系,sin@=|r|/(|a,b,c|*|x,y,z|).其中r与向量(a,b,c),(x,y,z)互相垂直。
二个向量的叉积结合相应坐标系来看,就是各项的代数余子式。
4.OpenGL里的摄像机与透视矩阵.
用F#实现OpenTK上的一个小例子。相关代码是画一个立方体,然后不断旋转。
用的OpenGL,对应的是列向量,右手坐标系。
相关个人理解,不论是OpenGL或是D3D,人站在地面上,在3维里,XZ面是表示地面,Z用来表示远近(人眼看的远近),Y用来表示高度。我们眼中看到的平面应该平行于XY面。人相当于是地面(XZ面)的法向量。
而透视投影用在实际当中来说,人眼选择一个近处1米到远处10米的所有物体,在这当中所有物体会影视到我们眼球表面。透视投影相当于就是计算视线内所有物体到眼球表面上的位置。
1 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 open System 3 open System.Collections.Generic 4 open System.Windows.Forms 5 open System.Drawing 6 7 open OpenTK 8 open OpenTK.Graphics 9 open OpenTK.Graphics.OpenGL 10 11 type ImmediateWindow() = 12 inherit GameWindow(800,600,new GraphicsMode(new ColorFormat(16),16)) 13 do base.VSync <- VSyncMode.On 14 let rotation_speed = 180.0f 15 let mutable angle = 0.0f 16 override v.OnLoad e = 17 base.OnLoad(e) 18 GL.ClearColor(Color.MidnightBlue) 19 GL.Enable(EnableCap.DepthTest) 20 override v.OnResize e = 21 base.OnResize e 22 GL.Viewport(0,0,v.Width,v.Height) 23 let ratio = float32 v.Width/float32 v.Height 24 let mutable pective = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,ratio,1.0f,64.0f) 25 GL.MatrixMode(MatrixMode.Projection) 26 GL.LoadMatrix(&pective) 27 28 member v.DrawCube() = 29 GL.Begin(BeginMode.Quads); 30 31 GL.Color3(Color.Silver); 32 GL.Vertex3(-1.0f, -1.0f, -1.0f); 33 GL.Vertex3(-1.0f, 1.0f, -1.0f); 34 GL.Vertex3(1.0f, 1.0f, -1.0f); 35 GL.Vertex3(1.0f, -1.0f, -1.0f); 36 37 GL.Color3(Color.Honeydew); 38 GL.Vertex3(-1.0f, -1.0f, -1.0f); 39 GL.Vertex3(1.0f, -1.0f, -1.0f); 40 GL.Vertex3(1.0f, -1.0f, 1.0f); 41 GL.Vertex3(-1.0f, -1.0f, 1.0f); 42 43 GL.Color3(Color.Moccasin); 44 45 GL.Vertex3(-1.0f, -1.0f, -1.0f); 46 GL.Vertex3(-1.0f, -1.0f, 1.0f); 47 GL.Vertex3(-1.0f, 1.0f, 1.0f); 48 GL.Vertex3(-1.0f, 1.0f, -1.0f); 49 50 GL.Color3(Color.IndianRed); 51 GL.Vertex3(-1.0f, -1.0f, 1.0f); 52 GL.Vertex3(1.0f, -1.0f, 1.0f); 53 GL.Vertex3(1.0f, 1.0f, 1.0f); 54 GL.Vertex3(-1.0f, 1.0f, 1.0f); 55 56 GL.Color3(Color.PaleVioletRed); 57 GL.Vertex3(-1.0f, 1.0f, -1.0f); 58 GL.Vertex3(-1.0f, 1.0f, 1.0f); 59 GL.Vertex3(1.0f, 1.0f, 1.0f); 60 GL.Vertex3(1.0f, 1.0f, -1.0f); 61 62 GL.Color3(Color.ForestGreen); 63 GL.Vertex3(1.0f, -1.0f, -1.0f); 64 GL.Vertex3(1.0f, 1.0f, -1.0f); 65 GL.Vertex3(1.0f, 1.0f, 1.0f); 66 GL.Vertex3(1.0f, -1.0f, 1.0f); 67 GL.End(); 68 override v.OnUpdateFrame e = 69 base.OnUpdateFrame e 70 if v.Keyboard.[OpenTK.Input.Key.Escape] 71 then 72 v.Exit() 73 override v.OnRenderFrame(e) = 74 base.OnRenderFrame e 75 GL.Clear (ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 76 let mutable eye = new Vector3(float32 0.0,float32 5.0,float32 5.0) 77 let mutable target = new Vector3(float32 0.0,float32 0.0,float32 0.0) 78 let mutable up = new Vector3(float32 0.0,float32 1.0,float32 0.0) 79 let mutable lookat = Matrix4.LookAt(0.f, 5.f, 5.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f); 80 GL.MatrixMode(MatrixMode.Modelview) 81 GL.LoadMatrix(&lookat) 82 angle <- angle + rotation_speed *(float32 e.Time) 83 GL.Rotate(angle,0.0f,1.0f,0.0f) 84 v.DrawCube() 85 v.SwapBuffers() 86 87 let example = new ImmediateWindow() 88 do example.Run(30.)
5.OpenGL里的矩阵变换.顺序,当前矩阵,主要变换.
1 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 open System 3 open System.Collections.Generic 4 open System.Windows.Forms 5 open System.Drawing 6 7 open OpenTK 8 open OpenTK.Graphics 9 open OpenTK.Graphics.OpenGL 10 11 type ImmediateWindow() = 12 inherit GameWindow(800,600,new GraphicsMode(new ColorFormat(16),16)) 13 let num_lists = 13 14 let lists = Array.create num_lists 0//[|num_lists|] 15 override v.OnLoad e = 16 base.OnLoad(e) 17 GL.ClearColor(Color.MidnightBlue) 18 GL.Enable(EnableCap.DepthTest) 19 GL.MatrixMode(MatrixMode.Modelview) 20 GL.LoadIdentity() 21 let first_list = GL.GenLists(num_lists) 22 let mutable c = 0. 23 for i = 0 to num_lists-1 do 24 lists.[i] <- first_list + i 25 GL.NewList(first_list + i,ListMode.Compile) 26 GL.Color3(0.3+0.7*c*c,0.3+1.4*c*c,0.7-0.7*c*c) 27 c <- c+1./float num_lists 28 GL.PushMatrix() 29 GL.Rotate(c*360.,0.,0.,1.0) 30 GL.Translate(5.,0.,0.) 31 GL.Begin(BeginMode.Quads) 32 GL.Vertex3(-1.,-1.,1.) 33 GL.Vertex3(1.,-1.,1.) 34 GL.Vertex3(1.,1.,1.) 35 GL.Vertex3(-1.,1.,1.) 36 GL.End() 37 GL.PopMatrix() 38 GL.EndList() 39 override v.OnUnload e = 40 GL.DeleteLists(lists.[0],num_lists) 41 override v.OnResize e = 42 GL.Viewport(0,0,v.Width,v.Height) 43 let ratio = float32 v.Width/float32 v.Height 44 let mutable pective = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,ratio,1.0f,64.0f) 45 GL.MatrixMode(MatrixMode.Projection) 46 GL.LoadMatrix(&pective) 47 override v.OnUpdateFrame e = 48 base.OnUpdateFrame e 49 if v.Keyboard.[OpenTK.Input.Key.Escape] 50 then 51 v.Exit() 52 override v.OnRenderFrame(e) = 53 // base.OnRenderFrame e 54 //GL.Clear (ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 55 let mutable lookat = Matrix4.LookAt(0.f, 0.f, 16.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f); 56 GL.MatrixMode(MatrixMode.Modelview) 57 GL.LoadMatrix(&lookat) 58 GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit); 59 GL.CallLists(num_lists, ListNameType.Int, lists); 60 v.SwapBuffers() 61 62 let example = new ImmediateWindow() 63 do example.Run(30.)
6.OpenGL里的缓冲区.
1 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 3 open System 4 open System.Collections.Generic 5 open System.Windows.Forms 6 open System.Threading 7 open System.Drawing 8 9 open OpenTK 10 open OpenTK.Graphics 11 open OpenTK.Graphics.OpenGL 12 13 type VBO = 14 struct 15 val mutable VboID : int 16 val mutable EboID : int 17 val mutable NumElements : int 18 end 19 20 type VertexPositionColor = 21 struct 22 val mutable Position : Vector3 23 val mutable Color : uint32 24 new(x,y,z,color:Color) = {Position = new Vector3(x,y,z);Color=VertexPositionColor.ToRgba(color)} 25 static member ToRgba (color:Color) = 26 uint32 color.A <<< 24 ||| uint32 color.B <<< 16 ||| uint32 color.G <<< 8 |||uint32 color.R 27 end 28 29 type VBOWindow() = 30 inherit GameWindow(400,300) 31 let speed = 180. 32 let mutable angle = 0. 33 let CubeVertices = [| 34 new VertexPositionColor(-1.0f, -1.0f, 1.0f, Color.DarkRed);//0 35 new VertexPositionColor( 1.0f, -1.0f, 1.0f, Color.DarkRed);//1 36 new VertexPositionColor( 1.0f, 1.0f, 1.0f, Color.Gold);//2 37 new VertexPositionColor(-1.0f, 1.0f, 1.0f, Color.Gold);//3 38 new VertexPositionColor(-1.0f, -1.0f, -1.0f, Color.DarkRed);//4 39 new VertexPositionColor( 1.0f, -1.0f, -1.0f, Color.DarkRed);//5 40 new VertexPositionColor( 1.0f, 1.0f, -1.0f, Color.Gold);//6 41 new VertexPositionColor(-1.0f, 1.0f, -1.0f, Color.Gold) |]//7 42 let CubeElements = [| 43 0s; 1s; 2s; 2s; 3s; 0s; // front face 44 3s; 2s; 6s; 6s; 7s; 3s; // top face 45 7s; 6s; 5s; 5s; 4s; 7s; // back face 46 4s; 0s; 3s; 3s; 7s; 4s; // left face 47 0s; 1s; 5s; 5s; 4s; 0s; // bottom face 48 1s; 5s; 6s; 6s; 2s; 1s // right face 49 |] 50 let mutable vbo = new VBO() 51 override v.OnLoad e = 52 base.OnLoad e 53 let version = GL.GetString(StringName.Version) 54 let major = int version.[0] 55 let minor =int version.[2] 56 if major <= 1 && minor < 5 then 57 v.Exit() 58 GL.ClearColor(System.Drawing.Color.MidnightBlue) 59 GL.Enable(EnableCap.DepthTest) 60 61 GL.GenBuffers(1,&vbo.VboID) 62 GL.BindBuffer(BufferTarget.ArrayBuffer,vbo.VboID) 63 let mutable size = CubeVertices.Length * BlittableValueType.StrideOf(CubeVertices.[0]); 64 GL.BufferData(BufferTarget.ArrayBuffer,new IntPtr(size),CubeVertices,BufferUsageHint.StaticDraw) 65 66 GL.GenBuffers(1,&vbo.EboID) 67 GL.BindBuffer(BufferTarget.ElementArrayBuffer,vbo.EboID) 68 let mutable elementSize = CubeElements.Length * sizeof<int16> 69 GL.BufferData(BufferTarget.ElementArrayBuffer,new IntPtr(elementSize),CubeElements,BufferUsageHint.StaticDraw) 70 71 vbo.NumElements <- CubeElements.Length 72 override v.OnResize e = 73 base.OnResize e 74 GL.Viewport(0,0,v.Width,v.Height) 75 let aspect = float32 v.Width / float32 v.Height 76 let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,1.f,64.f) 77 GL.MatrixMode(MatrixMode.Projection) 78 GL.LoadMatrix(&projection) 79 override v.OnRenderFrame e = 80 base.OnRenderFrame e 81 GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 82 let mutable lookat = Matrix4.LookAt(new Vector3(0.f,5.f,5.f),Vector3.Zero,Vector3.UnitY) 83 GL.MatrixMode(MatrixMode.Modelview) 84 GL.LoadMatrix(&lookat) 85 // angle <- angle + speed * float e.Time 86 // GL.Rotate(angle,0.,1.,0.) 87 // 88 // GL.EnableClientState ArrayCap.ColorArray 89 // GL.EnableClientState ArrayCap.VertexArray 90 // GL.BindBuffer(BufferTarget.ArrayBuffer,vbo.VboID) 91 // GL.BindBuffer(BufferTarget.ElementArrayBuffer,vbo.EboID) 92 // let size = BlittableValueType.StrideOf(CubeVertices.[0]) 93 // GL.VertexPointer(3,VertexPointerType.Float,size,new IntPtr(0)) 94 // GL.ColorPointer(4,ColorPointerType.UnsignedByte,size,new IntPtr(12)) 95 // GL.DrawElements(BeginMode.Triangles,vbo.NumElements,DrawElementsType.UnsignedShort,IntPtr.Zero) 96 97 GL.Begin(BeginMode.Triangles) 98 GL.Color3(Color.Red) 99 GL.Vertex3(-1.0f, -1.0f, 1.0f);//0 100 GL.Vertex3( 1.0f, -1.0f, 1.0f);//1 101 GL.Vertex3( 1.0f, 1.0f, 1.0f);//2 102 GL.Vertex3( 1.0f, 1.0f, 1.0f);//2 103 GL.Vertex3( -1.0f, 1.0f, 1.0f);//3 104 GL.Vertex3(-1.0f, -1.0f, 1.0f);//0 105 GL.Color3(Color.Black) 106 GL.End() 107 v.SwapBuffers() 108 109 110 let vboWindow = new VBOWindow() 111 do vboWindow.Run()
7.如何更新OpenGL里的缓冲区.
1 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 open System 3 open System.Collections.Generic 4 open System.Windows.Forms 5 open System.Threading 6 open System.Drawing 7 open OpenTK 8 open OpenTK.Graphics 9 open OpenTK.Graphics.OpenGL 10 11 type VertexColor = 12 struct 13 val mutable R : byte 14 val mutable G : byte 15 val mutable B : byte 16 val mutable A : byte 17 val mutable Position : Vector3 18 static member Size = 16 19 end 20 21 type Location = 22 struct 23 val mutable Direction : Vector3 24 val mutable Age : uint32 25 end 26 27 type DynamicWindow() = 28 inherit GameWindow(400,300) 29 static let mutable MaxCount = 20 30 let mutable visibleCount = 0 31 let VBO = Array.create MaxCount (new VertexColor()) 32 let Locations = Array.create MaxCount (new Location()) 33 let mutable handle = 0; 34 35 override v.OnLoad e = 36 GL.ClearColor(0.1f,0.f,0.1f,0.f) 37 GL.Enable EnableCap.DepthTest 38 39 GL.PointSize 5.f 40 GL.Enable EnableCap.PointSmooth 41 GL.Hint(HintTarget.PointSmoothHint,HintMode.Nicest) 42 GL.EnableClientState ArrayCap.ColorArray 43 GL.EnableClientState ArrayCap.VertexArray 44 45 GL.GenBuffers(1,&handle) 46 GL.BindBuffer(BufferTarget.ArrayBuffer,handle) 47 GL.ColorPointer(4,ColorPointerType.UnsignedByte,VertexColor.Size,0) 48 GL.VertexPointer(3,VertexPointerType.Float,VertexColor.Size,4) 49 50 let rnd = new Random() 51 let temp = Vector3.Zero 52 let getRndByte() = 53 let rnd = rnd.Next(0,255) 54 byte rnd 55 let getRndDir() = 56 let rnd = (rnd.NextDouble()-0.5)*0.5 57 float32 rnd 58 for i = 0 to MaxCount - 1 do 59 VBO.[i].R <- getRndByte() 60 VBO.[i].G <- getRndByte() 61 VBO.[i].B <- getRndByte() 62 VBO.[i].A <- getRndByte() 63 VBO.[i].Position <- Vector3.Zero 64 65 Locations.[i].Direction <- new Vector3(getRndDir(),getRndDir(),getRndDir()) 66 Locations.[i].Age <- uint32 0 67 visibleCount <- 0 68 override v.OnResize e = 69 GL.Viewport(0,0,v.Width,v.Height) 70 GL.MatrixMode MatrixMode.Projection 71 let mutable pro = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,float32 v.Width/ float32 v.Height,1.0f,50.f) 72 GL.LoadMatrix(&pro) 73 GL.MatrixMode MatrixMode.Modelview 74 let mutable view = Matrix4.LookAt(Vector3.UnitZ,Vector3.Zero,Vector3.UnitY) 75 GL.LoadMatrix(&view) 76 override v.OnUpdateFrame e = 77 if visibleCount < MaxCount then 78 visibleCount <- visibleCount + 1 79 let mutable temp = Vector3.Zero 80 for i = MaxCount - visibleCount to MaxCount - 1 do 81 if Locations.[i].Age >= uint32 MaxCount then 82 Locations.[i].Age <- uint32 0 83 VBO.[i].Position <- Vector3.Zero 84 else 85 let max =Math.Max(Locations.[i].Direction.LengthFast * 10.f,1.f) 86 Locations.[i].Age <- uint32 max 87 Vector3.Multiply(&Locations.[i].Direction,float32 e.Time,&temp) 88 Vector3.Add(&VBO.[i].Position,&temp,&VBO.[i].Position) 89 90 override v.OnRenderFrame e = 91 v.Title <- visibleCount.ToString() + " Points." 92 (ClearBufferMask.ColorBufferBit |||ClearBufferMask.DepthBufferBit) |> GL.Clear 93 GL.PushMatrix() 94 GL.Translate(0.f,0.f,-5.f) 95 GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (VertexColor.Size * MaxCount),IntPtr.Zero,BufferUsageHint.StaticDraw) 96 GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (VertexColor.Size * MaxCount),VBO,BufferUsageHint.StaticDraw) 97 GL.DrawArrays(BeginMode.Points,MaxCount - visibleCount,visibleCount) 98 GL.PopMatrix() 99 v.SwapBuffers() 100 101 let w = new DynamicWindow() 102 do w.Run() 103
8.OpenGL纹理简单应用.
1 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 open System 3 open System.Collections.Generic 4 open System.Windows.Forms 5 open System.Threading 6 open System.Drawing 7 open System.Drawing.Imaging 8 open OpenTK 9 open OpenTK.Graphics 10 open OpenTK.Graphics.OpenGL 11 12 type Textures() = 13 inherit GameWindow(400,300) 14 let mutable texture = 0 15 let bitmap = new Bitmap(@"F:\3D\1.0\ConsoleApplication1\logo.jpg") 16 override v.OnLoad e = 17 GL.ClearColor Color.MidnightBlue 18 GL.Enable EnableCap.Texture2D 19 GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest) 20 21 GL.GenTextures(1,&texture) 22 GL.BindTexture(TextureTarget.Texture2D,texture) 23 let data = bitmap.LockBits(new System.Drawing.Rectangle(0,0,bitmap.Width,bitmap.Height),ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb) 24 GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.Rgba, data.Width, data.Height, 0, 25 OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0) 26 bitmap.UnlockBits(data) 27 GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear) 28 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear) 29 override v.OnResize e = 30 GL.Viewport(0,0,v.Width,v.Height) 31 GL.MatrixMode(MatrixMode.Projection) 32 GL.LoadIdentity() 33 GL.Ortho(-1.,1.,-1.,1.,0.,4.) 34 override v.OnRenderFrame e = 35 GL.Clear ClearBufferMask.ColorBufferBit 36 GL.MatrixMode(MatrixMode.Modelview) 37 GL.LoadIdentity() 38 GL.BindTexture(TextureTarget.Texture2D,texture) 39 GL.Color3(Color.White) 40 GL.Begin(BeginMode.Quads); 41 GL.TexCoord2(0.0f, 1.0f); 42 GL.Vertex2(-0.6f, -0.4f); 43 GL.TexCoord2(1.0f, 1.0f); 44 GL.Vertex2(0.6f, -0.4f); 45 GL.TexCoord2(1.0f, 0.0f); 46 GL.Vertex2(0.6f, 0.4f); 47 GL.TexCoord2(0.0f, 0.0f); 48 GL.Vertex2(-0.6f, 0.4f); 49 GL.End(); 50 v.SwapBuffers() 51 52 let t = new Textures() 53 do t.Run()
9.启用OpenTK里的GLControl.
1 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" 3 4 open System 5 open System.Collections.Generic 6 open System.Windows.Forms 7 open System.Threading 8 open System.Drawing 9 open System.Drawing.Imaging 10 open OpenTK 11 open OpenTK.Graphics 12 open OpenTK.Graphics.OpenGL 13 14 type loopForm() as form= 15 inherit Form() 16 let mutable x = 5.f 17 let mutable y = 5.f 18 let mutable z = 10.f 19 let offest = 1.f 20 let glControl = new OpenTK.GLControl() 21 let textX= new TextBox() 22 let textY= new TextBox() 23 let textZ= new TextBox() 24 let textLR = new TextBox() 25 let textUD= new TextBox() 26 let labelX= new Label() 27 let labelY= new Label() 28 let labelZ= new Label() 29 let labelLR = new Label() 30 let labelUD= new Label() 31 let aArray = [| 32 1.f;1.f;1.f 33 -1.f;1.f;1.f 34 -1.f;-1.f;1.f 35 1.f;-1.f;1.f 36 1.f;1.f;-1.f 37 1.f;1.f;1.f 38 1.f;-1.f;1.f 39 1.f;-1.f;-1.f 40 1.f;1.f;-1.f 41 -1.f;1.f;-1.f 42 -1.f;1.f;1.f 43 1.f;1.f;1.f 44 -1.f;1.f;-1.f 45 1.f;1.f;-1.f 46 1.f;-1.f;-1.f 47 -1.f;-1.f;-1.f 48 |] 49 let vArray = [| 50 1.f;1.f;1.f 51 -1.f;1.f;1.f 52 -1.f;-1.f;1.f 53 1.f;-1.f;1.f 54 1.f;1.f;-1.f 55 1.f;-1.f;-1.f 56 -1.f;1.f;-1.f 57 -1.f;-1.f;-1.f 58 |] 59 let iArray = [| 60 0u;1u;2u;3u; 61 4u;0u;3u;5u; 62 4u;6u;1u;0u; 63 6u;4u;5u;7u; 64 |] 65 66 let cArray = [| 67 1.f;1.f;1.f 68 1.f;0.f;0.f 69 0.f;1.f;0.f 70 0.f;0.f;1.f 71 1.f;1.f;0.f 72 0.f;1.f;1.f 73 1.f;0.f;1.f 74 0.f;0.f;0.f 75 |] 76 do 77 form.SuspendLayout() 78 glControl.Location <- new Point(10,40) 79 glControl.Size <- new Size(400,300) 80 glControl.BackColor <- Color.Red 81 glControl.Resize.Add(form.resize) 82 glControl.Paint.Add(form.paint) 83 form.MouseWheel.Add(form.MouseDown) 84 glControl.Text <- "xxxxxx" 85 form.ClientSize <- new Size(600,400) 86 form.Text <- "opengl" 87 form.StartPosition <- FormStartPosition.Manual 88 form.Location <- new Point(1200,600) 89 form.Controls.Add(glControl) 90 form.ResumeLayout(false) 91 labelX.Location <- new Point(420,40) 92 labelY.Location <- new Point(420,70) 93 labelZ.Location <- new Point(420,100) 94 labelLR.Location <- new Point(420,130) 95 labelUD.Location <- new Point(420,160) 96 labelX.Text <- "X:" 97 labelY.Text <- "Y:" 98 labelZ.Text <- "Z:" 99 labelLR.Text <- "水平:" 100 labelUD.Text <-"上下:" 101 textX.Location <- new Point(460,40) 102 textY.Location <- new Point(460,70) 103 textZ.Location <- new Point(460,100) 104 textLR.Location <- new Point(460,130) 105 textUD.Location <- new Point(460,160) 106 form.Controls.Add(textX) 107 form.Controls.Add(textY) 108 form.Controls.Add(textZ) 109 form.Controls.Add(textLR) 110 form.Controls.Add(textUD) 111 form.Controls.Add(labelX) 112 form.Controls.Add(labelY) 113 form.Controls.Add(labelZ) 114 form.Controls.Add(labelLR) 115 form.Controls.Add(labelUD) 116 override v.OnLoad e = 117 base.OnLoad e 118 GL.ClearColor Color.MidnightBlue 119 Application.Idle.Add(v.AIdle) 120 v.ShowUI 121 textX.TextChanged.Add(form.TextChange) 122 textY.TextChanged.Add(form.TextChange) 123 textZ.TextChanged.Add(form.TextChange) 124 textLR.TextChanged.Add(form.TextChange) 125 textUD.TextChanged.Add(form.TextChange) 126 //踢除正反面 127 //GL.Enable EnableCap.CullFace 128 //GL.CullFace CullFaceMode.Back 129 //指定正反面 130 GL.FrontFace FrontFaceDirection.Ccw 131 //设置材料面填充模式 132 GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill) 133 GL.PolygonMode(MaterialFace.Back,PolygonMode.Line) 134 //启用数组功能. 135 GL.EnableClientState(ArrayCap.VertexArray) 136 GL.EnableClientState(ArrayCap.ColorArray) 137 member v.resize (e:EventArgs) = 138 GL.Viewport(0,0,glControl.ClientSize.Width,glControl.ClientSize.Height) 139 let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height 140 let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,0.1f,64.f) 141 GL.MatrixMode MatrixMode.Projection 142 GL.LoadMatrix(&projection) 143 member v.paint (e:PaintEventArgs) = 144 v.Render() 145 member v.AIdle (e:EventArgs) = 146 while (glControl.IsIdle) do 147 v.Render() 148 member v.TextChange (e:EventArgs) = 149 x <- v.UIValue(textX) 150 y <- v.UIValue(textY) 151 z <- v.UIValue(textZ) 152 member v.MouseDown(e:MouseEventArgs) = 153 match v.ActiveControl with 154 | :? TextBox as t1 -> 155 let mutable t = v.UIValue(t1) 156 t <- t + float32 e.Delta * offest * 0.01f 157 t1.Text <- t.ToString() 158 | _ -> 159 v.Text <- v.ActiveControl.Text 160 let state =float32 e.Delta * offest * 0.01f 161 z <- z + state 162 v.ShowUI 163 member x.UIValue 164 with get (text:TextBox) = 165 let mutable value = 0.f 166 if System.Single.TryParse(text.Text,&value) then 167 value <- value 168 value 169 and set (text:TextBox) (value:float32) = text.Text<- value.ToString() 170 member v.ShowUI = 171 textX.Text <- x.ToString() 172 textY.Text <- y.ToString() 173 textZ.Text <- z.ToString() 174 textLR.Text <- v.UIValue(textLR).ToString() 175 textUD.Text <- v.UIValue(textUD).ToString() 176 //lControl.Focus() |> ignore 177 member v.Render = 178 // v.ShowUI 179 let mutable lookat = Matrix4.LookAt(new Vector3(x,y,z),Vector3.Zero,Vector3.UnitY) 180 GL.MatrixMode(MatrixMode.Modelview) 181 GL.LoadMatrix(&lookat) 182 GL.Translate(1.f,1.f,1.f) 183 GL.Rotate(v.UIValue(textLR),0.f,1.f,0.f) 184 GL.Rotate(v.UIValue(textUD),1.f,0.f,0.f) 185 GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 186 GL.Color3(1.f,1.f,0.f) 187 GL.PointSize(10.f) 188 GL.Begin BeginMode.Points 189 GL.Vertex3(x-1.f,y-1.f,0.f) 190 GL.Vertex3(0.f,2.f,0.f) 191 GL.End() 192 GL.Begin BeginMode.Lines 193 GL.Vertex3(x,y,-200.f) 194 GL.Vertex3(x,y,200.f) 195 GL.End() 196 GL.Color3(Color.Black) 197 //GL.DrawArrays(BeginMode.Quads,0,cArray.Length) 198 // GL.Begin BeginMode.Quads 199 // GL.ArrayElement(0) 200 // GL.ArrayElement(1) 201 // GL.ArrayElement(2) 202 // GL.ArrayElement(3) 203 // GL.End() 204 GL.VertexPointer(3,VertexPointerType.Float,0,aArray) 205 GL.ColorPointer(3,ColorPointerType.Float,0,cArray) 206 GL.IndexPointer(IndexPointerType.Int,0,iArray) 207 //GL.DrawElements(BeginMode.Quads,4, DrawElementsType.UnsignedInt, [|0u;1u;2u;3u|]) 208 //GL.DrawElements(BeginMode.Quads,4, DrawElementsType.UnsignedInt, [|4u;5u;6u;7u|]) 209 GL.DrawElements(BeginMode.Quads,iArray.Length,DrawElementsType.UnsignedInt,iArray) 210 GL.Begin BeginMode.Lines 211 GL.Color3(Color.Black) 212 GL.Vertex3(Vector3.Zero) 213 GL.Vertex3(Vector3.UnitX * 1000.f) 214 GL.Color3(Color.White) 215 GL.Vertex3(Vector3.Zero) 216 GL.Vertex3(Vector3.UnitY * 1000.f) 217 GL.Color3(Color.Red) 218 GL.Vertex3(Vector3.Zero) 219 GL.Vertex3(Vector3.UnitZ * 1000.f) 220 GL.End() 221 glControl.SwapBuffers() 222 ignore 223 let t = new loopForm() 224 t.Show() 225 226 // GL.Begin BeginMode.Quads 227 // GL.Color3(Color.Black) 228 // GL.Vertex3(1.f,1.f,1.f) 229 // GL.Vertex3(-1.f,1.f,1.f) 230 // GL.Vertex3(-1.f,-1.f,1.f) 231 // GL.Vertex3(1.f,-1.f,1.f) 232 // 233 // GL.Color3(Color.White) 234 // GL.Vertex3(1.f,1.f,-1.f) 235 // GL.Vertex3(1.f,1.f,1.f) 236 // GL.Vertex3(1.f,-1.f,1.f) 237 // GL.Vertex3(1.f,-1.f,-1.f) 238 // 239 // GL.Color3(Color.Red) 240 // GL.Vertex3(1.f,1.f,-1.f) 241 // GL.Vertex3(-1.f,1.f,-1.f) 242 // GL.Vertex3(-1.f,1.f,1.f) 243 // GL.Vertex3(1.f,1.f,1.f) 244 // 245 // GL.Color3(0.f,1.f,0.f) 246 // GL.Vertex3(-1.f,1.f,-1.f) 247 // GL.Vertex3(1.f,1.f,-1.f) 248 // GL.Vertex3(1.f,-1.f,-1.f) 249 // GL.Vertex3(-1.f,-1.f,-1.f) 250 // GL.End()
9.OpenTK里的光照.
1 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.Compatibility.dll" 3 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" 4 5 open System 6 open System.Collections.Generic 7 open System.Windows.Forms 8 open System.Threading 9 open System.Drawing 10 open System.Drawing.Imaging 11 open OpenTK 12 open OpenTK.Graphics 13 open OpenTK.Graphics.OpenGL 14 15 type loopForm() as form= 16 inherit Form() 17 let mutable x = 10.f 18 let mutable y = 10.f 19 let mutable z = 10.f 20 let offest = 1.f 21 let glControl = new OpenTK.GLControl() 22 let textX= new TextBox() 23 let textY= new TextBox() 24 let textZ= new TextBox() 25 let textLR = new TextBox() 26 let textUD= new TextBox() 27 let textInfo = new TextBox() 28 let labelX= new Label() 29 let labelY= new Label() 30 let labelZ= new Label() 31 let labelLR = new Label() 32 let labelUD= new Label() 33 let mutable first = 0 34 let mutable buffer = 0 35 let list = 0 36 let scale = 3.f 37 let mutable r = 0.f 38 let q = Glu.NewQuadric() 39 let mutable ll = [|7.f;7.f;0.f;1.f|] 40 do 41 form.SuspendLayout() 42 glControl.Location <- new Point(10,40) 43 glControl.Size <- new Size(400,300) 44 glControl.BackColor <- Color.Red 45 glControl.Resize.Add(form.resize) 46 glControl.Paint.Add(form.paint) 47 form.MouseWheel.Add(form.MouseDown) 48 form.ClientSize <- new Size(600,400) 49 form.Text <- "opengl" 50 form.StartPosition <- FormStartPosition.Manual 51 form.Location <- new Point(100,200) 52 form.Controls.Add(glControl) 53 form.ResumeLayout(false) 54 labelX.Location <- new Point(420,40) 55 labelY.Location <- new Point(420,70) 56 labelZ.Location <- new Point(420,100) 57 labelLR.Location <- new Point(420,130) 58 labelUD.Location <- new Point(420,160) 59 labelX.Text <- "X:" 60 labelY.Text <- "Y:" 61 labelZ.Text <- "Z:" 62 labelLR.Text <- "水平:" 63 labelUD.Text <-"上下:" 64 textX.Location <- new Point(460,40) 65 textY.Location <- new Point(460,70) 66 textZ.Location <- new Point(460,100) 67 textLR.Location <- new Point(460,130) 68 textUD.Location <- new Point(460,160) 69 textInfo.Text <- "3" 70 textInfo.Location <- new Point(420,190) 71 textInfo.Width <- 140 72 form.Controls.Add(textX) 73 form.Controls.Add(textY) 74 form.Controls.Add(textZ) 75 form.Controls.Add(textLR) 76 form.Controls.Add(textUD) 77 form.Controls.Add(labelX) 78 form.Controls.Add(labelY) 79 form.Controls.Add(labelZ) 80 form.Controls.Add(labelLR) 81 form.Controls.Add(labelUD) 82 form.Controls.Add(textInfo) 83 //#endregion 84 override v.OnLoad e = 85 base.OnLoad e 86 GL.ClearColor Color.MidnightBlue 87 Application.Idle.Add(v.AIdle) 88 v.ShowUI 89 textX.TextChanged.Add(form.TextChange) 90 textY.TextChanged.Add(form.TextChange) 91 textZ.TextChanged.Add(form.TextChange) 92 textLR.TextChanged.Add(form.TextChange) 93 textUD.TextChanged.Add(form.TextChange) 94 GL.FrontFace FrontFaceDirection.Ccw 95 //设置材料面填充模式 96 GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill) 97 GL.PolygonMode(MaterialFace.Back,PolygonMode.Line) 98 //启用数组功能. 99 GL.EnableClientState(ArrayCap.VertexArray) 100 //GL.EnableClientState(ArrayCap.ColorArray) 101 GL.EnableClientState(ArrayCap.IndexArray) 102 GL.Light(LightName.Light0,LightParameter.Ambient,[|0.f;1.f;1.f;1.f|]) 103 GL.Light(LightName.Light0,LightParameter.Diffuse,[|1.f;0.f;1.f;1.f|]) 104 GL.Light(LightName.Light0,LightParameter.Specular,[|0.f;0.f;1.f;1.f|]) 105 GL.Light(LightName.Light0,LightParameter.SpotDirection,[|-1.f;-1.f;0.f|]) 106 GL.Light(LightName.Light0,LightParameter.SpotCutoff,[|60|]) 107 108 GL.Light(LightName.Light1,LightParameter.Position,[|20.f;20.f;20.f;0.f|]) 109 GL.Light(LightName.Light1,LightParameter.Ambient,[|0.5f;0.5f;0.5f;1.f|]) 110 GL.Light(LightName.Light1,LightParameter.Diffuse,[|0.5f;0.5f;0.5f;1.f|]) 111 GL.Light(LightName.Light1,LightParameter.Specular,[|1.f;1.f;1.f;1.f|]) 112 //GL.Light(LightName.Light0,LightParameter.Specular,[|1.f;0.f;1.f;1.f|]) 113 //GL.Material(MaterialFace.Front,MaterialParameter.Ambient,[|1.f;1.f;1.f;1.f|]) 114 GL.Enable(EnableCap.Lighting) 115 GL.Enable(EnableCap.Light0) 116 GL.Enable(EnableCap.Light1) 117 GL.Enable(EnableCap.DepthTest) 118 //#region "" 119 member v.resize (e:EventArgs) = 120 GL.Viewport(0,0,glControl.ClientSize.Width,glControl.ClientSize.Height) 121 let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height 122 let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,0.1f,64.f) 123 GL.MatrixMode MatrixMode.Projection 124 GL.LoadMatrix(&projection) 125 member v.paint (e:PaintEventArgs) = 126 v.Render() 127 member v.AIdle (e:EventArgs) = 128 while (glControl.IsIdle) do 129 v.Render() 130 member v.TextChange (e:EventArgs) = 131 x <- v.UIValue(textX) 132 y <- v.UIValue(textY) 133 z <- v.UIValue(textZ) 134 member v.MouseDown(e:MouseEventArgs) = 135 match v.ActiveControl with 136 | :? TextBox as t1 -> 137 let mutable t = v.UIValue(t1) 138 t <- t + float32 e.Delta * offest * 0.01f 139 t1.Text <- t.ToString() 140 | _ -> 141 v.Text <- v.ActiveControl.Text 142 let state =float32 e.Delta * offest * 0.01f 143 x<- x+state 144 y<- y + state 145 z <- z + state 146 v.ShowUI 147 member x.UIValue 148 with get (text:TextBox) = 149 let mutable value = 0.f 150 if System.Single.TryParse(text.Text,&value) then 151 value <- value 152 value 153 and set (text:TextBox) (value:float32) = text.Text<- value.ToString() 154 member v.ShowUI = 155 textX.Text <- x.ToString() 156 textY.Text <- y.ToString() 157 textZ.Text <- z.ToString() 158 textLR.Text <- v.UIValue(textLR).ToString() 159 textUD.Text <- v.UIValue(textUD).ToString() 160 member v.Render = 161 let mutable lookat = Matrix4.LookAt(new Vector3(x,y,z),Vector3.Zero,Vector3.UnitY) 162 GL.MatrixMode(MatrixMode.Modelview) 163 GL.LoadMatrix(&lookat) 164 GL.Rotate(v.UIValue(textLR),0.f,1.f,0.f) 165 GL.Rotate(v.UIValue(textUD),1.f,0.f,0.f) 166 GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 167 GL.Begin BeginMode.Lines 168 GL.Color3(Color.Black) 169 GL.Vertex3(Vector3.Zero) 170 GL.Vertex3(Vector3.UnitX * 1000.f) 171 GL.Color3(Color.White) 172 GL.Vertex3(Vector3.Zero) 173 GL.Vertex3(Vector3.UnitY * 1000.f) 174 GL.Color3(Color.Red) 175 GL.Vertex3(Vector3.Zero) 176 GL.Vertex3(Vector3.UnitZ * 1000.f) 177 GL.End() 178 r <- (r + 0.01f) % 360.f 179 textInfo.Text <- r.ToString() 180 GL.PushMatrix() 181 GL.Rotate(r,0.f,1.f,0.f) 182 GL.Light(LightName.Light0,LightParameter.Position,ll) 183 GL.PushMatrix() 184 GL.Translate(ll.[0],ll.[1],ll.[2]) 185 Glu.Sphere(q,1.0,20,20) 186 GL.PopMatrix() 187 GL.PopMatrix() 188 Glu.Sphere(q,3.0,20,20) 189 glControl.SwapBuffers() 190 ignore 191 let t = new loopForm() 192 t.Show()
10.相关画圆,画立方体类.
1 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.Compatibility.dll" 3 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" 4 5 open System 6 open System.Collections.Generic 7 open System.Windows.Forms 8 open System.Threading 9 open System.Drawing 10 open System.Drawing.Imaging 11 open OpenTK 12 open OpenTK.Graphics 13 open OpenTK.Graphics.OpenGL 14 15 module Shape = 16 17 type T2N3V3 = 18 struct 19 val mutable TexCoord : Vector2 20 val mutable Normal : Vector3 21 val mutable Position : Vector3 22 new(v,n,p) = {TexCoord = v;Normal = n;Position = p} 23 end 24 [<AbstractClass>] 25 type Shape() = 26 let mutable bCreate = false 27 let mutable vi = 0 28 let mutable ei = 0 29 let mutable count = 0 30 member this.vboID with get() = vi and set value = vi <- value 31 member this.eboID with get() = ei and set value = ei <- value 32 member this.TriangelCount with get() = count and set value = count <- value 33 member this.IsCreate with get() = bCreate and set value = bCreate <- value 34 abstract Draw : unit -> unit 35 abstract Init : unit -> unit 36 member this.InitQ : unit -> unit =fun () -> () 37 38 type Sphere(radius:float32,level:int) = 39 inherit Shape() 40 let mutable rad,lev = radius,level 41 let RightLevel = 42 if lev < 0 then lev <- 0 43 elif lev > 6 then lev <-6 44 override this.Draw() = 45 if this.IsCreate<>true then this.Init() 46 GL.BindBuffer(BufferTarget.ArrayBuffer,this.vboID) 47 GL.BindBuffer(BufferTarget.ElementArrayBuffer,this.eboID) 48 GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f,0,IntPtr.Zero) 49 GL.DrawElements(BeginMode.Triangles,this.TriangelCount,DrawElementsType.UnsignedInt,IntPtr.Zero) 50 override this.Init() = 51 let alls = Array.create 6 (new T2N3V3()) 52 alls.[0] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitX, Vector3.UnitX * rad ) 53 alls.[1] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitY, Vector3.UnitY * rad ) 54 alls.[2] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitZ, Vector3.UnitZ * rad ) 55 alls.[3] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitX, -Vector3.UnitX * rad ) 56 alls.[4] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitY, -Vector3.UnitY * rad ) 57 alls.[5] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitZ, -Vector3.UnitZ * rad ) 58 let is = [| 59 1;2;0 60 0;2;4 61 0;4;5 62 5;1;0 63 1;3;2 64 4;2;3 65 4;3;5 66 1;5;3 67 |] 68 let (vvv:T2N3V3 []),(iv: int[]) = this.Sub (alls,is) 69 let mutable vID,eID = 0,0 70 //let mutable tv,vv,pv = vvv |> Array.map (fun v -> v.TexCoord,v.Normal,v.Position) |> Array.unzip3 71 GL.GenBuffers(1,&vID) 72 GL.BindBuffer(BufferTarget.ArrayBuffer,vID) 73 GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (4 * 8 * vvv.Length),vvv,BufferUsageHint.StaticDraw) 74 75 GL.GenBuffers(1,&eID) 76 GL.BindBuffer(BufferTarget.ElementArrayBuffer,eID) 77 GL.BufferData(BufferTarget.ElementArrayBuffer,IntPtr (4 * iv.Length),iv,BufferUsageHint.StaticDraw) 78 79 this.vboID <- vID 80 this.eboID <- eID 81 this.TriangelCount <- iv.Length 82 this.IsCreate <- true 83 () 84 member v.GetMidValue (first:T2N3V3,second:T2N3V3) = 85 let midN = Vector3.Lerp(first.Position,second.Position,0.5f) |> Vector3.Normalize 86 let midP = midN *(float32 rad) 87 let midT = Vector2.Lerp(first.TexCoord,second.TexCoord,0.5f) |> Vector2.Normalize 88 let result = new T2N3V3(midT,midN,midP) 89 result 90 member v.Subdivide (v1:T2N3V3,v2:T2N3V3,v3:T2N3V3) = 91 let vs = Array.create 6 (new T2N3V3()) 92 vs.[0] <- v1 93 vs.[1] <- v.GetMidValue(v1,v2) 94 vs.[2] <- v.GetMidValue(v3,v1) 95 vs.[3] <- v2 96 vs.[4] <- v.GetMidValue(v2,v3) 97 vs.[5] <- v3 98 let is = Array.create 12 0 99 is.[0] <- 0 100 is.[1] <- 1 101 is.[2] <- 2 102 is.[3] <- 2 103 is.[4] <- 1 104 is.[5] <- 4 105 is.[6] <- 4 106 is.[7] <- 1 107 is.[8] <- 3 108 is.[9] <- 2 109 is.[10] <-4 110 is.[11] <- 5 111 (vs,is) 112 member this.Sub(alls:T2N3V3 [],is:int []) = 113 //let mutable tv,vv,pv = alls |> Array.map (fun v -> v.TexCoord,v.Normal,v.Position) |> Array.unzip3 114 let mutable allv = alls 115 let mutable iv = is 116 let show array = printfn "%A" array 117 for j in 0 .. lev do 118 let mutable av = Array.create 0 (new T2N3V3()) 119 let mutable ev = Array.create 0 0 120 printfn "%i" allv.Length 121 printfn "%i" iv.Length 122 for i in 0 .. 3 .. iv.Length - 1 do 123 let (vvv,iiv) = this.Subdivide(allv.[iv.[i]],allv.[iv.[i+1]],allv.[iv.[i+2]]) 124 let length = av.Length 125 av <- Array.append av vvv 126 let map = iiv |> Array.map (fun p -> p + length) 127 ev <- Array.append ev map 128 allv <- av 129 iv <- ev 130 allv |> Array.map (fun p -> p.Position) |> show 131 show iv 132 allv,iv 133 134 type Cube(width:float32,height:float32,length:float32,index:int) = 135 inherit Shape() 136 let mutable id = index 137 let xl,yl,zl =width/2.f,height/2.f,length/2.f 138 let mutable color = Color.White 139 let v8 = [| 140 new Vector3(xl,yl,zl) 141 new Vector3(-xl,yl,zl) 142 new Vector3(-xl,-yl,zl) 143 new Vector3(xl,-yl,zl) 144 new Vector3(xl,yl,-zl) 145 new Vector3(-xl,yl,-zl) 146 new Vector3(-xl,-yl,-zl) 147 new Vector3(xl,-yl,-zl) 148 |] 149 new(x,y,z) = 150 let rnd = System.Random().Next() 151 printfn "%i" rnd 152 Cube(x,y,z,-1) 153 override this.Draw() = 154 if this.IsCreate<>true then this.Init() 155 GL.EnableClientState(ArrayCap.VertexArray) 156 GL.EnableClientState(ArrayCap.NormalArray) 157 GL.BindBuffer(BufferTarget.ArrayBuffer,this.vboID) 158 GL.VertexPointer(3,VertexPointerType.Float,0,IntPtr.Zero) 159 GL.PushMatrix() 160 if id >= 0 && id < 8 then 161 GL.Translate(v8.[id]) 162 GL.Color3(this.Color:Color) 163 GL.Normal3(Vector3.UnitZ) 164 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|0;1;2;0;2;3|]) 165 //GL.Color3(Color.Black) 166 GL.Normal3(Vector3.UnitY) 167 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|4;5;1;4;1;0|]) 168 //GL.Color3(Color.Red) 169 GL.Normal3(Vector3.UnitX) 170 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|4;0;3;4;3;7|]) 171 //GL.Color3(Color.Green) 172 GL.Normal3(-Vector3.UnitY) 173 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|3;2;6;3;6;7|]) 174 //GL.Color3(Color.Blue) 175 GL.Normal3(-Vector3.UnitX) 176 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|1;5;6;1;6;2|]) 177 //GL.Color3(Color.DodgerBlue) 178 GL.Normal3(-Vector3.UnitZ) 179 GL.DrawElements(BeginMode.Triangles,6,DrawElementsType.UnsignedInt,[|5;4;7;5;7;6|]) 180 GL.PopMatrix() 181 override this.Init() = 182 let mutable vID = 0 183 GL.GenBuffers(1,&vID) 184 GL.BindBuffer(BufferTarget.ArrayBuffer,vID) 185 GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (4 * 3 * v8.Length),v8,BufferUsageHint.StaticDraw) 186 this.vboID <- vID 187 this.IsCreate <- true 188 let rnd = System.Random(this.GetHashCode()) 189 this.Color <- Color.FromArgb(rnd.Next(0,255),rnd.Next(0,255),rnd.Next(0,255)) 190 () 191 member this.Index with get() = id and set value = id <-value 192 member this.Color with get() = color and set value = color <- value 193 194 type Camera() = 195 let mutable eye = Vector3.Zero 196 let mutable eyeLength = 10. 197 let mutable yangle = 0. 198 let mutable xangle= Math.PI/2. 199 member this.Eye 200 with get() = eye 201 and set value = eye <- value 202 member this.EyeLength 203 with get() = eyeLength 204 and set value = 205 if value < 0. then eyeLength <- 0.1 206 eyeLength <- value 207 member this.YAngle 208 with get() = yangle 209 and set value = 210 if value >= Math.PI then yangle <- 0. 211 //elif value <= 0. then yangle <- Math.PI - 0.1 212 else yangle <- value 213 member this.XAngle 214 with get() = xangle 215 and set value = 216 if value >= 2.* Math.PI then xangle <- 0. 217 //elif value <= 0. then yangle <- 2. * Math.PI - 0.1 218 else xangle <- value 219 member this.Target 220 with get() = 221 //printfn "%f" this.XAngle 222 let x:float =float eye.X + eyeLength * Math.Cos(this.YAngle)* Math.Cos(this.XAngle) 223 let y:float =float eye.Y + eyeLength * Math.Sin(this.YAngle) 224 let z:float =float eye.Z + eyeLength * Math.Cos(this.YAngle)* Math.Sin(this.XAngle) 225 Vector3(float32 x,float32 y,float32 z) 226 member this.Transelt (x,y,z) = 227 let sinX = Math.Sin(this.XAngle) 228 let cosX = Math.Cos(this.XAngle) 229 let x1 = float this.Eye.X + x * sinX + z * cosX 230 let y1 = float this.Eye.Y + y 231 let z1 = float this.Eye.Z + z * sinX + x * cosX 232 printfn "angle:%f, sinx:%f, cosx:%f" x y z 233 printfn "x:%f, y:%f, z:%f" this.Eye.X this.Eye.Y this.Eye.Z 234 this.Eye <- new Vector3(float32 x1,float32 y1,float32 z1) 235 member this.UpAndDown y = 236 let ya = this.YAngle + y 237 this.YAngle <- ya 238 member this.RightAndLeft x = 239 let xa = this.XAngle + x 240 this.XAngle <- xa 241 member this.Rotate (x,y) = 242 let xa = this.XAngle + x 243 let ya = this.YAngle + y 244 this.YAngle <- ya 245 this.XAngle <- xa
11...
1 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll" 2 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" 3 #load "Shape.fsx" 4 5 open System 6 open System.Collections.Generic 7 open System.Windows.Forms 8 open System.Threading 9 open System.Drawing 10 open System.Drawing.Imaging 11 open OpenTK 12 open OpenTK.Graphics 13 open OpenTK.Graphics.OpenGL 14 open Shape 15 16 type loopForm() as form= 17 inherit Form() 18 let caram = Shape.Camera() 19 let offest = 0.1f 20 let offestd = float offest 21 let glControl = new OpenTK.GLControl() 22 23 let cubeEnter = Shape.Cube(1.5f,0.2f,1.8f,3) 24 let cubeParlor = Shape.Cube(4.9f,0.2f,6.3f,3) 25 let cubeBalcony1 = Shape.Cube(4.9f,0.2f,1.9f,3) 26 let cubeBalcony2 = Shape.Cube(1.6f,0.2f,2.1f,3) 27 let cubeBalcony3 = Shape.Cube(1.8f,0.2f,3.0f,3) 28 let cubeKitchen = Shape.Cube(4.5f,0.2f,1.8f,3) 29 let cubeBathroom = Shape.Cube(2.1f,0.2f,2.1f,3) 30 let cubeGallery = Shape.Cube(2.1f,0.2f,1.2f,3) 31 let cubeMasterBedroom = Shape.Cube(3.6f,0.2f,3.9f,3) 32 let cubeSecondbedroom = Shape.Cube(3.0f,0.2f,3.3f,3) 33 34 let cubeWall1 = Shape.Cube(0.2f,3.f,8.1f,0) 35 let cubeWall2 = Shape.Cube(4.5f,3.f,0.2f,0) 36 let cubeWall3 = Shape.Cube(0.2f,3.f,1.8f,0) 37 let cubeWall4 = Shape.Cube(0.2f,1.8f,1.8f,0) 38 let cubeWall5 = Shape.Cube(3.0f,3.f,0.2f,0) 39 let cubeWall6 = Shape.Cube(0.2f,3.f,3.7f,0) 40 let cubeBathroom1 = Shape.Cube(2.1f,3.f,0.2f,0) 41 let cubeBathroom2 = Shape.Cube(0.2f,3.f,2.1f,0) 42 let cubeFence1 = Shape.Cube(0.05f,0.02f,1.9f,0) 43 let cubeFence2 = Shape.Cube(2.3f,0.02f,0.05f,0) 44 let cubeFence3 = Shape.Cube(0.05f,0.02f,2.1f,0) 45 let cubeFence4 = Shape.Cube(3.4f,0.02f,0.05f,0) 46 let cubeWall7 = Shape.Cube(2.1f,3.f,0.2f,4) 47 let cubeWall8 = Shape.Cube(0.2f,3.f,3.0f,0) 48 let cubePillar = Shape.Cube(0.2f,3.f,0.2f,4) 49 let cubePillar2 = Shape.Cube(0.1f,1.1f,0.2f,4) 50 let mutable oldMouseLocation = Vector2.Zero 51 let path = [| 52 "D:\工程\OpenTK\1.0\ConsoleApplication1\白墙.jpg" 53 "D:\工程\OpenTK\1.0\ConsoleApplication1\电视墙.jpg" 54 "D:\工程\OpenTK\1.0\ConsoleApplication1\客厅.jpg" 55 |] 56 let mutable texs = Array.create path.Length 0 57 do 58 caram.Transelt(1.,1.7,0.0) 59 form.SuspendLayout() 60 glControl.Location <- new Point(10,40) 61 glControl.Size <- new Size(400,300) 62 glControl.BackColor <- Color.Red 63 glControl.Resize.Add(form.resize) 64 glControl.Paint.Add(form.paint) 65 glControl.KeyDown.Add(form.KeyDown) 66 glControl.MouseMove.Add(form.MouseDownv) 67 form.ClientSize <- new Size(450,350) 68 form.Text <- "opengl" 69 //form.StartPosition <- FormStartPosition.Manual 70 form.Location <- new Point(1200,600) 71 form.Controls.Add(glControl) 72 form.ResumeLayout(false) 73 //#endregion 74 override v.OnLoad e = 75 base.OnLoad e 76 GL.ClearColor Color.MidnightBlue 77 Application.Idle.Add(v.AIdle) 78 GL.FrontFace FrontFaceDirection.Ccw 79 GL.Enable( EnableCap.PointSmooth ) 80 GL.Enable EnableCap.Texture2D 81 //GL.Enable EnableCap.Lighting 82 //踢除正反面 83 GL.Enable EnableCap.CullFace 84 GL.CullFace CullFaceMode.Back 85 //设置材料面填充模式 86 GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill) 87 GL.PolygonMode(MaterialFace.Back,PolygonMode.Line) 88 texs <- Shape.TexTure.Load(path) 89 GL.Light(LightName.Light0,LightParameter.Position,[|20.f;20.f;20.f;0.f|]) 90 GL.Light(LightName.Light0,LightParameter.Ambient,[|0.5f;0.5f;0.5f;1.f|]) 91 GL.Light(LightName.Light0,LightParameter.Diffuse,[|0.5f;0.5f;0.5f;1.f|]) 92 GL.Light(LightName.Light0,LightParameter.Specular,[|1.f;1.f;1.f;1.f|]) 93 GL.Enable(EnableCap.Lighting) 94 //GL.Enable(EnableCap.Light0) 95 GL.Enable(EnableCap.DepthTest) 96 97 //#region 98 member v.resize (e:EventArgs) = 99 GL.Viewport(0,0,glControl.ClientSize.Width,glControl.ClientSize.Height) 100 let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height 101 let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2,aspect,0.1f,30.f) 102 GL.MatrixMode MatrixMode.Projection 103 GL.LoadMatrix(&projection) 104 member v.paint (e:PaintEventArgs) = 105 v.Render() 106 member v.AIdle (e:EventArgs) = 107 while (glControl.IsIdle) do 108 v.Render() 109 member v.MouseDownv(e:MouseEventArgs) = 110 if e.Button = MouseButtons.Right then 111 if oldMouseLocation = Vector2.Zero then 112 oldMouseLocation <- Vector2(float32 e.X,float32 e.Y) 113 else 114 let nx = (float32 e.X - oldMouseLocation.X) * offest * 0.1f 115 let ny = (float32 e.Y - oldMouseLocation.Y) * offest * -0.1f 116 caram.Rotate(float nx,float ny) 117 oldMouseLocation <- Vector2(float32 e.X,float32 e.Y) 118 member v.KeyDown(e:KeyEventArgs) = 119 //let keys = e.KeyData 120 match e.KeyCode with 121 | Keys.E ->caram.Transelt(0.,0.,1.* offestd) 122 | Keys.D ->caram.Transelt(0.,0.,-1.* offestd) 123 | Keys.S ->caram.Transelt(1.* offestd,0.,0.0) 124 | Keys.F ->caram.Transelt(-1.* offestd,0.,0.) 125 //| Keys.Space -> caram.Transelt() 126 | _ -> () 127 member x.UIValue 128 with get (text:TextBox) = 129 let mutable value = 0.f 130 if System.Single.TryParse(text.Text,&value) then 131 value <- value 132 value 133 and set (text:TextBox) (value:float32) = text.Text<- value.ToString() 134 //#endregion 135 member v.Render = 136 //Keyboard[OpenTK.Input.Key.F11] 137 GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit) 138 //地面 139 //v.DrawCube(cubeEnter,Vector3.Zero) 140 v.DrawCube(cubeEnter,0.f,0.f,0.f,0,texs.[0]) 141 v.DrawCube(cubeParlor,0.f,0.f,1.8f) 142 v.DrawCube(cubeBalcony1,0.f,0.f,8.1f) 143 v.DrawCube(cubeBalcony2,2.3f,0.f,10.f) 144 v.DrawCube(cubeBalcony3,3.9f,0.f,9.f) 145 v.DrawCube(cubeKitchen,1.5f,0.f,0.f) 146 v.DrawCube(cubeBathroom,3.9f,0.f,1.8f) 147 v.DrawCube(cubeGallery,3.9f,0.f,3.9f) 148 v.DrawCube(cubeMasterBedroom,3.9f,0.f,5.1f) 149 v.DrawCube(cubeSecondbedroom,6.0f,0.f,1.8f) 150 //墙 151 v.DrawCube(cubeWall1,Vector3.Zero) 152 //v.DrawCube(cubeWall2,Vector3(1.5f,0.f,0.f)) 153 v.DrawCube(cubeWall2,1.5f,0.f,0.f,0,texs.[2]) 154 v.DrawCube(cubeWall3,Vector3(1.5f,0.f,0.f)) 155 v.DrawCube(cubeWall4,Vector3(6.0f,0.f,0.f)) 156 v.DrawCube(cubeWall5,Vector3(6.0f,0.f,1.8f)) 157 v.DrawCube(cubeWall6,Vector3(9.0f,0.f,1.8f)) 158 v.DrawCube(cubeWall6,Vector3(7.5f,0.f,5.1f)) 159 v.DrawCube(cubeWall6,Vector3(3.9f,0.f,5.1f)) 160 v.DrawCube(cubeWall7,Vector3(3.9f,0.f,9.0f)) 161 v.DrawCube(cubeWall8,Vector3(6.0f,0.f,9.0f)) 162 v.DrawCube(cubeBathroom1,Vector3(3.9f,0.f,1.8f)) 163 //v.DrawCube(cubeBathroom1,Vector3(3.9f,0.f,3.9f)) 164 v.DrawCube(cubeBathroom2,Vector3(3.9f,0.f,1.8f)) 165 v.DrawCube(cubeBathroom2,Vector3(6.0f,0.f,1.8f)) 166 v.DrawCube(cubeFence1,0.0f,1.f,8.1f) 167 v.DrawCube(cubeFence2,0.0f,1.f,10.f) 168 v.DrawCube(cubeFence3,2.3f,1.f,10.f) 169 v.DrawCube(cubeFence4,2.3f,1.f,12.1f) 170 v.DrawCube(cubePillar,Vector3(2.3f,0.f,12.f)) 171 v.DrawCube(cubePillar,Vector3(2.3f,0.f,9.9f)) 172 v.DrawCube(cubePillar2,Vector3(0.f,0.f,9.9f)) 173 //房顶 174 GL.PushMatrix() 175 GL.Translate(0.f,3.f,0.f) 176 v.DrawCube(cubeEnter,Vector3.Zero) 177 v.DrawCube(cubeParlor,0.f,0.f,1.8f) 178 v.DrawCube(cubeBalcony1,0.f,0.f,8.1f) 179 v.DrawCube(cubeBalcony2,2.3f,0.f,10.f) 180 v.DrawCube(cubeBalcony3,3.9f,0.f,9.f) 181 v.DrawCube(cubeKitchen,1.5f,0.f,0.f) 182 v.DrawCube(cubeBathroom,3.9f,0.f,1.8f) 183 v.DrawCube(cubeGallery,3.9f,0.f,3.9f) 184 v.DrawCube(cubeMasterBedroom,3.9f,0.f,5.1f) 185 v.DrawCube(cubeSecondbedroom,6.0f,0.f,1.8f) 186 GL.PopMatrix() 187 let mutable lookat = Matrix4.LookAt(caram.Eye,caram.Target,Vector3.UnitY) 188 GL.MatrixMode(MatrixMode.Modelview) 189 GL.LoadMatrix(&lookat) 190 glControl.SwapBuffers() 191 ignore 192 member v.DrawCube(cube:Shape.Cube,pos:Vector3) = 193 v.DrawCube(cube,pos,cube.Index) 194 member v.DrawCube(cube:Shape.Cube,x,y,z) = 195 v.DrawCube(cube,new Vector3(x,y,z),cube.Index) 196 member v.DrawCube(cube:Shape.Cube,x,y,z,ind) = 197 v.DrawCube(cube,new Vector3(x,y,z),ind) 198 member v.DrawCube(cube:Shape.Cube, pos:Vector3, ind:int) = 199 GL.PushMatrix() 200 cube.Index <- ind 201 GL.Translate(pos) 202 cube.Draw() 203 GL.PopMatrix() 204 member v.DrawCube(cube:Shape.Cube,x,y,z,ind:int,tid:int) = 205 GL.PushMatrix() 206 GL.Translate(Vector3(x,y,z)) 207 cube.DrawTexTure(ind,tid,Shape.TexTure.GenTexture) 208 GL.PopMatrix() 209 let t = new loopForm() 210 t.Show() 211 GL.Enable(EnableCap.TextureGenS) 212 GL.Enable(EnableCap.TextureGenT) 213 GL.TexGen(TextureCoordName.S,TextureGenParameter.TextureGenMode,int TextureGenMode.ObjectLinear) 214 GL.TexGen(TextureCoordName.T,TextureGenParameter.TextureGenMode,int TextureGenMode.ObjectLinear) 215 GL.TexGen(TextureCoordName.S,TextureGenParameter.ObjectPlane,[|1.f;0.f;0.f;0.f|]) 216 GL.TexGen(TextureCoordName.T,TextureGenParameter.ObjectPlane,[|0.f;1.f;0.f;0.f|]) 217
type T