Beispiel #1
0
        void updateUniformBuffers()
        {
            // Vertex shader
            _uboVS.projection = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
            Matrix4x4 viewMatrix = Matrix4x4.CreateTranslation(new Vector3(0, 2, zoom));

            _uboVS.model = viewMatrix;
            _uboVS.model = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * _uboVS.model;
            _uboVS.model = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y)) * _uboVS.model;
            _uboVS.model = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * _uboVS.model;

            var localUbovs = _uboVS;

            Unsafe.CopyBlock(uniformBuffer.mapped, &localUbovs, (uint)sizeof(UboVS));
        }
Beispiel #2
0
        void updateUniformBuffers()
        {
            uboVS.projection = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(60f), Width / 3.0f / (float)Height, 0.1f, 256.0f);

            Matrix4x4 viewMatrix = Matrix4x4.CreateTranslation(new Vector3(0f, 0f, zoom));

            uboVS.modelView = viewMatrix * Matrix4x4.CreateTranslation(cameraPos);

            uboVS.modelView = Matrix4x4.CreateFromAxisAngle(Vector3.UnitX, Util.DegreesToRadians(rotation.X)) * uboVS.modelView;
            uboVS.modelView = Matrix4x4.CreateFromAxisAngle(Vector3.UnitY, Util.DegreesToRadians(rotation.Y)) * uboVS.modelView;
            uboVS.modelView = Matrix4x4.CreateFromAxisAngle(Vector3.UnitZ, Util.DegreesToRadians(rotation.Z)) * uboVS.modelView;

            UboVS uboVSLocal = uboVS;

            Unsafe.CopyBlock(uniformBuffer.mapped, &uboVSLocal, (uint)sizeof(UboVS));
        }
Beispiel #3
0
        void updateUniformBufferOffscreen()
        {
            uboShared.projection = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(60f), (float)width / (float)height, 0.1f, 256.0f);
            Matrix4x4 viewMatrix = Matrix4x4.CreateTranslation(new Vector3(0.0f, 0.0f, zoom));

            uboShared.model = viewMatrix * Matrix4x4.CreateTranslation(cameraPos);
            uboShared.model = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * uboShared.model;
            uboShared.model = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y + meshRot.Y)) * uboShared.model;
            uboShared.model = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * uboShared.model;

            uboShared.model = Matrix4x4.CreateScale(new Vector3(1, -1, 1)) * uboShared.model;
            uboShared.model = Matrix4x4.CreateTranslation(meshPos) * uboShared.model;

            var local = uboShared;
            Unsafe.CopyBlock(uniformBuffers_vsOffScreen.mapped, &local, (uint)sizeof(UBO));
        }
        void updateDynamicUniformBuffer(bool force = false)
        {
            // Update at max. 60 fps
            animationTimer += (frameTimer * 100.0f);
            if ((animationTimer <= 1.0f / 60.0f) && (!force))
            {
                return;
            }

            // Dynamic ubo with per-object model matrices indexed by offsets in the command buffer
            uint    dim    = (uint)(Math.Pow(OBJECT_INSTANCES, (1.0f / 3.0f)));
            Vector3 offset = new Vector3(5.0f);

            for (uint x = 0; x < dim; x++)
            {
                for (uint y = 0; y < dim; y++)
                {
                    for (uint z = 0; z < dim; z++)
                    {
                        uint index = x * dim * dim + y * dim + z;

                        // Aligned offset
                        Matrix4x4 *modelMat = (Matrix4x4 *)(((ulong)uboDataDynamic_model + (index * (ulong)dynamicAlignment)));

                        // Update rotations
                        rotations[index] += animationTimer * rotationSpeeds[index];

                        // Update matrices
                        Vector3 pos      = new Vector3(-((dim * offset.X) / 2.0f) + offset.X / 2.0f + x * offset.X, -((dim * offset.Y) / 2.0f) + offset.Y / 2.0f + y * offset.Y, -((dim * offset.Z) / 2.0f) + offset.Z / 2.0f + z * offset.Z);
                        *       modelMat = Matrix4x4.CreateTranslation(pos);
                        *       modelMat = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotations[index].X)) * *modelMat;
                        *       modelMat = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotations[index].Y)) * *modelMat;
                        *       modelMat = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotations[index].Z)) * *modelMat;
                    }
                }
            }

            animationTimer = 0.0f;

            Unsafe.CopyBlock(uniformBuffers_dynamic.mapped, uboDataDynamic_model, (uint)uniformBuffers_dynamic.size);
            // Flush to make changes visible to the host
            VkMappedMemoryRange memoryRange = Initializers.mappedMemoryRange();

            memoryRange.memory = uniformBuffers_dynamic.memory;
            memoryRange.size   = uniformBuffers_dynamic.size;
            vkFlushMappedMemoryRanges(device, 1, &memoryRange);
        }
Beispiel #5
0
        void updateUniformBuffers()
        {
            // Vertex shader
            uboVS.projection = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(60f), width / (float)height, 0.1f, 256.0f);
            Matrix4x4 viewMatrix = Matrix4x4.CreateTranslation(new Vector3(0f, 0f, zoom));

            uboVS.model = viewMatrix * Matrix4x4.CreateTranslation(cameraPos);
            uboVS.model = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * uboVS.model;
            uboVS.model = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y)) * uboVS.model;
            uboVS.model = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * uboVS.model;

            uboVS.viewPos = new Vector4(0.0f, 0.0f, -zoom, 0.0f);

            Util.CheckResult(uniformBufferVS.map());
            var local = uboVS;

            Unsafe.CopyBlock(uniformBufferVS.mapped, &local, (uint)sizeof(UboVS));
            uniformBufferVS.unmap();
        }
Beispiel #6
0
        public void updateViewMatrix()
        {
            Matrix4x4 rotM = Matrix4x4.Identity;
            Matrix4x4 transM;

            rotM = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * rotM;
            rotM = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y)) * rotM;
            rotM = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * rotM;

            transM = Matrix4x4.CreateTranslation(position);

            if (type == CameraType.firstperson)
            {
                matrices_view = rotM * transM;
            }
            else
            {
                matrices_view = transM * rotM;
            }
        }
Beispiel #7
0
        // Update uniform buffers for rendering the 3D scene
        void updateUniformBuffersScene()
        {
            uboScene.projection = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(45.0f), (float)width / (float)height, 1.0f, 256.0f);
            Matrix4x4 viewMatrix = Matrix4x4.CreateTranslation(0, 0, zoom);

            uboScene.model = Matrix4x4.Identity;
            uboScene.model = viewMatrix * uboScene.model * Matrix4x4.CreateTranslation(cameraPos);
            uboScene.model = Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * uboScene.model;
            uboScene.model = Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y)) * uboScene.model;
            uboScene.model = Matrix4x4.CreateRotationX(Util.DegreesToRadians(timer * 360.0f)) * uboScene.model;
            uboScene.model = Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * uboScene.model;

            if (!paused)
            {
                uboScene.gradientPos += frameTimer * 0.1f;
            }

            var local = uboScene;

            Unsafe.CopyBlock(uniformBuffers_scene.mapped, &local, (uint)sizeof(UboVS));
        }
Beispiel #8
0
        void updateUniformBuffers()
        {
            uboVS.projection = System.Numerics.Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(60.0f), (float)width / (float)height, 0.1f, 256.0f);
            System.Numerics.Matrix4x4 viewMatrix = System.Numerics.Matrix4x4.CreateTranslation(0.0f, 0.0f, zoom);

            uboVS.model = viewMatrix * System.Numerics.Matrix4x4.CreateTranslation(cameraPos);
            uboVS.model = System.Numerics.Matrix4x4.CreateRotationX(Util.DegreesToRadians(rotation.X)) * uboVS.model;
            uboVS.model = System.Numerics.Matrix4x4.CreateRotationY(Util.DegreesToRadians(rotation.Y)) * uboVS.model;
            uboVS.model = System.Numerics.Matrix4x4.CreateRotationZ(Util.DegreesToRadians(rotation.Z)) * uboVS.model;

            Unsafe.Copy(uniformBuffers_scene.mapped, ref uboVS);
        }
Beispiel #9
0
        protected override void buildCommandBuffers()
        {
            VkCommandBufferBeginInfo cmdBufInfo = Initializers.commandBufferBeginInfo();

            FixedArray2 <VkClearValue> clearValues = new FixedArray2 <VkClearValue>();

            clearValues.First.color         = defaultClearColor;
            clearValues.Second.depthStencil = new VkClearDepthStencilValue {
                depth = 1.0f, stencil = 0
            };

            VkRenderPassBeginInfo renderPassBeginInfo = Initializers.renderPassBeginInfo();

            renderPassBeginInfo.renderPass               = renderPass;
            renderPassBeginInfo.renderArea.offset.x      = 0;
            renderPassBeginInfo.renderArea.offset.y      = 0;
            renderPassBeginInfo.renderArea.extent.width  = width;
            renderPassBeginInfo.renderArea.extent.height = height;
            renderPassBeginInfo.clearValueCount          = 2;
            renderPassBeginInfo.pClearValues             = &clearValues.First;

            for (int i = 0; i < drawCmdBuffers.Count; ++i)
            {
                // Set target frame buffer
                renderPassBeginInfo.framebuffer = frameBuffers[i];

                Util.CheckResult(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));

                vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VkSubpassContents.Inline);

                VkViewport viewport = Initializers.viewport((float)width, (float)height, 0.0f, 1.0f);
                vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

                VkRect2D scissor = Initializers.rect2D(width, height, 0, 0);
                vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

                // Update light positions
                // w component = light radius scale
                const float r     = 7.5f;
                float       sin_t = (float)Math.Sin(Util.DegreesToRadians(timer * 360));
                float       cos_t = (float)Math.Cos(Util.DegreesToRadians(timer * 360));
                const float y     = -4.0f;
                pushConstants[0] = new Vector4(r * 1.1f * sin_t, y, r * 1.1f * cos_t, 1.0f);
                pushConstants[1] = new Vector4(-r * sin_t, y, -r * cos_t, 1.0f);
                pushConstants[2] = new Vector4(r * 0.85f * sin_t, y, -sin_t * 2.5f, 1.5f);
                pushConstants[3] = new Vector4(0.0f, y, r * 1.25f * cos_t, 1.5f);
                pushConstants[4] = new Vector4(r * 2.25f * cos_t, y, 0.0f, 1.25f);
                pushConstants[5] = new Vector4(r * 2.5f * (float)Math.Cos(Util.DegreesToRadians(timer * 360)), y, r * 2.5f * sin_t, 1.25f);

                // Submit via push constant (rather than a UBO)
                vkCmdPushConstants(
                    drawCmdBuffers[i],
                    pipelineLayout,
                    VkShaderStageFlags.Vertex,
                    0,
                    pushConstants.Count * (uint)sizeof(Vector4),
                    pushConstants.Data.ToPointer());

                vkCmdBindPipeline(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelines_solid);
                vkCmdBindDescriptorSets(drawCmdBuffers[i], VkPipelineBindPoint.Graphics, pipelineLayout, 0, 1, ref descriptorSet, 0, null);

                ulong offsets = 0;
                vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, ref models_scene.vertices.buffer, &offsets);
                vkCmdBindIndexBuffer(drawCmdBuffers[i], models_scene.indices.buffer, 0, VkIndexType.Uint32);

                vkCmdDrawIndexed(drawCmdBuffers[i], models_scene.indexCount, 1, 0, 0, 0);

                vkCmdEndRenderPass(drawCmdBuffers[i]);

                Util.CheckResult(vkEndCommandBuffer(drawCmdBuffers[i]));
            }
        }
Beispiel #10
0
 public void updateAspectRatio(float aspect)
 {
     matrices_perspective = Matrix4x4.CreatePerspectiveFieldOfView(Util.DegreesToRadians(fov), aspect, znear, zfar);
 }
Beispiel #11
0
        // Update camera passing separate axis data (gamepad)
        // Returns true if view or position has been changed
        bool updatePad(Vector2 axisLeft, Vector2 axisRight, float deltaTime)
        {
            bool retVal = false;

            if (type == CameraType.firstperson)
            {
                // Use the common console thumbstick layout
                // Left = view, right = move

                const float deadZone = 0.0015f;
                const float range    = 1.0f - deadZone;

                Vector3 camFront;
                camFront.X = (float)(-Math.Cos(Util.DegreesToRadians(rotation.X)) * Math.Sin(Util.DegreesToRadians(rotation.Y)));
                camFront.Y = (float)(Math.Sin(Util.DegreesToRadians(rotation.X)));
                camFront.Z = (float)(Math.Cos(Util.DegreesToRadians(rotation.X)) * Math.Cos(Util.DegreesToRadians(rotation.Y)));
                camFront   = Vector3.Normalize(camFront);

                float moveSpeed = deltaTime * movementSpeed * 2.0f;
                float rotSpeed  = deltaTime * rotationSpeed * 50.0f;

                // Move
                if (Math.Abs(axisLeft.Y) > deadZone)
                {
                    float pos = (Math.Abs(axisLeft.Y) - deadZone) / range;
                    position -= camFront * pos * ((axisLeft.Y < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
                    retVal    = true;
                }
                if (Math.Abs(axisLeft.X) > deadZone)
                {
                    float pos = (Math.Abs(axisLeft.X) - deadZone) / range;
                    position += Vector3.Normalize(Vector3.Cross(camFront, new Vector3(0.0f, 1.0f, 0.0f))) * pos * ((axisLeft.X < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
                    retVal    = true;
                }

                // Rotate
                if (Math.Abs(axisRight.X) > deadZone)
                {
                    float pos = (Math.Abs(axisRight.X) - deadZone) / range;
                    rotation.Y += pos * ((axisRight.X < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
                    retVal      = true;
                }
                if (Math.Abs(axisRight.Y) > deadZone)
                {
                    float pos = (Math.Abs(axisRight.Y) - deadZone) / range;
                    rotation.X -= pos * ((axisRight.Y < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
                    retVal      = true;
                }
            }
            else
            {
                // todo: move code from example base class for look-at
            }

            if (retVal)
            {
                updateViewMatrix();
            }

            return(retVal);
        }
Beispiel #12
0
        public void update(float deltaTime)
        {
            if (type == CameraType.firstperson)
            {
                if (moving())
                {
                    Vector3 camFront;
                    camFront.X = (float)(-Math.Cos(Util.DegreesToRadians(rotation.X)) * Math.Sin(Util.DegreesToRadians(rotation.Y)));
                    camFront.Y = (float)(Math.Sin(Util.DegreesToRadians(rotation.X)));
                    camFront.Z = (float)(Math.Cos(Util.DegreesToRadians(rotation.X)) * Math.Cos(Util.DegreesToRadians(rotation.Y)));
                    camFront   = Vector3.Normalize(camFront);

                    float moveSpeed = deltaTime * movementSpeed;

                    if (keys_up)
                    {
                        position += camFront * moveSpeed;
                    }
                    if (keys_down)
                    {
                        position -= camFront * moveSpeed;
                    }
                    if (keys_left)
                    {
                        position -= Vector3.Normalize(Vector3.Cross(camFront, new Vector3(0.0f, 1.0f, 0.0f))) * moveSpeed;
                    }
                    if (keys_right)
                    {
                        position += Vector3.Normalize(Vector3.Cross(camFront, new Vector3(0.0f, 1.0f, 0.0f))) * moveSpeed;
                    }

                    updateViewMatrix();
                }
            }
        }