private void CreateVertexBuffer() { _vertexCount = _xRes * _yRes; var vertexStream = new DataStream(_vertexCount * PointVertex.SizeOf, true, true); // store pixel coordinates in each vertex for (short y = 0; y < _yRes; y++) { for (short x = 0; x < _xRes; x++) { var pt = new PointVertex(x, y); vertexStream.Write(pt); } } vertexStream.Position = 0; // create vertex buffer _vertexBuffer = new Buffer(_dxDevice, vertexStream, _vertexCount * PointVertex.SizeOf, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); vertexStream.Close(); }
protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { ContentManager cm = PrimalDevistation.Instance.CM; GraphicsDevice gd = GraphicsDevice; Random r = new Random(); // Load the standard point sprite render effect _renderEffect = cm.Load <Effect>(@"Shaders/PointSpriteRender"); _texture = cm.Load <Texture2D>(@"Sprites/particle2x2"); // Create the vertex buffer _vertexBuffer = new VertexBuffer(gd, _maxParticles * PointVertex.Size, ResourceUsage.Points | ResourceUsage.WriteOnly, ResourceManagementMode.Automatic); // Create the declaration _vertexDeclaration = new VertexDeclaration(gd, PointVertex.Elements); // init the points _pointVertices = new PointVertex[_maxParticles]; _particles = new cCPUParticle[_maxParticles]; for (int i = 0; i < _maxParticles; i++) { _pointVertices[i] = new PointVertex(); _pointVertices[i].position = new Vector3(-100, -100, 0); _pointVertices[i].color = _colors[r.Next(0, _colors.Length)].ToVector4(); _pointVertices[i].pointSize = 2; _particles[i] = new cCPUParticle(); } // Create a matrix to convert from pixel coordinates to clip space // So, before transformation, the screen boundaries go from 0 to (say) 640 horizontally, // and 0 to (say) 480 vertically. After transformation, they go from -1 to 1 horizontally, // and -1 to 1 vertically. Note in pixel coordinates, increasing Y values are further down // the screen, while in clip space, increasing Y values are further up the screen -- so there // is a Y scaling factor of -1. float viewportWidth = (float)gd.Viewport.Width; float viewportHeight = (float)gd.Viewport.Height; float scaleX = 1.0f / (viewportWidth / 2); float scaleY = 1.0f / (viewportHeight / 2); _proj = Matrix.CreateScale(scaleX, scaleY, 1) * Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(-1, 1, 0); } }
private void FillBlobVB(Matrix pmatWorldView, Matrix pmatProjection) { ScreenVertex[] pBlobVertex = (ScreenVertex[])pBlobVB.Lock(0, LockFlags.None); PointVertex[] blobPos = new PointVertex[blobPoints.Length]; for (int i = 0; i < blobPoints.Length; i++) { //transform point to camera space Vector4 blobPosCamera = Vector3.Transform(blobPoints[i].pos, pmatWorldView); blobPos[i] = blobPoints[i]; blobPos[i].pos.X = blobPosCamera.X; blobPos[i].pos.Y = blobPosCamera.Y; blobPos[i].pos.Z = blobPosCamera.Z; } int posCount = 0; for (int i = 0; i < blobPoints.Length; i++) { Vector4 blobScreenPos; // For calculating billboarding Vector4 billOfs = new Vector4(blobPos[i].size, blobPos[i].size, blobPos[i].pos.Z, 1); Vector4 billOfsScreen; // Transform to screenspace blobScreenPos = Vector3.Transform(blobPos[i].pos, pmatProjection); billOfsScreen = Vector4.Transform(billOfs, pmatProjection); // Project blobScreenPos = Vector4.Scale(blobScreenPos, 1.0f / blobScreenPos.W); billOfsScreen = Vector4.Scale(billOfsScreen, 1.0f / billOfsScreen.W); Vector2[] vTexCoords = new Vector2[] { new Vector2(0.0f, 0.0f), new Vector2(1.0f, 0.0f), new Vector2(0.0f, 1.0f), new Vector2(0.0f, 1.0f), new Vector2(1.0f, 0.0f), new Vector2(1.0f, 1.0f) }; Vector4[] vPosOffset = new Vector4[] { new Vector4(-billOfsScreen.X, -billOfsScreen.Y, 0.0f, 0.0f), new Vector4(billOfsScreen.X, -billOfsScreen.Y, 0.0f, 0.0f), new Vector4(-billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f), new Vector4(-billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f), new Vector4(billOfsScreen.X, -billOfsScreen.Y, 0.0f, 0.0f), new Vector4(billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f) }; SurfaceDescription pBackBufferSurfaceDesc = device.GetBackBuffer(0, 0, BackBufferType.Mono).Description; // Set constants across quad for (int j = 0; j < 6; j++) { // Scale to pixels pBlobVertex[posCount].pos = Vector4.Add(blobScreenPos, vPosOffset[j]); pBlobVertex[posCount].pos.X *= pBackBufferSurfaceDesc.Width; pBlobVertex[posCount].pos.Y *= pBackBufferSurfaceDesc.Height; pBlobVertex[posCount].pos.X += 0.5f * pBackBufferSurfaceDesc.Width; pBlobVertex[posCount].pos.Y += 0.5f * pBackBufferSurfaceDesc.Height; pBlobVertex[posCount].tCurr = vTexCoords[j]; pBlobVertex[posCount].tBack = new Vector2((0.5f + pBlobVertex[posCount].pos.X) * (1.0f / pBackBufferSurfaceDesc.Width), (0.5f + pBlobVertex[posCount].pos.Y) * (1.0f / pBackBufferSurfaceDesc.Height)); pBlobVertex[posCount].fSize = blobPos[i].size; pBlobVertex[posCount].vColor = blobPos[i].color; posCount++; } } pBlobVB.Unlock(); }
private void FillBlobVB(Matrix pmatWorldView, Matrix pmatProjection, ref PointVertex[] blobPoints) { ScreenVertex[] pBlobVertex = (ScreenVertex[])pBlobVB.Lock(0, LockFlags.None); PointVertex[] blobPos = new PointVertex[blobPoints.Length]; for (int i = 0; i < blobPoints.Length; i++) { //transform point to camera space Vector4 blobPosCamera = Vector3.Transform(blobPoints[i].pos, pmatWorldView); blobPos[i] = blobPoints[i]; blobPos[i].pos.X = blobPosCamera.X; blobPos[i].pos.Y = blobPosCamera.Y; blobPos[i].pos.Z = blobPosCamera.Z; } int posCount = 0; for (int i = 0; i < blobPoints.Length; i++) { Vector4 blobScreenPos; // For calculating billboarding Vector4 billOfs = new Vector4(blobPos[i].size, blobPos[i].size, blobPos[i].pos.Z, 1); Vector4 billOfsScreen; // Transform to screenspace blobScreenPos = Vector3.Transform(blobPos[i].pos, pmatProjection); billOfsScreen = Vector4.Transform(billOfs, pmatProjection); // Project blobScreenPos = Vector4.Scale(blobScreenPos, 1.0f / blobScreenPos.W); billOfsScreen = Vector4.Scale(billOfsScreen, 1.0f / billOfsScreen.W); Vector2[] vTexCoords = new Vector2[] { new Vector2(0.0f, 0.0f), new Vector2(1.0f, 0.0f), new Vector2(0.0f, 1.0f), new Vector2(0.0f, 1.0f), new Vector2(1.0f, 0.0f), new Vector2(1.0f, 1.0f) }; Vector4[] vPosOffset = new Vector4[] { new Vector4(-billOfsScreen.X,-billOfsScreen.Y, 0.0f, 0.0f), new Vector4( billOfsScreen.X,-billOfsScreen.Y, 0.0f, 0.0f), new Vector4(-billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f), new Vector4(-billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f), new Vector4( billOfsScreen.X,-billOfsScreen.Y, 0.0f, 0.0f), new Vector4( billOfsScreen.X, billOfsScreen.Y, 0.0f, 0.0f) }; SurfaceDescription pBackBufferSurfaceDesc = device.GetBackBuffer(0, 0, BackBufferType.Left).Description; // Set constants across quad for (int j = 0; j < 6; j++) { // Scale to pixels pBlobVertex[posCount].pos = Vector4.Add(blobScreenPos, vPosOffset[j]); pBlobVertex[posCount].pos.X *= pBackBufferSurfaceDesc.Width; pBlobVertex[posCount].pos.Y *= pBackBufferSurfaceDesc.Height; pBlobVertex[posCount].pos.X += 0.5f * pBackBufferSurfaceDesc.Width; pBlobVertex[posCount].pos.Y += 0.5f * pBackBufferSurfaceDesc.Height; pBlobVertex[posCount].tCurr = vTexCoords[j]; pBlobVertex[posCount].tBack = new Vector2((0.5f + pBlobVertex[posCount].pos.X) * (1.0f / pBackBufferSurfaceDesc.Width), (0.5f + pBlobVertex[posCount].pos.Y) * (1.0f / pBackBufferSurfaceDesc.Height)); pBlobVertex[posCount].fSize = blobPos[i].size; pBlobVertex[posCount].vColor = blobPoints[i].color; posCount++; } } pBlobVB.Unlock(); }
private void CreateVertexBuffer() { _vertexCount = _xRes*_yRes; var vertexStream = new DataStream(_vertexCount*PointVertex.SizeOf, true, true); // store pixel coordinates in each vertex for (short y = 0; y < _yRes; y++) { for (short x = 0; x < _xRes; x++) { var pt = new PointVertex(x, y); vertexStream.Write(pt); } } vertexStream.Position = 0; // create vertex buffer _vertexBuffer = new Buffer(_dxDevice, vertexStream, _vertexCount*PointVertex.SizeOf, ResourceUsage.Immutable, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None); vertexStream.Close(); }
private void AddEdge(RotationTreeNode p, RotationTreeNode q) { var pVertex = new PointVertex(p.Point); var qVertex = new PointVertex(q.Point); if (_graph.ContainsVertex(pVertex) && _graph.ContainsVertex(qVertex)) _graph.AddEdge(new Edge<PointVertex>(pVertex, qVertex)); }