private unsafe void InitVertexBuffers() { { VR01PositionBuffer positionBuffer = new VR01PositionBuffer(strin_Position); positionBuffer.Alloc(zFrameCount); QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadPosition( new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f) ); } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR01UVBuffer uvBuffer = new VR01UVBuffer(strin_uv); uvBuffer.Alloc(zFrameCount); QuadUV *array = (QuadUV *)uvBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadUV( new vec3(0, 0, (float)i / (float)zFrameCount), new vec3(1, 0, (float)i / (float)zFrameCount), new vec3(1, 1, (float)i / (float)zFrameCount), new vec3(0, 1, (float)i / (float)zFrameCount) ); } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, 0, zFrameCount * 4); indexBuffer.Alloc(zFrameCount);// this actually does nothing. this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; indexBuffer.Dispose(); } this.vao = new VertexArrayObject( this.indexBufferRenderer, this.positionBufferRenderer, this.uvBufferRenderer); }
private unsafe DynamicUnstructureGeometry DoCreateMesh(DynamicUnstructuredGridderSource src) { MatrixPositionBuffer matrixPositions = null; IndexBuffer matrixIndicesBuffer = null; FracturePositionBuffer fractionPositionsBuffer = null; //生成母体 if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON) { matrixPositions = new TetrahedronMatrixPositionBuffer(); //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TETRAHEDRON; //int memSize = src.ElementNum * sizeof(TetrahedronPositions); //matrixPositions.AllocMem(memSize); matrixPositions.AllocMem(src.ElementNum); TetrahedronPosition *tets = (TetrahedronPosition *)matrixPositions.Data; int[][] matrixIndices = src.Elements; Vertex[] positions = src.Nodes; for (int i = 0; i < src.ElementNum; i++) { tets[i].p1 = src.Transform * positions[matrixIndices[i][0] - 1]; tets[i].p2 = src.Transform * positions[matrixIndices[i][1] - 1]; tets[i].p3 = src.Transform * positions[matrixIndices[i][2] - 1]; tets[i].p4 = src.Transform * positions[matrixIndices[i][3] - 1]; } matrixIndicesBuffer = new TetrahedronMatrixIndexBuffer(); //int triangleCount = src.ElementNum * 4; //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex)); matrixIndicesBuffer.AllocMem(src.ElementNum); TetrahedronIndex *header = (TetrahedronIndex *)matrixIndicesBuffer.Data; for (int i = 0; i < src.ElementNum; i++) { header[i].dot0 = (uint)(i * 4 + 0); header[i].dot1 = (uint)(i * 4 + 1); header[i].dot2 = (uint)(i * 4 + 2); header[i].dot3 = (uint)(i * 4 + 3); header[i].dot4 = (uint)(i * 4 + 0); header[i].dot5 = (uint)(i * 4 + 1); header[i].restartIndex = uint.MaxValue; } } if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE) { matrixPositions = new TriangleMatrixPositionBuffer(); //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE; matrixPositions.AllocMem(src.ElementNum); int[][] matrixIndices = src.Elements; Vertex[] positions = src.Nodes; TrianglePosition *triangles = (TrianglePosition *)matrixPositions.Data; for (int i = 0; i < src.ElementNum; i++) { triangles[i].P1 = src.Transform * positions[matrixIndices[i][0] - 1]; triangles[i].P2 = src.Transform * positions[matrixIndices[i][1] - 1]; triangles[i].P3 = src.Transform * positions[matrixIndices[i][2] - 1]; } } if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT6_TRIANGULAR_PRISM) { matrixPositions = new TriangularPrismMatrixPositionBuffer(); //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE; matrixPositions.AllocMem(src.ElementNum); int[][] matrixIndices = src.Elements; Vertex[] positions = src.Nodes; TriangularPrismPosition *prism = (TriangularPrismPosition *)matrixPositions.Data; for (int i = 0; i < src.ElementNum; i++) { try { prism[i].P1 = src.Transform * positions[matrixIndices[i][0] - 1]; prism[i].P2 = src.Transform * positions[matrixIndices[i][1] - 1]; prism[i].P3 = src.Transform * positions[matrixIndices[i][2] - 1]; prism[i].P4 = src.Transform * positions[matrixIndices[i][3] - 1]; prism[i].P5 = src.Transform * positions[matrixIndices[i][4] - 1]; prism[i].P6 = src.Transform * positions[matrixIndices[i][5] - 1]; } catch (IndexOutOfRangeException e) { throw new IndexOutOfRangeException(String.Format("Maxtrix positions row:{0},out of range", i + 1)); } } matrixIndicesBuffer = new TetrahedronMatrixTriangularPrismIndexBuffer(); //int triangleCount = src.ElementNum * 4; //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex)); matrixIndicesBuffer.AllocMem(src.ElementNum); TetrahedronTriangularPrismIndex *indexes = (TetrahedronTriangularPrismIndex *)matrixIndicesBuffer.Data; for (int i = 0; i < src.ElementNum; i++) { indexes[i].dot0 = (uint)(i * 6 + 0); indexes[i].dot1 = (uint)(i * 6 + 3); indexes[i].dot2 = (uint)(i * 6 + 1); indexes[i].dot3 = (uint)(i * 6 + 4); indexes[i].dot4 = (uint)(i * 6 + 2); indexes[i].dot5 = (uint)(i * 6 + 5); indexes[i].dot6 = (uint)(i * 6 + 0); indexes[i].dot7 = (uint)(i * 6 + 3); indexes[i].restartIndex = uint.MaxValue; } } //生成裂缝 if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE) { fractionPositionsBuffer = new TriangleFracturePositionBuffer(); //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_TRIANGLE; int triangleCount = src.FractureNum; //fractionPositionsBuffer.AllocMem(triangleCount * sizeof(TrianglePositions)); fractionPositionsBuffer.AllocMem(triangleCount); int[][] triangleIndices = src.Fractures; Vertex[] positions = src.Nodes; TrianglePosition *triangles = (TrianglePosition *)fractionPositionsBuffer.Data; for (int i = 0; i < triangleCount; i++) { triangles[i].P1 = src.Transform * positions[triangleIndices[i][0] - 1]; triangles[i].P2 = src.Transform * positions[triangleIndices[i][1] - 1]; triangles[i].P3 = src.Transform * positions[triangleIndices[i][2] - 1]; } } if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE) { fractionPositionsBuffer = new LineFracturePositionBuffer(); //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_LINE; int lineCount = src.FractureNum; //fractionPositionsBuffer.AllocMem(lineCount * sizeof(LinePositions)); fractionPositionsBuffer.AllocMem(lineCount); LinePosition *lines = (LinePosition *)fractionPositionsBuffer.Data; Vertex[] positions = src.Nodes; int[][] lineIndices = src.Fractures; for (int i = 0; i < lineCount; i++) { lines[i].P1 = src.Transform * positions[lineIndices[i][0] - 1]; lines[i].P2 = src.Transform * positions[lineIndices[i][1] - 1]; } } if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT4_QUAD) { fractionPositionsBuffer = new QuadFracturePositionBuffer(); int fractureCount = src.FractureNum; fractionPositionsBuffer.AllocMem(fractureCount); QuadPosition *quad = (QuadPosition *)fractionPositionsBuffer.Data; Vertex[] positions = src.Nodes; int[][] quadIndices = src.Fractures; for (int i = 0; i < fractureCount; i++) { quad[i].P1 = src.Transform * positions[quadIndices[i][0] - 1]; quad[i].P2 = src.Transform * positions[quadIndices[i][1] - 1]; quad[i].P3 = src.Transform * positions[quadIndices[i][3] - 1]; quad[i].P4 = src.Transform * positions[quadIndices[i][2] - 1]; } } DynamicUnstructureGeometry geometry = new DynamicUnstructureGeometry(matrixPositions); geometry.MatrixIndices = matrixIndicesBuffer; geometry.FracturePositions = fractionPositionsBuffer; geometry.Min = src.TransformedActiveBounds.Min; geometry.Max = src.TransformedActiveBounds.Max; return(geometry); }
private unsafe void InitVertexBuffers() { { VR04PositionBuffer positionBuffer = new VR04PositionBuffer(strin_Position); positionBuffer.Alloc(zFrameCount); QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadPosition( new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f) ); } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR04UVBuffer uvBuffer = new VR04UVBuffer(strin_uv); uvBuffer.Alloc(zFrameCount); QuadUV *array = (QuadUV *)uvBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadUV( new vec3(0, 0, (float)i / (float)zFrameCount), new vec3(1, 0, (float)i / (float)zFrameCount), new vec3(1, 1, (float)i / (float)zFrameCount), new vec3(0, 1, (float)i / (float)zFrameCount) ); } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new VR04IndexBuffer(); indexBuffer.Alloc(zFrameCount); QuadIndex *array = (QuadIndex *)indexBuffer.FirstElement(); for (uint i = 0; i < zFrameCount; i++) { if (this.reverSide) { array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0, (zFrameCount - i - 1) * 4 + 1, (zFrameCount - i - 1) * 4 + 2, (zFrameCount - i - 1) * 4 + 3 ); } else { array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3); } } this.indexBufferRenderer = indexBuffer.GetRenderer(); indexBuffer.Dispose(); } //if (!this.reverSide) //{ // var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, zFrameCount * 4); // indexBuffer.Alloc(zFrameCount);// this actually does nothing. // this.indexBufferRenderer = indexBuffer.GetRenderer(); // indexBuffer.Dispose(); //} //else //{ // var indexBuffer = new VR04IndexBuffer(); // indexBuffer.Alloc(zFrameCount); // QuadIndex* array = (QuadIndex*)indexBuffer.FirstElement(); // for (uint i = 0; i < zFrameCount; i++) // { // //array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3); // array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0, // (zFrameCount - i - 1) * 4 + 1, // (zFrameCount - i - 1) * 4 + 2, // (zFrameCount - i - 1) * 4 + 3 // ); // } // this.indexBufferRenderer = indexBuffer.GetRenderer(); // indexBuffer.Dispose(); //} this.vao = new VertexArrayObject(this.positionBufferRenderer, this.uvBufferRenderer, this.indexBufferRenderer); }