public static unsafe void DrawToScreen(DepthTexture screenArray, FormMain drawTo) { //simple error checking to keep program from crashing if array is oversized if (screenArray.Width != bitmap.Width || screenArray.Height != bitmap.Height) { throw new System.ArgumentException("Parameter array is not the same size as the screen", "original"); } //lock bits to draw screen buffer System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rectScreen, System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); //Pixel array to bitmap UInt32 *pixel = (UInt32 *)bmpData.Scan0.ToPointer(); int tempW = bitmap.Width; int tempH = bitmap.Height; //for (int x = 0; x < bitmap.Width; x++) Parallel.For(0, tempH, y => { int tmpY = y * tempW; for (int x = 0; x < tempW; x++) { BasePixel tmpPx = screenArray.GetPixel(x, y); pixel[tmpY + x] = 0xFF000000 | (((UInt32)tmpPx.R) << 16 | (((UInt32)tmpPx.G) << 8)) | tmpPx.B; } }); //unlock bits then draw bitmap.UnlockBits(bmpData); drawTo.DrawImage(bitmap); }
private void DrawVerticalLine(DepthTexture texture, Pixel color, float depth, int startX, int startY, float width) { //make sure the range is in drawing area startY = Math.Max(startY, 0); int xEnd = Math.Min(startX + (int)Math.Ceiling(width), texture.Width - 1); //draw the rectangle for (int xOff = Math.Max(0, startX); xOff < xEnd; xOff++) { for (int y = startY; y < texture.Height; y++) { BasePixel tmpPx = texture.GetPixel(xOff, y); if (tmpPx.Depth < depth) { break; } tmpPx.R = color.R; tmpPx.G = color.G; tmpPx.B = color.B; tmpPx.Depth = depth; } } }
public Terrain(DepthTexture diffuseMap, DepthTexture bumpMap, float yScale, float xzScale) { if (diffuseMap.Height != bumpMap.Height || diffuseMap.Width != bumpMap.Width) { throw new Exception("diffuseMap and bumpMap must be the same size"); } if (yScale < 1.0f || xzScale < 1.0f) { throw new Exception("only can scale up or keep a scale of 1"); } //make bare-bones simplified arrays/per-computed values this.xzScale = xzScale; h = bumpMap.Height; w = bumpMap.Width; this.bumpMap = new float[bumpMap.Width, bumpMap.Height]; this.diffuseMap = new Pixel[diffuseMap.Width, diffuseMap.Height]; Parallel.For(0, bumpMap.Width, x => { for (int y = 0; y < bumpMap.Height; y++) { this.bumpMap[x, y] = bumpMap.GetPixel(x, y).R *yScale; ref Pixel tmpPix = ref this.diffuseMap[x, y]; BasePixel tmpPix2 = diffuseMap.GetPixel(x, y); tmpPix.R = tmpPix2.R; tmpPix.G = tmpPix2.G; tmpPix.B = tmpPix2.B; } }); }
//Check if its in the screenTexture area. Left/right/... are the bounding box of the triangle limited to screen space. //Returns False if not in screen screenTexture area. private bool OnScreen(TriangleData tData, DepthTexture screenTexture, ref int left, ref int right, ref int top, ref int bottom) { left = (int)Left(tData); if (left > screenTexture.Width) { return(false); } right = (int)Right(tData); if (right < 0) { return(false); } top = (int)Top(tData); if (top > screenTexture.Height) { return(false); } bottom = (int)Bottom(tData); if (bottom < 0) { return(false); } left = Math.Max(0, left); right = Math.Min(screenTexture.Width, right); top = Math.Max(0, top); bottom = Math.Min(screenTexture.Height, bottom); return(true); }
void DrawFrame() { WriteMatrixToBuffer(UniformCpuBuffer, GetTransformationMatrix()); Device.DefaultQueue.WriteBuffer(UniformBuffer, 0, UniformCpuBuffer); var swapChainTexture = SwapChain.GetCurrentTexture(); GpuRenderPassDescriptor renderPassDescriptor = new GpuRenderPassDescriptor(new GpuRenderPassColorAttachment[] { new GpuRenderPassColorAttachment(swapChainTexture.CreateView(), new GpuColorDict { R = 0.5f, G = 0.5f, B = 0.5f, A = 1.0f }) }) { DepthStencilAttachment = new GpuRenderPassDepthStencilAttachment(DepthTexture.CreateView(), 1.0f, GpuStoreOp.Store, null, GpuStoreOp.Store) }; var commandEncoder = Device.CreateCommandEncoder(); var passEncoder = commandEncoder.BeginRenderPass(renderPassDescriptor); passEncoder.SetPipeline(Pipeline); passEncoder.SetBindGroup(0, UniformBindGroup); passEncoder.SetVertexBuffer(0, VerticesBuffer, 0, VerticesBuffer.Size); passEncoder.Draw(36, 1, 0, 0); passEncoder.EndPass(); commandEncoder.CopyTextureToTexture(new GpuImageCopyTexture(swapChainTexture), new GpuImageCopyTexture(CubeTexture), new GpuExtend3DDict { Width = SwapChainDescriptor.Width, Height = SwapChainDescriptor.Height, Depth = 1 }); Device.DefaultQueue.Sumit(new GpuCommandBuffer[] { commandEncoder.Finish() }); SwapChain.Present(); }
public ModelAnimationManager(DepthTexture texture, bool orthographic, bool backfaceCulling, float scale, Position3D position) { Texture = texture; Orthographic = orthographic; BackfaceCulling = backfaceCulling; Scale = scale; Position = position; }
public Camera(Position3D cameraPosition, int width, int height, float farClippingPlane, float fovInDegrees) { screen = new DepthTexture(width, height); pos = cameraPosition; Fov = fovInDegrees; this.farClippingPlane = farClippingPlane; }
internal void RecreateRenderTarget() { RecreateViewTexture(Width, Height); View.SetNewRenderTarget( ViewTexture.GetNativeTexturePtr(), DepthTexture.GetNativeTexturePtr(), (uint)Width, (uint)Height, 1); CoherentUIGTRenderEvents.SendRenderEvent(CoherentRenderEventType.SetNewRenderTarget, View.GetId()); }
public void Dispose() { RenderTexture.Dispose(); DepthTexture.Dispose(); RenderTargetView.Dispose(); RenderSourceView.Dispose(); DepthTargetView.Dispose(); DepthResourceView.Dispose(); ResolveTexture.Dispose(); ResolveTargetView.Dispose(); ResolveSourceView.Dispose(); }
private void PerspectiveVertices(ref TriangleData tData, DepthTexture screenTexture, float calculatedFov) { if (tData.Vertex1.z > 0) { tData.Vertex1.Perspective(calculatedFov, screenTexture.HalfWidth, screenTexture.HalfHeight); } if (tData.Vertex2.z > 0) { tData.Vertex2.Perspective(calculatedFov, screenTexture.HalfWidth, screenTexture.HalfHeight); } if (tData.Vertex3.z > 0) { tData.Vertex3.Perspective(calculatedFov, screenTexture.HalfWidth, screenTexture.HalfHeight); } }
public void Dispose() { AccumTexture.Dispose(); AccumTargetView.Dispose(); AccumResourceView.Dispose(); RevealageTexture.Dispose(); RevealageTargetView.Dispose(); RevealageSourceView.Dispose(); DepthTexture.Dispose(); DepthTargetView.Dispose(); DepthSourceView.Dispose(); depthCopyingStates.Dispose(); compositingStates.Dispose(); }
/// <summary> /// /// </summary> public void Dispose() { if (ColorTexture != null) { ColorTexture.Dispose(); } if (DepthTexture != null) { DepthTexture.Dispose(); } //if (FBOHandle != 0) // TK.GL.Ext.DeleteFramebuffers(1, ref FBOHandle); GC.SuppressFinalize(this); }
private void DestroyD3D() { _textureManager.Dispose(); _dxCube.Dispose(); //_dxEffect.Dispose(); _kinectPoints.Dispose(); if (_dxFont != null) { _dxFont.Dispose(); _dxFont = null; } if (DepthTexture != null) { DepthTexture.Dispose(); DepthTexture = null; } if (SharedTexture != null) { SharedTexture.Dispose(); SharedTexture = null; } if (_dxRenderView != null) { _dxRenderView.Dispose(); _dxRenderView = null; } if (_dxDepthStencilView != null) { _dxDepthStencilView.Dispose(); _dxDepthStencilView = null; } if (_dxDevice != null) { _dxDevice.Dispose(); _dxDevice = null; } }
/// <summary> /// Create Render targets /// </summary> /// <param name="cameraResources">camera Resources</param> /// <param name="newWidth">render width</param> /// <param name="newHeight">render height</param> /// <param name="backBufferPointer">target handle</param> /// <param name="adapter">adapter instance</param> /// <param name="renderTargetManager">render Target Manager</param> private void CreateRenderTargets(CameraResources cameraResources, int newWidth, int newHeight, int backBufferPointer, Adapter.Adapter adapter, RenderTargetManager renderTargetManager) { adapter.SetSize(newWidth, newHeight); DepthTexture depthTexture = renderTargetManager.CreateDepthTexture(newWidth, newHeight); var lenght = cameraResources.IsRenderingStereoscopic ? 2 : 1; for (int i = 0; i < lenght; i++) { RenderTargetViewDescription rtViewDescription = new RenderTargetViewDescription() { Format = cameraResources.BackBufferTexture2D.Description.Format, Dimension = RenderTargetViewDimension.Texture2DArray, Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource() { FirstArraySlice = i, ArraySize = 1, MipSlice = 0 } }; var renderTarget = renderTargetManager.CreateRenderTarget(cameraResources.BackBufferTexture2D, rtViewDescription); renderTarget.DepthTexture = depthTexture; var eyeTexture = new VREyeTexture() { Viewport = new Viewport(0, 0, 1, 1), NearPlane = 0.01f, FarPlane = 1000, RenderTarget = renderTarget }; this.eyesProperties[i].Texture = eyeTexture; } var eyeRT = this.eyesProperties[0].Texture.RenderTarget; var dxRT = renderTargetManager.TargetFromHandle <DXRenderTarget>(eyeRT.TextureHandle); adapter.GraphicsDevice.BackBuffer = dxRT.TargetView; }
public void Layout() { // Layout is called on CoherentUIGTSystem component Update // because UISystem.Advance() should happen before View.Layout() var view = View; if (!enabled || view == null) { return; } if ((!ViewTexture.IsCreated() || !DepthTexture.IsCreated()) && Screen.fullScreen == m_WasLastFrameFullscreen) { // Make sure that we don't call RecreateRenderTarget twice when transitioning from // Fullscreen to Windowed mode and vice versa; calling RecreateRenderTarget second // time sets wrong render texture target that leads to missing UI on the screen Debug.Log("[Debug] No render texture present. Recreating it"); RecreateRenderTarget(); } m_WasLastFrameFullscreen = Screen.fullScreen; if (m_UseCameraDimensions) { int width = 0; int height = 0; if (GetCamDimensions(out width, out height)) { if (width != m_Width || height != m_Height) { Resize(width, height); } } } if (ViewRenderer != null) { view.Layout(); } }
public FormMain() { InitializeComponent(); //make buffer that will be drawn on screen screenMain = pictureBoxMain.CreateGraphics(); //test camera cameraTest = new Camera(new Position3D(0, 0, 0, 0, 0, 0), GlobalConstants.SCREEN_WIDTH, GlobalConstants.SCREEN_HEIGHT, GlobalConstants.FAR_CLIPPING_PLANE, GlobalConstants.CAMERA_FOV); //test terrain testT = new Terrain(new DepthTexture(@"SceneData\bin\bumpMapColor.png"), new DepthTexture(@"SceneData\bin\bumpMapTest.png"), hs, xys); //models test mopos = new Position3D(0, 0, 0, 0, 180, 0); mot = new DepthTexture(@"SceneData\bin\testImage.png"); mo = new ModelAnimationManager(mot, false, true, 1.7f, mopos); mo.AddAnimationFolder(@"SceneData\bin\Run\run", 0, 110); mo.AddAnimationFolder(@"SceneData\bin\Walk\walk", 1, 110); }
public void InitBuffers() { InitScene(); ScreenBuffer = new Framebuffer(FramebufferTarget.Framebuffer, this.Width, this.Height, 16, PixelInternalFormat.Rgba16f, 1); ScreenBuffer.Resize(Width, Height); PostEffects = new Framebuffer(FramebufferTarget.Framebuffer, Width, Height, PixelInternalFormat.Rgba16f, 1); PostEffects.Resize(Width, Height); BloomEffects = new Framebuffer(FramebufferTarget.Framebuffer, Width, Height, PixelInternalFormat.Rgba16f, 1); BloomEffects.Resize(Width, Height); if (USE_GBUFFER) { DepthTexture = new DepthTexture(Width, Height, PixelInternalFormat.DepthComponent24); //Set the GBuffer (Depth, Normals and another output) GBuffer = new Framebuffer(FramebufferTarget.Framebuffer); GBuffer.AddAttachment(FramebufferAttachment.ColorAttachment0, GLTexture2D.CreateUncompressedTexture(Width, Height, PixelInternalFormat.R11fG11fB10f, PixelFormat.Rgba, PixelType.Float)); GBuffer.AddAttachment(FramebufferAttachment.ColorAttachment3, GLTexture2D.CreateUncompressedTexture(Width, Height, PixelInternalFormat.Rgb10A2, PixelFormat.Rgba, PixelType.Float)); GBuffer.AddAttachment(FramebufferAttachment.ColorAttachment4, GLTexture2D.CreateUncompressedTexture(Width, Height, PixelInternalFormat.Rgb10A2, PixelFormat.Rgba, PixelType.Float)); GBuffer.AddAttachment(FramebufferAttachment.DepthAttachment, DepthTexture); GBuffer.SetReadBuffer(ReadBufferMode.None); GBuffer.SetDrawBuffers( DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.None, DrawBuffersEnum.None, DrawBuffersEnum.ColorAttachment3, DrawBuffersEnum.ColorAttachment4); GBuffer.Unbind(); } FinalBuffer = new Framebuffer(FramebufferTarget.Framebuffer, this.Width, this.Height, PixelInternalFormat.Rgba16f, 1); }
void DrawFrame() { UpdateTransformationMatrix(); WriteMatricesToBuffer(UniformCpuBuffer, MvpMatrices); Device.DefaultQueue.WriteBuffer(UniformBuffer, 0, UniformCpuBuffer); GpuRenderPassDescriptor renderPassDescriptor = new GpuRenderPassDescriptor(new GpuRenderPassColorAttachment[] { new GpuRenderPassColorAttachment(SwapChain.GetCurrentTexture().CreateView(), new GpuColorDict { R = 0.5f, G = 0.5f, B = 0.5f, A = 1.0f }) }) { DepthStencilAttachment = new GpuRenderPassDepthStencilAttachment(DepthTexture.CreateView(), 1.0f, GpuStoreOp.Store, null, GpuStoreOp.Store) }; var commandEncoder = Device.CreateCommandEncoder(); var passEncoder = commandEncoder.BeginRenderPass(renderPassDescriptor); passEncoder.SetPipeline(Pipeline); passEncoder.SetBindGroup(0, UniformBindGroup); passEncoder.SetVertexBuffer(0, VerticesBuffer, 0, VerticesBuffer.Size); passEncoder.Draw(36, NumInstances, 0, 0); passEncoder.EndPass(); Device.DefaultQueue.Sumit(new GpuCommandBuffer[] { commandEncoder.Finish() }); SwapChain.Present(); }
private void InitAndAttachDepthTexture() { // dump old depth texture if (DepthTexture != null) { DepthTexture.Unload(); DepthTexture = null; } // create & bind depth texture DepthTexture = new Texture(Width, Height, TextureTarget.Texture2D, PixelInternalFormat.DepthComponent32, PixelFormat.DepthComponent, PixelType.Float); DepthTexture.Set(TextureParameterName.TextureWrapS, TextureWrapMode.ClampToEdge); DepthTexture.Set(TextureParameterName.TextureWrapT, TextureWrapMode.ClampToEdge); DepthTexture.Set(TextureParameterName.TextureMinFilter, TextureMinFilter.Nearest); DepthTexture.Set(TextureParameterName.TextureMagFilter, TextureMagFilter.Nearest); DepthTexture.Set(TextureParameterName.TextureCompareMode, TextureCompareMode.None); DepthTexture.Set(TextureParameterName.TextureCompareFunc, All.None); DepthTexture.Load(); LogTrace($"Depth Texture: {DepthTexture}"); DepthTexture.UploadEmpty(); DepthTexture.Bind(); GL.FramebufferTexture2D(FBO.Target, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2D, DepthTexture.ID, 0); }
static void Main(string[] args) { Window window = new Window(1024, 768, "Aiv.Fast3D Perspective Test", false, 24, 0); window.SetDefaultOrthographicSize(10); //Window window = new Window("Aiv.Fast3D Perspective Test", 24, 4); window.EnableDepthTest(); //window.CullBackFaces(); window.SetCursor(false); PerspectiveCamera camera = new PerspectiveCamera(new Vector3(0, 6, 30), new Vector3(-10, 180, 0), 60, 0.01f, 1000); Texture crate = new Texture("Assets/crate.jpg"); Texture floorTexture = new Texture("Assets/floor.jpg"); floorTexture.SetRepeatX(); floorTexture.SetRepeatY(); Texture stormTrooperTexture = new Texture("Assets/Stormtrooper.png"); stormTrooperTexture.SetRepeatX(); stormTrooperTexture.SetRepeatY(); Cube cube = new Cube(); Cube floor = new Cube(); floor.Scale3 = new Vector3(150, 50f, 150); floor.Position3 = new Vector3(0, -25, 0); // tiling texture for (int i = 0; i < floor.uv.Length; i++) { floor.uv[i] *= 10; } floor.UpdateUV(); Mesh3 stormTrooper = ObjLoader.Load("Assets/Stormtrooper.obj", Vector3.One)[0]; //stormTrooper.RegenerateNormals(); Pyramid pyramid = new Pyramid(); Texture logoAiv = new Texture("Assets/LogoAIV.png"); Sprite logo = new Sprite(1 * logoAiv.Ratio, 1); Camera hudCamera = new Camera(); logo.Camera = hudCamera; float lightRotation = -30; DepthTexture shadowTexture = new DepthTexture(4096, 4096, 24); Sprite shadow = new Sprite(5, 5); shadow.Camera = hudCamera; float shadow_left = -30; float shadow_right = 30; float shadow_bottom = -30; float shadow_top = 30; float shadow_near = -30; float shadow_far = 30; DirectionalLight directionalLight = new DirectionalLight(Utils.EulerRotationToDirection(new Vector3(lightRotation, 180, 0))); directionalLight.SetShadowProjection(shadow_left, shadow_right, shadow_bottom, shadow_top, shadow_near, shadow_far); //directionalLight.Color = new Vector3(0.5f, 1, 0.5f); float crateRotation = 0; Mesh3[] botMesh = FbxLoader.Load("Assets/running.fbx", new Vector3(0.02f, 0.02f, 0.02f)); SkeletalAnimation[] animations = FbxLoader.LoadAnimations("Assets/running.fbx", new Vector3(0.02f, 0.02f, 0.02f)); int numFrames = 0; Mesh3 movingTrooper = ObjLoader.Load("Assets/Stormtrooper.obj", Vector3.One)[0]; movingTrooper.Position3 = new Vector3(0, 0, 2); foreach (SkeletalAnimation animation in animations) { Console.WriteLine(animation.Name); foreach (string subject in animation.KeyFrames.Keys) { Console.WriteLine(subject); int currentFrames = 0; foreach (SkeletalAnimation.KeyFrame frame in animation.KeyFrames[subject]) { Console.WriteLine(frame.Time + " " + frame.Position + " " + frame.Rotation + " " + frame.Scale); currentFrames++; } if (currentFrames > numFrames) { numFrames = currentFrames; } } } float neckRotation = 0; int animationIndex = 0; float animationTimer = 1f / animations[0].Fps; movingTrooper.SetParent(cube); Mesh3[] tank = ObjLoader.Load("Assets/T34.obj", Vector3.One); Texture tankDiffuse = new Texture("Assets/T-34.png"); Texture tankSpecular = new Texture("Assets/T-34_S.png"); //window.AddPostProcessingEffect(new DepthViewer()); window.AddPostProcessingEffect(new Fog()); //window.AddPostProcessingEffect(new Sobel()); Vector3 pyramidRotation = Vector3.Zero; PostProcessingEffect fxaa = window.AddPostProcessingEffect(new FXAA()); Plane plane = new Plane(); Vector3 shadowOffset = new Vector3(0, 0, 0.22f); Sphere sphere = new Sphere(); Texture world = new Texture("Assets/world.jpg"); while (window.IsOpened) { if (window.GetKey(KeyCode.Esc)) { break; } if (window.GetKey(KeyCode.W)) { camera.Position3 += camera.Forward * 10 * window.deltaTime; } if (window.GetKey(KeyCode.S)) { camera.Position3 += -camera.Forward * 10 * window.deltaTime; } if (window.GetKey(KeyCode.D)) { camera.Position3 += camera.Right * 10 * window.deltaTime; } if (window.GetKey(KeyCode.A)) { camera.Position3 += -camera.Right * 10 * window.deltaTime; } if (window.GetKey(KeyCode.Up)) { camera.Position3 += camera.Up * 10 * window.deltaTime; } if (window.GetKey(KeyCode.Down)) { camera.Position3 += -camera.Up * 10 * window.deltaTime; } if (window.HasFocus) { // currently broken, the mouse is too flaky //float yaw = MouseX(window) * (90 + 45f); //float pitch = MouseY(window) * 90f; //camera.EulerRotation3 += new Vector3(pitch, yaw, 0) * window.deltaTime; } if (window.GetKey(KeyCode.Right)) { camera.EulerRotation3 += new Vector3(0, 90 + 45, 0) * window.deltaTime; } if (window.GetKey(KeyCode.Left)) { camera.EulerRotation3 -= new Vector3(0, 90 + 45, 0) * window.deltaTime; } if (window.GetKey(KeyCode.P)) { camera.EulerRotation3 += new Vector3(90 + 45, 0, 0) * window.deltaTime; } if (window.GetKey(KeyCode.L)) { camera.EulerRotation3 -= new Vector3(90 + 45, 0, 0) * window.deltaTime; } //fxaa.enabled = window.GetKey(KeyCode.X); if (window.GetKey(KeyCode.Num1)) { shadowOffset += Vector3.UnitX * 2 * window.deltaTime; Console.WriteLine(shadowOffset); } if (window.GetKey(KeyCode.Num2)) { shadowOffset -= Vector3.UnitX * 2 * window.deltaTime; Console.WriteLine(shadowOffset); } if (window.GetKey(KeyCode.Num3)) { shadowOffset += Vector3.UnitY * 2 * window.deltaTime; Console.WriteLine(shadowOffset); } if (window.GetKey(KeyCode.Num4)) { shadowOffset -= Vector3.UnitY * 2 * window.deltaTime; Console.WriteLine(shadowOffset); } if (window.GetKey(KeyCode.Num5)) { shadowOffset += Vector3.UnitZ * 0.2f * window.deltaTime; Console.WriteLine(shadowOffset); } if (window.GetKey(KeyCode.Num6)) { shadowOffset -= Vector3.UnitZ * 2 * window.deltaTime; Console.WriteLine(shadowOffset); } directionalLight.SetShadowProjection(shadow_left, shadow_right, shadow_bottom, shadow_top, shadow_near, shadow_far); animationTimer -= window.deltaTime; if (animationTimer <= 0) { animationIndex++; if (animationIndex >= numFrames) { animationIndex = 0; } animationTimer = 1f / animations[0].Fps; } foreach (string subject in animations[0].KeyFrames.Keys) { if (botMesh[0].HasBone(subject)) { Mesh3.Bone bone = botMesh[0].GetBone(subject); if (animationIndex < animations[0].KeyFrames[subject].Count) { //bone.Position = animations[0].KeyFrames[subject][animationIndex].Position; //bone.Rotation = animations[0].KeyFrames[subject][animationIndex].Rotation; } } } pyramidRotation += new Vector3(0, 10, 0) * window.deltaTime; neckRotation += window.deltaTime; string boneName = "mixamorig:Neck"; if (botMesh[0].HasBone(boneName)) { Mesh3.Bone bone = botMesh[0].GetBone(boneName); bone.Rotation = Quaternion.FromEulerAngles(new Vector3(0, (float)Math.Sin(neckRotation) / 2, 0)); } if (botMesh[1].HasBone(boneName)) { Mesh3.Bone bone = botMesh[1].GetBone(boneName); bone.Rotation = Quaternion.FromEulerAngles(new Vector3(0, (float)Math.Sin(neckRotation) / 2, 0)); } crateRotation += 30 * window.deltaTime; //lightRotation += 10 * window.deltaTime; //directionalLight.UpdateDirection(Utils.EulerRotationToDirection(new Vector3(lightRotation, 180, 0))); window.DisableCullFaces(); // draw shadow map texture window.RenderTo(shadowTexture); window.CullFrontFaces(); floor.DrawShadowMap(directionalLight); pyramid.Scale3 = new Vector3(1, 2, 1); pyramid.EulerRotation3 = pyramidRotation; pyramid.Position3 = new Vector3(-6, 2, 10) + shadowOffset; pyramid.DrawShadowMap(directionalLight); pyramid.Scale3 = new Vector3(1, 2, 1); pyramid.Rotation3 = Vector3.Zero; pyramid.Position3 = new Vector3(-30, 2, 10) + shadowOffset; pyramid.DrawShadowMap(directionalLight); botMesh[0].Position3 = new Vector3(6, 0, 10) + shadowOffset; botMesh[0].DrawShadowMap(directionalLight); botMesh[1].Position3 = new Vector3(6, 0, 10) + shadowOffset; botMesh[1].DrawShadowMap(directionalLight); stormTrooper.Position3 = new Vector3(0, 0, 5) + shadowOffset; stormTrooper.Rotation3 = Vector3.Zero; stormTrooper.DrawShadowMap(directionalLight); stormTrooper.Position3 = new Vector3(-5, 0, 5) + shadowOffset; stormTrooper.Rotation3 = Vector3.Zero; stormTrooper.DrawShadowMap(directionalLight); stormTrooper.Position3 = new Vector3(7, 0, 5) + shadowOffset; stormTrooper.Rotation3 = Utils.LookAt((camera.Position3 - stormTrooper.Position3).Normalized()); stormTrooper.DrawShadowMap(directionalLight); cube.EulerRotation3 = new Vector3(0, crateRotation, 0); cube.Position3 = new Vector3(0, 7, 0) + shadowOffset; cube.DrawShadowMap(directionalLight); cube.EulerRotation3 = Vector3.Zero; cube.Position3 = new Vector3(5, 1, 5) + shadowOffset; cube.DrawShadowMap(directionalLight); foreach (Mesh3 item in tank) { item.Position3 = new Vector3(-10, 0, 20) + shadowOffset; item.DrawShadowMap(directionalLight); } window.DisableCullFaces(); window.RenderTo(null); window.CullBackFaces(); //window.DisableCullFaces(); floor.DrawPhong(floorTexture, directionalLight, new Vector3(0.5f, 0.5f, 0.5f), 0, shadowTexture); pyramid.Scale3 = new Vector3(1, 2, 1); pyramid.EulerRotation3 = pyramidRotation; pyramid.Position3 = new Vector3(-6, 2, 10); pyramid.DrawGouraud(new Vector4(1, 0, 0, 1), directionalLight, shadowTexture); pyramid.Scale3 = new Vector3(1, 2, 1); pyramid.EulerRotation3 = Vector3.Zero; pyramid.Position3 = new Vector3(-30, 2, 10); pyramid.DrawGouraud(new Vector4(1, 1, 0, 1), directionalLight, shadowTexture); botMesh[0].Position3 = new Vector3(6, 0, 10); botMesh[0].DrawGouraud(new Vector4(0, 0.8f, 1, 1), directionalLight, shadowTexture, 0.01f); botMesh[1].Position3 = new Vector3(6, 0, 10); botMesh[1].DrawGouraud(new Vector4(1, 0, 0, 1), directionalLight, shadowTexture, 0.01f); stormTrooper.Position3 = new Vector3(0, 0, 5); stormTrooper.Rotation3 = Vector3.Zero; stormTrooper.DrawGouraud(stormTrooperTexture, directionalLight, shadowTexture); stormTrooper.Position3 = new Vector3(-5, 0, 5); stormTrooper.Rotation3 = Vector3.Zero; stormTrooper.DrawPhong(stormTrooperTexture, directionalLight, new Vector3(0, 0.1f, 0), 0.75f, shadowTexture); stormTrooper.Position3 = new Vector3(7, 0, 5); stormTrooper.Rotation3 = Utils.LookAt((camera.Position3 - stormTrooper.Position3).Normalized()); stormTrooper.DrawCel(stormTrooperTexture, directionalLight, new Vector3(0, 0.1f, 0), 0.75f, shadowTexture); //cube.DrawColor(new Vector4(1, 0, 0, 1)); cube.EulerRotation3 = new Vector3(0, crateRotation, 0); cube.Position3 = new Vector3(0, 7, 0); cube.DrawTexture(crate); if (window.GetKey(KeyCode.Space)) { movingTrooper.Position3 += movingTrooper.Forward * window.deltaTime * 2; } if (window.GetKey(KeyCode.G)) { movingTrooper.EulerRotation3 += new Vector3(0, 90, 0) * window.deltaTime; } if (window.GetKey(KeyCode.H)) { movingTrooper.EulerRotation3 -= new Vector3(0, 90, 0) * window.deltaTime; } movingTrooper.DrawGouraud(new Vector4(1, 0, 0, 1), directionalLight); cube.EulerRotation3 = Vector3.Zero; cube.Position3 = new Vector3(5, 1, 5); cube.DrawGouraud(new Vector4(0, 0, 1, 1), directionalLight, shadowTexture); foreach (Mesh3 item in tank) { item.Position3 = new Vector3(-10, 0, 20); item.DrawPhong(tankDiffuse, directionalLight, new Vector3(0.1f, 0.1f, 0.1f), tankSpecular); } plane.Position3 = new Vector3(-13, 5, 0); plane.EulerRotation3 += Vector3.UnitY * 30 * window.deltaTime; plane.DrawColor(new Vector4(1, 1, 0, 1)); sphere.Position3 = new Vector3(-5, 2, 20); sphere.EulerRotation3 += Vector3.UnitY * 10 * window.deltaTime; sphere.Scale3 = new Vector3(3); sphere.DrawPhong(world, directionalLight, new Vector3(0.2f, 0.2f, 0.2f)); //sphere.DrawColor(new Vector4(1, 0, 0, 1)); logo.DrawTexture(logoAiv); shadow.DrawTexture(shadowTexture); // this ensure postprocessing works window.DisableCullFaces(); plane.Position3 = new Vector3(-10, 5, 0); plane.EulerRotation3 += Vector3.UnitY * 30 * window.deltaTime; plane.DrawColor(new Vector4(0, 1, 1, 1)); window.Update(); } }
private void EnsureOutputBuffers() { if (SharedTexture == null || SharedTexture.Description.Width != ClientWidth || SharedTexture.Description.Height != ClientHeight) { if (SharedTexture != null) { SharedTexture.Dispose(); SharedTexture = null; } var colordesc = new Texture2DDescription { BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = Format.B8G8R8A8_UNorm, Width = ClientWidth, Height = ClientHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, // needed for D3DImage CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; if (_dxRenderView != null) { _dxRenderView.Dispose(); _dxRenderView = null; } SharedTexture = new Texture2D(_dxDevice, colordesc); var descRtv = new RenderTargetViewDescription(); if (colordesc.SampleDescription.Count > 1) { descRtv.Dimension = RenderTargetViewDimension.Texture2DMultisampled; } else { descRtv.Dimension = RenderTargetViewDimension.Texture2D; } _dxRenderView = new RenderTargetView(_dxDevice, SharedTexture, descRtv); } if (DepthTexture == null || DepthTexture.Description.Width != ClientWidth || DepthTexture.Description.Height != ClientHeight) { if (DepthTexture != null) { DepthTexture.Dispose(); DepthTexture = null; } var depthDesc = new Texture2DDescription { BindFlags = BindFlags.DepthStencil, Format = Format.D32_Float_S8X24_UInt, Width = ClientWidth, Height = ClientHeight, MipLevels = 1, SampleDescription = new SampleDescription(1, 0), // not using multisampling Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.None, CpuAccessFlags = CpuAccessFlags.None, ArraySize = 1 }; // create depth texture DepthTexture = new Texture2D(_dxDevice, depthDesc); if (_dxDepthStencilView != null) { _dxDepthStencilView.Dispose(); _dxDepthStencilView = null; } var descDsv = new DepthStencilViewDescription(); descDsv.Format = depthDesc.Format; if (depthDesc.SampleDescription.Count > 1) { descDsv.Dimension = DepthStencilViewDimension.Texture2DMultisampled; } else { descDsv.Dimension = DepthStencilViewDimension.Texture2D; } descDsv.MipSlice = 0; // create depth/stencil view _dxDepthStencilView = new DepthStencilView(_dxDevice, DepthTexture, descDsv); } }
void DrawFrame() { var renderPassDescriptor = new GpuRenderPassDescriptor(new GpuRenderPassColorAttachment[] { new GpuRenderPassColorAttachment(SwapChain.GetCurrentTexture().CreateView(), new GpuColorDict { R = 0, G = 0, B = 0, A = 0 }) }); renderPassDescriptor.DepthStencilAttachment = new GpuRenderPassDepthStencilAttachment(DepthTexture.CreateView(), 1.0f, GpuStoreOp.Store, null, GpuStoreOp.Store); var commandEncoder = Device.CreateCommandEncoder(); var cpass = commandEncoder.BeginComputePass(); cpass.SetPipeline(ComputePipeline); cpass.SetBindGroup(0, ParticleBindGroups[T % 2]); cpass.Dispatch(NumParticles, 1, 1); cpass.EndPass(); var rpass = commandEncoder.BeginRenderPass(renderPassDescriptor); rpass.SetPipeline(RenderPipeline); rpass.SetVertexBuffer(0, ParticleBuffers[(T + 1) % 2], 0, ParticleBuffers[(T + 1) % 2].Size); rpass.SetVertexBuffer(1, VerticesBuffer, 0, VerticesBuffer.Size); rpass.Draw(3, NumParticles, 0, 0); rpass.EndPass(); Device.DefaultQueue.Sumit(new GpuCommandBuffer[] { commandEncoder.Finish() }); SwapChain.Present(); ++T; }
public void Render(Camera camera, DepthTexture depthMap, Matrix4 lightMatrix, Vector2 screenDimensions, bool drawShadow = false) { if (!Checked) { return; } Shader shader; // 3DS MBN shader = OpenTKSharedResources.shaders["Mbn"]; shader.UseProgram(); SetMbnUniforms(camera, shader); if (MeleeData != null) { MeleeData.Render(camera); } if (Bch != null) { foreach (BCH_Model mo in Bch.Models.Nodes) { mo.Render(camera.MvpMatrix); } } if (DatMelee != null && OpenTKSharedResources.shaders["Dat"].LinkStatusIsOk) { DatMelee.Render(camera.MvpMatrix); } LightColor diffuseColor = Runtime.lightSetParam.characterDiffuse.diffuseColor; LightColor ambientColor = Runtime.lightSetParam.characterDiffuse.ambientColor; if (Kcl != null) { shader = OpenTKSharedResources.shaders["KCL"]; if (!shader.LinkStatusIsOk) { return; } shader.UseProgram(); shader.SetVector3("difLightColor", diffuseColor.R, diffuseColor.G, diffuseColor.B); shader.SetVector3("ambLightColor", ambientColor.R, ambientColor.G, ambientColor.B); Kcl.Render(camera.MvpMatrix); } if (Bfres != null) { Bfres.Render(camera, Runtime.drawNudColorIdPass); } if (NUD != null && OpenTKSharedResources.shaders["Nud"].LinkStatusIsOk && OpenTKSharedResources.shaders["NudDebug"].LinkStatusIsOk) { // Choose the appropriate shader. if (drawShadow) { shader = OpenTKSharedResources.shaders["Shadow"]; } else if (Runtime.renderType != Runtime.RenderTypes.Shaded) { shader = OpenTKSharedResources.shaders["NudDebug"]; } else { shader = OpenTKSharedResources.shaders["Nud"]; } shader.UseProgram(); // Matrices. Matrix4 lightMatrixRef = lightMatrix; shader.SetMatrix4x4("lightMatrix", ref lightMatrixRef); SetCameraMatrixUniforms(camera, shader); SetRenderSettingsUniforms(shader); SetLightingUniforms(shader, camera); shader.SetInt("renderType", (int)Runtime.renderType); shader.SetInt("debugOption", (int)Runtime.uvChannel); shader.SetBoolToInt("drawShadow", Runtime.drawModelShadow); shader.SetTexture("depthMap", depthMap, 14); SetElapsedDirectUvTime(shader); NUD.Render(VBN, camera, drawShadow, Runtime.drawNudColorIdPass); } }
public void Render(Camera cam, bool orthographic, bool backfaceCulling, float scale, DepthTexture texture, Position3D modelPosition) { //multi threaded Parallel.ForEach(frames[currentFrame], triangle => { triangle.Render(cam, orthographic, backfaceCulling, scale, texture, modelPosition); }); }
private void DrawTriangle(TriangleData tData, DepthTexture modelTexture, DepthTexture screenTexture) { //set array boundes int left = 0; int right = 0; int top = 0; int bottom = 0; if (!OnScreen(tData, screenTexture, ref left, ref right, ref top, ref bottom)) { return; //cull out triangles not in screen space } //setup values for the draw loop int x1 = (int)tData.Vertex1.x; int y1 = (int)tData.Vertex1.y; float z1 = tData.Vertex1.z; int x2 = (int)tData.Vertex2.x; int y2 = (int)tData.Vertex2.y; float z2 = tData.Vertex2.z; int x3 = (int)tData.Vertex3.x; int y3 = (int)tData.Vertex3.y; float z3 = tData.Vertex3.z; //precomputed values int totalArea = Math.Abs(Area(tData)); if (totalArea < 1) { return; //filter subpixel triangles } //precompute area pt1 int y2my3 = y2 - y3; int y3my1 = y3 - y1; int y1my2 = y1 - y2; //computing uvs float uvX1 = tData.Uv1.u * modelTexture.Width; float uvX2 = tData.Uv2.u * modelTexture.Width; float uvX3 = tData.Uv3.u * modelTexture.Width; float uvY1 = tData.Uv1.v * modelTexture.Height; float uvY2 = tData.Uv2.v * modelTexture.Height; float uvY3 = tData.Uv3.v * modelTexture.Height; //draw loop for (int y = top; y < bottom; y++) { //precompute area pt2 int apre = x2 * (y3 - y) + x3 * (y - y2); int bpre = x1 * (y - y3) + x3 * (y1 - y); int cpre = x1 * (y2 - y) + x2 * (y - y1); bool inTriangle = false; //right side exit scanline optimization for (int x = left; x < right; x++) { //find out what points are in the triangle with the precompute area int ba = Math.Abs(x * y2my3 + apre); int bb = Math.Abs(x * y3my1 + bpre); int bc = Math.Abs(x * y1my2 + cpre); if (ba + bb + bc <= totalArea) { inTriangle = true; //interpolate pixel amounts float a = ba / (float)totalArea; float b = bb / (float)totalArea; float c = 1 - (a + b); //get the uv/depth int u = (int)(uvX1 * a + uvX2 * b + uvX3 * c); int v = (int)(uvY1 * a + uvY2 * b + uvY3 * c); float depth = z1 * a + z2 * b + z3 * c; //set the pixel if passes depth test screenTexture.SetPixelDepthTest(x, y, modelTexture.GetPixel(u, v), depth); } else if (inTriangle) { break; //end scanline if exiting right side of the triangle } } } }
public void DepthFormat() { // Doesn't throw an exception. DepthTexture texture = new DepthTexture(1, 1, PixelInternalFormat.DepthComponent); }
IEnumerator SendCreateView() { if (m_IsCoroutineStarted) { yield break; } m_IsCoroutineStarted = true; if (string.IsNullOrEmpty(Page)) { throw new System.ApplicationException("The Page of a view must not be null or empty."); } if (ResourceHandlerFactoryFunc != null) { m_ResourceHandler = ResourceHandlerFactoryFunc(); } if (m_ResourceHandler == null) { Debug.LogWarning("Unable to create file handler using factory function! Falling back to default handler."); m_ResourceHandler = new UnityGTResourceHandler(); } ViewLoadPolicy policy = ViewLoadPolicy.VLP_UseCacheOrLoad; #if UNITY_EDITOR m_ResourcesInUse = new HashSet <string>(); m_ResourceHandler = new UnityGTResourceHandlerDecorator(m_ResourceHandler, this); if (AutoRefresh) { policy = ViewLoadPolicy.VLP_IgnoreCache; } #endif var viewInfo = new ViewInfo(); viewInfo.Width = (uint)this.m_Width; viewInfo.Height = (uint)this.m_Height; viewInfo.IsTransparent = this.m_IsTransparent; viewInfo.ViewListenerInstance = m_Listener; viewInfo.ResourceHandlerInstance = m_ResourceHandler; viewInfo.ClickThroughAlphaThreshold = ClickThroughThreshold; if (string.IsNullOrEmpty(InitialScript)) { View = m_UISystem.UISystem.CreateView(viewInfo, Page, policy); } else { View = m_UISystem.UISystem.CreateView(viewInfo, Page, InitialScript, policy); } RecreateViewTexture(m_Width, m_Height); View.SetNewRenderTarget(ViewTexture.GetNativeTexturePtr(), DepthTexture.GetNativeTexturePtr(), (uint)m_Width, (uint)m_Height, 1); CoherentUIGTRenderEvents.SendRenderEvent(CoherentRenderEventType.CreateViewRenderer, View.GetId()); while (ViewRenderer == null) { ViewRenderer = View.GetViewRenderer(); yield return(null); } AddViewRendererComponent(); }
protected virtual void CreateShadowMap() { Program.Settings.GetShadowMapSize(out int width, out int height); ShadowMap = new DepthTexture(width, height); }
public void Render(Camera cam, bool orthographic, bool backfaceCulling, float scale, DepthTexture texture, Position3D modelPosition) { //make a copy, don't touch original points to keep accuracy TriangleData tDataTmp = tData; //move the vertices in local space ScaleVertices(ref tDataTmp, scale); RotateVertices(ref tDataTmp, modelPosition); TranslateVerticesLocal(ref tDataTmp, modelPosition); //move the vertices in world space TranslateVerticesWorld(ref tDataTmp, cam.CameraPosition); RotateVertices(ref tDataTmp, cam.CameraPosition); //culling/drawing/perspective if (InZRange(tDataTmp, cam.FarClippingPlane)) { if (!orthographic) { PerspectiveVertices(ref tDataTmp, cam.ScreenDepthTexture, cam.CalculatedFov); } if (backfaceCulling && NotFacingCamera(tDataTmp)) { return; } DrawTriangle(tDataTmp, texture, cam.ScreenDepthTexture); } }
PassData.DepthBuffer = PassBuilder.UseDepthBuffer(DepthTexture, EDepthAccess.Read);