private void Render(object sender, GlControlEventArgs e) { if (_shader == null) { return; } Gl.Clear(ClearBufferMask.DepthBufferBit); Gl.MatrixMode(MatrixMode.Modelview); var v = _cameraHelper.CameraMatrix; var u = Matrix4x4.CreateRotationX(Mathf.ToRadian(90)); var m = u * v; _shader.M = m; FiguresHelper.Draw3DCross(50f, 1f); Figure3DHelper.DrawSphere(15, 20, 20, true, Colors.Yellow); Gl.LineWidth(1); Figure3DHelper.DrawSphere(15, 20, 20, false, Colors.Red); _satelite.Draw(); }
protected override void Draw(float deltaSeconds) { UpdateAnimation(deltaSeconds); UpdateUniforms(); _cl.Begin(); _cl.SetFramebuffer(GraphicsDevice.SwapchainFramebuffer); _cl.ClearColorTarget(0, RgbaFloat.Black); _cl.ClearDepthStencil(1f); Matrix4x4 worldMatrix = Matrix4x4.CreateTranslation(0, 15000, -5000) * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2) * Matrix4x4.CreateScale(0.05f); _cl.UpdateBuffer(_worldBuffer, 0, ref worldMatrix); DrawMesh(); worldMatrix = Matrix4x4.CreateTranslation(0, 15000, -5000) * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2) * Matrix4x4.CreateScale(0.07f); _cl.UpdateBuffer(_worldBuffer, 0, ref worldMatrix); DrawMesh(); _cl.End(); GraphicsDevice.SubmitCommands(_cl); GraphicsDevice.SwapBuffers(); }
private void Render(object sender, GlControlEventArgs e) { if (_shader == null) { return; } var v = _cameraHelper.CameraMatrix; _shader.M = v; _shader.ShadingLevel = 0; FiguresHelper.Draw3DCross(4, 1); _shader.ShadingLevel = 1; _shader.LightPos = new Vector3(1f, 1f, 1f); var r1 = Matrix4x4.CreateRotationY(Mathf.ToRadian(_phi)); var t = Matrix4x4.CreateTranslation(new Vector3(_radius, 2f, 0f)); var r2 = Matrix4x4.CreateRotationX(Mathf.ToRadian(90)) * Matrix4x4.CreateRotationY(Mathf.ToRadian(90)) * Matrix4x4.CreateRotationZ(Mathf.ToRadian(45)); var scaleM = Matrix4x4.CreateScale(0.2f); var r3 = Matrix4x4.CreateRotationY(Mathf.ToRadian(-_phi * 10)); var r4 = Matrix4x4.CreateRotationZ(Mathf.ToRadian(25)); var m = r3 * r2 * t * r1 * scaleM * r4; _shader.M = m * v; Figure3DHelper.DrawMesh(_boomerang, Colors.Chocolate); _phi += _step; }
private void OnMouseMove(object sender, MouseEventArgs e) { Control senderControl = (Control)sender; GetNormalized(e.Location, senderControl, out float newX, out float newY); if (_isLeftMousePressed) { float dx = newX - _prevPoint.X; float dy = newY - _prevPoint.Y; ModelMatrix *= Matrix4x4.CreateRotationX(-dy, Vector3.Zero); ModelMatrix *= Matrix4x4.CreateRotationY(dx, Vector3.Zero); } if (_isRightMousePressed) { float dx = newX - _prevPoint.X; float dy = newY - _prevPoint.Y; float sensitivity = 50.0f; dx = dx * sensitivity; dy = dy * sensitivity; _yaw += dx; _pitch += dy; if (_pitch > 89.0f) { _pitch = 89.0f; } if (_pitch < -89.0f) { _pitch = -89.0f; } Vector3 newFront; newFront.X = (float)(Math.Cos(_yaw.ToRadians()) * Math.Cos(_pitch.ToRadians())); newFront.Y = (float)Math.Sin(_pitch.ToRadians()); newFront.Z = (float)(Math.Sin(_yaw.ToRadians()) * Math.Cos(_pitch.ToRadians())); _cameraFront = Vector3.Normalize(newFront); } _prevPoint.X = newX; _prevPoint.Y = newY; }
protected override void CreateResources(ResourceFactory factory) { _projectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _viewBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _worldBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); Matrix4x4 worldMatrix = Matrix4x4.CreateTranslation(0, 15000, -5000) * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2) * Matrix4x4.CreateScale(0.05f); GraphicsDevice.UpdateBuffer(_worldBuffer, 0, ref worldMatrix); ResourceLayout layout = factory.CreateResourceLayout(new ResourceLayoutDescription( new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("Bones", ResourceKind.UniformBuffer, ShaderStages.Vertex), new ResourceLayoutElementDescription("SurfaceTex", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment))); Texture texture; using (Stream ktxStream = OpenEmbeddedAssetStream("goblin_bc3_unorm.ktx")) { texture = KtxFile.LoadTexture( GraphicsDevice, factory, ktxStream, PixelFormat.BC3_UNorm); } _texView = ResourceFactory.CreateTextureView(texture); VertexLayoutDescription vertexLayouts = new VertexLayoutDescription( new[] { new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3), new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("BoneWeights", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4), new VertexElementDescription("BoneIndices", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt4), }); GraphicsPipelineDescription gpd = new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, DepthStencilStateDescription.DepthOnlyLessEqual, new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.CounterClockwise, true, false), PrimitiveTopology.TriangleList, new ShaderSetDescription( new[] { vertexLayouts }, factory.CreateFromSpirv( new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(VertexCode), "main"), new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(FragmentCode), "main"))), layout, GraphicsDevice.SwapchainFramebuffer.OutputDescription); _pipeline = factory.CreateGraphicsPipeline(ref gpd); AssimpContext ac = new AssimpContext(); using (Stream modelStream = OpenEmbeddedAssetStream("goblin.dae")) { _scene = ac.ImportFileFromStream(modelStream, "dae"); } _rootNodeInverseTransform = _scene.RootNode.Transform; _rootNodeInverseTransform.Inverse(); _firstMesh = _scene.Meshes[0]; AnimatedVertex[] vertices = new AnimatedVertex[_firstMesh.VertexCount]; for (int i = 0; i < vertices.Length; i++) { vertices[i].Position = new Vector3(_firstMesh.Vertices[i].X, _firstMesh.Vertices[i].Y, _firstMesh.Vertices[i].Z); vertices[i].UV = new Vector2(_firstMesh.TextureCoordinateChannels[0][i].X, _firstMesh.TextureCoordinateChannels[0][i].Y); } _animation = _scene.Animations[0]; List <int> indices = new List <int>(); foreach (Face face in _firstMesh.Faces) { if (face.IndexCount == 3) { indices.Add(face.Indices[0]); indices.Add(face.Indices[1]); indices.Add(face.Indices[2]); } } for (uint boneID = 0; boneID < _firstMesh.BoneCount; boneID++) { Bone bone = _firstMesh.Bones[(int)boneID]; _boneIDsByName.Add(bone.Name, boneID); foreach (VertexWeight weight in bone.VertexWeights) { vertices[weight.VertexID].AddBone(boneID, weight.Weight); } } Array.Resize(ref _boneTransformations, _firstMesh.BoneCount); _bonesBuffer = ResourceFactory.CreateBuffer(new BufferDescription( 64 * 64, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); _rs = factory.CreateResourceSet(new ResourceSetDescription(layout, _projectionBuffer, _viewBuffer, _worldBuffer, _bonesBuffer, _texView, GraphicsDevice.Aniso4xSampler)); _indexCount = (uint)indices.Count; _vertexBuffer = ResourceFactory.CreateBuffer(new BufferDescription( (uint)(vertices.Length * Unsafe.SizeOf <AnimatedVertex>()), BufferUsage.VertexBuffer)); GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, vertices); _indexBuffer = ResourceFactory.CreateBuffer(new BufferDescription( _indexCount * 4, BufferUsage.IndexBuffer)); GraphicsDevice.UpdateBuffer(_indexBuffer, 0, indices.ToArray()); _cl = factory.CreateCommandList(); _camera.Position = new Vector3(110, -87, -532); _camera.Yaw = 0.45f; _camera.Pitch = -0.55f; _camera.MoveSpeed = 1000f; _camera.FarDistance = 100000; }
protected override void CreateInternal(ReadOnlyMemory <byte> data) { var str = new ReadOnlyLinkedMemoryStream(); str.AddMemory(data); _assContext ??= new AssimpContext(); Scene scene = _assContext.ImportFileFromStream(str, PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs | PostProcessSteps.OptimizeGraph | PostProcessSteps.OptimizeMeshes); var embeddedTextures = new List <Texture>(); for (var i = 0; i < scene.TextureCount; i++) { EmbeddedTexture assTexture = scene.Textures[i]; var embeddedTexture = new TextureAsset(); embeddedTexture.Create(assTexture.CompressedData); embeddedTextures.Add(embeddedTexture.Texture); } _materials = new List <MeshMaterial>(); for (var i = 0; i < scene.MaterialCount; i++) { Material material = scene.Materials[i]; Color4D diffColor = material.ColorDiffuse; bool embeddedTexture = material.HasTextureDiffuse && embeddedTextures.Count > material.TextureDiffuse.TextureIndex; var emotionMaterial = new MeshMaterial { Name = material.Name, DiffuseColor = new Color(new Vector4(diffColor.R, diffColor.G, diffColor.B, diffColor.A)), DiffuseTextureName = embeddedTexture ? $"EmbeddedTexture{material.TextureDiffuse.TextureIndex}" : null, DiffuseTexture = embeddedTexture ? embeddedTextures[material.TextureDiffuse.TextureIndex] : null }; _materials.Add(emotionMaterial); } _animations = new List <SkeletalAnimation>(); ProcessAnimations(scene); _meshes = new List <Mesh>(); Node rootNode = scene.RootNode; SkeletonAnimRigNode animRigRoot = ProcessNode(scene, rootNode); animRigRoot.LocalTransform *= Matrix4x4.CreateRotationX(-90 * Maths.DEG2_RAD); // Convert to right handed Z is up. Entity = new MeshEntity { Name = Name, Meshes = _meshes.ToArray(), Animations = _animations.ToArray(), AnimationRig = animRigRoot }; object scaleData = scene.RootNode.Metadata.GetValueOrDefault("UnitScaleFactor").Data; var scaleF = 1f; if (scaleData is float f) { scaleF = f; } Entity.Scale = scaleF; }