public void PopulateAsHollowCone(float radius, float height, LCC3Tessellation angleAndHeightDivs) { uint numAngularDivs = Math.Max(angleAndHeightDivs.X, 3); uint numHeightDivs = Math.Max(angleAndHeightDivs.Y, 1); float radiusHeightRatio = radius / height; float angularDivSpan = (float)(Math.PI * 2) / numAngularDivs; float heightDivSpan = height / numHeightDivs; float radialDivSpan = radius / numHeightDivs; float texAngularDivSpan = 1.0f / numAngularDivs; float texHeightDivSpan = 1.0f / numHeightDivs; uint vertexCount = (numAngularDivs + 1) * (numHeightDivs + 1); uint triangleCount = 2 * numAngularDivs * numHeightDivs - numAngularDivs; this.EnsureVertexContent(); this.AllocatedVertexCapacity = vertexCount; this.AllocatedVertexIndexCapacity = (triangleCount * 3); uint vIdx = 0; uint iIdx = 0; for (uint ia = 0; ia <= numAngularDivs; ia++) { float angle = angularDivSpan * ia; float ca = (float)-Math.Cos(angle); float sa = (float)-Math.Sin(angle); LCC3Vector vtxNormal = new LCC3Vector(sa, radiusHeightRatio, ca).NormalizedVector(); for (uint ih = 0; ih <= numHeightDivs; ih++, vIdx++) { float vtxRadius = radius - (radialDivSpan * ih); float vtxHt = heightDivSpan * ih; LCC3Vector vtxLoc = new LCC3Vector(vtxRadius * sa, vtxHt, vtxRadius * ca); this.SetVertexLocationAtIndex(vtxLoc,vIdx); this.SetNormalAtIndex(vtxNormal, vIdx); CCTex2F texCoord = new CCTex2F(texAngularDivSpan * ia, (1.0f - texHeightDivSpan * ih)); this.SetVertexTexCoord2FAtIndex(texCoord, vIdx); if (ia < numAngularDivs && ih < numHeightDivs) { this.SetVertexIndexAtIndex( vIdx, iIdx++); this.SetVertexIndexAtIndex((vIdx + numHeightDivs + 1),iIdx++); this.SetVertexIndexAtIndex((vIdx + numHeightDivs + 2),iIdx++); if (ih < numHeightDivs - 1) { this.SetVertexIndexAtIndex((vIdx + numHeightDivs + 2), iIdx++); this.SetVertexIndexAtIndex((vIdx + 1), iIdx++); this.SetVertexIndexAtIndex(vIdx, iIdx++); } } } } }
public void PopulateAsSphere(float radius, LCC3Tessellation divsPerAxis) { divsPerAxis.X = Math.Max(divsPerAxis.X, 3); divsPerAxis.Y = Math.Max(divsPerAxis.Y, 2); CCSize divSpan = new CCSize((float)((Math.PI * 2) / divsPerAxis.X), (float)(Math.PI / divsPerAxis.Y)); CCSize divTexSpan = new CCSize((float)(1.0 / divsPerAxis.X), (float)(1.0 / divsPerAxis.Y)); float halfDivTexSpanWidth = divTexSpan.Width * 0.5f; LCC3Tessellation verticesPerAxis = new LCC3Tessellation(0,0); verticesPerAxis.X = divsPerAxis.X + 1; verticesPerAxis.Y = divsPerAxis.Y + 1; uint vertexCount = verticesPerAxis.X * verticesPerAxis.Y; uint triangleCount = divsPerAxis.X * (divsPerAxis.Y - 1) * 2; this.EnsureVertexContent(); this.AllocatedVertexCapacity = vertexCount; this.AllocatedVertexIndexCapacity = (triangleCount * 3); LCC3VertexIndices indices = this.VertexIndices; uint vIndx = 0; uint iIndx = 0; for (uint iy = 0; iy < verticesPerAxis.Y; iy++) { float y = divSpan.Height * iy; float sy = (float)Math.Sin(y); float cy = (float)Math.Cos(y); for (uint ix = 0; ix < verticesPerAxis.X; ix++) { float x = divSpan.Width * ix; float sx = (float)Math.Sin(x); float cx = (float)Math.Cos(x); LCC3Vector unitRadial = new LCC3Vector(-(sy * sx), cy, -(sy * cx)); this.SetVertexLocationAtIndex(unitRadial * radius, vIndx); this.SetNormalAtIndex(unitRadial.NormalizedVector(), vIndx); float uOffset = 0.0f; if (iy == 0) { uOffset = halfDivTexSpanWidth; } if (iy == (verticesPerAxis.Y - 1)) { uOffset = -halfDivTexSpanWidth; } float u = divTexSpan.Width * ix + uOffset; float v = divTexSpan.Height * iy; this.SetVertexTexCoord2FAtIndex(new CCTex2F(u, v), vIndx); if (iy > 0 && ix > 0) { if (iy > 1) { indices.SetIndex(vIndx, iIndx++); indices.SetIndex(vIndx - verticesPerAxis.X, iIndx++); indices.SetIndex(vIndx - verticesPerAxis.Y - 1, iIndx++); } if (iy < (verticesPerAxis.Y - 1)) { indices.SetIndex(vIndx - verticesPerAxis.X - 1, iIndx++); indices.SetIndex(vIndx - 1, iIndx++); indices.SetIndex(vIndx, iIndx++); } } vIndx++; } } }
public void PopulateAsRectangle(CCSize rectSize, CCPoint origin, LCC3Tessellation divsPerAxis) { divsPerAxis.X = Math.Max(divsPerAxis.X, 1); divsPerAxis.Y = Math.Max(divsPerAxis.Y, 1); CCPoint rectExtent = new CCPoint(rectSize.Width, rectSize.Height); origin = new CCPoint(rectExtent.X * origin.X, rectExtent.Y * origin.Y); CCPoint botLeft = CCPoint.Zero - origin; CCPoint topRight = rectExtent - origin; CCSize divSize = new CCSize((topRight.X - botLeft.X) / divsPerAxis.X, (topRight.Y - botLeft.Y) / divsPerAxis.Y); CCSize divTexSpan = new CCSize((1.0f / divsPerAxis.X), (1.0f / divsPerAxis.Y)); LCC3Tessellation verticesPerAxis = new LCC3Tessellation(0,0); verticesPerAxis.X = divsPerAxis.X + 1; verticesPerAxis.Y = divsPerAxis.Y + 1; uint vertexCount = verticesPerAxis.X * verticesPerAxis.Y; uint triangleCount = divsPerAxis.X * divsPerAxis.Y * 2; this.EnsureVertexContent(); this.AllocatedVertexCapacity = vertexCount; this.AllocatedVertexIndexCapacity = (triangleCount * 3); for (uint iy = 0; iy < verticesPerAxis.Y; iy++) { for (uint ix = 0; ix < verticesPerAxis.X; ix++) { uint vIndx = iy * verticesPerAxis.X + ix; float vx = botLeft.X + (divSize.Width * ix); float vy = botLeft.Y + (divSize.Height * iy); this.SetVertexLocationAtIndex(new LCC3Vector(vx, vy, 0.0f), vIndx); this.SetNormalAtIndex(LCC3Vector.CC3UnitZPositive, vIndx); float u = divTexSpan.Width * ix; float v = divTexSpan.Height * iy; this.SetVertexTexCoord2FAtIndex(new CCTex2F(u, (1.0f - v)), vIndx); } } LCC3VertexIndices indices = this.VertexIndices; uint iIndx = 0; for (uint iy = 0; iy < divsPerAxis.Y; iy++) { for (uint ix = 0; ix < divsPerAxis.X; ix++) { uint botLeftOfFace; // First triangle of face wound counter-clockwise botLeftOfFace = iy * verticesPerAxis.X + ix; indices.SetIndex(botLeftOfFace, iIndx++); indices.SetIndex(botLeftOfFace + 1, iIndx++); indices.SetIndex(botLeftOfFace + verticesPerAxis.X + 1, iIndx++); // Second triangle of face wound counter-clockwise indices.SetIndex(botLeftOfFace + verticesPerAxis.X + 1, iIndx++); indices.SetIndex(botLeftOfFace + verticesPerAxis.X, iIndx++); indices.SetIndex(botLeftOfFace, iIndx++); } } }