public void RenderOnScreen(Microsoft.DirectX.Direct3D.Device d3dDevice, float elapsedTime) { d3dDevice.BeginScene(); //Cargamos para renderizar el unico modelo que tenemos, un Quad que ocupa toda la pantalla, con la textura de todo lo dibujado antes d3dDevice.VertexFormat = CustomVertex.PositionTextured.Format; d3dDevice.SetStreamSource(0, screenQuadVB, 0); //Ver si el efecto de alarma esta activado, configurar Technique del shader segun corresponda //Cargamos parametros en el shader de Post-Procesado this.postProcessMerge.SetValue("escenaTextura", this.escena); if (cant_pasadas == 0) { this.postProcessMerge.SetValue("propulsoresTextura", this.propulsores); } else { this.postProcessMerge.SetValue("propulsoresTextura", this.propulsoresBlurAux2); } this.postProcessMerge.Technique = "TechniqueMerge"; //Limiamos la pantalla y ejecutamos el render del shader d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); this.postProcessMerge.Begin(FX.None); this.postProcessMerge.BeginPass(0); d3dDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); this.postProcessMerge.EndPass(); this.postProcessMerge.End(); //DrawText.drawText("Desfase: " + TGCVector3.PrintVector3(desfase), 0, 500, Color.White); //Terminamos el renderizado de la escena RenderFPS(); RenderAxis(); d3dDevice.EndScene(); d3dDevice.Present(); }
void init() { Microsoft.DirectX.Direct3D.PresentParameters pps = new Microsoft.DirectX.Direct3D.PresentParameters(); pps.SwapEffect = Microsoft.DirectX.Direct3D.SwapEffect.Discard; pps.Windowed = true; pps.BackBufferCount = 1; pps.PresentationInterval = PresentInterval.One; pps.BackBufferFormat = Format.A8R8G8B8; if (GameWindow == null) { //pps.BackBufferWidth = GameControl.Width; //pps.BackBufferHeight = GameControl.Height; D3DDev = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, GameControl, Microsoft.DirectX.Direct3D.CreateFlags.SoftwareVertexProcessing, pps); } else { //pps.BackBufferWidth = GameWindow.Width; //pps.BackBufferHeight = GameWindow.Height; D3DDev = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, GameWindow, Microsoft.DirectX.Direct3D.CreateFlags.SoftwareVertexProcessing, pps); } D3DDev.VertexFormat = CustomVertex.PositionColoredTextured.Format; //D3DDev.ShowCursor(false); DSoundDev = new Microsoft.DirectX.DirectSound.Device(); if (GameWindow == null) { DSoundDev.SetCooperativeLevel(GameControl, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); } else { DSoundDev.SetCooperativeLevel(GameWindow, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal); } DKeyboardDev = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Keyboard); DKeyboardDev.Acquire(); DMouseDev = new Microsoft.DirectX.DirectInput.Device(Microsoft.DirectX.DirectInput.SystemGuid.Mouse); DMouseDev.Acquire(); VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColoredTextured), 6, D3DDev, Usage.None, CustomVertex.PositionColoredTextured.Format, Pool.Managed); D3DDev.SetStreamSource(0, VertexBuffer, 0); D3DDev.TextureState[0].AlphaOperation = TextureOperation.Modulate; Sprite = new Sprite(D3DDev); FontDescription fd = new FontDescription(); fd.FaceName = "新宋体"; fd.Height = -12; Font2D = new Microsoft.DirectX.Direct3D.Font(D3DDev, fd); Font3D = new System.Drawing.Font("新宋体", 12); }
static public void LineTo(ref Microsoft.DirectX.Direct3D.Device device, Vector3 v1, Vector3 v2, Color color) { // Line #1 verts[0].Position = v1; verts[0].Color = color.ToArgb(); verts[1].Position = v2; verts[1].Color = color.ToArgb(); lineList_VB.SetData(verts, 0, LockFlags.None); device.VertexFormat = CustomVertex.PositionColored.Format; device.RenderState.Lighting = false; device.RenderState.PointSize = 4.0f; device.SetStreamSource(0, lineList_VB, 0); device.DrawPrimitives(PrimitiveType.LineList, 0, 2); device.RenderState.Lighting = true; }
public void Render() { if (DXDevice == null) { return; } DXDevice.Clear(Direct3D.ClearFlags.Target, Color.Gray, 1, 0); DXDevice.BeginScene(); DXDevice.VertexFormat = Direct3D.CustomVertex.PositionColored.Format; DXDevice.SetStreamSource(0, vb, 0); DXDevice.Indices = ib; DXDevice.Transform.World = Matrix.RotationY(angle); DXDevice.DrawIndexedPrimitives(Direct3D.PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indexes.Length / 3); DXDevice.EndScene(); DXDevice.Present(); angle += 0.02f; }
public void PostRender(bool invisibilidadActivada, float Tiempo) { pSurf.Dispose(); if (invisibilidadActivada) { D3DDevice.DepthStencilSurface = pOldDS; D3DDevice.SetRenderTarget(0, pOldRT); Invisibilidad.Technique = "PostProcess"; Invisibilidad.SetValue("time", Tiempo); D3DDevice.VertexFormat = CustomVertex.PositionTextured.Format; D3DDevice.SetStreamSource(0, g_pVBV3D, 0); Invisibilidad.SetValue("g_RenderTarget", g_pRenderTarget); D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); Invisibilidad.Begin(FX.None); Invisibilidad.BeginPass(0); D3DDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); Invisibilidad.EndPass(); Invisibilidad.End(); } }
/// <summary> /// Se toma todo lo dibujado antes, que se guardo en una textura, y se le aplica un shader para distorsionar la imagen /// </summary> private void drawPostProcess(Microsoft.DirectX.Direct3D.Device d3dDevice) { //Arrancamos la escena d3dDevice.BeginScene(); //Cargamos para renderizar el unico modelo que tenemos, un Quad que ocupa toda la pantalla, con la textura de todo lo dibujado antes d3dDevice.VertexFormat = CustomVertex.PositionTextured.Format; d3dDevice.SetStreamSource(0, screenQuadVB, 0); //Ver si el efecto de oscurecer esta activado, configurar Technique del shader segun corresponda effect.Technique = "RainTechnique"; if (!presentacion && lloviendo) { effect.Technique = "RainTechnique"; } else { effect.Technique = "DefaultTechnique"; } //Cargamos parametros en el shader de Post-Procesado effect.SetValue("render_target2D", renderTarget2D); effect.SetValue("textura_alarma", lluviaTexture.D3dTexture); effect.SetValue("time", this.ElapsedTime); //Limiamos la pantalla y ejecutamos el render del shader d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); effect.Begin(FX.None); effect.BeginPass(0); d3dDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); effect.EndPass(); effect.End(); //Terminamos el renderizado de la escena d3dDevice.EndScene(); d3dDevice.Present(); }
public void Draw(DX.Matrix transform, int pickValue = 0) { D3D.Device device = Renderer.current.device; device.VertexDeclaration = Renderer.current.vtxDeclaration; device.SetStreamSource(0, vertexBuffer, 0); Renderer.current.SetValues(usedEffect, transform); usedEffect.SetValue(Renderer.EffectHandles.PickColor, Renderer.pickIndexToColor(pickValue)); foreach (Subset sub in subsets) { float shiftTValue = sub.shiftT >= 11 ? (int)1 << (0x10 - sub.shiftT) : 1.0f / (1 << sub.shiftT); float shiftSValue = sub.shiftS >= 11 ? (int)1 << (0x10 - sub.shiftS) : 1.0f / (1 << sub.shiftS); usedEffect.SetValue(Renderer.EffectHandles.ShiftMul, new DX.Vector4(shiftTValue / (float)sub.texture.width, shiftSValue / (float)sub.texture.height, 0, 0)); usedEffect.SetValue(Renderer.EffectHandles.Texture0, sub.texture0); usedEffect.SetValue(Renderer.EffectHandles.Texture1, sub.texture1); usedEffect.Begin(D3D.FX.None); usedEffect.BeginPass(0); device.Indices = sub.indexBuffer; device.DrawIndexedPrimitives(D3D.PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, sub.indices.Length / 3); usedEffect.EndPass(); usedEffect.End(); } }
/// <summary> /// The load mesh. /// </summary> /// <remarks></remarks> public void LoadMesh() { dxcoll = new DirectXCollision(ref device, ref coll); for (int mc = 0; mc < coll.Meshes.Count; mc++) { for (int i = 0; i < coll.Meshes[mc].SurfaceData.Length; i++) { // for (int i = 0; i < 1; i++) Mesh m = new Mesh( coll.Meshes[mc].SurfaceData.Length, coll.Meshes[mc].Vertices.Length, MeshFlags.Managed, HaloVertex.FVF, device); VertexBuffer vb = m.VertexBuffer; HaloVertex[] tempv = new HaloVertex[coll.Meshes[mc].Vertices.Length]; for (int x = 0; x < coll.Meshes[mc].Vertices.Length; x++) { tempv[x].Position = coll.Meshes[mc].Vertices[x]; } vb.SetData(tempv, 0, LockFlags.None); vb.Unlock(); short[] b = new short[(coll.Meshes[mc].SurfaceData[i].Vertices.Count - 2) * 3]; for (int ii = 0; ii < 3; ii++) { b[ii] = (short)coll.Meshes[mc].SurfaceData[i].Vertices[ii]; } for (int ii = 1; ii < coll.Meshes[mc].SurfaceData[i].Vertices.Count - 2; ii++) { b[ii * 3] = b[0]; b[ii * 3 + 1] = b[ii * 3 - 1]; b[ii * 3 + 2] = (short)coll.Meshes[mc].SurfaceData[i].Vertices[ii + 2]; } IndexBuffer ibx = m.IndexBuffer; ibx.SetData(b, 0, LockFlags.None); mList.Add(m); } normalVertices = new CustomVertex.PositionColored[coll.Meshes[mc].Normals.Length * 2]; for (int i = 0; i < coll.Meshes[mc].Normals.Length; i++) { normalVertices[i * 2 + 0].Color = Color.Yellow.ToArgb(); normalVertices[i * 2 + 0].Position = new Vector3( coll.Meshes[mc].Normals[i].X * coll.Meshes[mc].Normals[i].W, coll.Meshes[mc].Normals[i].Y * coll.Meshes[mc].Normals[i].W, coll.Meshes[mc].Normals[i].Z * coll.Meshes[mc].Normals[i].W); normalVertices[i * 2 + 1].Color = Color.White.ToArgb(); normalVertices[i * 2 + 1].Position = new Vector3( coll.Meshes[mc].Normals[i].X, coll.Meshes[mc].Normals[i].Y, coll.Meshes[mc].Normals[i].Z); // normalVertices[i * 3 + 2].Position = new Vector4(1, 1, 1, 1.0f); // normalVertices[i * 3 + 2].Color = Color.White.ToArgb(); } } Vb = new VertexBuffer( typeof(CustomVertex.PositionColored), normalVertices.Length, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default); Vb.SetData(normalVertices, 0, LockFlags.None); device.SetStreamSource(0, Vb, 0); }
/// <summary> /// Draw some text on the screen /// </summary> public void DrawText(float xpos, float ypos, Color color, string text, RenderFlags flags) { if (text == null) { return; } // Setup renderstate savedStateBlock.Capture(); drawTextStateBlock.Apply(); device.SetTexture(0, fontTexture); device.VertexFormat = CustomVertex.TransformedColoredTextured.Format; device.PixelShader = null; device.SetStreamSource(0, vertexBuffer, 0); // Set filter states if ((flags & RenderFlags.Filtered) != 0) { samplerState0.MinFilter = TextureFilter.Linear; samplerState0.MagFilter = TextureFilter.Linear; } // Adjust for character spacing xpos -= spacingPerChar; float fStartX = xpos; // Fill vertex buffer int iv = 0; int dwNumTriangles = 0; foreach (char c in text) { if (c == '\n') { xpos = fStartX; ypos += (textureCoords[0, 3] - textureCoords[0, 1]) * textureHeight; } if ((c - 32) < 0 || (c - 32) >= 128 - 32) { continue; } float tx1 = textureCoords[c - 32, 0]; float ty1 = textureCoords[c - 32, 1]; float tx2 = textureCoords[c - 32, 2]; float ty2 = textureCoords[c - 32, 3]; float w = (tx2 - tx1) * textureWidth / textureScale; float h = (ty2 - ty1) * textureHeight / textureScale; int intColor = color.ToArgb(); if (c != ' ') { fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + 0 - 0.5f, ypos + h - 0.5f, 0.9f, 1.0f), intColor, tx1, ty2); fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + 0 - 0.5f, ypos + 0 - 0.5f, 0.9f, 1.0f), intColor, tx1, ty1); fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + w - 0.5f, ypos + h - 0.5f, 0.9f, 1.0f), intColor, tx2, ty2); fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + w - 0.5f, ypos + 0 - 0.5f, 0.9f, 1.0f), intColor, tx2, ty1); fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + w - 0.5f, ypos + h - 0.5f, 0.9f, 1.0f), intColor, tx2, ty2); fontVertices[iv++] = new CustomVertex.TransformedColoredTextured(new Vector4(xpos + 0 - 0.5f, ypos + 0 - 0.5f, 0.9f, 1.0f), intColor, tx1, ty1); dwNumTriangles += 2; if (dwNumTriangles * 3 > (MaxNumfontVertices - 6)) { // Set the data for the vertexbuffer vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard); device.DrawPrimitives(PrimitiveType.TriangleList, 0, dwNumTriangles); dwNumTriangles = 0; iv = 0; } } xpos += w - (2 * spacingPerChar); } // Set the data for the vertex buffer vertexBuffer.SetData(fontVertices, 0, LockFlags.Discard); if (dwNumTriangles > 0) { device.DrawPrimitives(PrimitiveType.TriangleList, 0, dwNumTriangles); } // Restore the modified renderstates savedStateBlock.Apply(); }
protected void Paint() { float xOffset = 0; float yOffset = 0; int i = 0; currentTime = DateTime.Now; TimeSpan elapsedTimeSpan = currentTime.Subtract(lastTime); lastTime = currentTime; fpsFrameCount += 1; fpsMillisecondsCount += elapsedTimeSpan.Milliseconds; if (fpsMillisecondsCount >= 1000) { fpsRate = fpsFrameCount / (fpsMillisecondsCount / 1000); fpsMillisecondsCount = 0; fpsFrameCount = 0; } // motion TimeSpan runTimeSpan = currentTime.Subtract(runTime); device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0); device.BeginScene(); for (i = 0; i < rails.Length; i++) { device.SetStreamSource(0, vb_RedRail[i], 0); device.Transform.World = Matrix.Translation((0) + LeftRightOffset - (1 * 7), (0) + UpDownOffset, zoom); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 100, 0, indices.Length / 3); } text.DrawText(null, string.Format("fps: {0}", fpsRate), new Point(10, 20), Color.White); text.DrawText(null, string.Format("scan rate: {0} ms", timerDelay), new Point(10, 40), Color.White); text.DrawText(null, string.Format("pluse per sec: {0}", PulseRate_CountPerSec), new Point(10, 60), Color.White); text.DrawText(null, string.Format("link per sec: {0}", LinkRate_CountPerSec), new Point(10, 80), Color.White); for (i = 0; i < links.Length; i++) { xOffset = LeftRightOffset + links[i].center_loc_x;// -(i * (cGripperStepPluse * 2)); yOffset = UpDownOffset + links[i].center_loc_y; if (i == 0) { device.SetStreamSource(0, vb_Link_Blue, 0); } else if (i == 99) { device.SetStreamSource(0, vb_Link_Green, 0); } else if (i == 199) { device.SetStreamSource(0, vb_Link_Orange, 0); } //else if (i == 3) //{ // device.SetStreamSource(0, vb_Link_Orange, 0); //} else { device.SetStreamSource(0, vb_Link_White, 0); } // handle orientation if (rails[links[i].currentRail].directionOfTravel == (int)ClassDef.clsRail.direction.right) { device.Transform.World = Matrix.Translation(xOffset, yOffset, zoom); } else if (rails[links[i].currentRail].directionOfTravel == (int)ClassDef.clsRail.direction.left) { device.Transform.World = Matrix.Translation(xOffset, yOffset, zoom); } else if (rails[links[i].currentRail].directionOfTravel == (int)ClassDef.clsRail.direction.up) { // device.Transform.World *= Matrix.RotationZ(90f); device.Transform.World = Matrix.Translation(xOffset, yOffset, zoom); } else if (rails[links[i].currentRail].directionOfTravel == (int)ClassDef.clsRail.direction.down) { //device.Transform.World *= Matrix.RotationZ(90f); device.Transform.World = Matrix.RotationZ(1.5708f) * Matrix.Translation(xOffset, yOffset, zoom); } device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 100, 0, indices.Length / 3); } device.EndScene(); device.Present(); this.Invalidate(); ReadKeyboard(); }