private VoxelChunkManager() { chunks = new Dictionary<Tuple<int, int>, VoxelChunk>(); chunksToMesh = new Dictionary<Tuple<int, int>, VoxelChunk>(); chunkLoadQueue = new Queue<Tuple<int, int>>(); noise = new OpenSimplexNoise(2); frustum = new Frustum(); chunkScalingMatrix = Matrix4.CreateScaling(new Vector3(ChunkWidth, ChunkHeight, ChunkDepth)); chunkShader = new ShaderProgram(chunkvertexshader, chunkfragmentshader); chunkShader["lightDirection"].SetValue(new Vector3(-0.75, -0.5, -1).Normalize()); }
/// <summary> /// Returns true if the AxisAlignedBoundingBox is within the frustum. Cheats by using spherical representation of the bounding box. /// </summary> /// <param name="f">The Frustum to check for intersections with</param> /// <returns>True if the spherical representation of the AxisAlignedBoundingBox is withing the frustum</returns> public bool Intersects(Frustum f) { return f.Intersects(this); }
static void Main(string[] args) { timefromstart = System.Diagnostics.Stopwatch.StartNew(); Console.WriteLine("Initializing GLUT - {0}s", getTimeFromStart().ToString()); Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("kVoxGame"); Console.WriteLine("GLUT window created - {0}s", getTimeFromStart().ToString()); //Callbacks Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); //Mouse Glut.glutMouseFunc(OnMouse); Glut.glutMotionFunc(OnMove); //Keyboard Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Console.WriteLine("GLUT callbacks binded - {0}s", getTimeFromStart().ToString()); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.Enable(EnableCap.Multisample); Gl.Enable(EnableCap.SampleAlphaToCoverage); Gl.Enable(EnableCap.FragmentLightingSgix); Gl.Enable(EnableCap.CullFace); Console.WriteLine("GL Enables enabled :D - {0}s", getTimeFromStart().ToString()); skydomeTexture = new Texture("skydome.jpg"); //Gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); // create main shader program terrainSP = new ShaderProgram(terrainVS, terrainFS); terrainSP.Use(); terrainSP["color"].SetValue(new Vector3(0, 0.8, 0)); projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f); terrainSP["projection_matrix"].SetValue(projectionMatrix); terrainSP["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateRotation(new Vector3(0, 0, 0), 0.0f)); Console.WriteLine("Shader program (main) compiled and data injected - {0}s", getTimeFromStart().ToString()); // create main skybox program skyboxSP = new ShaderProgram(skyboxVS, skyboxFS); skyboxSP.Use(); skyboxSP["color"].SetValue(new Vector3(0.2, 1.0, 1.0)); skyboxSP["resolution"].SetValue(new Vector2(width, height)); projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f); skyboxSP["projection_matrix"].SetValue(projectionMatrix); skyboxSP["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateRotation(new Vector3(0, 0, 0), 0.0f)); Console.WriteLine("Shader program (skybox) compiled and data injected - {0}s", getTimeFromStart().ToString()); //Setup camera camera = new Camera(new Vector3(0, 0, 10), Quaternion.Identity); camera.SetDirection(new Vector3(0, 0, -1)); Console.WriteLine("Cam setup complete - {0}s", getTimeFromStart().ToString()); watch = System.Diagnostics.Stopwatch.StartNew(); chunks = new VoxelChunk[4]; //instance few voxel chunks chunks[0] = new VoxelChunk(0,0); chunks[1] = new VoxelChunk(0, 32); chunks[2] = new VoxelChunk(32, 0); chunks[3] = new VoxelChunk(32, 32); Console.WriteLine("Chunks added to array - {0}s", getTimeFromStart().ToString()); //init frustum frustum = new Frustum(); frustum.UpdateFrustum(projectionMatrix, camera.ViewMatrix); Console.WriteLine("starting main GLUT loop - {0}s", getTimeFromStart().ToString()); Glut.glutMainLoop(); }
/// <summary> /// Returns true if the AxisAlignedBoundingBox is within the frustum. /// Cheats by using spherical representation of the bounding box. /// </summary> /// <param name="f">The Frustum to check for intersections with.</param> /// <returns>True if the spherical representation of the AxisAlignedBoundingBox is withing the frustum.</returns> public bool Intersects(Frustum f) { return(f.Intersects(this)); }