public void SubDivideNoNormalize(List <Triangle> triList, List <PositionTexture> vertexList) { Vector3d a1 = Vector3d.Lerp(vertexList[B].Position, vertexList[C].Position, .5f); Vector3d b1 = Vector3d.Lerp(vertexList[C].Position, vertexList[A].Position, .5f); Vector3d c1 = Vector3d.Lerp(vertexList[A].Position, vertexList[B].Position, .5f); Vector2d a1uv = Vector2d.Lerp(Vector2d.Create(vertexList[B].Tu, vertexList[B].Tv), Vector2d.Create(vertexList[C].Tu, vertexList[C].Tv), .5f); Vector2d b1uv = Vector2d.Lerp(Vector2d.Create(vertexList[C].Tu, vertexList[C].Tv), Vector2d.Create(vertexList[A].Tu, vertexList[A].Tv), .5f); Vector2d c1uv = Vector2d.Lerp(Vector2d.Create(vertexList[A].Tu, vertexList[A].Tv), Vector2d.Create(vertexList[B].Tu, vertexList[B].Tv), .5f); //a1.Normalize(); //b1.Normalize(); //c1.Normalize(); int aIndex = vertexList.Count; int bIndex = vertexList.Count + 1; int cIndex = vertexList.Count + 2; vertexList.Add(PositionTexture.CreatePosRaw(a1, a1uv.X, a1uv.Y)); vertexList.Add(PositionTexture.CreatePosRaw(b1, b1uv.X, b1uv.Y)); vertexList.Add(PositionTexture.CreatePosRaw(c1, c1uv.X, c1uv.Y)); triList.Add(Triangle.Create(A, cIndex, bIndex)); triList.Add(Triangle.Create(B, aIndex, cIndex)); triList.Add(Triangle.Create(C, bIndex, aIndex)); triList.Add(Triangle.Create(aIndex, bIndex, cIndex)); }
public override bool CreateGeometry(RenderContext renderContext) { base.CreateGeometry(renderContext); if (GeometryCreated) { return(true); } GeometryCreated = true; for (int i = 0; i < 4; i++) { RenderTriangleLists[i] = new List <RenderTriangle>(); } ComputeMatrix(); Height = texture.NaturalHeight; Width = texture.NaturalWidth; double latMin = 0 + (ScaleY * (Height - PixelCenterY)); double latMax = 0 - (ScaleY * PixelCenterY); double lngMin = 0 + (ScaleX * PixelCenterX); double lngMax = 0 - (ScaleX * (Width - PixelCenterX)); TopLeft = GeoTo3dTan(latMin, lngMin); BottomRight = GeoTo3dTan(latMax, lngMax); TopRight = GeoTo3dTan(latMin, lngMax); BottomLeft = GeoTo3dTan(latMax, lngMin); Vector3d topCenter = Vector3d.Lerp(TopLeft, TopRight, .5f); Vector3d bottomCenter = Vector3d.Lerp(BottomLeft, BottomRight, .5f); Vector3d center = Vector3d.Lerp(topCenter, bottomCenter, .5f); Vector3d rightCenter = Vector3d.Lerp(TopRight, BottomRight, .5f); Vector3d leftCenter = Vector3d.Lerp(TopLeft, BottomLeft, .5f); //Vector3d center = Vector3d.MidPoint(TopLeft, BottomRight); //Vector3d leftCenter = Vector3d.MidPoint(TopLeft, BottomLeft); //Vector3d rightCenter = Vector3d.MidPoint(TopRight, BottomRight); //Vector3d topCenter = Vector3d.MidPoint(TopLeft, TopRight); //Vector3d bottomCenter = Vector3d.MidPoint(BottomLeft, BottomRight); //verts[0].Position = TopLeft; //verts[0].Normal = TopLeft; //verts[0].Tu = 0; //verts[0].Tv = 0; //verts[1].Position = TopRight; //verts[1].Normal = TopRight; //verts[1].Tu = 1; //verts[1].Tv = 0; //verts[2].Position = BottomRight; //verts[2].Normal = BottomRight; //verts[2].Tu = 1; //verts[2].Tv = 1; //verts[3].Position = BottomLeft; //verts[3].Normal = BottomLeft; //verts[3].Tu = 0; //verts[3].Tv = 1; if (renderContext.gl == null) { //RenderTriangleLists[0].Add(RenderTriangle.Create(PositionTexture.CreatePosSize(TopLeft, 0, 0, Width, Height), PositionTexture.CreatePosSize(BottomLeft, 0, 1, Width, Height), PositionTexture.CreatePosSize(TopRight, 1, 0, Width, Height), texture, Level)); //RenderTriangleLists[0].Add(RenderTriangle.Create(PositionTexture.CreatePosSize(BottomLeft, 0, 1, Width, Height), PositionTexture.CreatePosSize(BottomRight, 1, 1, Width, Height), PositionTexture.CreatePosSize(TopRight, 1, 0, Width, Height), texture, Level)); vertexList = new List <PositionTexture>(); vertexList.Add(PositionTexture.CreatePosSize(TopLeft, 0, 0, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(TopRight, 1, 0, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(BottomLeft, 0, 1, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(BottomRight, 1, 1, Width, Height)); childTriangleList = new List <Triangle>(); if (dataset.BottomsUp) { childTriangleList.Add(Triangle.Create(0, 1, 2)); childTriangleList.Add(Triangle.Create(2, 1, 3)); } else { childTriangleList.Add(Triangle.Create(0, 2, 1)); childTriangleList.Add(Triangle.Create(2, 3, 1)); } int count = 3; while (count-- > 1) { List <Triangle> newList = new List <Triangle>(); foreach (Triangle tri in childTriangleList) { tri.SubDivide(newList, vertexList); } childTriangleList = newList; } double miter = .6 / (Width / 256); foreach (Triangle tri in childTriangleList) { PositionTexture p1 = vertexList[tri.A]; PositionTexture p2 = vertexList[tri.B]; PositionTexture p3 = vertexList[tri.C]; RenderTriangleLists[0].Add(RenderTriangle.CreateWithMiter(p1, p2, p3, texture, Level, miter)); } } else { //process vertex list VertexBuffer = PrepDevice.createBuffer(); PrepDevice.bindBuffer(GL.ARRAY_BUFFER, VertexBuffer); Float32Array f32array = new Float32Array(9 * 5); float[] buffer = (float[])(object)f32array; int index = 0; index = AddVertex(buffer, index, PositionTexture.CreatePos(bottomCenter, .5, 1)); //0 index = AddVertex(buffer, index, PositionTexture.CreatePos(BottomLeft, 0, 1)); //1 index = AddVertex(buffer, index, PositionTexture.CreatePos(BottomRight, 1, 1)); //2 index = AddVertex(buffer, index, PositionTexture.CreatePos(center, .5, .5)); //3 index = AddVertex(buffer, index, PositionTexture.CreatePos(leftCenter, 0, .5)); //4 index = AddVertex(buffer, index, PositionTexture.CreatePos(rightCenter, 1, .5)); //5 index = AddVertex(buffer, index, PositionTexture.CreatePos(topCenter, .5, 0)); //6 index = AddVertex(buffer, index, PositionTexture.CreatePos(TopLeft, 0, 0)); //7 index = AddVertex(buffer, index, PositionTexture.CreatePos(TopRight, 1, 0)); //8 PrepDevice.bufferData(GL.ARRAY_BUFFER, f32array, GL.STATIC_DRAW); // process index buffers for (int i = 0; i < 4; i++) { index = 0; TriangleCount = 2; Uint16Array ui16array = new Uint16Array(TriangleCount * 3); UInt16[] indexArray = (UInt16[])(object)ui16array; switch (i) { case 0: indexArray[index++] = 7; indexArray[index++] = 4; indexArray[index++] = 6; indexArray[index++] = 4; indexArray[index++] = 3; indexArray[index++] = 6; break; case 1: indexArray[index++] = 6; indexArray[index++] = 5; indexArray[index++] = 8; indexArray[index++] = 6; indexArray[index++] = 3; indexArray[index++] = 5; break; case 2: indexArray[index++] = 4; indexArray[index++] = 0; indexArray[index++] = 3; indexArray[index++] = 4; indexArray[index++] = 1; indexArray[index++] = 0; break; case 3: indexArray[index++] = 3; indexArray[index++] = 2; indexArray[index++] = 5; indexArray[index++] = 3; indexArray[index++] = 0; indexArray[index++] = 2; break; } IndexBuffers[i] = PrepDevice.createBuffer(); PrepDevice.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, IndexBuffers[i]); PrepDevice.bufferData(GL.ELEMENT_ARRAY_BUFFER, ui16array, GL.STATIC_DRAW); } } return(true); }
public override bool CreateGeometry(RenderContext renderContext) { base.CreateGeometry(renderContext); if (GeometryCreated) { return(true); } if (dataset.WcsImage is FitsImage && RenderContext.UseGlVersion2) { FitsImage fitsImage = dataset.WcsImage as FitsImage; texture2d = PrepDevice.createTexture(); PrepDevice.bindTexture(GL.TEXTURE_2D, texture2d); PrepDevice.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE); PrepDevice.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE); PrepDevice.texImage2D(GL.TEXTURE_2D, 0, GL.R32F, (int)fitsImage.SizeX, (int)fitsImage.SizeY, 0, GL.RED, GL.FLOAT, fitsImage.dataUnit); PrepDevice.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST); PrepDevice.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST); Width = fitsImage.SizeX; Height = fitsImage.SizeY; } else { WcsImage wcsImage = dataset.WcsImage as WcsImage; if (wcsImage != null) { Bitmap bmp = wcsImage.GetBitmap(); texture2d = bmp.GetTexture(); if (bmp.Height != wcsImage.SizeY) { PixelCenterY += bmp.Height - wcsImage.SizeY; } if (renderContext.gl != null) { Height = bmp.Height; Width = bmp.Width; } } else { Height = texture.NaturalHeight; Width = texture.NaturalWidth; } } GeometryCreated = true; for (int i = 0; i < 4; i++) { RenderTriangleLists[i] = new List <RenderTriangle>(); } ComputeMatrix(); double latMin = 0 + (ScaleY * (Height - PixelCenterY)); double latMax = 0 - (ScaleY * PixelCenterY); double lngMin = 0 + (ScaleX * PixelCenterX); double lngMax = 0 - (ScaleX * (Width - PixelCenterX)); TopLeft = GeoTo3dTan(latMin, lngMin); BottomRight = GeoTo3dTan(latMax, lngMax); TopRight = GeoTo3dTan(latMin, lngMax); BottomLeft = GeoTo3dTan(latMax, lngMin); Vector3d topCenter = Vector3d.Lerp(TopLeft, TopRight, .5f); Vector3d bottomCenter = Vector3d.Lerp(BottomLeft, BottomRight, .5f); Vector3d center = Vector3d.Lerp(topCenter, bottomCenter, .5f); Vector3d rightCenter = Vector3d.Lerp(TopRight, BottomRight, .5f); Vector3d leftCenter = Vector3d.Lerp(TopLeft, BottomLeft, .5f); if (renderContext.gl == null) { vertexList = new List <PositionTexture>(); vertexList.Add(PositionTexture.CreatePosSize(TopLeft, 0, 0, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(TopRight, 1, 0, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(BottomLeft, 0, 1, Width, Height)); vertexList.Add(PositionTexture.CreatePosSize(BottomRight, 1, 1, Width, Height)); childTriangleList = new List <Triangle>(); if (dataset.BottomsUp) { childTriangleList.Add(Triangle.Create(0, 1, 2)); childTriangleList.Add(Triangle.Create(2, 1, 3)); } else { childTriangleList.Add(Triangle.Create(0, 2, 1)); childTriangleList.Add(Triangle.Create(2, 3, 1)); } int count = 3; while (count-- > 1) { List <Triangle> newList = new List <Triangle>(); foreach (Triangle tri in childTriangleList) { tri.SubDivide(newList, vertexList); } childTriangleList = newList; } double miter = .6 / (Width / 256); foreach (Triangle tri in childTriangleList) { PositionTexture p1 = vertexList[tri.A]; PositionTexture p2 = vertexList[tri.B]; PositionTexture p3 = vertexList[tri.C]; RenderTriangleLists[0].Add(RenderTriangle.CreateWithMiter(p1, p2, p3, texture, Level, miter)); } } else { //process vertex list VertexBuffer = PrepDevice.createBuffer(); PrepDevice.bindBuffer(GL.ARRAY_BUFFER, VertexBuffer); Float32Array f32array = new Float32Array(9 * 5); float[] buffer = (float[])(object)f32array; int index = 0; index = AddVertex(buffer, index, PositionTexture.CreatePos(bottomCenter, .5, 1)); //0 index = AddVertex(buffer, index, PositionTexture.CreatePos(BottomLeft, 0, 1)); //1 index = AddVertex(buffer, index, PositionTexture.CreatePos(BottomRight, 1, 1)); //2 index = AddVertex(buffer, index, PositionTexture.CreatePos(center, .5, .5)); //3 index = AddVertex(buffer, index, PositionTexture.CreatePos(leftCenter, 0, .5)); //4 index = AddVertex(buffer, index, PositionTexture.CreatePos(rightCenter, 1, .5)); //5 index = AddVertex(buffer, index, PositionTexture.CreatePos(topCenter, .5, 0)); //6 index = AddVertex(buffer, index, PositionTexture.CreatePos(TopLeft, 0, 0)); //7 index = AddVertex(buffer, index, PositionTexture.CreatePos(TopRight, 1, 0)); //8 PrepDevice.bufferData(GL.ARRAY_BUFFER, f32array, GL.STATIC_DRAW); // process index buffers for (int i = 0; i < 4; i++) { index = 0; TriangleCount = 2; Uint16Array ui16array = new Uint16Array(TriangleCount * 3); UInt16[] indexArray = (UInt16[])(object)ui16array; switch (i) { case 0: indexArray[index++] = 7; indexArray[index++] = 4; indexArray[index++] = 6; indexArray[index++] = 4; indexArray[index++] = 3; indexArray[index++] = 6; break; case 1: indexArray[index++] = 6; indexArray[index++] = 5; indexArray[index++] = 8; indexArray[index++] = 6; indexArray[index++] = 3; indexArray[index++] = 5; break; case 2: indexArray[index++] = 4; indexArray[index++] = 0; indexArray[index++] = 3; indexArray[index++] = 4; indexArray[index++] = 1; indexArray[index++] = 0; break; case 3: indexArray[index++] = 3; indexArray[index++] = 2; indexArray[index++] = 5; indexArray[index++] = 3; indexArray[index++] = 0; indexArray[index++] = 2; break; } IndexBuffers[i] = PrepDevice.createBuffer(); PrepDevice.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, IndexBuffers[i]); PrepDevice.bufferData(GL.ELEMENT_ARRAY_BUFFER, ui16array, GL.STATIC_DRAW); } } return(true); }
private void InitializeGrids() { vertexList = new List <PositionTexture>(); childTriangleList = new List <Triangle> [4]; childTriangleList[0] = new List <Triangle>(); childTriangleList[1] = new List <Triangle>(); childTriangleList[2] = new List <Triangle>(); childTriangleList[3] = new List <Triangle>(); bounds = new PositionTexture[9]; if (Level > 0) { // Set in constuctor now //ToastTile parent = (ToastTile)TileCache.GetTile(level - 1, x / 2, y / 2, dataset, null); if (Parent == null) { Parent = TileCache.GetTile(Level - 1, tileX / 2, tileY / 2, dataset, null); } ToastTile parent = (ToastTile)Parent; int xIndex = tileX % 2; int yIndex = tileY % 2; if (Level > 1) { backslash = parent.backslash; } else { backslash = xIndex == 1 ^ yIndex == 1; } bounds[0 + 3 * 0] = parent.bounds[xIndex + 3 * yIndex].Copy(); bounds[1 + 3 * 0] = Midpoint(parent.bounds[xIndex + 3 * yIndex], parent.bounds[xIndex + 1 + 3 * yIndex]); bounds[2 + 3 * 0] = parent.bounds[xIndex + 1 + 3 * yIndex].Copy(); bounds[0 + 3 * 1] = Midpoint(parent.bounds[xIndex + 3 * yIndex], parent.bounds[xIndex + 3 * (yIndex + 1)]); if (backslash) { bounds[1 + 3 * 1] = Midpoint(parent.bounds[xIndex + 3 * yIndex], parent.bounds[xIndex + 1 + 3 * (yIndex + 1)]); } else { bounds[1 + 3 * 1] = Midpoint(parent.bounds[xIndex + 1 + 3 * yIndex], parent.bounds[xIndex + 3 * (yIndex + 1)]); } bounds[2 + 3 * 1] = Midpoint(parent.bounds[xIndex + 1 + 3 * yIndex], parent.bounds[xIndex + 1 + 3 * (yIndex + 1)]); bounds[0 + 3 * 2] = parent.bounds[xIndex + 3 * (yIndex + 1)].Copy(); bounds[1 + 3 * 2] = Midpoint(parent.bounds[xIndex + 3 * (yIndex + 1)], parent.bounds[xIndex + 1 + 3 * (yIndex + 1)]); bounds[2 + 3 * 2] = parent.bounds[xIndex + 1 + 3 * (yIndex + 1)].Copy(); bounds[0 + 3 * 0].Tu = 0 * uvMultiple; bounds[0 + 3 * 0].Tv = 0 * uvMultiple; bounds[1 + 3 * 0].Tu = .5f * uvMultiple; bounds[1 + 3 * 0].Tv = 0 * uvMultiple; bounds[2 + 3 * 0].Tu = 1 * uvMultiple; bounds[2 + 3 * 0].Tv = 0 * uvMultiple; bounds[0 + 3 * 1].Tu = 0 * uvMultiple; bounds[0 + 3 * 1].Tv = .5f * uvMultiple; bounds[1 + 3 * 1].Tu = .5f * uvMultiple; bounds[1 + 3 * 1].Tv = .5f * uvMultiple; bounds[2 + 3 * 1].Tu = 1 * uvMultiple; bounds[2 + 3 * 1].Tv = .5f * uvMultiple; bounds[0 + 3 * 2].Tu = 0 * uvMultiple; bounds[0 + 3 * 2].Tv = 1 * uvMultiple; bounds[1 + 3 * 2].Tu = .5f * uvMultiple; bounds[1 + 3 * 2].Tv = 1 * uvMultiple; bounds[2 + 3 * 2].Tu = 1 * uvMultiple; bounds[2 + 3 * 2].Tv = 1 * uvMultiple; vertexList.Add(bounds[0 + 3 * 0]); vertexList.Add(bounds[1 + 3 * 0]); vertexList.Add(bounds[2 + 3 * 0]); vertexList.Add(bounds[0 + 3 * 1]); vertexList.Add(bounds[1 + 3 * 1]); vertexList.Add(bounds[2 + 3 * 1]); vertexList.Add(bounds[0 + 3 * 2]); vertexList.Add(bounds[1 + 3 * 2]); vertexList.Add(bounds[2 + 3 * 2]); if (backslash) { childTriangleList[0].Add(Triangle.Create(4, 1, 0)); childTriangleList[0].Add(Triangle.Create(3, 4, 0)); childTriangleList[1].Add(Triangle.Create(5, 2, 1)); childTriangleList[1].Add(Triangle.Create(4, 5, 1)); childTriangleList[2].Add(Triangle.Create(7, 4, 3)); childTriangleList[2].Add(Triangle.Create(6, 7, 3)); childTriangleList[3].Add(Triangle.Create(8, 5, 4)); childTriangleList[3].Add(Triangle.Create(7, 8, 4)); } else { childTriangleList[0].Add(Triangle.Create(3, 1, 0)); childTriangleList[0].Add(Triangle.Create(4, 1, 3)); childTriangleList[1].Add(Triangle.Create(4, 2, 1)); childTriangleList[1].Add(Triangle.Create(5, 2, 4)); childTriangleList[2].Add(Triangle.Create(6, 4, 3)); childTriangleList[2].Add(Triangle.Create(7, 4, 6)); childTriangleList[3].Add(Triangle.Create(7, 5, 4)); childTriangleList[3].Add(Triangle.Create(8, 5, 7)); } } else { bounds[0 + 3 * 0] = PositionTexture.Create(0, -1, 0, 0, 0); bounds[1 + 3 * 0] = PositionTexture.Create(0, 0, 1, .5f, 0); bounds[2 + 3 * 0] = PositionTexture.Create(0, -1, 0, 1, 0); bounds[0 + 3 * 1] = PositionTexture.Create(-1, 0, 0, 0, .5f); bounds[1 + 3 * 1] = PositionTexture.Create(0, 1, 0, .5f, .5f); bounds[2 + 3 * 1] = PositionTexture.Create(1, 0, 0, 1, .5f); bounds[0 + 3 * 2] = PositionTexture.Create(0, -1, 0, 0, 1); bounds[1 + 3 * 2] = PositionTexture.Create(0, 0, -1, .5f, 1); bounds[2 + 3 * 2] = PositionTexture.Create(0, -1, 0, 1, 1); vertexList.Add(bounds[0 + 3 * 0]); vertexList.Add(bounds[1 + 3 * 0]); vertexList.Add(bounds[2 + 3 * 0]); vertexList.Add(bounds[0 + 3 * 1]); vertexList.Add(bounds[1 + 3 * 1]); vertexList.Add(bounds[2 + 3 * 1]); vertexList.Add(bounds[0 + 3 * 2]); vertexList.Add(bounds[1 + 3 * 2]); vertexList.Add(bounds[2 + 3 * 2]); childTriangleList[0].Add(Triangle.Create(3, 1, 0)); childTriangleList[0].Add(Triangle.Create(4, 1, 3)); childTriangleList[1].Add(Triangle.Create(5, 2, 1)); childTriangleList[1].Add(Triangle.Create(4, 5, 1)); childTriangleList[2].Add(Triangle.Create(7, 4, 3)); childTriangleList[2].Add(Triangle.Create(6, 7, 3)); childTriangleList[3].Add(Triangle.Create(7, 5, 4)); childTriangleList[3].Add(Triangle.Create(8, 5, 7)); // Setup default matrix of points. } }