public override void render(ref Frustum frustum) { Frustum.InFrustumCheck primitiveInFrustum = frustum.CubeInFrustum(pos, 1.0f); if (primitiveInFrustum == Frustum.InFrustumCheck.IN) { GL.PushMatrix(); GL.Translate(this.pos.X, this.pos.Y, this.pos.Z); //GL.EnableClientState(ArrayCap.ColorArray); GL.EnableClientState(ArrayCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.vboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo.eboID); GL.VertexPointer(3, VertexPointerType.Float, vbo.sizeOfVertices, new IntPtr(0)); //GL.ColorPointer(4, ColorPointerType.UnsignedByte, vbo.sizeOfVertices, new IntPtr(12)); //Debug.Trace(color.ToString()); //GL.Color4(this.color.R, this.color.G, this.color.B, this.color.A); GL.Color3(this.color.R, this.color.G, this.color.B); //GL.Color3(255, 255, 255); GL.DrawElements(BeginMode.Triangles, vbo.numElements, DrawElementsType.UnsignedShort, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); //GL.DisableClientState(ArrayCap.ColorArray); GL.Color4(1.0, 1.0, 1.0, 1.0); GL.PopMatrix(); Game.Instance.polyCount += 6; } }
public override void render(ref Frustum frustum) { Frustum.InFrustumCheck primitiveInFrustum = frustum.CubeInFrustum(pos, 1.0f); if (primitiveInFrustum == Frustum.InFrustumCheck.IN) { GL.Disable(EnableCap.Lighting); GL.PushMatrix(); GL.Translate(this.pos.X, this.pos.Y, this.pos.Z); GL.Color4(this.color.R, this.color.G, this.color.B, this.color.A); GL.Begin(BeginMode.Quads); GL.Vertex3(1, 1, -1); // Top Right Of The Quad (Top) GL.Vertex3(-1, 1, -1); // Top Left Of The Quad (Top) GL.Vertex3(-1, 1, 1); // Bottom Left Of The Quad (Top) GL.Vertex3(1, 1, 1); // Bottom Right Of The Quad (Top) GL.Vertex3(1, -1, 1); // Top Right Of The Quad (Bottom) GL.Vertex3(-1, -1, 1); // Top Left Of The Quad (Bottom) GL.Vertex3(-1, -1, -1); // Bottom Left Of The Quad (Bottom) GL.Vertex3(1, -1, -1); // Bottom Right Of The Quad (Bottom) GL.Vertex3(1, 1, 1); // Top Right Of The Quad (Front) GL.Vertex3(-1, 1, 1); // Top Left Of The Quad (Front) GL.Vertex3(-1, -1, 1); // Bottom Left Of The Quad (Front) GL.Vertex3(1, -1, 1); // Bottom Right Of The Quad (Front) GL.Vertex3(1, -1, -1); // Top Right Of The Quad (Back) GL.Vertex3(-1, -1, -1); // Top Left Of The Quad (Back) GL.Vertex3(-1, 1, -1); // Bottom Left Of The Quad (Back) GL.Vertex3(1, 1, -1); // Bottom Right Of The Quad (Back) GL.Vertex3(-1, 1, 1); // Top Right Of The Quad (Left) GL.Vertex3(-1, 1, -1); // Top Left Of The Quad (Left) GL.Vertex3(-1, -1, -1); // Bottom Left Of The Quad (Left) GL.Vertex3(-1, -1, 1); // Bottom Right Of The Quad (Left) GL.Vertex3(1, 1, -1); // Top Right Of The Quad (Right) GL.Vertex3(1, 1, 1); // Top Left Of The Quad (Right) GL.Vertex3(1, -1, 1); // Bottom Left Of The Quad (Right) GL.Vertex3(1, -1, -1); // Bottom Right Of The Quad (Right) GL.End(); GL.Color4(1.0, 1.0, 1.0, 1.0); GL.PopMatrix(); GL.Enable(EnableCap.Lighting); Game.Instance.polyCount += 6; } }
public void Render(ref Frustum frustum) { Frustum.InFrustumCheck nodeInFrustum = frustum.CubeInFrustum(mainNode.BoundingBox3D); if (((nodeInFrustum != Frustum.InFrustumCheck.OUT) && (mainNode.HasChilds()))) { bool checkFrustum = true; // if all boundingbox corner where inside the frustum, there is no need to check the childs too if (nodeInFrustum == Frustum.InFrustumCheck.IN) { checkFrustum = false; } Core.pushState(); //GL.Scale(terrainRef.scale); //GL.Translate(terrainRef.position); GL.Disable(EnableCap.Lighting); GL.Color4(1.0, 1.0, 1.0, 0); GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.TextureCoordArray); if (terrainRef.TextureID != -1) { Texture.Bind(terrainRef.TextureID); GL.Enable(EnableCap.Texture2D); } CheckNodeInsideFrustum(ref frustum, ref mainNode, checkFrustum); if (terrainRef.TextureID != -1) { GL.Disable(EnableCap.Texture2D); } GL.DisableClientState(ArrayCap.TextureCoordArray); GL.DisableClientState(ArrayCap.VertexArray); Core.popState(); } return; }
private void CheckNodeInsideFrustum(ref Frustum frustum, ref QuadTreeNode mainNode, bool checkFrustum = true) { Frustum.InFrustumCheck nodeInFrustum; bool checkChildFrustum = true; // if the node has childs, check if nodes childs are in or outside the frustum if (mainNode.HasChilds()) { for (int i = 1; i <= 4; i++) { QuadTreeNode node = mainNode.GetChild(i); if (checkFrustum) { if (node.HasChilds()) { nodeInFrustum = frustum.CubeInFrustum(node.BoundingBox3D); } else { nodeInFrustum = frustum.CubeInFrustum(node.Patch); } if (nodeInFrustum == Frustum.InFrustumCheck.OUT) { continue; } if (nodeInFrustum == Frustum.InFrustumCheck.IN) { checkChildFrustum = false; } } CheckNodeInsideFrustum(ref frustum, ref node, checkChildFrustum); } } // if it don't, it's a leaf and the terrain patch can be rendered else { int maxLODLevels = terrainRef.LODLEVELS; int patchResolution = mainNode.Patch.GetResolution(maxLODLevels); int mainPatchIndexBuffer = terrainRef.IndexBuffer[patchResolution]; List <int> defaultBridgeIndexBuffer = new List <int>(); List <int> lowerBridgeIndexBuffer = new List <int>(); if (mainNode.Neighbor_N != null) { // check north neighbor patch resolution if (mainNode.Neighbor_N.Patch.GetResolution(maxLODLevels) > patchResolution) { lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 4]); } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 0]); } } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 0]); } if (mainNode.Neighbor_E != null) { // check east neighbor patch resolution if (mainNode.Neighbor_E.Patch.GetResolution(maxLODLevels) > patchResolution) { lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 5]); } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 1]); } } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 1]); } if (mainNode.Neighbor_S != null) { /* * Cube cube = new Cube(1); * cube.setPosition(new Vector3(mainNode.Patch.CenterVertex)); * cube.setColor(new ColorRGBA(1.0f, 0.0f, 0.0f, 1.0f)); * cube.render(ref frustum); * */ // check south neighbor patch resolution if (mainNode.Neighbor_S.Patch.GetResolution(maxLODLevels) > patchResolution) { lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 6]); } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 2]); } } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 2]); } if (mainNode.Neighbor_W != null) { // check west neighbor patch resolution if (mainNode.Neighbor_W.Patch.GetResolution(maxLODLevels) > patchResolution) { lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 7]); } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 3]); } } else { defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 3]); } int[] indicesCount = new int[3]; indicesCount[0] = terrainRef.IndicesCount[patchResolution]; // main patch indices count indicesCount[1] = terrainRef.BridgeIndicesCount[0, patchResolution]; // default bridge indices count indicesCount[2] = terrainRef.BridgeIndicesCount[1, patchResolution]; // lower bridge indices count mainNode.Patch.Render(mainPatchIndexBuffer, defaultBridgeIndexBuffer.ToArray(), lowerBridgeIndexBuffer.ToArray(), indicesCount); } return; }
private void CheckNodeInsideFrustum(ref Frustum frustum, ref QuadTreeNode mainNode, bool checkFrustum = true) { Frustum.InFrustumCheck nodeInFrustum; bool checkChildFrustum = true; // if the node has childs, check if nodes childs are in or outside the frustum if (mainNode.HasChilds()) { for (int i = 1; i <= 4; i++) { QuadTreeNode node = mainNode.GetChild(i); if (checkFrustum) { if (node.HasChilds()) { nodeInFrustum = frustum.CubeInFrustum(node.BoundingBox3D); } else { nodeInFrustum = frustum.CubeInFrustum(node.Patch); } if (nodeInFrustum == Frustum.InFrustumCheck.OUT) { continue; } if (nodeInFrustum == Frustum.InFrustumCheck.IN) { checkChildFrustum = false; } } CheckNodeInsideFrustum(ref frustum, ref node, checkChildFrustum); } } // if it don't, it's a leaf and the terrain patch can be rendered else { int maxLODLevels = terrainRef.LODLEVELS; int patchResolution = mainNode.Patch.GetResolution(maxLODLevels); int mainPatchIndexBuffer = terrainRef.IndexBuffer[patchResolution]; List<int> defaultBridgeIndexBuffer = new List<int>(); List<int> lowerBridgeIndexBuffer = new List<int>(); if (mainNode.Neighbor_N != null) { // check north neighbor patch resolution if (mainNode.Neighbor_N.Patch.GetResolution(maxLODLevels) > patchResolution) lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 4]); else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 0]); } else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 0]); if (mainNode.Neighbor_E != null) { // check east neighbor patch resolution if (mainNode.Neighbor_E.Patch.GetResolution(maxLODLevels) > patchResolution) lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 5]); else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 1]); } else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 1]); if (mainNode.Neighbor_S != null) { /* Cube cube = new Cube(1); cube.setPosition(new Vector3(mainNode.Patch.CenterVertex)); cube.setColor(new ColorRGBA(1.0f, 0.0f, 0.0f, 1.0f)); cube.render(ref frustum); * */ // check south neighbor patch resolution if (mainNode.Neighbor_S.Patch.GetResolution(maxLODLevels) > patchResolution) lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 6]); else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 2]); } else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 2]); if (mainNode.Neighbor_W != null) { // check west neighbor patch resolution if (mainNode.Neighbor_W.Patch.GetResolution(maxLODLevels) > patchResolution) lowerBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 7]); else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 3]); } else defaultBridgeIndexBuffer.Add(terrainRef.BridgeIndexBuffer[patchResolution, 3]); int[] indicesCount = new int[3]; indicesCount[0] = terrainRef.IndicesCount[patchResolution]; // main patch indices count indicesCount[1] = terrainRef.BridgeIndicesCount[0, patchResolution]; // default bridge indices count indicesCount[2] = terrainRef.BridgeIndicesCount[1, patchResolution]; // lower bridge indices count mainNode.Patch.Render(mainPatchIndexBuffer, defaultBridgeIndexBuffer.ToArray(), lowerBridgeIndexBuffer.ToArray(), indicesCount); } return; }