public override void Write <T>(T[] vertices) { var stream = mBuffer.Lock(0, 0, 0); stream.WriteRange(vertices); mBuffer.Unlock(); }
/// <summary> /// In a derived class, implements logic to fill the buffer with vertex data. /// </summary> /// <param name="vertices">The vertex data.</param> protected override void FillBuffer(List <T> vertices) { DataStream stream = buffer.Lock(0, 0, D3D.LockFlags.Discard); try { stream.WriteRange(vertices.ToArray()); } finally { buffer.Unlock(); } }
private void AddVertexDataNative <VertexType>(VertexType[] vertices, int numberOfVertices) where VertexType : struct { if (nativeVertexBuffer.Disposed) { CreateNativeVertexBuffer(); } var stream = nativeVertexBuffer.Lock(totalVertexOffsetInBytes, numberOfVertices * vertexSize, LockFlags.None); stream.WriteRange(vertices, 0, numberOfVertices); nativeVertexBuffer.Unlock(); }
protected override void RenderBuffer() { if (!hasloaded) { internbuffer = new SlimDX.Direct3D9.VertexBuffer(engine.graphicsDevice, Vertex.getSize() * verts.Length, Usage.None, VertexFormat.None, Pool.Managed); List<Vertex> mtex = new List<Vertex>(); for (int i = 0; i < verts.Length; i++) { mtex.Add(new Vertex(verts[i], norms[i], texas[i])); } DataStream mstr = internbuffer.Lock(0, 0, LockFlags.None); mstr.WriteRange(mtex.ToArray()); internbuffer.Unlock(); List<VertexElement> elements = new List<VertexElement>(); elements.Add(new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0)); elements.Add(new VertexElement(0, sizeof(float)*3, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0)); elements.Add(new VertexElement(0, sizeof(float)*3+sizeof(float)*3, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0)); elements.Add(VertexElement.VertexDeclarationEnd); declaration = new VertexDeclaration(engine.graphicsDevice, elements.ToArray()); hasloaded = true; } engine.graphicsDevice.VertexDeclaration = declaration; engine.graphicsDevice.SetStreamSource(0, internbuffer, 0, Vertex.getSize()); engine.effect.Technique = engine.effect.GetTechnique("technique0"); EffectHandle lightdirection = engine.effect.GetParameter(null, "LightDirection"); EffectHandle lightenabled = engine.effect.GetParameter(null, "LightEnabled"); engine.effect.SetValue<bool>(lightenabled, engine.LightingEnabled); engine.effect.SetValue<Vector3>(lightdirection, new Vector3(0, 1.4f, -2f)); EffectHandle texhandle = engine.effect.GetParameter(null, "shaderTexture"); engine.effect.SetTexture(texhandle, engine.texture); // Matrix mtrix = Matrix.Identity*Matrix.Translation(new Vector3(2,.5f,-9.4f))*Matrix.RotationY(rotation); Matrix worldrotation; bool mtr = false; if (!engine.rtarget) { worldrotation = Matrix.Transformation(new Vector3(), Quaternion.Identity, new Vector3(1, 1, 1), new Vector3(0, 0, 0), Quaternion.RotationMatrix(Matrix.RotationAxis(Vector3.UnitY,MathHelpers.DegreesToRadians(engine.worldRotation.Y))*Matrix.RotationAxis(Vector3.UnitX,MathHelpers.DegreesToRadians(engine.worldRotation.X))*Matrix.RotationAxis(Vector3.UnitZ, MathHelpers.DegreesToRadians(engine.worldRotation.Z))), new Vector3(0, 0, 0));//Matrix.RotationX(engine.worldRotation.X) * Matrix.RotationY(engine.worldRotation.Y) * Matrix.RotationZ(engine.worldRotation.Z); } else { worldrotation = Matrix.Transformation(new Vector3(), Quaternion.Identity, new Vector3(1, 1, 1), new Vector3(0, 0, 0), Quaternion.RotationMatrix(Matrix.RotationAxis(Vector3.UnitY,MathHelpers.DegreesToRadians(RenderTargetChange.mchange.camrot.Y))*Matrix.RotationAxis(Vector3.UnitX, MathHelpers.DegreesToRadians(RenderTargetChange.mchange.camrot.X))*Matrix.RotationAxis(Vector3.UnitZ, MathHelpers.DegreesToRadians(RenderTargetChange.mchange.camrot.Z))), new Vector3(0, 0, 0));//Matrix.RotationX(engine.worldRotation.X) * Matrix.RotationY(engine.worldRotation.Y) * Matrix.RotationZ(engine.worldRotation.Z); mtr = true; } Matrix worldmatrix = Matrix.LookAtLH(new Vector3(0, 0, 1), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); Matrix projectionmatrix = worldrotation*Matrix.PerspectiveFovLH(MathHelpers.DegreesToRadians(45), (float)engine.graphicsDevice.Viewport.Width / (float)engine.graphicsDevice.Viewport.Height, 1, 5000000); Matrix viewmatrix = Matrix.RotationX(rotation.X) * Matrix.RotationY(rotation.Y) * Matrix.RotationZ(rotation.Z); Matrix mtrix; if (!sta) { if (mtr) { mtrix = viewmatrix * worldmatrix * Matrix.Translation(new Vector3(Position.X - RenderTargetChange.mchange.campos.X, Position.Y - RenderTargetChange.mchange.campos.Y, Position.Z - RenderTargetChange.mchange.campos.Z)) * projectionmatrix; } else { mtrix = viewmatrix * worldmatrix * Matrix.Translation(new Vector3(Position.X - engine.cameraPosition.X, Position.Y - engine.cameraPosition.Y, Position.Z - engine.cameraPosition.Z)) * projectionmatrix; } } else { mtrix = viewmatrix * worldmatrix * Matrix.Translation(new Vector3(Position.X, Position.Y, Position.Z)) * projectionmatrix; } EffectHandle mathandle = engine.effect.GetParameter(null,"WorldViewProj"); engine.effect.SetValue<Matrix>(mathandle, mtrix); if (!depthtest) { engine.graphicsDevice.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer); } engine.effect.Begin(); engine.effect.BeginPass(0); if (engine.PMode == PrimitiveMode.TriangleList) { engine.graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, verts.Length / 3); } else { engine.graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, verts.Length); } engine.effect.EndPass(); engine.effect.End(); if (!depthtest) { engine.graphicsDevice.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer); } }