public void Render(ICamera camera, ICamera lodCamera, Vector3d lightPosition) { var transformation = new Matrix4d( new Vector4d(1, 0, 0, 0), new Vector4d(0, 0, 1, 0), new Vector4d(0, 1, 0, 1), new Vector4d(0, 0, 0, 1)); var visibleChunks = _chunkedLod.Calculate( _tree, lodCamera.Width, lodCamera.HorizontalFieldOfView, Vector3d.Transform(lodCamera.Position, transformation), 30, FrustumPlaneExtractor.ExtractRowMajor(transformation * lodCamera.ComputeCameraMatrix() * lodCamera.ComputeProjectionMatrix())); GL.ClearColor(Color4.White); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); GL.CullFace(CullFaceMode.Back); GL.Enable(EnableCap.CullFace); GL.FrontFace(FrontFaceDirection.Cw); _simpleMaterial.Bind(); _simpleMaterial.ProjectionMatrix.Set(camera.ComputeProjectionMatrix().ToMatrix4()); _simpleMaterial.ViewMatrix.Set(camera.ComputeCameraMatrix().ToMatrix4()); _simpleMaterial.LightDirection.Set(new Vector3(1, 1, 0).Normalized()); _simpleMaterial.Color.Set(new Vector4(0.9f, 0.9f, 0.9f, 1.0f)); foreach (var chunkedLodTreeNode in visibleChunks) { var renderableMesh = _cache.GetRenderable(chunkedLodTreeNode.Bounds); if (renderableMesh == null) { continue; } renderableMesh.CreateVAO(); renderableMesh.VertexArrayObject.Bind(); var bounds = chunkedLodTreeNode.Bounds; var translation = Matrix4.CreateTranslation((float)bounds.Center.X, 0, (float)bounds.Center.Y); var delta = bounds.Max - bounds.Min; var scale = Matrix4.CreateScale((float)delta.X, 1, (float)delta.Y); var modelMatrix = scale * translation; _simpleMaterial.ModelMatrix.Set(modelMatrix); _simpleMaterial.NormalToWorld3x3.Set(Matrix3.Identity); _simpleMaterial.LightPosition.Set((Vector3)lightPosition); GL.DrawElements(BeginMode.Triangles, renderableMesh.Faces * 3, DrawElementsType.UnsignedInt, 0); renderableMesh.VertexArrayObject.Unbind(); } _simpleMaterial.Unbind(); }
public void Render(ICamera camera, ICamera lodCamera, Vector3d lightPosition) { var transformation = new Matrix4d( new Vector4d(1, 0, 0, 0), new Vector4d(0, 0, 1, 0), new Vector4d(0, 1, 0, 1), new Vector4d(0, 0, 0, 1)); var visibleChunks = _chunkedLod.Calculate( _tree, lodCamera.Width, lodCamera.HorizontalFieldOfView, Vector3d.Transform(lodCamera.Position, transformation), 30, FrustumPlaneExtractor.ExtractRowMajor(transformation * lodCamera.ComputeCameraMatrix() * lodCamera.ComputeProjectionMatrix())); GL.ClearColor(Color4.White); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); GL.CullFace(CullFaceMode.Back); GL.Enable(EnableCap.CullFace); GL.FrontFace(FrontFaceDirection.Cw); _simpleMaterial.Bind(); _simpleMaterial.ProjectionMatrix.Set(camera.ComputeProjectionMatrix().ToMatrix4()); _simpleMaterial.ViewMatrix.Set(camera.ComputeCameraMatrix().ToMatrix4()); _simpleMaterial.LightDirection.Set(new Vector3(1, 1, 0).Normalized()); _simpleMaterial.Color.Set(new Vector4(0.9f, 0.9f, 0.9f, 1.0f)); foreach (var chunkedLodTreeNode in visibleChunks) { var renderableMesh = _cache.GetRenderable(chunkedLodTreeNode.Bounds); if (renderableMesh == null) continue; renderableMesh.CreateVAO(); renderableMesh.VertexArrayObject.Bind(); var bounds = chunkedLodTreeNode.Bounds; var translation = Matrix4.CreateTranslation((float)bounds.Center.X, 0, (float)bounds.Center.Y); var delta = bounds.Max - bounds.Min; var scale = Matrix4.CreateScale((float)delta.X, 1, (float)delta.Y); var modelMatrix = scale * translation; _simpleMaterial.ModelMatrix.Set(modelMatrix); _simpleMaterial.NormalToWorld3x3.Set(new Matrix3(Matrix4.Transpose(modelMatrix.Inverted()))); _simpleMaterial.LightPosition.Set((Vector3)lightPosition); GL.DrawElements(BeginMode.Triangles, renderableMesh.Faces * 3, DrawElementsType.UnsignedInt, 0); renderableMesh.VertexArrayObject.Unbind(); } _simpleMaterial.Unbind(); }
public void Render(ICamera camera, Vector3d lightPosition) { _simpleMaterial.Bind(); _simpleMaterial.ProjectionMatrix.Set(camera.ComputeProjectionMatrix().ToMatrix4()); _simpleMaterial.ViewMatrix.Set(camera.ComputeCameraMatrix().ToMatrix4()); _simpleMaterial.Color.Set(new Vector4(0f, 1f, 0f, 1f)); _simpleMaterial.ModelMatrix.Set(Matrix4.CreateTranslation((Vector3)lightPosition)); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); GL.Disable(EnableCap.CullFace); _renderable.VertexArrayObject.Bind(); GL.DrawElements(BeginMode.Triangles, _renderable.Faces * 3, DrawElementsType.UnsignedInt, 0); _renderable.VertexArrayObject.Unbind(); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Enable(EnableCap.CullFace); _simpleMaterial.Unbind(); }
public void Render(ICamera camera, Vector3d lightPosition) { _simpleMaterial.Bind(); _simpleMaterial.ProjectionMatrix.Set(camera.ComputeProjectionMatrix().ToMatrix4()); _simpleMaterial.ViewMatrix.Set(camera.ComputeCameraMatrix().ToMatrix4()); _simpleMaterial.Color.Set(new Vector4(0f, 1f, 0f, 1f)); _simpleMaterial.ModelMatrix.Set(Matrix4.CreateTranslation((Vector3)lightPosition)); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); GL.Disable(EnableCap.CullFace); _renderable.VertexArrayObject.Bind(); GL.DrawElements(BeginMode.Triangles, _renderable.Faces * 3, DrawElementsType.UnsignedInt, 0); _renderable.VertexArrayObject.Unbind(); GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); GL.Enable(EnableCap.CullFace); _simpleMaterial.Unbind(); }
public static Vector4d[] ExtractRowMajor(this ICamera camera) { var matrix = camera.ComputeCameraMatrix() * camera.ComputeProjectionMatrix(); return(ExtractRowMajor(matrix)); }
public void Update(double elapsedTime, EntityManager entityManager) { GL.ClearColor(Color4.White); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); GL.CullFace(CullFaceMode.Back); GL.Enable(EnableCap.CullFace); GL.FrontFace(FrontFaceDirection.Cw); _simpleMaterial.Bind(); var light = entityManager.GetEntitiesWithComponent <PositionalLightComponent>().Single(); var positionalLightComponent = entityManager.GetComponent <PositionalLightComponent>(light); _simpleMaterial.LightPosition.Set((Vector3)positionalLightComponent.Position); _simpleMaterial.ProjectionMatrix.Set(_camera.ComputeProjectionMatrix().ToMatrix4()); _simpleMaterial.ViewMatrix.Set(_camera.ComputeCameraMatrix().ToMatrix4()); foreach (var entity in entityManager.GetEntitiesWithComponent <StaticMesh>()) { var component = entityManager.GetComponent <StaticMesh>(entity); //if(!component.IsVisible) //continue; //component.IsVisible = false; var resources = EnsureResources(component); resources.VertexArrayObject.Bind(); _simpleMaterial.ModelMatrix.Set(component.ModelMatrix); _simpleMaterial.Color.Set(component.Color); //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); //GL.Disable(EnableCap.CullFace); GL.DrawElements(BeginMode.Triangles, component.Mesh.Faces.Length * 3, DrawElementsType.UnsignedInt, 0); //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); //GL.Enable(EnableCap.CullFace); resources.VertexArrayObject.Unbind(); } _simpleMaterial.Unbind(); _normalDebugProgram.Bind(); _normalDebugProgram.ProjectionMatrix.Set(_camera.ComputeProjectionMatrix().ToMatrix4()); _normalDebugProgram.ViewMatrix.Set(_camera.ComputeCameraMatrix().ToMatrix4()); foreach (var entity in entityManager.GetEntitiesWithComponent <NormalComponent>()) { var component = entityManager.GetComponent <StaticMesh>(entity); var resources = EnsureResources(component); resources.VertexArrayObject.Bind(); _normalDebugProgram.ModelMatrix.Set(component.ModelMatrix); GL.DrawElements(BeginMode.Triangles, component.Mesh.Faces.Length * 3, DrawElementsType.UnsignedInt, 0); resources.VertexArrayObject.Unbind(); } _normalDebugProgram.Unbind(); }