这几天,在看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米的所有物体,在这当中所有物体会影视到我们眼球表面。透视投影相当于就是计算视线内所有物体到眼球表面上的位置。

 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
open System
open System.Collections.Generic
open System.Windows.Forms
open System.Drawing open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type ImmediateWindow() =
inherit GameWindow(,,new GraphicsMode(new ColorFormat(),))
do base.VSync <- VSyncMode.On
let rotation_speed = 180.0f
let mutable angle = 0.0f
override v.OnLoad e =
base.OnLoad(e)
GL.ClearColor(Color.MidnightBlue)
GL.Enable(EnableCap.DepthTest)
override v.OnResize e =
base.OnResize e
GL.Viewport(,,v.Width,v.Height)
let ratio = float32 v.Width/float32 v.Height
let mutable pective = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,ratio,1.0f,64.0f)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadMatrix(&pective) member v.DrawCube() =
GL.Begin(BeginMode.Quads); GL.Color3(Color.Silver);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f); GL.Color3(Color.Honeydew);
GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f); GL.Color3(Color.Moccasin); GL.Vertex3(-1.0f, -1.0f, -1.0f);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, -1.0f); GL.Color3(Color.IndianRed);
GL.Vertex3(-1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f); GL.Color3(Color.PaleVioletRed);
GL.Vertex3(-1.0f, 1.0f, -1.0f);
GL.Vertex3(-1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f); GL.Color3(Color.ForestGreen);
GL.Vertex3(1.0f, -1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, -1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f);
GL.Vertex3(1.0f, -1.0f, 1.0f);
GL.End();
override v.OnUpdateFrame e =
base.OnUpdateFrame e
if v.Keyboard.[OpenTK.Input.Key.Escape]
then
v.Exit()
override v.OnRenderFrame(e) =
base.OnRenderFrame e
GL.Clear (ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
let mutable eye = new Vector3(float32 0.0,float32 5.0,float32 5.0)
let mutable target = new Vector3(float32 0.0,float32 0.0,float32 0.0)
let mutable up = new Vector3(float32 0.0,float32 1.0,float32 0.0)
let mutable lookat = Matrix4.LookAt(.f, .f, .f, .f, .f, .f, .f, .f, .f);
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
angle <- angle + rotation_speed *(float32 e.Time)
GL.Rotate(angle,0.0f,1.0f,0.0f)
v.DrawCube()
v.SwapBuffers() let example = new ImmediateWindow()
do example.Run(.)

5.OpenGL里的矩阵变换.顺序,当前矩阵,主要变换.

 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
open System
open System.Collections.Generic
open System.Windows.Forms
open System.Drawing open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type ImmediateWindow() =
inherit GameWindow(,,new GraphicsMode(new ColorFormat(),))
let num_lists =
let lists = Array.create num_lists //[|num_lists|]
override v.OnLoad e =
base.OnLoad(e)
GL.ClearColor(Color.MidnightBlue)
GL.Enable(EnableCap.DepthTest)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadIdentity()
let first_list = GL.GenLists(num_lists)
let mutable c = .
for i = to num_lists- do
lists.[i] <- first_list + i
GL.NewList(first_list + i,ListMode.Compile)
GL.Color3(0.3+0.7*c*c,0.3+1.4*c*c,0.7-0.7*c*c)
c <- c+./float num_lists
GL.PushMatrix()
GL.Rotate(c*.,.,.,1.0)
GL.Translate(.,.,.)
GL.Begin(BeginMode.Quads)
GL.Vertex3(-.,-.,.)
GL.Vertex3(.,-.,.)
GL.Vertex3(.,.,.)
GL.Vertex3(-.,.,.)
GL.End()
GL.PopMatrix()
GL.EndList()
override v.OnUnload e =
GL.DeleteLists(lists.[],num_lists)
override v.OnResize e =
GL.Viewport(,,v.Width,v.Height)
let ratio = float32 v.Width/float32 v.Height
let mutable pective = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,ratio,1.0f,64.0f)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadMatrix(&pective)
override v.OnUpdateFrame e =
base.OnUpdateFrame e
if v.Keyboard.[OpenTK.Input.Key.Escape]
then
v.Exit()
override v.OnRenderFrame(e) =
// base.OnRenderFrame e
//GL.Clear (ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
let mutable lookat = Matrix4.LookAt(.f, .f, .f, .f, .f, .f, .f, .f, .f);
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit);
GL.CallLists(num_lists, ListNameType.Int, lists);
v.SwapBuffers() let example = new ImmediateWindow()
do example.Run(.)

6.OpenGL里的缓冲区.

 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll"

 open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type VBO =
struct
val mutable VboID : int
val mutable EboID : int
val mutable NumElements : int
end type VertexPositionColor =
struct
val mutable Position : Vector3
val mutable Color : uint32
new(x,y,z,color:Color) = {Position = new Vector3(x,y,z);Color=VertexPositionColor.ToRgba(color)}
static member ToRgba (color:Color) =
uint32 color.A <<< ||| uint32 color.B <<< ||| uint32 color.G <<< |||uint32 color.R
end type VBOWindow() =
inherit GameWindow(,)
let speed = .
let mutable angle = .
let CubeVertices = [|
new VertexPositionColor(-1.0f, -1.0f, 1.0f, Color.DarkRed);//
new VertexPositionColor( 1.0f, -1.0f, 1.0f, Color.DarkRed);//
new VertexPositionColor( 1.0f, 1.0f, 1.0f, Color.Gold);//
new VertexPositionColor(-1.0f, 1.0f, 1.0f, Color.Gold);//
new VertexPositionColor(-1.0f, -1.0f, -1.0f, Color.DarkRed);//
new VertexPositionColor( 1.0f, -1.0f, -1.0f, Color.DarkRed);//
new VertexPositionColor( 1.0f, 1.0f, -1.0f, Color.Gold);//
new VertexPositionColor(-1.0f, 1.0f, -1.0f, Color.Gold) |]//
let CubeElements = [|
0s; 1s; 2s; 2s; 3s; 0s; // front face
3s; 2s; 6s; 6s; 7s; 3s; // top face
7s; 6s; 5s; 5s; 4s; 7s; // back face
4s; 0s; 3s; 3s; 7s; 4s; // left face
0s; 1s; 5s; 5s; 4s; 0s; // bottom face
1s; 5s; 6s; 6s; 2s; 1s // right face
|]
let mutable vbo = new VBO()
override v.OnLoad e =
base.OnLoad e
let version = GL.GetString(StringName.Version)
let major = int version.[]
let minor =int version.[]
if major <= && minor < then
v.Exit()
GL.ClearColor(System.Drawing.Color.MidnightBlue)
GL.Enable(EnableCap.DepthTest) GL.GenBuffers(,&vbo.VboID)
GL.BindBuffer(BufferTarget.ArrayBuffer,vbo.VboID)
let mutable size = CubeVertices.Length * BlittableValueType.StrideOf(CubeVertices.[]);
GL.BufferData(BufferTarget.ArrayBuffer,new IntPtr(size),CubeVertices,BufferUsageHint.StaticDraw) GL.GenBuffers(,&vbo.EboID)
GL.BindBuffer(BufferTarget.ElementArrayBuffer,vbo.EboID)
let mutable elementSize = CubeElements.Length * sizeof<int16>
GL.BufferData(BufferTarget.ElementArrayBuffer,new IntPtr(elementSize),CubeElements,BufferUsageHint.StaticDraw) vbo.NumElements <- CubeElements.Length
override v.OnResize e =
base.OnResize e
GL.Viewport(,,v.Width,v.Height)
let aspect = float32 v.Width / float32 v.Height
let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,.f,.f)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadMatrix(&projection)
override v.OnRenderFrame e =
base.OnRenderFrame e
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
let mutable lookat = Matrix4.LookAt(new Vector3(.f,.f,.f),Vector3.Zero,Vector3.UnitY)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
// angle <- angle + speed * float e.Time
// GL.Rotate(angle,0.,1.,0.)
//
// GL.EnableClientState ArrayCap.ColorArray
// GL.EnableClientState ArrayCap.VertexArray
// GL.BindBuffer(BufferTarget.ArrayBuffer,vbo.VboID)
// GL.BindBuffer(BufferTarget.ElementArrayBuffer,vbo.EboID)
// let size = BlittableValueType.StrideOf(CubeVertices.[0])
// GL.VertexPointer(3,VertexPointerType.Float,size,new IntPtr(0))
// GL.ColorPointer(4,ColorPointerType.UnsignedByte,size,new IntPtr(12))
// GL.DrawElements(BeginMode.Triangles,vbo.NumElements,DrawElementsType.UnsignedShort,IntPtr.Zero) GL.Begin(BeginMode.Triangles)
GL.Color3(Color.Red)
GL.Vertex3(-1.0f, -1.0f, 1.0f);//
GL.Vertex3( 1.0f, -1.0f, 1.0f);//
GL.Vertex3( 1.0f, 1.0f, 1.0f);//2
GL.Vertex3( 1.0f, 1.0f, 1.0f);//
GL.Vertex3( -1.0f, 1.0f, 1.0f);//
GL.Vertex3(-1.0f, -1.0f, 1.0f);//
GL.Color3(Color.Black)
GL.End()
v.SwapBuffers() let vboWindow = new VBOWindow()
do vboWindow.Run()

7.如何更新OpenGL里的缓冲区.

 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type VertexColor =
struct
val mutable R : byte
val mutable G : byte
val mutable B : byte
val mutable A : byte
val mutable Position : Vector3
static member Size =
end type Location =
struct
val mutable Direction : Vector3
val mutable Age : uint32
end type DynamicWindow() =
inherit GameWindow(,)
static let mutable MaxCount =
let mutable visibleCount =
let VBO = Array.create MaxCount (new VertexColor())
let Locations = Array.create MaxCount (new Location())
let mutable handle = ; override v.OnLoad e =
GL.ClearColor(0.1f,.f,0.1f,.f)
GL.Enable EnableCap.DepthTest GL.PointSize .f
GL.Enable EnableCap.PointSmooth
GL.Hint(HintTarget.PointSmoothHint,HintMode.Nicest)
GL.EnableClientState ArrayCap.ColorArray
GL.EnableClientState ArrayCap.VertexArray GL.GenBuffers(,&handle)
GL.BindBuffer(BufferTarget.ArrayBuffer,handle)
GL.ColorPointer(,ColorPointerType.UnsignedByte,VertexColor.Size,)
GL.VertexPointer(,VertexPointerType.Float,VertexColor.Size,) let rnd = new Random()
let temp = Vector3.Zero
let getRndByte() =
let rnd = rnd.Next(,)
byte rnd
let getRndDir() =
let rnd = (rnd.NextDouble()-0.5)*0.5
float32 rnd
for i = to MaxCount - do
VBO.[i].R <- getRndByte()
VBO.[i].G <- getRndByte()
VBO.[i].B <- getRndByte()
VBO.[i].A <- getRndByte()
VBO.[i].Position <- Vector3.Zero Locations.[i].Direction <- new Vector3(getRndDir(),getRndDir(),getRndDir())
Locations.[i].Age <- uint32
visibleCount <-
override v.OnResize e =
GL.Viewport(,,v.Width,v.Height)
GL.MatrixMode MatrixMode.Projection
let mutable pro = Matrix4.CreatePerspectiveFieldOfView(float32 MathHelper.PiOver4,float32 v.Width/ float32 v.Height,1.0f,.f)
GL.LoadMatrix(&pro)
GL.MatrixMode MatrixMode.Modelview
let mutable view = Matrix4.LookAt(Vector3.UnitZ,Vector3.Zero,Vector3.UnitY)
GL.LoadMatrix(&view)
override v.OnUpdateFrame e =
if visibleCount < MaxCount then
visibleCount <- visibleCount +
let mutable temp = Vector3.Zero
for i = MaxCount - visibleCount to MaxCount - do
if Locations.[i].Age >= uint32 MaxCount then
Locations.[i].Age <- uint32
VBO.[i].Position <- Vector3.Zero
else
let max =Math.Max(Locations.[i].Direction.LengthFast * .f,.f)
Locations.[i].Age <- uint32 max
Vector3.Multiply(&Locations.[i].Direction,float32 e.Time,&temp)
Vector3.Add(&VBO.[i].Position,&temp,&VBO.[i].Position) override v.OnRenderFrame e =
v.Title <- visibleCount.ToString() + " Points."
(ClearBufferMask.ColorBufferBit |||ClearBufferMask.DepthBufferBit) |> GL.Clear
GL.PushMatrix()
GL.Translate(.f,.f,-.f)
GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (VertexColor.Size * MaxCount),IntPtr.Zero,BufferUsageHint.StaticDraw)
GL.BufferData(BufferTarget.ArrayBuffer,IntPtr (VertexColor.Size * MaxCount),VBO,BufferUsageHint.StaticDraw)
GL.DrawArrays(BeginMode.Points,MaxCount - visibleCount,visibleCount)
GL.PopMatrix()
v.SwapBuffers() let w = new DynamicWindow()
do w.Run()

8.OpenGL纹理简单应用.

 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open System.Drawing.Imaging
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type Textures() =
inherit GameWindow(,)
let mutable texture =
let bitmap = new Bitmap(@"F:\3D\1.0\ConsoleApplication1\logo.jpg")
override v.OnLoad e =
GL.ClearColor Color.MidnightBlue
GL.Enable EnableCap.Texture2D
GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest) GL.GenTextures(,&texture)
GL.BindTexture(TextureTarget.Texture2D,texture)
let data = bitmap.LockBits(new System.Drawing.Rectangle(,,bitmap.Width,bitmap.Height),ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb)
GL.TexImage2D(TextureTarget.Texture2D,,PixelInternalFormat.Rgba, data.Width, data.Height, ,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0)
bitmap.UnlockBits(data)
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear)
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear)
override v.OnResize e =
GL.Viewport(,,v.Width,v.Height)
GL.MatrixMode(MatrixMode.Projection)
GL.LoadIdentity()
GL.Ortho(-.,.,-.,.,.,.)
override v.OnRenderFrame e =
GL.Clear ClearBufferMask.ColorBufferBit
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadIdentity()
GL.BindTexture(TextureTarget.Texture2D,texture)
GL.Color3(Color.White)
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0.0f, 1.0f);
GL.Vertex2(-0.6f, -0.4f);
GL.TexCoord2(1.0f, 1.0f);
GL.Vertex2(0.6f, -0.4f);
GL.TexCoord2(1.0f, 0.0f);
GL.Vertex2(0.6f, 0.4f);
GL.TexCoord2(0.0f, 0.0f);
GL.Vertex2(-0.6f, 0.4f);
GL.End();
v.SwapBuffers() let t = new Textures()
do t.Run()

9.启用OpenTK里的GLControl.

 #r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
#r "F:\3D\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open System.Drawing.Imaging
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type loopForm() as form=
inherit Form()
let mutable x = .f
let mutable y = .f
let mutable z = .f
let offest = .f
let glControl = new OpenTK.GLControl()
let textX= new TextBox()
let textY= new TextBox()
let textZ= new TextBox()
let textLR = new TextBox()
let textUD= new TextBox()
let labelX= new Label()
let labelY= new Label()
let labelZ= new Label()
let labelLR = new Label()
let labelUD= new Label()
let aArray = [|
.f;.f;.f
-.f;.f;.f
-.f;-.f;.f
.f;-.f;.f
.f;.f;-.f
.f;.f;.f
.f;-.f;.f
.f;-.f;-.f
.f;.f;-.f
-.f;.f;-.f
-.f;.f;.f
.f;.f;.f
-.f;.f;-.f
.f;.f;-.f
.f;-.f;-.f
-.f;-.f;-.f
|]
let vArray = [|
.f;.f;.f
-.f;.f;.f
-.f;-.f;.f
.f;-.f;.f
.f;.f;-.f
.f;-.f;-.f
-.f;.f;-.f
-.f;-.f;-.f
|]
let iArray = [|
0u;1u;2u;3u;
4u;0u;3u;5u;
4u;6u;1u;0u;
6u;4u;5u;7u;
|] let cArray = [|
.f;.f;.f
.f;.f;.f
.f;.f;.f
.f;.f;.f
.f;.f;.f
.f;.f;.f
.f;.f;.f
.f;.f;.f
|]
do
form.SuspendLayout()
glControl.Location <- new Point(,)
glControl.Size <- new Size(,)
glControl.BackColor <- Color.Red
glControl.Resize.Add(form.resize)
glControl.Paint.Add(form.paint)
form.MouseWheel.Add(form.MouseDown)
glControl.Text <- "xxxxxx"
form.ClientSize <- new Size(,)
form.Text <- "opengl"
form.StartPosition <- FormStartPosition.Manual
form.Location <- new Point(,)
form.Controls.Add(glControl)
form.ResumeLayout(false)
labelX.Location <- new Point(,)
labelY.Location <- new Point(,)
labelZ.Location <- new Point(,)
labelLR.Location <- new Point(,)
labelUD.Location <- new Point(,)
labelX.Text <- "X:"
labelY.Text <- "Y:"
labelZ.Text <- "Z:"
labelLR.Text <- "水平:"
labelUD.Text <-"上下:"
textX.Location <- new Point(,)
textY.Location <- new Point(,)
textZ.Location <- new Point(,)
textLR.Location <- new Point(,)
textUD.Location <- new Point(,)
form.Controls.Add(textX)
form.Controls.Add(textY)
form.Controls.Add(textZ)
form.Controls.Add(textLR)
form.Controls.Add(textUD)
form.Controls.Add(labelX)
form.Controls.Add(labelY)
form.Controls.Add(labelZ)
form.Controls.Add(labelLR)
form.Controls.Add(labelUD)
override v.OnLoad e =
base.OnLoad e
GL.ClearColor Color.MidnightBlue
Application.Idle.Add(v.AIdle)
v.ShowUI
textX.TextChanged.Add(form.TextChange)
textY.TextChanged.Add(form.TextChange)
textZ.TextChanged.Add(form.TextChange)
textLR.TextChanged.Add(form.TextChange)
textUD.TextChanged.Add(form.TextChange)
//踢除正反面
//GL.Enable EnableCap.CullFace
//GL.CullFace CullFaceMode.Back
//指定正反面
GL.FrontFace FrontFaceDirection.Ccw
//设置材料面填充模式
GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill)
GL.PolygonMode(MaterialFace.Back,PolygonMode.Line)
//启用数组功能.
GL.EnableClientState(ArrayCap.VertexArray)
GL.EnableClientState(ArrayCap.ColorArray)
member v.resize (e:EventArgs) =
GL.Viewport(,,glControl.ClientSize.Width,glControl.ClientSize.Height)
let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height
let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,0.1f,.f)
GL.MatrixMode MatrixMode.Projection
GL.LoadMatrix(&projection)
member v.paint (e:PaintEventArgs) =
v.Render()
member v.AIdle (e:EventArgs) =
while (glControl.IsIdle) do
v.Render()
member v.TextChange (e:EventArgs) =
x <- v.UIValue(textX)
y <- v.UIValue(textY)
z <- v.UIValue(textZ)
member v.MouseDown(e:MouseEventArgs) =
match v.ActiveControl with
| :? TextBox as t1 ->
let mutable t = v.UIValue(t1)
t <- t + float32 e.Delta * offest * 0.01f
t1.Text <- t.ToString()
| _ ->
v.Text <- v.ActiveControl.Text
let state =float32 e.Delta * offest * 0.01f
z <- z + state
v.ShowUI
member x.UIValue
with get (text:TextBox) =
let mutable value = .f
if System.Single.TryParse(text.Text,&value) then
value <- value
value
and set (text:TextBox) (value:float32) = text.Text<- value.ToString()
member v.ShowUI =
textX.Text <- x.ToString()
textY.Text <- y.ToString()
textZ.Text <- z.ToString()
textLR.Text <- v.UIValue(textLR).ToString()
textUD.Text <- v.UIValue(textUD).ToString()
//lControl.Focus() |> ignore
member v.Render =
// v.ShowUI
let mutable lookat = Matrix4.LookAt(new Vector3(x,y,z),Vector3.Zero,Vector3.UnitY)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
GL.Translate(.f,.f,.f)
GL.Rotate(v.UIValue(textLR),.f,.f,.f)
GL.Rotate(v.UIValue(textUD),.f,.f,.f)
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
GL.Color3(.f,.f,.f)
GL.PointSize(.f)
GL.Begin BeginMode.Points
GL.Vertex3(x-.f,y-.f,.f)
GL.Vertex3(.f,.f,.f)
GL.End()
GL.Begin BeginMode.Lines
GL.Vertex3(x,y,-.f)
GL.Vertex3(x,y,.f)
GL.End()
GL.Color3(Color.Black)
//GL.DrawArrays(BeginMode.Quads,0,cArray.Length)
// GL.Begin BeginMode.Quads
// GL.ArrayElement(0)
// GL.ArrayElement(1)
// GL.ArrayElement(2)
// GL.ArrayElement(3)
// GL.End()
GL.VertexPointer(,VertexPointerType.Float,,aArray)
GL.ColorPointer(,ColorPointerType.Float,,cArray)
GL.IndexPointer(IndexPointerType.Int,,iArray)
//GL.DrawElements(BeginMode.Quads,4, DrawElementsType.UnsignedInt, [|0u;1u;2u;3u|])
//GL.DrawElements(BeginMode.Quads,4, DrawElementsType.UnsignedInt, [|4u;5u;6u;7u|])
GL.DrawElements(BeginMode.Quads,iArray.Length,DrawElementsType.UnsignedInt,iArray)
GL.Begin BeginMode.Lines
GL.Color3(Color.Black)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitX * .f)
GL.Color3(Color.White)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitY * .f)
GL.Color3(Color.Red)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitZ * .f)
GL.End()
glControl.SwapBuffers()
ignore
let t = new loopForm()
t.Show() // GL.Begin BeginMode.Quads
// GL.Color3(Color.Black)
// GL.Vertex3(1.f,1.f,1.f)
// GL.Vertex3(-1.f,1.f,1.f)
// GL.Vertex3(-1.f,-1.f,1.f)
// GL.Vertex3(1.f,-1.f,1.f)
//
// GL.Color3(Color.White)
// GL.Vertex3(1.f,1.f,-1.f)
// GL.Vertex3(1.f,1.f,1.f)
// GL.Vertex3(1.f,-1.f,1.f)
// GL.Vertex3(1.f,-1.f,-1.f)
//
// GL.Color3(Color.Red)
// GL.Vertex3(1.f,1.f,-1.f)
// GL.Vertex3(-1.f,1.f,-1.f)
// GL.Vertex3(-1.f,1.f,1.f)
// GL.Vertex3(1.f,1.f,1.f)
//
// GL.Color3(0.f,1.f,0.f)
// GL.Vertex3(-1.f,1.f,-1.f)
// GL.Vertex3(1.f,1.f,-1.f)
// GL.Vertex3(1.f,-1.f,-1.f)
// GL.Vertex3(-1.f,-1.f,-1.f)
// GL.End()

9.OpenTK里的光照.

 #r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
#r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.Compatibility.dll"
#r "D:\Program Files\openTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open System.Drawing.Imaging
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL type loopForm() as form=
inherit Form()
let mutable x = .f
let mutable y = .f
let mutable z = .f
let offest = .f
let glControl = new OpenTK.GLControl()
let textX= new TextBox()
let textY= new TextBox()
let textZ= new TextBox()
let textLR = new TextBox()
let textUD= new TextBox()
let textInfo = new TextBox()
let labelX= new Label()
let labelY= new Label()
let labelZ= new Label()
let labelLR = new Label()
let labelUD= new Label()
let mutable first =
let mutable buffer =
let list =
let scale = .f
let mutable r = .f
let q = Glu.NewQuadric()
let mutable ll = [|.f;.f;.f;.f|]
do
form.SuspendLayout()
glControl.Location <- new Point(,)
glControl.Size <- new Size(,)
glControl.BackColor <- Color.Red
glControl.Resize.Add(form.resize)
glControl.Paint.Add(form.paint)
form.MouseWheel.Add(form.MouseDown)
form.ClientSize <- new Size(,)
form.Text <- "opengl"
form.StartPosition <- FormStartPosition.Manual
form.Location <- new Point(,)
form.Controls.Add(glControl)
form.ResumeLayout(false)
labelX.Location <- new Point(,)
labelY.Location <- new Point(,)
labelZ.Location <- new Point(,)
labelLR.Location <- new Point(,)
labelUD.Location <- new Point(,)
labelX.Text <- "X:"
labelY.Text <- "Y:"
labelZ.Text <- "Z:"
labelLR.Text <- "水平:"
labelUD.Text <-"上下:"
textX.Location <- new Point(,)
textY.Location <- new Point(,)
textZ.Location <- new Point(,)
textLR.Location <- new Point(,)
textUD.Location <- new Point(,)
textInfo.Text <- ""
textInfo.Location <- new Point(,)
textInfo.Width <-
form.Controls.Add(textX)
form.Controls.Add(textY)
form.Controls.Add(textZ)
form.Controls.Add(textLR)
form.Controls.Add(textUD)
form.Controls.Add(labelX)
form.Controls.Add(labelY)
form.Controls.Add(labelZ)
form.Controls.Add(labelLR)
form.Controls.Add(labelUD)
form.Controls.Add(textInfo)
//#endregion
override v.OnLoad e =
base.OnLoad e
GL.ClearColor Color.MidnightBlue
Application.Idle.Add(v.AIdle)
v.ShowUI
textX.TextChanged.Add(form.TextChange)
textY.TextChanged.Add(form.TextChange)
textZ.TextChanged.Add(form.TextChange)
textLR.TextChanged.Add(form.TextChange)
textUD.TextChanged.Add(form.TextChange)
GL.FrontFace FrontFaceDirection.Ccw
//设置材料面填充模式
GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill)
GL.PolygonMode(MaterialFace.Back,PolygonMode.Line)
//启用数组功能.
GL.EnableClientState(ArrayCap.VertexArray)
//GL.EnableClientState(ArrayCap.ColorArray)
GL.EnableClientState(ArrayCap.IndexArray)
GL.Light(LightName.Light0,LightParameter.Ambient,[|.f;.f;.f;.f|])
GL.Light(LightName.Light0,LightParameter.Diffuse,[|.f;.f;.f;.f|])
GL.Light(LightName.Light0,LightParameter.Specular,[|.f;.f;.f;.f|])
GL.Light(LightName.Light0,LightParameter.SpotDirection,[|-.f;-.f;.f|])
GL.Light(LightName.Light0,LightParameter.SpotCutoff,[||]) GL.Light(LightName.Light1,LightParameter.Position,[|.f;.f;.f;.f|])
GL.Light(LightName.Light1,LightParameter.Ambient,[|0.5f;0.5f;0.5f;.f|])
GL.Light(LightName.Light1,LightParameter.Diffuse,[|0.5f;0.5f;0.5f;.f|])
GL.Light(LightName.Light1,LightParameter.Specular,[|.f;.f;.f;.f|])
//GL.Light(LightName.Light0,LightParameter.Specular,[|1.f;0.f;1.f;1.f|])
//GL.Material(MaterialFace.Front,MaterialParameter.Ambient,[|1.f;1.f;1.f;1.f|])
GL.Enable(EnableCap.Lighting)
GL.Enable(EnableCap.Light0)
GL.Enable(EnableCap.Light1)
GL.Enable(EnableCap.DepthTest)
//#region ""
member v.resize (e:EventArgs) =
GL.Viewport(,,glControl.ClientSize.Width,glControl.ClientSize.Height)
let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height
let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4,aspect,0.1f,.f)
GL.MatrixMode MatrixMode.Projection
GL.LoadMatrix(&projection)
member v.paint (e:PaintEventArgs) =
v.Render()
member v.AIdle (e:EventArgs) =
while (glControl.IsIdle) do
v.Render()
member v.TextChange (e:EventArgs) =
x <- v.UIValue(textX)
y <- v.UIValue(textY)
z <- v.UIValue(textZ)
member v.MouseDown(e:MouseEventArgs) =
match v.ActiveControl with
| :? TextBox as t1 ->
let mutable t = v.UIValue(t1)
t <- t + float32 e.Delta * offest * 0.01f
t1.Text <- t.ToString()
| _ ->
v.Text <- v.ActiveControl.Text
let state =float32 e.Delta * offest * 0.01f
x<- x+state
y<- y + state
z <- z + state
v.ShowUI
member x.UIValue
with get (text:TextBox) =
let mutable value = .f
if System.Single.TryParse(text.Text,&value) then
value <- value
value
and set (text:TextBox) (value:float32) = text.Text<- value.ToString()
member v.ShowUI =
textX.Text <- x.ToString()
textY.Text <- y.ToString()
textZ.Text <- z.ToString()
textLR.Text <- v.UIValue(textLR).ToString()
textUD.Text <- v.UIValue(textUD).ToString()
member v.Render =
let mutable lookat = Matrix4.LookAt(new Vector3(x,y,z),Vector3.Zero,Vector3.UnitY)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
GL.Rotate(v.UIValue(textLR),.f,.f,.f)
GL.Rotate(v.UIValue(textUD),.f,.f,.f)
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
GL.Begin BeginMode.Lines
GL.Color3(Color.Black)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitX * .f)
GL.Color3(Color.White)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitY * .f)
GL.Color3(Color.Red)
GL.Vertex3(Vector3.Zero)
GL.Vertex3(Vector3.UnitZ * .f)
GL.End()
r <- (r + 0.01f) % .f
textInfo.Text <- r.ToString()
GL.PushMatrix()
GL.Rotate(r,.f,.f,.f)
GL.Light(LightName.Light0,LightParameter.Position,ll)
GL.PushMatrix()
GL.Translate(ll.[],ll.[],ll.[])
Glu.Sphere(q,1.0,,)
GL.PopMatrix()
GL.PopMatrix()
Glu.Sphere(q,3.0,,)
glControl.SwapBuffers()
ignore
let t = new loopForm()
t.Show()

10.相关画圆,画立方体类.

 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
#r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.Compatibility.dll"
#r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll" open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open System.Drawing.Imaging
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL module Shape = type T2N3V3 =
struct
val mutable TexCoord : Vector2
val mutable Normal : Vector3
val mutable Position : Vector3
new(v,n,p) = {TexCoord = v;Normal = n;Position = p}
end
[<AbstractClass>]
type Shape() =
let mutable bCreate = false
let mutable vi =
let mutable ei =
let mutable count =
member this.vboID with get() = vi and set value = vi <- value
member this.eboID with get() = ei and set value = ei <- value
member this.TriangelCount with get() = count and set value = count <- value
member this.IsCreate with get() = bCreate and set value = bCreate <- value
abstract Draw : unit -> unit
abstract Init : unit -> unit
member this.InitQ : unit -> unit =fun () -> () type Sphere(radius:float32,level:int) =
inherit Shape()
let mutable rad,lev = radius,level
let RightLevel =
if lev < then lev <-
elif lev > then lev <-
override this.Draw() =
if this.IsCreate<>true then this.Init()
GL.BindBuffer(BufferTarget.ArrayBuffer,this.vboID)
GL.BindBuffer(BufferTarget.ElementArrayBuffer,this.eboID)
GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f,,IntPtr.Zero)
GL.DrawElements(BeginMode.Triangles,this.TriangelCount,DrawElementsType.UnsignedInt,IntPtr.Zero)
override this.Init() =
let alls = Array.create (new T2N3V3())
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitX, Vector3.UnitX * rad )
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitY, Vector3.UnitY * rad )
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), Vector3.UnitZ, Vector3.UnitZ * rad )
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitX, -Vector3.UnitX * rad )
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitY, -Vector3.UnitY * rad )
alls.[] <- new T2N3V3(new Vector2( 0.0f, 0.0f ), -Vector3.UnitZ, -Vector3.UnitZ * rad )
let is = [|
;;
;;
;;
;;
;;
;;
;;
;;
|]
let (vvv:T2N3V3 []),(iv: int[]) = this.Sub (alls,is)
let mutable vID,eID = ,
//let mutable tv,vv,pv = vvv |> Array.map (fun v -> v.TexCoord,v.Normal,v.Position) |> Array.unzip3
GL.GenBuffers(,&vID)
GL.BindBuffer(BufferTarget.ArrayBuffer,vID)
GL.BufferData(BufferTarget.ArrayBuffer,IntPtr ( * * vvv.Length),vvv,BufferUsageHint.StaticDraw) GL.GenBuffers(,&eID)
GL.BindBuffer(BufferTarget.ElementArrayBuffer,eID)
GL.BufferData(BufferTarget.ElementArrayBuffer,IntPtr ( * iv.Length),iv,BufferUsageHint.StaticDraw) this.vboID <- vID
this.eboID <- eID
this.TriangelCount <- iv.Length
this.IsCreate <- true
()
member v.GetMidValue (first:T2N3V3,second:T2N3V3) =
let midN = Vector3.Lerp(first.Position,second.Position,0.5f) |> Vector3.Normalize
let midP = midN *(float32 rad)
let midT = Vector2.Lerp(first.TexCoord,second.TexCoord,0.5f) |> Vector2.Normalize
let result = new T2N3V3(midT,midN,midP)
result
member v.Subdivide (v1:T2N3V3,v2:T2N3V3,v3:T2N3V3) =
let vs = Array.create (new T2N3V3())
vs.[] <- v1
vs.[] <- v.GetMidValue(v1,v2)
vs.[] <- v.GetMidValue(v3,v1)
vs.[] <- v2
vs.[] <- v.GetMidValue(v2,v3)
vs.[] <- v3
let is = Array.create
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
is.[] <-
(vs,is)
member this.Sub(alls:T2N3V3 [],is:int []) =
//let mutable tv,vv,pv = alls |> Array.map (fun v -> v.TexCoord,v.Normal,v.Position) |> Array.unzip3
let mutable allv = alls
let mutable iv = is
let show array = printfn "%A" array
for j in .. lev do
let mutable av = Array.create (new T2N3V3())
let mutable ev = Array.create
printfn "%i" allv.Length
printfn "%i" iv.Length
for i in .. .. iv.Length - do
let (vvv,iiv) = this.Subdivide(allv.[iv.[i]],allv.[iv.[i+]],allv.[iv.[i+]])
let length = av.Length
av <- Array.append av vvv
let map = iiv |> Array.map (fun p -> p + length)
ev <- Array.append ev map
allv <- av
iv <- ev
allv |> Array.map (fun p -> p.Position) |> show
show iv
allv,iv type Cube(width:float32,height:float32,length:float32,index:int) =
inherit Shape()
let mutable id = index
let xl,yl,zl =width/.f,height/.f,length/.f
let mutable color = Color.White
let v8 = [|
new Vector3(xl,yl,zl)
new Vector3(-xl,yl,zl)
new Vector3(-xl,-yl,zl)
new Vector3(xl,-yl,zl)
new Vector3(xl,yl,-zl)
new Vector3(-xl,yl,-zl)
new Vector3(-xl,-yl,-zl)
new Vector3(xl,-yl,-zl)
|]
new(x,y,z) =
let rnd = System.Random().Next()
printfn "%i" rnd
Cube(x,y,z,-)
override this.Draw() =
if this.IsCreate<>true then this.Init()
GL.EnableClientState(ArrayCap.VertexArray)
GL.EnableClientState(ArrayCap.NormalArray)
GL.BindBuffer(BufferTarget.ArrayBuffer,this.vboID)
GL.VertexPointer(,VertexPointerType.Float,,IntPtr.Zero)
GL.PushMatrix()
if id >= && id < then
GL.Translate(v8.[id])
GL.Color3(this.Color:Color)
GL.Normal3(Vector3.UnitZ)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
//GL.Color3(Color.Black)
GL.Normal3(Vector3.UnitY)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
//GL.Color3(Color.Red)
GL.Normal3(Vector3.UnitX)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
//GL.Color3(Color.Green)
GL.Normal3(-Vector3.UnitY)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
//GL.Color3(Color.Blue)
GL.Normal3(-Vector3.UnitX)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
//GL.Color3(Color.DodgerBlue)
GL.Normal3(-Vector3.UnitZ)
GL.DrawElements(BeginMode.Triangles,,DrawElementsType.UnsignedInt,[|;;;;;|])
GL.PopMatrix()
override this.Init() =
let mutable vID =
GL.GenBuffers(,&vID)
GL.BindBuffer(BufferTarget.ArrayBuffer,vID)
GL.BufferData(BufferTarget.ArrayBuffer,IntPtr ( * * v8.Length),v8,BufferUsageHint.StaticDraw)
this.vboID <- vID
this.IsCreate <- true
let rnd = System.Random(this.GetHashCode())
this.Color <- Color.FromArgb(rnd.Next(,),rnd.Next(,),rnd.Next(,))
()
member this.Index with get() = id and set value = id <-value
member this.Color with get() = color and set value = color <- value type Camera() =
let mutable eye = Vector3.Zero
let mutable eyeLength = .
let mutable yangle = .
let mutable xangle= Math.PI/.
member this.Eye
with get() = eye
and set value = eye <- value
member this.EyeLength
with get() = eyeLength
and set value =
if value < . then eyeLength <- 0.1
eyeLength <- value
member this.YAngle
with get() = yangle
and set value =
if value >= Math.PI then yangle <- .
//elif value <= 0. then yangle <- Math.PI - 0.1
else yangle <- value
member this.XAngle
with get() = xangle
and set value =
if value >= .* Math.PI then xangle <- .
//elif value <= 0. then yangle <- 2. * Math.PI - 0.1
else xangle <- value
member this.Target
with get() =
//printfn "%f" this.XAngle
let x:float =float eye.X + eyeLength * Math.Cos(this.YAngle)* Math.Cos(this.XAngle)
let y:float =float eye.Y + eyeLength * Math.Sin(this.YAngle)
let z:float =float eye.Z + eyeLength * Math.Cos(this.YAngle)* Math.Sin(this.XAngle)
Vector3(float32 x,float32 y,float32 z)
member this.Transelt (x,y,z) =
let sinX = Math.Sin(this.XAngle)
let cosX = Math.Cos(this.XAngle)
let x1 = float this.Eye.X + x * sinX + z * cosX
let y1 = float this.Eye.Y + y
let z1 = float this.Eye.Z + z * sinX + x * cosX
printfn "angle:%f, sinx:%f, cosx:%f" x y z
printfn "x:%f, y:%f, z:%f" this.Eye.X this.Eye.Y this.Eye.Z
this.Eye <- new Vector3(float32 x1,float32 y1,float32 z1)
member this.UpAndDown y =
let ya = this.YAngle + y
this.YAngle <- ya
member this.RightAndLeft x =
let xa = this.XAngle + x
this.XAngle <- xa
member this.Rotate (x,y) =
let xa = this.XAngle + x
let ya = this.YAngle + y
this.YAngle <- ya
this.XAngle <- xa

11...

 #r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.dll"
#r "D:\工程\OpenTK\1.0\Binaries\OpenTK\Debug\OpenTK.GLControl.dll"
#load "Shape.fsx" open System
open System.Collections.Generic
open System.Windows.Forms
open System.Threading
open System.Drawing
open System.Drawing.Imaging
open OpenTK
open OpenTK.Graphics
open OpenTK.Graphics.OpenGL
open Shape type loopForm() as form=
inherit Form()
let caram = Shape.Camera()
let offest = 0.1f
let offestd = float offest
let glControl = new OpenTK.GLControl() let cubeEnter = Shape.Cube(1.5f,0.2f,1.8f,)
let cubeParlor = Shape.Cube(4.9f,0.2f,6.3f,)
let cubeBalcony1 = Shape.Cube(4.9f,0.2f,1.9f,)
let cubeBalcony2 = Shape.Cube(1.6f,0.2f,2.1f,)
let cubeBalcony3 = Shape.Cube(1.8f,0.2f,3.0f,)
let cubeKitchen = Shape.Cube(4.5f,0.2f,1.8f,)
let cubeBathroom = Shape.Cube(2.1f,0.2f,2.1f,)
let cubeGallery = Shape.Cube(2.1f,0.2f,1.2f,)
let cubeMasterBedroom = Shape.Cube(3.6f,0.2f,3.9f,)
let cubeSecondbedroom = Shape.Cube(3.0f,0.2f,3.3f,) let cubeWall1 = Shape.Cube(0.2f,.f,8.1f,)
let cubeWall2 = Shape.Cube(4.5f,.f,0.2f,)
let cubeWall3 = Shape.Cube(0.2f,.f,1.8f,)
let cubeWall4 = Shape.Cube(0.2f,1.8f,1.8f,)
let cubeWall5 = Shape.Cube(3.0f,.f,0.2f,)
let cubeWall6 = Shape.Cube(0.2f,.f,3.7f,)
let cubeBathroom1 = Shape.Cube(2.1f,.f,0.2f,)
let cubeBathroom2 = Shape.Cube(0.2f,.f,2.1f,)
let cubeFence1 = Shape.Cube(0.05f,0.02f,1.9f,)
let cubeFence2 = Shape.Cube(2.3f,0.02f,0.05f,)
let cubeFence3 = Shape.Cube(0.05f,0.02f,2.1f,)
let cubeFence4 = Shape.Cube(3.4f,0.02f,0.05f,)
let cubeWall7 = Shape.Cube(2.1f,.f,0.2f,)
let cubeWall8 = Shape.Cube(0.2f,.f,3.0f,)
let cubePillar = Shape.Cube(0.2f,.f,0.2f,)
let cubePillar2 = Shape.Cube(0.1f,1.1f,0.2f,)
let mutable oldMouseLocation = Vector2.Zero
let path = [|
"D:\工程\OpenTK\1.0\ConsoleApplication1\白墙.jpg"
"D:\工程\OpenTK\1.0\ConsoleApplication1\电视墙.jpg"
"D:\工程\OpenTK\1.0\ConsoleApplication1\客厅.jpg"
|]
let mutable texs = Array.create path.Length
do
caram.Transelt(.,1.7,0.0)
form.SuspendLayout()
glControl.Location <- new Point(,)
glControl.Size <- new Size(,)
glControl.BackColor <- Color.Red
glControl.Resize.Add(form.resize)
glControl.Paint.Add(form.paint)
glControl.KeyDown.Add(form.KeyDown)
glControl.MouseMove.Add(form.MouseDownv)
form.ClientSize <- new Size(,)
form.Text <- "opengl"
//form.StartPosition <- FormStartPosition.Manual
form.Location <- new Point(,)
form.Controls.Add(glControl)
form.ResumeLayout(false)
//#endregion
override v.OnLoad e =
base.OnLoad e
GL.ClearColor Color.MidnightBlue
Application.Idle.Add(v.AIdle)
GL.FrontFace FrontFaceDirection.Ccw
GL.Enable( EnableCap.PointSmooth )
GL.Enable EnableCap.Texture2D
//GL.Enable EnableCap.Lighting
//踢除正反面
GL.Enable EnableCap.CullFace
GL.CullFace CullFaceMode.Back
//设置材料面填充模式
GL.PolygonMode(MaterialFace.Front,PolygonMode.Fill)
GL.PolygonMode(MaterialFace.Back,PolygonMode.Line)
texs <- Shape.TexTure.Load(path)
GL.Light(LightName.Light0,LightParameter.Position,[|.f;.f;.f;.f|])
GL.Light(LightName.Light0,LightParameter.Ambient,[|0.5f;0.5f;0.5f;.f|])
GL.Light(LightName.Light0,LightParameter.Diffuse,[|0.5f;0.5f;0.5f;.f|])
GL.Light(LightName.Light0,LightParameter.Specular,[|.f;.f;.f;.f|])
GL.Enable(EnableCap.Lighting)
//GL.Enable(EnableCap.Light0)
GL.Enable(EnableCap.DepthTest) //#region
member v.resize (e:EventArgs) =
GL.Viewport(,,glControl.ClientSize.Width,glControl.ClientSize.Height)
let aspect = float32 glControl.ClientSize.Width /float32 glControl.ClientSize.Height
let mutable projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2,aspect,0.1f,.f)
GL.MatrixMode MatrixMode.Projection
GL.LoadMatrix(&projection)
member v.paint (e:PaintEventArgs) =
v.Render()
member v.AIdle (e:EventArgs) =
while (glControl.IsIdle) do
v.Render()
member v.MouseDownv(e:MouseEventArgs) =
if e.Button = MouseButtons.Right then
if oldMouseLocation = Vector2.Zero then
oldMouseLocation <- Vector2(float32 e.X,float32 e.Y)
else
let nx = (float32 e.X - oldMouseLocation.X) * offest * 0.1f
let ny = (float32 e.Y - oldMouseLocation.Y) * offest * -0.1f
caram.Rotate(float nx,float ny)
oldMouseLocation <- Vector2(float32 e.X,float32 e.Y)
member v.KeyDown(e:KeyEventArgs) =
//let keys = e.KeyData
match e.KeyCode with
| Keys.E ->caram.Transelt(.,.,.* offestd)
| Keys.D ->caram.Transelt(.,.,-.* offestd)
| Keys.S ->caram.Transelt(.* offestd,.,0.0)
| Keys.F ->caram.Transelt(-.* offestd,.,.)
//| Keys.Space -> caram.Transelt()
| _ -> ()
member x.UIValue
with get (text:TextBox) =
let mutable value = .f
if System.Single.TryParse(text.Text,&value) then
value <- value
value
and set (text:TextBox) (value:float32) = text.Text<- value.ToString()
//#endregion
member v.Render =
//Keyboard[OpenTK.Input.Key.F11]
GL.Clear(ClearBufferMask.ColorBufferBit ||| ClearBufferMask.DepthBufferBit)
//地面
//v.DrawCube(cubeEnter,Vector3.Zero)
v.DrawCube(cubeEnter,.f,.f,.f,,texs.[])
v.DrawCube(cubeParlor,.f,.f,1.8f)
v.DrawCube(cubeBalcony1,.f,.f,8.1f)
v.DrawCube(cubeBalcony2,2.3f,.f,.f)
v.DrawCube(cubeBalcony3,3.9f,.f,.f)
v.DrawCube(cubeKitchen,1.5f,.f,.f)
v.DrawCube(cubeBathroom,3.9f,.f,1.8f)
v.DrawCube(cubeGallery,3.9f,.f,3.9f)
v.DrawCube(cubeMasterBedroom,3.9f,.f,5.1f)
v.DrawCube(cubeSecondbedroom,6.0f,.f,1.8f)
//墙
v.DrawCube(cubeWall1,Vector3.Zero)
//v.DrawCube(cubeWall2,Vector3(1.5f,0.f,0.f))
v.DrawCube(cubeWall2,1.5f,.f,.f,,texs.[])
v.DrawCube(cubeWall3,Vector3(1.5f,.f,.f))
v.DrawCube(cubeWall4,Vector3(6.0f,.f,.f))
v.DrawCube(cubeWall5,Vector3(6.0f,.f,1.8f))
v.DrawCube(cubeWall6,Vector3(9.0f,.f,1.8f))
v.DrawCube(cubeWall6,Vector3(7.5f,.f,5.1f))
v.DrawCube(cubeWall6,Vector3(3.9f,.f,5.1f))
v.DrawCube(cubeWall7,Vector3(3.9f,.f,9.0f))
v.DrawCube(cubeWall8,Vector3(6.0f,.f,9.0f))
v.DrawCube(cubeBathroom1,Vector3(3.9f,.f,1.8f))
//v.DrawCube(cubeBathroom1,Vector3(3.9f,0.f,3.9f))
v.DrawCube(cubeBathroom2,Vector3(3.9f,.f,1.8f))
v.DrawCube(cubeBathroom2,Vector3(6.0f,.f,1.8f))
v.DrawCube(cubeFence1,0.0f,.f,8.1f)
v.DrawCube(cubeFence2,0.0f,.f,.f)
v.DrawCube(cubeFence3,2.3f,.f,.f)
v.DrawCube(cubeFence4,2.3f,.f,12.1f)
v.DrawCube(cubePillar,Vector3(2.3f,.f,.f))
v.DrawCube(cubePillar,Vector3(2.3f,.f,9.9f))
v.DrawCube(cubePillar2,Vector3(.f,.f,9.9f))
//房顶
GL.PushMatrix()
GL.Translate(.f,.f,.f)
v.DrawCube(cubeEnter,Vector3.Zero)
v.DrawCube(cubeParlor,.f,.f,1.8f)
v.DrawCube(cubeBalcony1,.f,.f,8.1f)
v.DrawCube(cubeBalcony2,2.3f,.f,.f)
v.DrawCube(cubeBalcony3,3.9f,.f,.f)
v.DrawCube(cubeKitchen,1.5f,.f,.f)
v.DrawCube(cubeBathroom,3.9f,.f,1.8f)
v.DrawCube(cubeGallery,3.9f,.f,3.9f)
v.DrawCube(cubeMasterBedroom,3.9f,.f,5.1f)
v.DrawCube(cubeSecondbedroom,6.0f,.f,1.8f)
GL.PopMatrix()
let mutable lookat = Matrix4.LookAt(caram.Eye,caram.Target,Vector3.UnitY)
GL.MatrixMode(MatrixMode.Modelview)
GL.LoadMatrix(&lookat)
glControl.SwapBuffers()
ignore
member v.DrawCube(cube:Shape.Cube,pos:Vector3) =
v.DrawCube(cube,pos,cube.Index)
member v.DrawCube(cube:Shape.Cube,x,y,z) =
v.DrawCube(cube,new Vector3(x,y,z),cube.Index)
member v.DrawCube(cube:Shape.Cube,x,y,z,ind) =
v.DrawCube(cube,new Vector3(x,y,z),ind)
member v.DrawCube(cube:Shape.Cube, pos:Vector3, ind:int) =
GL.PushMatrix()
cube.Index <- ind
GL.Translate(pos)
cube.Draw()
GL.PopMatrix()
member v.DrawCube(cube:Shape.Cube,x,y,z,ind:int,tid:int) =
GL.PushMatrix()
GL.Translate(Vector3(x,y,z))
cube.DrawTexTure(ind,tid,Shape.TexTure.GenTexture)
GL.PopMatrix()
let t = new loopForm()
t.Show()
GL.Enable(EnableCap.TextureGenS)
GL.Enable(EnableCap.TextureGenT)
GL.TexGen(TextureCoordName.S,TextureGenParameter.TextureGenMode,int TextureGenMode.ObjectLinear)
GL.TexGen(TextureCoordName.T,TextureGenParameter.TextureGenMode,int TextureGenMode.ObjectLinear)
GL.TexGen(TextureCoordName.S,TextureGenParameter.ObjectPlane,[|.f;.f;.f;.f|])
GL.TexGen(TextureCoordName.T,TextureGenParameter.ObjectPlane,[|.f;.f;.f;.f|])
type T
05-15 01:13