static void AddThicknessLine(Camera camera, Vec3 start, Vec3 end, float thickness) { Vec3 diff = end - start; Vec3 direction = diff.GetNormalize(); Quat rotation = Quat.FromDirectionZAxisUp(direction); float length = diff.Length(); float thickness2 = thickness; Mat4 t = new Mat4(rotation.ToMat3() * Mat3.FromScale(new Vec3(length, thickness2, thickness2)), (start + end) * .5f); if (addThicknessLinePositions == null) { GeometryGenerator.GenerateBox(new Vec3(1, 1, 1), out addThicknessLinePositions, out addThicknessLineIndices); } camera.DebugGeometry.AddVertexIndexBuffer(addThicknessLinePositions, addThicknessLineIndices, t, false, true); }
private void CreateMesh() { Vec3 size = new Vec3(.97f, .97f, .1f); Vec3[] positions; Vec3[] normals; int[] indices; GeometryGenerator.GenerateBox(size, out positions, out normals, out indices); string meshName = MeshManager.Instance.GetUniqueName( string.Format("JigsawPuzzlePiece[{0},{1}]", index.X, index.Y)); mesh = MeshManager.Instance.CreateManual(meshName); //create submesh SubMesh subMesh = mesh.CreateSubMesh(); subMesh.UseSharedVertices = false; //init VertexData VertexDeclaration declaration = subMesh.VertexData.VertexDeclaration; declaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); declaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal); declaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TextureCoordinates, 0); VertexBufferBinding bufferBinding = subMesh.VertexData.VertexBufferBinding; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( 32, positions.Length, HardwareBuffer.Usage.StaticWriteOnly); bufferBinding.SetBinding(0, vertexBuffer, true); subMesh.VertexData.VertexCount = positions.Length; unsafe { Vertex *buffer = (Vertex *)vertexBuffer.Lock(HardwareBuffer.LockOptions.Normal); for (int n = 0; n < positions.Length; n++) { Vertex vertex = new Vertex(); vertex.position = positions[n]; vertex.normal = normals[n]; if (JigsawPuzzleManager.Instance != null) { Vec2I pieceCount = JigsawPuzzleManager.Instance.PieceCount; Vec2I i = index; if (vertex.position.X > 0) { i.X++; } if (vertex.position.Y > 0) { i.Y++; } vertex.texCoord = new Vec2( (float)i.X / (float)pieceCount.X, 1.0f - (float)i.Y / (float)pieceCount.Y); } *buffer = vertex; buffer++; } vertexBuffer.Unlock(); } //calculate mesh bounds Bounds bounds = Bounds.Cleared; float radius = 0; foreach (Vec3 position in positions) { bounds.Add(position); float r = position.Length(); if (r > radius) { radius = r; } } mesh.SetBoundsAndRadius(bounds, radius); //init IndexData subMesh.IndexData = IndexData.CreateFromArray(indices, 0, indices.Length, false); //init material subMesh.MaterialName = "JigsawPuzzleImage"; }
void workAreaControl_Render(MultiViewRenderTargetControl sender, int viewIndex, Camera camera) { //update camera if (Map.Instance != null) { Vec3 position; Vec3 forward; Vec3 up; Degree fov; if (!freeCameraEnabled) { //usual camera mode //add here your special camera management code and set "freeCameraEnabled = false;" position = Vec3.Zero; forward = new Vec3(1, 0, 0); up = Vec3.ZAxis; fov = Map.Instance.Fov; //MapCamera mapCamera = Entities.Instance.GetByName( "MapCamera_0" ) as MapCamera; //if( mapCamera != null ) //{ // position = mapCamera.Position; // forward = mapCamera.Rotation * new Vec3( 1, 0, 0 ); // fov = mapCamera.Fov; //} } else { //free camera mode position = freeCameraPosition; forward = freeCameraDirection.GetVector(); up = Vec3.ZAxis; fov = Map.Instance.Fov; } //update control RenderTargetUserControl control = workAreaControl.Views[viewIndex].Control; if (viewIndex == 0) { //first view control.CameraNearFarClipDistance = Map.Instance.NearFarClipDistance; control.CameraFixedUp = up; control.CameraFov = fov; control.CameraPosition = position; control.CameraDirection = forward; } else { //all views except first view. set orthographic camera projection. control.CameraNearFarClipDistance = Map.Instance.NearFarClipDistance; control.CameraProjectionType = ProjectionTypes.Orthographic; control.CameraOrthoWindowHeight = 30; Vec3 lookAt = position; switch (viewIndex) { case 1: control.CameraFixedUp = Vec3.ZAxis; control.CameraDirection = -Vec3.XAxis; control.CameraPosition = lookAt - control.CameraDirection * 100; break; case 2: control.CameraFixedUp = Vec3.ZAxis; control.CameraDirection = -Vec3.YAxis; control.CameraPosition = lookAt - control.CameraDirection * 100; break; case 3: control.CameraFixedUp = Vec3.YAxis; control.CameraDirection = -Vec3.ZAxis; control.CameraPosition = lookAt - control.CameraDirection * 100; break; } } } //update sound listener if (viewIndex == 0 && SoundWorld.Instance != null) { SoundWorld.Instance.SetListener(camera.Position, Vec3.Zero, camera.Direction, camera.Up); } //draw 2D graphics as 3D by means DebugGeometry if (workAreaViewsConfiguration == ViewsConfigurations.FourViews && viewIndex == 3) { const float cameraOffset = 10; Vec3 center = camera.Position + camera.Direction * cameraOffset; //draw box Vec3[] positions; int[] indices; GeometryGenerator.GenerateBox(new Vec3(10, 4, 1), out positions, out indices); Mat4 transform = new Mat4(Mat3.Identity, center); camera.DebugGeometry.Color = new ColorValue(.5f, 0, 0); camera.DebugGeometry.AddVertexIndexBuffer(positions, indices, transform, false, true); camera.DebugGeometry.Color = new ColorValue(1, 1, 0); camera.DebugGeometry.AddVertexIndexBuffer(positions, indices, transform, true, true); //draw axes camera.DebugGeometry.Color = new ColorValue(1, 0, 0); camera.DebugGeometry.AddArrow(center, center + new Vec3(5, 0, 0)); camera.DebugGeometry.Color = new ColorValue(0, 1, 0); camera.DebugGeometry.AddArrow(center, center + new Vec3(0, 5, 0)); camera.DebugGeometry.Color = new ColorValue(0, 0, 1); camera.DebugGeometry.AddArrow(center, center + new Vec3(0, 0, 5)); } }