private void SetupBasicEffect(BasicEffect effect, CCamera camera, C3DRenderable model, Scene scene, CTransform transform, ModelMesh mesh, Matrix anim, Matrix boneTransform, LightingConfig config) { // TODO: we want the same lighting everywhere so no cheating with this ;) (other shaders dont have this) effect.EnableDefaultLighting(); effect.PreferPerPixelLighting = true; effect.VertexColorEnabled = model.enableVertexColor; effect.LightingEnabled = true; effect.AmbientLightColor = config.AmbientColor; effect.DirectionalLight0.Enabled = true; effect.DirectionalLight0.Direction = config.Direction; effect.DirectionalLight0.DiffuseColor = config.DiffuseColor; effect.DirectionalLight0.SpecularColor = config.SpecularColor * model.specular; effect.DirectionalLight1.Enabled = true; effect.DirectionalLight1.DiffuseColor = config.DiffuseColor * 0.7f; effect.DirectionalLight1.SpecularColor = config.SpecularColor * model.specular; effect.DirectionalLight2.Enabled = true; effect.DirectionalLight2.DiffuseColor = config.DiffuseColor * 0.5f; effect.DirectionalLight2.SpecularColor = config.SpecularColor * model.specular; effect.SpecularPower = 100; effect.FogEnabled = config.FogEnabled; effect.FogStart = config.FogStart; effect.FogEnd = config.FogEnd; effect.FogColor = config.ClearColor; effect.Projection = camera.Projection; effect.View = camera.View; effect.World = boneTransform * anim * transform.Frame; }
public void DrawScene(CCamera camera, int excludeEid = -1) { // TODO: Clean code below up, hard to read. Game1.Inst.GraphicsDevice.Clear(new Color(Game1.Inst.Scene.LightConfig.ClearColor)); Game1.Inst.GraphicsDevice.DepthStencilState = DepthStencilState.Default; foreach (CTransform transformComponent in Game1.Inst.Scene.GetComponents <CTransform>().Values) { transformComponent.Frame = Matrix.CreateScale(transformComponent.Scale) * transformComponent.Rotation * Matrix.CreateTranslation(transformComponent.Position); } var numDraws1 = 0; var numDraws = 0; var device = Game1.Inst.GraphicsDevice; var scene = Game1.Inst.Scene; var frustrum = camera.Frustum; foreach (var component in scene.GetComponents <C3DRenderable>()) { numDraws1++; var key = component.Key; if (key == excludeEid) { // TODO: This is originally a hack to simplify rendering of environment maps. continue; } C3DRenderable model = (C3DRenderable)component.Value; if (model.model == null) { continue; // TODO: <- Should be an error, not silent fail? } CTransform transform = (CTransform)scene.GetComponentFromEntity <CTransform>(key); Matrix[] bones = new Matrix[model.model.Bones.Count]; model.model.CopyAbsoluteBoneTransformsTo(bones); var anim = Matrix.Identity; if (model.animFn != null) { anim = model.animFn(mT); } if (Game1.Inst.Scene.EntityHasComponent <CPlayer>(key)) { var cp = (CPlayer)Game1.Inst.Scene.GetComponentFromEntity <CPlayer>(key); if (cp.HitId > 0) { var playerData = (CHit)Game1.Inst.Scene.GetComponentFromEntity <CHit>(cp.HitId); if (playerData.IsAttacking) { bones[1] *= Matrix.CreateTranslation(0.0f, 0.5f, 0f); bones[1] *= Matrix.CreateRotationX(2 * (float)Math.Sin(-playerData.AnimationProgress)); bones[1] *= Matrix.CreateTranslation(0.0f, -0.5f, 0f); } } } for (int k = 0; k < model.model.Meshes.Count; k++) { var mesh = model.model.Meshes[k]; string tag = model.model.Tag as string; // TODO: This is a really ugly hack. :-( if (tag != "Heightmap") { if (frustrum.Contains(mesh.BoundingSphere.Transform(transform.Frame)) == ContainmentType.Disjoint) { break; } } Effect lastEffect = null; for (var i = 0; i < mesh.MeshParts.Count; i++) { var part = mesh.MeshParts[i]; if (part.PrimitiveCount == 0) { continue; } MaterialShader mat = null; if (model.materials != null) { model.materials.TryGetValue(i, out mat); } var effect = mat?.mEffect ?? part.Effect; if (lastEffect != effect) { if (mat != null) { mat.CamPos = camera.Position; mat.Model = bones[mesh.ParentBone.Index] * anim * transform.Frame; mat.View = camera.View; mat.Proj = camera.Projection; mat.FogStart = Game1.Inst.Scene.LightConfig.FogStart; mat.FogEnd = Game1.Inst.Scene.LightConfig.FogEnd; mat.FogColor = Game1.Inst.Scene.LightConfig.ClearColor; mat.Prerender(); } else { SetupBasicEffect((BasicEffect)effect, camera, model, scene, transform, mesh, anim, bones[mesh.ParentBone.Index], Game1.Inst.Scene.LightConfig); } lastEffect = effect; } device.SetVertexBuffer(part.VertexBuffer); device.Indices = part.IndexBuffer; for (var j = 0; j < effect.CurrentTechnique.Passes.Count; j++) { effect.CurrentTechnique.Passes[j].Apply(); numDraws++; device.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.VertexOffset, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); } } } } //-------------------- // Billboards //-------------------- device.SetVertexBuffer(mBillboardVbo); device.Indices = mBillboardIbo; var bbMat = mBillboardMat; var pass0 = bbMat.mEffect.CurrentTechnique.Passes[0]; bbMat.CamPos = camera.Position; bbMat.Proj = camera.Projection; bbMat.View = camera.View; bbMat.FogStart = scene.LightConfig.FogStart; bbMat.FogEnd = scene.LightConfig.FogEnd; bbMat.FogColor = scene.LightConfig.ClearColor; var camPos = camera.Position; var o = camPos - camera.Target; o.Normalize(); o *= 2.0f; foreach (var e in scene.GetComponents <CBillboard>()) { var bb = (CBillboard)e.Value; var bbRot = Matrix.CreateBillboard(bb.Pos, camPos + o, Vector3.Up, null); bbMat.Tex = bb.Tex; bbMat.Model = Matrix.CreateScale(bb.Scale) * bbRot; bbMat.Prerender(); pass0.Apply(); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); } foreach (var component in scene.GetComponents <CWater>()) { var model = (C3DRenderable)component.Value; var key = component.Key; if (key == excludeEid) { // TODO: This is originally a hack to simplify rendering of environment maps. continue; } CTransform transform = (CTransform)scene.GetComponentFromEntity <CTransform>(key); Matrix[] bones = new Matrix[model.model.Bones.Count]; model.model.CopyAbsoluteBoneTransformsTo(bones); foreach (var mesh in model.model.Meshes) { foreach (ModelMeshPart part in mesh.MeshParts) { Matrix world = mesh.ParentBone.Transform * transform.Frame; part.Effect.Parameters["World"].SetValue(world); part.Effect.Parameters["View"].SetValue(camera.View); part.Effect.Parameters["Projection"].SetValue(camera.Projection); part.Effect.Parameters["CameraPosition"].SetValue(camera.Position); part.Effect.Parameters["Time"].SetValue(mT); foreach (var pass in part.Effect.CurrentTechnique.Passes) { pass.Apply(); } mesh.Draw(); } } } // - }
private void createEnvMapping() { C3DRenderable model = null; CTransform transform = null; foreach (var component in Game1.Inst.Scene.GetComponents <C3DRenderable>()) { var key = component.Key; model = (C3DRenderable)component.Value; if (model.effect == null) { continue; } transform = (CTransform)Game1.Inst.Scene.GetComponentFromEntity <CTransform>(key); } RenderTarget2D renderTarget = new RenderTarget2D(Game1.Inst.GraphicsDevice, 256, 256); Game1.Inst.GraphicsDevice.Clear(Color.Black); Matrix view = Matrix.Identity; for (int i = 0; i < 6; i++) { // render the scene to all cubemap faces CubeMapFace cubeMapFace = (CubeMapFace)i; switch (cubeMapFace) { case CubeMapFace.NegativeX: { view = Matrix.CreateLookAt(transform.Position, Vector3.Left, Vector3.Up); break; } case CubeMapFace.NegativeY: { view = Matrix.CreateLookAt(transform.Position, Vector3.Down, Vector3.Forward); break; } case CubeMapFace.NegativeZ: { view = Matrix.CreateLookAt(transform.Position, Vector3.Backward, Vector3.Up); break; } case CubeMapFace.PositiveX: { view = Matrix.CreateLookAt(transform.Position, Vector3.Right, Vector3.Up); break; } case CubeMapFace.PositiveY: { view = Matrix.CreateLookAt(transform.Position, Vector3.Up, Vector3.Backward); break; } case CubeMapFace.PositiveZ: { view = Matrix.CreateLookAt(transform.Position, Vector3.Forward, Vector3.Up); break; } } // Set the cubemap render target, using the selected face Game1.Inst.GraphicsDevice.SetRenderTarget(renderTarget); Game1.Inst.GraphicsDevice.Clear(Color.White); drawNormal(view); //skysystem.DrawOnRequest(view); Color[] colordata = new Color[256 * 256]; renderTarget.GetData(colordata); model.environmentMap.SetData((CubeMapFace)i, colordata); //DEBUG Stream stream = File.Create("test" + i + ".jpg"); renderTarget.SaveAsJpeg(stream, 256, 256); stream.Dispose(); // restore our matrix after changing during the cube map rendering //view = Matrix.CreateLookAt(new Vector3(0, 0, 0), Vector3.Zero, Vector3.Up); } Game1.Inst.GraphicsDevice.SetRenderTarget(null); }
public void drawNormal(Matrix view) { Game1.Inst.GraphicsDevice.DepthStencilState = DepthStencilState.Default; int counter = 0; foreach (CCamera camera in Game1.Inst.Scene.GetComponents <CCamera>().Values) { foreach (var component in Game1.Inst.Scene.GetComponents <C3DRenderable>()) { var key = component.Key; C3DRenderable model = (C3DRenderable)component.Value; if (model.model == null) { continue; } CTransform transform = (CTransform)Game1.Inst.Scene.GetComponentFromEntity <CTransform>(key); var objectWorld = transform.ObjectWorld; foreach (ModelMesh modelMesh in model.model.Meshes) { { if (model.effect != null) { if (view.Equals(Matrix.Identity)) { view = camera.View; } foreach (ModelMeshPart part in modelMesh.MeshParts) { part.Effect = model.effect; part.Effect.Parameters["World"].SetValue(modelMesh.ParentBone.Transform * objectWorld * world); part.Effect.Parameters["View"].SetValue(view); part.Effect.Parameters["Projection"].SetValue(camera.Projection); part.Effect.Parameters["AmbientColor"].SetValue(Color.White.ToVector3()); part.Effect.Parameters["AmbientIntensity"].SetValue(0.1f); part.Effect.Parameters["LightDirection"].SetValue(new Vector3(0, 1f, 0)); part.Effect.Parameters["SpecularPower"].SetValue(400f); part.Effect.Parameters["SpecularColor"].SetValue(Color.Gray.ToVector3()); part.Effect.Parameters["SpecularIntensity"].SetValue(3.0f); part.Effect.Parameters["ViewVector"].SetValue(viewVector); part.Effect.Parameters["ModelTexture"].SetValue(model.texture); part.Effect.Parameters["BumpConstant"].SetValue(1f); part.Effect.Parameters["NormalMap"].SetValue(model.normalMap); part.Effect.Parameters["EnvTexture"].SetValue(model.environmentMap); } modelMesh.Draw(); } if (model.effect == null) { foreach (BasicEffect effect in modelMesh.Effects) { effect.World = modelMesh.ParentBone.Transform * objectWorld * world; effect.View = camera.View; effect.Projection = camera.Projection; effect.EnableDefaultLighting(); effect.LightingEnabled = true; effect.DirectionalLight0.DiffuseColor = new Vector3(1f, 1f, 1f); effect.DirectionalLight0.Direction = new Vector3(-0.5f, 1f, -3.5f); effect.DirectionalLight0.SpecularColor = new Vector3(-0.1f, -0.1f, -0.1f); foreach (EffectPass p in effect.CurrentTechnique.Passes) { p.Apply(); modelMesh.Draw(); } } } } } } } }
public void DrawScene(CCamera camera, int excludeEid = -1) { // TODO: Clean code below up, hard to read. foreach (CTransform transformComponent in Game1.Inst.Scene.GetComponents <CTransform>().Values) { transformComponent.Frame = Matrix.CreateScale(transformComponent.Scale) * transformComponent.Rotation * Matrix.CreateTranslation(transformComponent.Position); } int counter = 0; foreach (var component in Game1.Inst.Scene.GetComponents <C3DRenderable>()) { var key = component.Key; if (key == excludeEid) { // TODO: This is originally a hack to simplify rendering of environment maps. continue; } C3DRenderable model = (C3DRenderable)component.Value; if (model.model == null) { continue; // TODO: <- Should be an error, not silent fail? } CTransform transform = (CTransform)Game1.Inst.Scene.GetComponentFromEntity <CTransform>(key); foreach (var mesh in model.model.Meshes) { if (camera.Frustum.Contains(mesh.BoundingSphere.Transform(transform.Frame)) == ContainmentType.Disjoint) { continue; } // TODO: This might bug out with multiple mesh parts. if (model.material != null) { model.material.Model = mesh.ParentBone.Transform * transform.Frame; model.material.View = camera.View; model.material.Proj = camera.Projection; model.material.Prerender(); model.material.mEffect.Parameters["BumpPower"].SetValue(bump); model.material.mEffect.Parameters["FogColor"].SetValue(new Vector4(0.4f, 0.7f, 1.0f, 1.0f)); model.material.mEffect.Parameters["FogStart"].SetValue(20.0f); model.material.mEffect.Parameters["FogEnd"].SetValue(200.0f); model.material.mEffect.Parameters["LightDir"].SetValue(lightDir); var device = Game1.Inst.GraphicsDevice; for (var i = 0; i < mesh.MeshParts.Count; i++) { var part = mesh.MeshParts[i]; var effect = model.material.mEffect; device.SetVertexBuffer(part.VertexBuffer); device.Indices = part.IndexBuffer; for (var j = 0; j < effect.CurrentTechnique.Passes.Count; j++) { effect.CurrentTechnique.Passes[j].Apply(); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.VertexOffset, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); } } } else { foreach (BasicEffect effect in mesh.Effects) { //effect.EnableDefaultLighting(); effect.LightingEnabled = true; effect.DirectionalLight0.Enabled = true; //effect.AmbientLightColor = new Vector3(1f, 1f, 1f); effect.DirectionalLight0.DiffuseColor = new Vector3(0.3f, 0.3f, 0.3f); effect.DirectionalLight0.Direction = lightDir; //effect.SpecularColor = new Vector3(1, 1, 1); effect.SpecularPower = 100; effect.DirectionalLight0.SpecularColor = new Vector3(1f, 1f, 1f); effect.PreferPerPixelLighting = true; effect.FogEnabled = true; effect.FogStart = 20.0f; effect.FogEnd = 200.0f; effect.FogColor = new Vector3(0.4f, 0.7f, 1.0f); effect.Projection = camera.Projection; effect.View = camera.View; effect.World = mesh.ParentBone.Transform * transform.Frame; } mesh.Draw(); } } } }