public static void Main(string[] args) { WindowCreateInfo windowCI = new WindowCreateInfo() { X = 100, Y = 100, WindowWidth = 960, WindowHeight = 540, WindowTitle = "Veldrid TinyDemo", }; RenderContextCreateInfo contextCI = new RenderContextCreateInfo(); VeldridStartup.CreateWindowAndRenderContext(ref windowCI, ref contextCI, out var window, out RenderContext rc); VertexBuffer vb = rc.ResourceFactory.CreateVertexBuffer(Cube.Vertices, new VertexDescriptor(VertexPositionColor.SizeInBytes, 2), false); IndexBuffer ib = rc.ResourceFactory.CreateIndexBuffer(Cube.Indices, false); string folder = rc.BackendType == GraphicsBackend.Direct3D11 ? "HLSL" : "GLSL"; string extension = rc.BackendType == GraphicsBackend.Direct3D11 ? "hlsl" : "glsl"; VertexInputLayout inputLayout = rc.ResourceFactory.CreateInputLayout(new VertexInputDescription[] { new VertexInputDescription( new VertexInputElement("Position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("Color", VertexSemanticType.Color, VertexElementFormat.Float4)) }); string vsPath = Path.Combine(AppContext.BaseDirectory, folder, $"vertex.{extension}"); string fsPath = Path.Combine(AppContext.BaseDirectory, folder, $"fragment.{extension}"); Shader vs = rc.ResourceFactory.CreateShader(ShaderStages.Vertex, File.ReadAllText(vsPath)); Shader fs = rc.ResourceFactory.CreateShader(ShaderStages.Fragment, File.ReadAllText(fsPath)); ShaderSet shaderSet = rc.ResourceFactory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots bindingSlots = rc.ResourceFactory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ViewProjectionMatrix", ShaderConstantType.Matrix4x4)); ConstantBuffer viewProjectionBuffer = rc.ResourceFactory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); while (window.Exists) { InputSnapshot snapshot = window.PumpEvents(); rc.ClearBuffer(); rc.SetViewport(0, 0, window.Width, window.Height); float timeFactor = Environment.TickCount / 1000f; viewProjectionBuffer.SetData( Matrix4x4.CreateLookAt( new Vector3(2 * (float)Math.Sin(timeFactor), (float)Math.Sin(timeFactor), 2 * (float)Math.Cos(timeFactor)), Vector3.Zero, Vector3.UnitY) * Matrix4x4.CreatePerspectiveFieldOfView(1.05f, (float)window.Width / window.Height, .5f, 10f)); rc.SetVertexBuffer(0, vb); rc.IndexBuffer = ib; rc.ShaderSet = shaderSet; rc.ShaderResourceBindingSlots = bindingSlots; rc.SetConstantBuffer(0, viewProjectionBuffer); rc.DrawIndexedPrimitives(Cube.Indices.Length); rc.SwapBuffers(); } }
public void Dispose() { if (VertexBuffer != null && !VertexBuffer.Disposed) { VertexBuffer.Dispose(); } if (VertexInputLayout != null && !VertexInputLayout.Disposed) { VertexInputLayout.Dispose(); } if (SpriteEffect != null && !SpriteEffect.Disposed) { SpriteEffect.Dispose(); } if (SpriteTexture != null && !SpriteTexture.Disposed) { SpriteTexture.Dispose(); } if (mapedBitmap != null) { mapedBitmap.Dispose(); } if (mapedGraphic != null) { mapedGraphic.Dispose(); } }
protected override void LoadContent() { // Creates a basic effect basicEffect = ToDisposeContent(new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, View = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY), Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f), World = Matrix.Identity }); // Creates vertices for the cube vertices = ToDisposeContent(Buffer.Vertex.New( GraphicsDevice, new[] { new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), // Front new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), // BACK new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), // Top new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), // Bottom new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), // Left new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), // Right new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange), })); ToDisposeContent(vertices); // Create an input layout from the vertices inputLayout = VertexInputLayout.FromBuffer(0, vertices); base.LoadContent(); }
public MyModel(Project1Game game, VertexPositionColor[] shapeArray, String textureName) { this.vertices = Buffer.Vertex.New(game.GraphicsDevice, shapeArray); this.inputLayout = VertexInputLayout.New<VertexPositionColor>(0); vertexStride = Utilities.SizeOf<VertexPositionColor>(); modelType = ModelType.Colored; }
/// <summary> /// Creates a <see cref="ShaderSet"/> from the given vertex inputs and shaders. /// </summary> /// <param name="inputLayout">The device-specific vertex input layout of the vertex shader.</param> /// <param name="vertexShader">The vertex shader.</param> /// <param name="tessellationControlShader">The tessellation control shader.</param> /// <param name="tessellationEvaluationShader">The tessellation evaluation shader.</param> /// <param name="geometryShader">The geometry shader.</param> /// <param name="fragmentShader">The fragment shader.</param> /// <returns></returns> public abstract ShaderSet CreateShaderSet( VertexInputLayout inputLayout, Shader vertexShader, Shader tessellationControlShader, Shader tessellationEvaluationShader, Shader geometryShader, Shader fragmentShader);
public D3DShaderSet(VertexInputLayout inputLayout, Shader vertexShader, Shader geometryShader, Shader fragmentShader) { InputLayout = (D3DVertexInputLayout)inputLayout; VertexShader = (D3DVertexShader)vertexShader; GeometryShader =(D3DGeometryShader)geometryShader; FragmentShader = (D3DFragmentShader)fragmentShader; }
public static ShaderSet LoadSet(ResourceFactory factory, string setName, VertexInputLayout inputLayout) { Shader vertexShader = LoadShader(factory, setName, ShaderStages.Vertex); Shader fragmentShader = LoadShader(factory, setName, ShaderStages.Fragment); return(factory.CreateShaderSet(inputLayout, vertexShader, fragmentShader)); }
public static Material CreateRegularPassMaterial(ResourceFactory factory) { Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("shadow-vertex", ShaderStages.Vertex, factory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("shadow-frag", ShaderStages.Fragment, factory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription( 32, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightInfoBuffer", ShaderConstantType.Float4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("SurfaceTexture", ShaderResourceType.Texture), new ShaderResourceDescription("SurfaceTexture", ShaderResourceType.Sampler), new ShaderResourceDescription("ShadowMap", ShaderResourceType.Texture), new ShaderResourceDescription("ShadowMap", ShaderResourceType.Sampler)); return(new Material(shaderSet, constantSlots)); }
public override void SetGeometry(IGraphicsContext context) { context.Device.SetVertexBuffer(0, VertexBuffer); context.Device.SetVertexBuffer(1, InstanceData); context.Device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, VertexBuffer)); context.Device.SetVertexInputLayout(VertexInputLayout.FromBuffer(1, InstanceData)); }
public ParticleSystem(GraphicsDevice device, ContentManager content) { this.device = device; // Create vertex buffer used to spawn new particles this.particleStart = Buffer.Vertex.New<ParticleVertex>(device, MAX_NEW); // Create vertex buffers to use for updating and drawing the particles alternatively var vbFlags = BufferFlags.VertexBuffer | BufferFlags.StreamOutput; this.particleDrawFrom = Buffer.New<ParticleVertex>(device, MAX_PARTICLES, vbFlags); this.particleStreamTo = Buffer.New<ParticleVertex>(device, MAX_PARTICLES, vbFlags); this.layout = VertexInputLayout.FromBuffer(0, this.particleStreamTo); this.effect = content.Load<Effect>("ParticleEffect"); this.texture = content.Load<Texture2D>("Dot"); this.viewParameter = effect.Parameters["_view"]; this.projParameter = effect.Parameters["_proj"]; this.lookAtMatrixParameter = effect.Parameters["_lookAtMatrix"]; this.elapsedSecondsParameter = effect.Parameters["_elapsedSeconds"]; this.camDirParameter = effect.Parameters["_camDir"]; this.gravityParameter = effect.Parameters["_gravity"]; this.textureParameter = effect.Parameters["_texture"]; this.samplerParameter = effect.Parameters["_sampler"]; this.updatePass = effect.Techniques["UpdateTeq"].Passes[0]; this.renderPass = effect.Techniques["RenderTeq"].Passes[0]; }
private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(1024, true); _ib = factory.CreateIndexBuffer(1024, true); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("wireframe-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("wireframe-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4)); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots cbs = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); _material = new Material(shaderSet, cbs); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); Matrix4x4 identity = Matrix4x4.Identity; _worldBuffer.SetData(ref identity, 64); _wireframeState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); }
private Project1Game gameaccess; //Access to public game functions public Landscape(Project1Game game, int degree) { this.degree = degree; this.size = (int)Math.Pow(2, this.degree) + 1; this.maxheight = this.size / 2; this.polycount = (int)Math.Pow(this.size - 1, 2) * 2; this.rngesus = new Random(); this.coords = new float[size, size]; //Generate the heightmap using DiamondSquare Generate(0, this.size, 0, size, maxheight, size / 2); //Generate the terrain model this.terrain = TerrainModel(this.coords); //Place terrain model into vertex buffer vertices = Buffer.Vertex.New(game.GraphicsDevice, TerrainModel(this.coords)); basicEffect = new BasicEffect(game.GraphicsDevice) { VertexColorEnabled = true, LightingEnabled = true, View = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY), Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, 0.1f, 1000.0f), World = Matrix.Identity }; inputLayout = VertexInputLayout.FromBuffer(0, vertices); this.gameaccess = game; this.game = game; }
public ParticleSystem(GraphicsDevice device, ContentManager content) { this.device = device; // Create vertex buffer used to spawn new particles this.particleStart = Buffer.Vertex.New <ParticleVertex>(device, MAX_NEW); // Create vertex buffers to use for updating and drawing the particles alternatively var vbFlags = BufferFlags.VertexBuffer | BufferFlags.StreamOutput; this.particleDrawFrom = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags); this.particleStreamTo = Buffer.New <ParticleVertex>(device, MAX_PARTICLES, vbFlags); this.layout = VertexInputLayout.FromBuffer(0, this.particleStreamTo); this.effect = content.Load <Effect>("ParticleEffect"); this.texture = content.Load <Texture2D>("Dot"); this.viewParameter = effect.Parameters["_view"]; this.projParameter = effect.Parameters["_proj"]; this.lookAtMatrixParameter = effect.Parameters["_lookAtMatrix"]; this.elapsedSecondsParameter = effect.Parameters["_elapsedSeconds"]; this.camDirParameter = effect.Parameters["_camDir"]; this.gravityParameter = effect.Parameters["_gravity"]; this.textureParameter = effect.Parameters["_texture"]; this.samplerParameter = effect.Parameters["_sampler"]; this.updatePass = effect.Techniques["UpdateTeq"].Passes[0]; this.renderPass = effect.Techniques["RenderTeq"].Passes[0]; }
protected override void LoadContent() { //initialize the array of cubes Random random = new Random(); for (int i = 0; i < cubes.Length; ++i) { cubes[i] = new Cube(random); } for (int i = 0; i < lights.Length; ++i) { lights[i] = new Light(random); } //Instantiate a SpriteBatch spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice)); // Loads a sprite font // The [Arial16.xml] file is defined with the build action [ToolkitFont] in the project arial16Font = Content.Load <SpriteFont>("Arial16"); skyEffect = Content.Load <Effect>("skybox"); skyEffect.Parameters["SkyBoxTexture"].SetResource(Content.Load <TextureCube>("SkyBoxTexture")); basicEffect = new BasicEffect(GraphicsDevice); cubeEffect = Content.Load <Effect>("point"); //textures[0] = Content.Load<Texture2D>("gorko"); //textures[1] = Content.Load<Texture2D>("hunsberger"); //textures[2] = Content.Load<Texture2D>("kontostathis"); //textures[3] = Content.Load<Texture2D>("mcdevitt"); //textures[4] = Content.Load<Texture2D>("mustardo"); //textures[5] = Content.Load<Texture2D>("young"); vertices = new VertexPositionNormalTexture[4] { new VertexPositionNormalTexture(new Vector3(0, 1, 0), Vector3.UnitZ, new Vector2(0, 0)), new VertexPositionNormalTexture(new Vector3(1, 1, 0), Vector3.UnitZ, new Vector2(1, 0)), new VertexPositionNormalTexture(new Vector3(0, 0, 0), Vector3.UnitZ, new Vector2(0, 1)), new VertexPositionNormalTexture(new Vector3(1, 0, 0), Vector3.UnitZ, new Vector2(1, 1)) }; vertexBuffer = Buffer.New <VertexPositionNormalTexture>(GraphicsDevice, 4, BufferFlags.VertexBuffer); vertexBuffer.SetData <VertexPositionNormalTexture>(vertices); inputLayout = VertexInputLayout.New <VertexPositionNormalTexture>(0); short[] indices = new short[4] { 0, 1, 2, 3 }; indexBuffer = Buffer.New <short>(GraphicsDevice, 4, BufferFlags.IndexBuffer); indexBuffer.SetData <short>(indices); primitive = ToDisposeContent(GeometricPrimitive.Cube.New(GraphicsDevice)); Cube.LoadCube(GraphicsDevice, primitive); ballPrimitive = ToDisposeContent(GeometricPrimitive.Sphere.New(GraphicsDevice)); base.LoadContent(); }
public MyModel(LabGame game, VertexPositionColor[] shapeArray, String textureName, float collisionRadius) { this.vertices = Buffer.Vertex.New(game.GraphicsDevice, shapeArray); this.inputLayout = VertexInputLayout.New<VertexPositionColor>(0); vertexStride = Utilities.SizeOf<VertexPositionColor>(); modelType = ModelType.Colored; this.collisionRadius = collisionRadius; }
public void Dispose() { D3D頂点バッファ?.Dispose(); D3D頂点バッファ = null; VertexInputLayout?.Dispose(); VertexInputLayout = null; }
public SkyBox(GameCore core) { this.core = core; this.effect = this.core.ContentManager.Load <Effect>("Effects/Sky"); this.texture = this.core.ContentManager.Load <TextureCube>("Textures/World/skyBox"); SetUpVertices(); SetUpIndices(); this.vertexInputLayout = VertexInputLayout.FromBuffer(0, this.vertexBuffer); }
public Cube(LabGame game) { rand = new Random(); Vector3 frontNormal = new Vector3(0.0f, 0.0f, -1.0f); Vector3 backNormal = new Vector3(0.0f, 0.0f, 1.0f); Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f); Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f); Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f); Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f); vertices = Buffer.Vertex.New( game.GraphicsDevice, new[] { new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), frontNormal, Color.Orange), // Front new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, -1.0f), frontNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, -1.0f), frontNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), frontNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, -1.0f), frontNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, -1.0f), frontNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, 1.0f), backNormal, Color.Orange), // BACK new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), backNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, 1.0f), backNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, 1.0f), backNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, 1.0f), backNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), backNormal, Color.Orange), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, -1.0f), topNormal, Color.OrangeRed), // Top new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, 1.0f), topNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), topNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, -1.0f), topNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), topNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, -1.0f), topNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), bottomNormal, Color.OrangeRed), // Bottom new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, 1.0f), bottomNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, 1.0f), bottomNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), bottomNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, -1.0f), bottomNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, 1.0f), bottomNormal, Color.OrangeRed), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), leftNormal, Color.DarkOrange), // Left new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, 1.0f), leftNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, 1.0f), leftNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(-1.0f, -1.0f, -1.0f), leftNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, 1.0f), leftNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(-1.0f, 1.0f, -1.0f), leftNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, -1.0f), rightNormal, Color.DarkOrange), // Right new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), rightNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, 1.0f), rightNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(1.0f, -1.0f, -1.0f), rightNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, -1.0f), rightNormal, Color.DarkOrange), new VertexPositionNormalColor(new Vector3(1.0f, 1.0f, 1.0f), rightNormal, Color.DarkOrange), }); effect = game.Content.Load <Effect>("myShader"); inputLayout = VertexInputLayout.FromBuffer(0, vertices); this.game = game; }
public override ShaderSet CreateShaderSet(VertexInputLayout inputLayout, Shader vertexShader, Shader geometryShader, Shader fragmentShader) { return(new OpenGLShaderSet( (OpenGLVertexInputLayout)inputLayout, (OpenGLShader)vertexShader, null, null, (OpenGLShader)geometryShader, (OpenGLShader)fragmentShader)); }
public Cube(Lab4Game game) { vertices = Buffer.Vertex.New( game.GraphicsDevice, new[] { new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), // Front new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), // BACK new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), // Top new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), // Bottom new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), // Left new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), // Right new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange), }); basicEffect = new BasicEffect(game.GraphicsDevice) { VertexColorEnabled = true, View = Matrix.LookAtLH(new Vector3(0, 0, -10), new Vector3(0, 0, 0), Vector3.UnitY), Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)game.GraphicsDevice.BackBuffer.Width / game.GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f), World = Matrix.Identity }; inputLayout = VertexInputLayout.FromBuffer(0, vertices); this.game = game; }
void LoadContent() { m_basicEffect = ToDispose(new BasicEffect(this.GraphicsDevice)); m_basicEffect.VertexColorEnabled = true; m_basicEffect.LightingEnabled = false; m_plane = ToDispose(GeometricPrimitive.Plane.New(this.GraphicsDevice, 2, 2, 1, true)); m_buffer = ToDispose(Buffer <VertexPositionColor> .Vertex.New(this.GraphicsDevice, m_vertices)); m_layout = VertexInputLayout.FromBuffer(0, m_buffer); }
public void GetOrCreate(VertexInputLayout layout, out InputLayoutPair currentPassPreviousPair) { lock (Cache) { if (!Cache.TryGetValue(layout, out currentPassPreviousPair)) { currentPassPreviousPair = new InputLayoutPair(layout, ToDispose(new InputLayout(device, Bytecode, layout.InputElements))); Cache.Add(layout, currentPassPreviousPair); } } }
public unsafe void ChangeRenderContext(AssetDatabase ad, RenderContext rc) { var factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(s_vertices.Length * VertexPosition.SizeInBytes, false); _vb.SetVertexData(s_vertices, new VertexDescriptor(VertexPosition.SizeInBytes, 1, IntPtr.Zero)); _ib = factory.CreateIndexBuffer(s_indices.Length * sizeof(int), false); _ib.SetIndices(s_indices); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("skybox-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("skybox-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription( 12, new VertexInputElement("position", VertexSemanticType.Position, VertexElementFormat.Float3))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("Skybox", ShaderResourceType.Texture), new ShaderResourceDescription("Skybox", ShaderResourceType.Sampler)); _material = new Material(shaderSet, constantSlots); _viewMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); fixed(Rgba32 *frontPin = &_front.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * backPin = &_back.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * leftPin = &_left.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * rightPin = &_right.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * topPin = &_top.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * bottomPin = &_bottom.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) { var cubemapTexture = factory.CreateCubemapTexture( (IntPtr)frontPin, (IntPtr)backPin, (IntPtr)leftPin, (IntPtr)rightPin, (IntPtr)topPin, (IntPtr)bottomPin, _front.Width, _front.Height, _front.PixelSizeInBytes, _front.Format); _cubemapBinding = factory.CreateShaderTextureBinding(cubemapTexture); } _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, false, false); _viewProvider = new DependantDataProvider <Matrix4x4>(SharedDataProviders.GetProvider <Matrix4x4>("ViewMatrix"), Utilities.ConvertToMatrix3x3); }
private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; MeshData sphere = ad.LoadAsset <ObjFile>(new AssetID("Models/Sphere.obj")).GetFirstMesh(); Vector3[] spherePositions = sphere.GetVertexPositions(); _sphereGeometryVB = factory.CreateVertexBuffer(spherePositions.Length * 12, false); _sphereGeometryVB.SetVertexData(spherePositions, new VertexDescriptor(12, 1)); _ib = sphere.CreateIndexBuffer(factory, out _indexCount); Random r = new Random(); int width = InstanceRows; InstanceData[] instanceData = new InstanceData[width * width * width]; for (int z = 0; z < width; z++) { for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { instanceData[z * width * width + y * width + x] = new InstanceData( new Vector3(x * 10, y * 10, z * 10), new RgbaFloat((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble())); } } } _instanceVB = factory.CreateVertexBuffer(instanceData.Length * InstanceData.SizeInBytes, false); _instanceVB.SetVertexData(instanceData, new VertexDescriptor(InstanceData.SizeInBytes, 2, 0, IntPtr.Zero)); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("instanced-simple-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("instanced-simple-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription(VertexPosition.SizeInBytes, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3)), new VertexInputDescription( InstanceData.SizeInBytes, new VertexInputElement("in_offset", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float3, VertexElementInputClass.PerInstance, 1), new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Float4, VertexElementInputClass.PerInstance, 1))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantBindings = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); _material = new Material(shaderSet, constantBindings); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); Matrix4x4 identity = Matrix4x4.Identity; _worldBuffer.SetData(ref identity, 64); }
public void RenderDirectionalLight(DirectionalLight light) { // set gbuffer targets as texture inputs this.SetAsShaderInput(gbufferEffect); // set light cbuffer parameters gbufferEffect.Parameters["dLightDirection"].SetValue(light.Direction); gbufferEffect.Parameters["dLightColor"].SetValue(light.Color); context.Device.SetVertexBuffer(lightVb); context.Device.SetVertexInputLayout(VertexInputLayout.FromBuffer(0, lightVb)); gbufferEffect.Techniques["DirectionalLight"].Passes[0].Apply(); context.Device.Draw(PrimitiveType.TriangleList, 6); }
/// <summary> /// Generates the complete grass field. /// </summary> private void GenerateField(int numberOfPatchRows = 50, int numberOfRootsInPatch = 70) { this.NumberOfPatchRows = numberOfPatchRows; this.NumberOfRootsInPatch = numberOfRootsInPatch; if (this.terrainSize > 1000) { this.NumberOfPatchRows = 160; } else if (this.terrainSize > 500) { this.NumberOfPatchRows = 190; } else { this.NumberOfPatchRows = 75; } this.NumberOfPatches = this.NumberOfPatchRows * this.NumberOfPatchRows; this.NumberOfRoots = this.NumberOfPatches * this.NumberOfRootsInPatch; this.vertices = new VertexPositionNormalTexture[this.NumberOfRoots]; Random rnd = new Random(); int currentVertex = 0; Vector3 startPosition = new Vector3(0, 0, 0); Vector3 patchSize = new Vector3(terrainSize / this.NumberOfPatchRows, 0, terrainSize / this.NumberOfPatchRows); // Generate grid of patches for (int x = 0; x < this.NumberOfPatchRows; x++) { for (int y = 0; y < this.NumberOfPatchRows; y++) { Vector3 _centerPos = new Vector3(startPosition.X + patchSize.X / 2, heightData[(int)(startPosition.X + patchSize.X / 2), (int)(startPosition.Z + patchSize.Z / 2)], startPosition.Z + patchSize.Z / 2); boundingDict[x * this.NumberOfPatchRows + y] = new BoundingSphere(_centerPos, BOUNDING_RADUIS); currentVertex = this.GeneratePatch(startPosition, patchSize, currentVertex, rnd); startPosition.X += patchSize.X; } startPosition.X = 0; startPosition.Z += patchSize.Z; } this.vertexBuffer = Buffer.Vertex.New(this.core.GraphicsDevice, this.vertices); this.vertexInputLayout = VertexInputLayout.FromBuffer(0, this.vertexBuffer); this.InitializeWind(); }
public Material GetMaterial( RenderContext rc, string vertexShaderName, string geometryShaderName, string fragmentShaderName, MaterialVertexInput vertexInputs, MaterialInputs <MaterialGlobalInputElement> globalInputs, MaterialInputs <MaterialPerObjectInputElement> perObjectInputs, MaterialTextureInputs textureInputs) { MaterialKey key = new MaterialKey() { VertexShaderName = vertexShaderName, FragmentShaderName = fragmentShaderName, GeometryShaderName = geometryShaderName, VertexInputs = vertexInputs, GlobalInputs = globalInputs }; Material m; if (!_materials.TryGetValue(key, out m)) { Shader vs = _factory.CreateShader(ShaderType.Vertex, vertexShaderName); Shader fs = _factory.CreateShader(ShaderType.Fragment, fragmentShaderName); VertexInputLayout inputLayout = _factory.CreateInputLayout(vs, vertexInputs); ShaderSet shaderSet; if (geometryShaderName != null) { Shader gs = _factory.CreateShader(ShaderType.Geometry, geometryShaderName); shaderSet = _factory.CreateShaderSet(inputLayout, vs, gs, fs); } else { shaderSet = _factory.CreateShaderSet(inputLayout, vs, fs); } ShaderConstantBindings constantBindings = _factory.CreateShaderConstantBindings(rc, shaderSet, globalInputs, perObjectInputs); ShaderTextureBindingSlots textureSlots = _factory.CreateShaderTextureBindingSlots(shaderSet, textureInputs); m = new Material(rc, shaderSet, constantBindings, textureSlots, _factory.CreateDefaultTextureBindingInfos(rc, textureInputs)); if (!_materials.TryAdd(key, m)) { return(_materials[key]); } } return(m); }
public Terrain(GameCore core) { this.core = core; this.effect = this.core.ContentManager.Load <Effect>("Effects/Terrain"); this.texture = this.core.ContentManager.Load <Texture2D>("Textures/Terrain/dirt"); this.heightMap = this.core.ContentManager.Load <Texture2D>("Textures/Terrain/heightMap512"); nIndices = (this.heightMap.Width - 1) * (this.heightMap.Height - 1) * 6; LoadHeightData(this.heightMap); SetUpVertices(); SetUpIndices(); GenerateNormals(); this.vertexInputLayout = VertexInputLayout.FromBuffer(0, this.vertexBuffer); }
public D3DShaderSet( VertexInputLayout inputLayout, Shader vertexShader, Shader tessellationControlShader, Shader tessellationEvaluationShader, Shader geometryShader, Shader fragmentShader) { InputLayout = (D3DVertexInputLayout)inputLayout; VertexShader = (D3DVertexShader)vertexShader; TessellationControlShader = (D3DTessellationControlShader)tessellationControlShader; TessellationEvaluationShader = (D3DTessellationEvaluationShader)tessellationEvaluationShader; GeometryShader = (D3DGeometryShader)geometryShader; FragmentShader = (D3DFragmentShader)fragmentShader; }
public void Dispose() { if (BatchDisposing != null) { BatchDisposing(this, new EventArgs()); } if (TextureD3D10 != null && !TextureD3D10.Disposed) { TextureD3D10.Dispose(); } if (TextureD3D11 != null && !TextureD3D11.Disposed) { TextureD3D11.Dispose(); } if (MutexD3D10 != null && !MutexD3D10.Disposed) { MutexD3D10.Dispose(); } if (MutexD3D11 != null && !MutexD3D11.Disposed) { MutexD3D11.Dispose(); } if (DWRenderTarget != null && !DWRenderTarget.Disposed) { DWRenderTarget.Dispose(); } if (VertexBuffer != null && !VertexBuffer.Disposed) { VertexBuffer.Dispose(); } if (VertexInputLayout != null && !VertexInputLayout.Disposed) { VertexInputLayout.Dispose(); } if (SpriteEffect != null && !SpriteEffect.Disposed) { SpriteEffect.Dispose(); } if (sampler != null && !sampler.Disposed) { sampler.Dispose(); } //以下の二つはRenderContextがDisposeされるときに呼ばれないとバグが出る。(画面真っ黒) //これは同じ種類のブレンドステートがDisposeされてしまうからだと予測できる。 //このため、Dispose予定リストに含めておき、RenderContextの破棄時に処理をする context.Disposables.Add(TransParentBlendState); context.Disposables.Add(state); }
public override ShaderSet CreateShaderSet( VertexInputLayout inputLayout, Shader vertexShader, Shader tessellationControlShader, Shader tessellationEvaluationShader, Shader geometryShader, Shader fragmentShader) { return(new OpenGLShaderSet( (OpenGLVertexInputLayout)inputLayout, (OpenGLShader)vertexShader, (OpenGLShader)tessellationControlShader, (OpenGLShader)tessellationEvaluationShader, (OpenGLShader)geometryShader, (OpenGLShader)fragmentShader)); }
public Mesh([NotNull] Buffer <T> vertexBuffer, [NotNull] Effect effect, bool disposeEffect = false) { if (vertexBuffer == null) { throw new ArgumentNullException("vertexBuffer"); } if (effect == null) { throw new ArgumentNullException("effect"); } VertexBuffer = vertexBuffer; _vertexInputLayout = VertexInputLayout.FromBuffer(0, vertexBuffer); _effect = effect; _disposeEffect = disposeEffect; }
public WarpSceneRenderer(Scene scene, int width, int height) { _scene = scene; _width = width; _height = height; _aspectRatio = width / (float)height; _device = GraphicsDevice.New(DriverType.Warp, DeviceCreationFlags.None, FeatureLevel.Level_10_1); var serviceProvider = new ServiceProvider(); serviceProvider.AddService <IGraphicsDeviceService>(new GraphicsDeviceService(_device)); _contentManager = new ContentManager(serviceProvider); _contentManager.Resolvers.Add(new ContentResolver()); var viewport = new Viewport(0, 0, _width, _height); _device.SetViewports(viewport); const MSAALevel msaaLevel = MSAALevel.None; _depthStencilTexture = DepthStencilBuffer.New(_device, _width, _height, msaaLevel, DepthFormat.Depth24Stencil8); _renderTexture = RenderTarget2D.New(_device, _width, _height, msaaLevel, PixelFormat.R8G8B8A8.UNorm); Options = new RenderOptions(); _effect = new BasicEffect(_device); _effect.EnableDefaultLighting(); _inputLayout = VertexInputLayout.New(0, typeof(VertexPositionNormalTexture)); _device.SetVertexInputLayout(_inputLayout); _meshes = new List <WarpMesh>(); foreach (Mesh mesh in _scene.Meshes) { if (!mesh.Positions.Any()) { continue; } var warpMesh = new WarpMesh(_device, mesh); _meshes.Add(warpMesh); warpMesh.Initialize(_contentManager); } }
private void Init(GraphicsDevice graphicsDevice) { _screenTriangle = SharpDX.Toolkit.Graphics.Buffer.Vertex.New(graphicsDevice, new[] { new Vector2(-1.0f, -1.0f), new Vector2(3.0f, -1.0f), new Vector2(-1.0f, 3.0f) }, SharpDX.Direct3D11.ResourceUsage.Immutable); _vertexInputLayout = VertexInputLayout.New( VertexBufferLayout.New(0, new VertexElement[]{new VertexElement("POSITION", 0, SharpDX.DXGI.Format.R32G32_Float, 0)}, 0)); var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default(); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None; _noneCullingState = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc); initalised = true; }
private EffectTechnique GetTechniqueFromVertexBuffer <T>(Buffer <T> vertexBuffer) where T : struct { EffectTechnique technique = null; VertexInputLayout inputLayout = VertexInputLayout.FromBuffer(0, vertexBuffer); VertexBufferLayout bufferLayout = inputLayout.BufferLayouts.First(); bool pos = false, tex = false, col = false; var semantics = bufferLayout.VertexElements.Select(ve => ve.SemanticName.ToString()).ToArray(); if (semantics.Contains("POSITION")) { pos = true; } if (semantics.Contains("TEXCOORD")) { tex = true; } if (semantics.Contains("COLOR")) { col = true; } if (pos && tex) { technique = gbufferEffect.Techniques["PositionTexture"]; } if (pos && col) { technique = gbufferEffect.Techniques["PositionColor"]; } if (pos) { technique = gbufferEffect.Techniques["Position"]; } if (technique == null) { throw new NotSupportedException(); } return(technique); }
public void GetOrCreate(VertexInputLayout layout, out InputLayoutPair currentPassPreviousPair) { if (!ContextCache.TryGetValue(layout, out currentPassPreviousPair)) { lock (DeviceCache) { if (!DeviceCache.TryGetValue(layout, out currentPassPreviousPair)) { currentPassPreviousPair.InputLayout = new InputLayout(device, Bytecode, layout.InputElements); currentPassPreviousPair.VertexInputLayout = layout; DeviceCache.Add(layout, currentPassPreviousPair); } } ContextCache.Add(layout, currentPassPreviousPair); } }
protected override void LoadContent() { // Loads the effect metaTunnelEffect = Content.Load<Effect>("metatunnel.fxo"); // Prepare a quad buffer quadVertices = Buffer.Vertex.New( GraphicsDevice, new[] { new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)), new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)), new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)), }); // Create an input layout from the vertices inputLayout = VertexInputLayout.FromBuffer(0, quadVertices); }
public void CreateBillboardVerticesFromList(List<Tuple<Vector3, Vector3>> treeList) { if (!treeList.Any()) return; var billboardVertices = new BillboardVertex[treeList.Count * 6]; int i = 0; var random = new Random(); foreach (var t in treeList) createOne( ref i, billboardVertices, t.Item1 + _world.TranslationVector, t.Item2, 0.0001f + (float) random.NextDouble()); _vertexBuffer = Buffer.Vertex.New(Effect.GraphicsDevice, billboardVertices); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); }
public WarpSceneRenderer(Scene scene, int width, int height) { _scene = scene; _width = width; _height = height; _aspectRatio = width / (float)height; _device = GraphicsDevice.New(DriverType.Warp, DeviceCreationFlags.None, FeatureLevel.Level_10_1); var serviceProvider = new ServiceProvider(); serviceProvider.AddService<IGraphicsDeviceService>(new GraphicsDeviceService(_device)); _contentManager = new ContentManager(serviceProvider); _contentManager.Resolvers.Add(new ContentResolver()); var viewport = new Viewport(0, 0, _width, _height); _device.SetViewports(viewport); const MSAALevel msaaLevel = MSAALevel.None; _depthStencilTexture = DepthStencilBuffer.New(_device, _width, _height, msaaLevel, DepthFormat.Depth24Stencil8); _renderTexture = RenderTarget2D.New(_device, _width, _height, msaaLevel, PixelFormat.R8G8B8A8.UNorm); Options = new RenderOptions(); _effect = new BasicEffect(_device); _effect.EnableDefaultLighting(); _inputLayout = VertexInputLayout.New(0, typeof(VertexPositionNormalTexture)); _device.SetVertexInputLayout(_inputLayout); _meshes = new List<WarpMesh>(); foreach (Mesh mesh in _scene.Meshes) { if (!mesh.Positions.Any()) continue; var warpMesh = new WarpMesh(_device, mesh); _meshes.Add(warpMesh); warpMesh.Initialize(_contentManager); } }
public Arcs( VisionContent vContent, Archipelag archipelag) : base(vContent.LoadEffect("Effects/ArcsEffect")) { Archipelag = archipelag; Effect.World = Matrix.Identity; var lines = new List<ArcVertex>(); var modules = archipelag.CodeIslands.ToDictionary(ci => ci.VAssembly.Name, ci => ci); foreach (var island in modules.Values) processOneIsland(lines, island, island.VAssembly.VProgram, modules); if (lines.Any()) { _vertexBuffer = Buffer.Vertex.New(vContent.GraphicsDevice, lines.ToArray()); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); } }
public void CreateBillboardVertices() { if (_items==null || !_items.Any()) return; var billboardVertices = new StaticBillboardVertex[_items.Count * 6]; var i = 0; var random = new Random(); foreach (var t in _items) createOne( ref i, billboardVertices, t.Item1 + _world.TranslationVector, t.Item2, t.Item3); _items = null; _vertexBuffer = Buffer.Vertex.New(Effect.GraphicsDevice, billboardVertices); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); }
public PlayingField(LarvContent lContent, Texture2D texture, int level) : base(lContent.TextureEffect) { _texture = texture; var pfInfo = lContent.PlayingFieldInfos[level]; TheField = pfInfo.PlayingField; Floors = pfInfo.Floors; Height = pfInfo.Height; Width = pfInfo.WIdth; PlayerWhereaboutsStart = pfInfo.PlayerSerpentStart; EnemyWhereaboutsStart = pfInfo.EnemySerpentStart; MiddleX = Width/2f; MiddleY = Height/2f; var verts = new List<VertexPositionNormalTexture> { new VertexPositionNormalTexture(new Vector3(-1, 0, -1), Vector3.Up, new Vector2(0, 0)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, -1), Vector3.Up, new Vector2(Width*0.25f, 0)), new VertexPositionNormalTexture(new Vector3(-1, 0, Height + 1), Vector3.Up, new Vector2(0, Height*0.25f)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, -1), Vector3.Up, new Vector2(Width*0.25f, 0)), new VertexPositionNormalTexture(new Vector3(Width + 1, 0, Height + 1), Vector3.Up, new Vector2(Width*0.25f, Height*0.25f)), new VertexPositionNormalTexture(new Vector3(-1, 0, Height + 1), Vector3.Up, new Vector2(0, Height*0.25f)) }; var vertsShadow = new List<VertexPositionNormalTexture>(); for (var floor = 0; floor < Floors; floor++) for (var y = 0; y < Height; y++ ) for (var x = 0; x < Width; x++) { var sq = TheField[floor, y, x]; if ((!sq.IsNone && floor != 0) || sq.IsSlope) contructSquare(verts, vertsShadow, floor, new Point(x,y), sq.Corners); } VertexBuffer = Buffer.Vertex.New(lContent.GraphicsDevice, verts.ToArray()); if(vertsShadow.Any()) VertexBufferShadow = Buffer.Vertex.New(lContent.GraphicsDevice, vertsShadow.ToArray()); VertexInputLayout = VertexInputLayout.FromBuffer(0, VertexBuffer); }
void WPFHost.IScene.Attach(WPFHost.ISceneHost host) { this.host = host; camera = new Camera((float)host.RenderTargetWidth / host.RenderTargetHeight, (float)(75.0 * Math.PI / 180.0), 0.1f, 10000.0f); // device setup if (host.Device == null) throw new Exception("Scene host device is null"); GraphicsDevice = GraphicsDevice.New(host.Device); RenderTarget2D backbufferRenderTarget = RenderTarget2D.New(GraphicsDevice, host.RenderTargetView, true); GraphicsDevice.Presenter = new RenderTargetGraphicsPresenter(GraphicsDevice, backbufferRenderTarget); GraphicsDevice.SetRenderTargets(backbufferRenderTarget); #if RAYMARCH_TERRAIN terrain = new TerrainRaymarcher(GraphicsDevice, (float)host.RenderTargetWidth / host.RenderTargetHeight); #else terrain = new TerrainRasterizer(GraphicsDevice); #endif terrain.HeightScale = terrainScale; // load shader sphereBillboardShader = new ShaderAutoReload("shader/spherebillboards.fx", GraphicsDevice); // vertex input layout sphereVertexInputLayout = VertexInputLayout.New(VertexBufferLayout.New(0, VertexElement.Position(SharpDX.DXGI.Format.R32G32B32_Float))); // generate sky skyShader = new ShaderAutoReload("shader/sky.fx", GraphicsDevice); skyShader.OnReload += ReGenerateSkyCubeMap; skyCubemap = RenderTargetCube.New(GraphicsDevice, CUBEMAP_RES, 0, PixelFormat.R8G8B8A8.SNorm); TimeOfDay = timeOfDay; // constant buffer to defaults SetupConstants(); }
public SharedData(GraphicsDevice device) { VertexBuffer = ToDispose(Buffer.Vertex.New(device, QuadsVertices)); VertexInputLayout = VertexInputLayout.FromBuffer(0, VertexBuffer); }
protected override void LoadContent() { _fx = Content.Load<Effect>("fx1"); var verticies = new List<VertexPositionColor>(); addPlane(verticies, Vector3.Up); addPlane(verticies, Vector3.Down); addPlane(verticies, Vector3.Left); addPlane(verticies, Vector3.Right); addPlane(verticies, Vector3.ForwardRH); addPlane(verticies, Vector3.BackwardRH); _vertexBuffer = Buffer.Vertex.New(GraphicsDevice, verticies.ToArray()); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); base.LoadContent(); }
public InputLayoutPair(VertexInputLayout vertexInputLayout, InputLayout inputLayout) { VertexInputLayout = vertexInputLayout; InputLayout = inputLayout; }
public CxBillboard CreateVertices(bool randomize = true) { if (_items==null || !_items.Any()) return this; var billboardVertices = new CxBillboardVertex[_items.Count]; var i = 0; var random = new Random(); foreach (var t in _items) createOne( ref i, billboardVertices, t.Item1 + World.TranslationVector, t.Item2, randomize ? 0.0001f + (float) random.NextDouble() : 0.5f); _items = null; _vertexBuffer = Buffer.Vertex.New(Effect.GraphicsDevice, billboardVertices); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); return this; }
void LoadContent() { var device = this.GraphicsDevice; m_effect = this.Content.Load<Effect>("SelectionEffect"); var tex = new[] { new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), }; var vertices = new List<VertexPositionColorTexture>(); for (int i = 0; i < 6; ++i) { var color = i == (int)DirectionOrdinal.South ? new Color(255, 255, 255) : new Color(128, 128, 128); var ver = Chunk.s_cubeFaceInfo[i].Vertices.Select(v => v.ToVector3()) .ToArray(); var vs = new List<VertexPositionColorTexture>(); vs.Add(new VertexPositionColorTexture(ver[0], color, tex[0])); vs.Add(new VertexPositionColorTexture(ver[1], color, tex[1])); vs.Add(new VertexPositionColorTexture(ver[2], color, tex[2])); vs.Add(new VertexPositionColorTexture(ver[2], color, tex[2])); vs.Add(new VertexPositionColorTexture(ver[3], color, tex[3])); vs.Add(new VertexPositionColorTexture(ver[0], color, tex[0])); vertices.AddRange(vs); } m_layout = VertexInputLayout.New<VertexPositionColorTexture>(0); m_vertexBuffer = ToDispose(SharpDX.Toolkit.Graphics.Buffer.Vertex.New<VertexPositionColorTexture>(device, vertices.ToArray())); }
public InstancedGeomClipMapping(GraphicsDevice graphicsDevice, float minPatchSizeWorld, uint ringThinkness, uint numRings) { this.ringThinkness = ringThinkness; this.numRings = numRings; this.minPatchSizeWorld = minPatchSizeWorld; // Patch vertex buffer Half2[] patchVertices = new Half2[9]; for (int x = 0; x < 3; ++x) { for (int y = 0; y < 3; ++y) { patchVertices[x + y * 3] = new Half2(x * 0.5f, y * 0.5f); } } patchVertexBuffer = Buffer<Half2>.New(graphicsDevice, patchVertices, BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Immutable); // Instance buffers // Try to guess max number of patches: maxPatchInstances[(uint)PatchType.FULL] = ringThinkness * ringThinkness * 4 * numRings; maxPatchInstances[(uint)PatchType.STITCH1] = (uint)(maxPatchInstances[(uint)PatchType.FULL] * (float)(4 * 2 * ringThinkness) / (2 * ringThinkness * 2 * ringThinkness)); maxPatchInstances[(uint)PatchType.STITCH2] = maxPatchInstances[(uint)PatchType.STITCH1] / 4; for(int i = 0; i < patchInstanceBuffer.Length; ++i) { patchInstanceBuffer[i] = SharpDX.Toolkit.Graphics.Buffer.New( graphicsDevice, (int)maxPatchInstances[i] * sizeof(float) * 4, sizeof(float) * 4, BufferFlags.VertexBuffer, SharpDX.Direct3D11.ResourceUsage.Dynamic); currentInstanceData[i] = new List<PatchInstanceData>(); } // Input layout. vertexInputLayout = VertexInputLayout.New( VertexBufferLayout.New(0, new VertexElement[]{ new VertexElement("RELPOS", 0, SharpDX.DXGI.Format.R16G16_Float, 0) }, 0), VertexBufferLayout.New(1, new VertexElement[]{ new VertexElement("WORLDPOS", 0, SharpDX.DXGI.Format.R32G32_Float, 0), // worldPosition new VertexElement("SCALE", 0, SharpDX.DXGI.Format.R32_Float, 8), // worldScale new VertexElement("ROTATION", 0, SharpDX.DXGI.Format.R32_UInt, 12) }, 1)); // rotationType // Patch index buffer // Full patch ushort[] indicesFull = new ushort[]{ 0, 1, 4, 4, 1, 2, 0, 4, 3, 4, 2, 5, 3, 4, 6, 6, 4, 7, 7, 4, 8, 8, 4, 5 }; // optimize? patchIndexBuffer[(uint)PatchType.FULL] = Buffer<ushort>.New(graphicsDevice, indicesFull, BufferFlags.IndexBuffer, SharpDX.Direct3D11.ResourceUsage.Immutable); // First stitch: Only one triangle at bottom ushort[] indicesStitch1 = new ushort[]{ 0, 1, 4, 4, 1, 2, 0, 4, 3, 4, 2, 5, 3, 4, 6, 6, 4, 8, 8, 4, 5 }; // optimize? patchIndexBuffer[(uint)PatchType.STITCH1] = Buffer<ushort>.New(graphicsDevice, indicesStitch1, BufferFlags.IndexBuffer, SharpDX.Direct3D11.ResourceUsage.Immutable); // Second stitch: Only one triangle at bottom and right ushort[] indicesStitch2 = new ushort[]{ 0, 1, 4, 4, 1, 2, 0, 4, 3, 3, 4, 6, 6, 4, 8, 8, 4, 2 }; // optimize? patchIndexBuffer[(uint)PatchType.STITCH2] = Buffer<ushort>.New(graphicsDevice, indicesStitch2, BufferFlags.IndexBuffer, SharpDX.Direct3D11.ResourceUsage.Immutable); }
/// <summary> /// Create a new model. /// </summary> /// <param name="game"></param> /// <param name="shapeArray"></param> /// <param name="textureName"></param> /// <param name="collisionRadius"></param> /// //Create a textured model with normals public MyModel(LabGame game, VertexPositionNormalTexture[] shapeArray, String textureName, float collisionRadius) { this.vertices = Buffer.Vertex.New(game.GraphicsDevice, shapeArray); this.inputLayout = VertexInputLayout.New<VertexPositionNormalTexture>(0); vertexStride = Utilities.SizeOf<VertexPositionNormalTexture>(); modelType = ModelType.Textured; Texture = game.Content.Load<Texture2D>(textureName); this.collisionRadius = collisionRadius; wasLoaded = false; }
private void createBillboardVerticesFromList(List<Vector3> treeList) { if (!treeList.Any()) return; var billboardVertices = new VertexPositionTexture[treeList.Count*6]; var i = 0; foreach (var currentV3 in treeList) { billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(0, 0)); billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(1, 0)); billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(1, 1)); billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(0, 0)); billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(1, 1)); billboardVertices[i++] = new VertexPositionTexture(currentV3, new Vector2(0, 1)); } _vertexBuffer = Buffer.Vertex.New(Effect.GraphicsDevice, billboardVertices); _vertexInputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); }
void LoadContent() { m_basicEffect = ToDispose(new BasicEffect(this.GraphicsDevice)); m_basicEffect.VertexColorEnabled = true; m_basicEffect.LightingEnabled = false; m_plane = ToDispose(GeometricPrimitive.Plane.New(this.GraphicsDevice, 2, 2, 1, true)); m_buffer = ToDispose(Buffer<VertexPositionColor>.Vertex.New(this.GraphicsDevice, m_vertices)); m_layout = VertexInputLayout.FromBuffer(0, m_buffer); }
internal InputLayout GetInputLayout(VertexInputLayout layout) { if (layout == null) return null; if (!ReferenceEquals(currentInputLayoutPair.VertexInputLayout, layout)) { // Use a local cache to speed up retrieval if (!LocalInputLayoutCache.TryGetValue(layout, out currentInputLayoutPair)) { inputSignatureManager.GetOrCreate(layout, out currentInputLayoutPair); LocalInputLayoutCache.Add(layout, currentInputLayoutPair); } } return currentInputLayoutPair.InputLayout; }
public Platform(ProjectGame game) { // Avoid null reference, it changes on the first update if (standing_platform == null) { standing_platform = this; next_standing_platform = this; } // Less tiles if the difficulty is higher min_extra_tiles = (int)(max_extra_tiles - game.difficulty); type = GameObjectType.Platform; //Load textures textureName = "Platform_Textures"; texture = game.Content.Load<Texture2D>(textureName); // Store information of the platform and create information for the next one platform = next_platform; next_platform = create_platform(platform); // Create the vertices based on the platform information VertexPositionNormalTexture[] platform_vertices = create_vertices(platform, last_platform, next_platform); last_platform = platform; // Set the platform Z position of the intance and uptade the new Z position for the next one z_position_start = z_position; z_position -= tile_depth; z_position_end = z_position; // Calculates midpoint and base platfom_midpoint = (platform.GetLength(0) * tile_width) - tile_width/2; platform_base = Levels[0] - base_offset; //Load effects effect = game.Content.Load<Effect>("Phong"); lightManager = new LightManager(game); lightManager.SetLighting(effect); // Pass the vertices data vertices = Buffer.Vertex.New(game.GraphicsDevice, platform_vertices); inputLayout = VertexInputLayout.FromBuffer(0, vertices); this.game = game; }
internal InputLayout GetInputLayout(VertexInputLayout layout) { if (layout == null) return null; if (!ReferenceEquals(currentInputLayoutPair.VertexInputLayout, layout)) inputSignatureManager.GetOrCreate(layout, out currentInputLayoutPair); return currentInputLayoutPair.InputLayout; }
private unsafe void InitializeGraphics(GraphicsDevice graphicsDevice) { var colorBitmap = new Bitmap(gridlet.XLength, gridlet.YLength, PixelFormat.Format32bppRgb); var bitmapData = colorBitmap.LockBits(new System.Drawing.Rectangle(0, 0, gridlet.XLength, gridlet.YLength), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); var pScan0 = (byte*)bitmapData.Scan0; for (var y = 0; y < gridlet.YLength; y++) { var pCurrentPixel = pScan0 + bitmapData.Stride * y; for (var x = 0; x < gridlet.XLength; x++) { var cell = gridlet.Cells[x + gridlet.XLength * y]; uint color = 0xEFEFEF; if (cell.Flags.HasFlag(CellFlags.Debug)) { color = 0x0000FF; } else if (cell.Flags.HasFlag(CellFlags.Connector)) { color = 0xFF7F00; } else if (cell.Flags.HasFlag(CellFlags.Edge)) { color = 0x00FF00; } else if (cell.Flags.HasFlag(CellFlags.Blocked)) { color = 0xFF0000; } *(uint*)pCurrentPixel = color; pCurrentPixel += 4; } } colorBitmap.UnlockBits(bitmapData); var ms = new MemoryStream(); colorBitmap.Save(ms, ImageFormat.Bmp); ms.Position = 0; var image = SharpDX.Toolkit.Graphics.Image.Load(ms); // debugTexture = Texture2D.Load(graphicsDevice, @"E:\lolmodprojects\Project Master Yi\masteryi_base_r_cas_shockwave.dds"); debugTexture = Texture2D.New(graphicsDevice, image); var vertices = new VertexPositionNormalTexture[gridlet.XLength * gridlet.YLength * 4]; var indices = new int[gridlet.XLength * gridlet.YLength * 6 + (gridlet.XLength - 1) * (gridlet.YLength - 1) * 12]; int ibOffsetBase = 0; var xOffset = -gridlet.XLength / 2.0f; var yOffset = -gridlet.YLength / 2.0f; for (var y = 0; y < gridlet.YLength; y++) { for (var x = 0; x < gridlet.XLength; x++) { var cellIndex = x + y * gridlet.XLength; var cell = gridlet.Cells[cellIndex]; var cellHeight = cell.Height; var vbOffset = cellIndex * 4; var uv = new Vector2(x / (float)(gridlet.XLength - 1), y / (float)(gridlet.YLength - 1)); vertices[vbOffset + 0] = new VertexPositionNormalTexture(new Vector3(x + xOffset, y + yOffset, cellHeight), Vector3.UnitZ, uv); vertices[vbOffset + 1] = new VertexPositionNormalTexture(new Vector3(x + 1 + xOffset, y + yOffset, cellHeight), Vector3.UnitZ, uv); vertices[vbOffset + 2] = new VertexPositionNormalTexture(new Vector3(x + 1 + xOffset, y + 1 + yOffset, cellHeight), Vector3.UnitZ, uv); vertices[vbOffset + 3] = new VertexPositionNormalTexture(new Vector3(x + xOffset, y + 1 + yOffset, cellHeight), Vector3.UnitZ, uv); var ibOffset = ibOffsetBase + cellIndex * 6; indices[ibOffset + 0] = vbOffset; indices[ibOffset + 1] = vbOffset + 3; indices[ibOffset + 2] = vbOffset + 1; indices[ibOffset + 3] = vbOffset + 1; indices[ibOffset + 4] = vbOffset + 3; indices[ibOffset + 5] = vbOffset + 2; } } ibOffsetBase = gridlet.XLength * gridlet.YLength * 6; for (var y = 0; y < gridlet.YLength - 1; y++) { for (var x = 0; x < gridlet.XLength - 1; x++) { var cellIndex = x + y * gridlet.XLength; var cell = gridlet.Cells[cellIndex]; var cellHeight = cell.Height; var vbOffset = cellIndex * 4; var rightVbOffset = vbOffset + 4; var downVbOffset = vbOffset + gridlet.XLength * 4; var ibOffset = ibOffsetBase + (x + y * (gridlet.XLength - 1)) * 12; indices[ibOffset + 0] = vbOffset + 1; indices[ibOffset + 1] = vbOffset + 2; indices[ibOffset + 2] = rightVbOffset; indices[ibOffset + 3] = rightVbOffset; indices[ibOffset + 4] = vbOffset + 2; indices[ibOffset + 5] = rightVbOffset + 3; indices[ibOffset + 6] = vbOffset + 2; indices[ibOffset + 7] = vbOffset + 3; indices[ibOffset + 8] = downVbOffset; indices[ibOffset + 9] = downVbOffset; indices[ibOffset + 10] = downVbOffset + 1; indices[ibOffset + 11] = vbOffset + 2; } } vertexBuffer = Buffer.Vertex.New(graphicsDevice, vertices); vertexInputLayout = VertexInputLayout.FromBuffer(0, vertexBuffer); indexBuffer = Buffer.Index.New(graphicsDevice, indices); }
public VoxelRenderer(GraphicsDevice graphicsDevice, ContentManager contentManager) { _cubeVertexBuffer = Buffer.Vertex.New( graphicsDevice, new[] { // 3D coordinates UV Texture coordinates new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, // Front new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, // BACK new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, // Top new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, // Bottom new CubeVertex() { Position = new Vector3(1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f,-1.0f, -1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f,-1.0f, 1.0f ), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, // Left new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, // Right new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f) }, new CubeVertex() { Position = new Vector3(1.0f, -1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, -1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f) }, new CubeVertex() { Position = new Vector3(1.0f, 1.0f, 1.0f ), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f) } }, SharpDX.Direct3D11.ResourceUsage.Immutable); // Create an input layout from the vertices _vertexInputLayout = VertexInputLayout.New( VertexBufferLayout.New(0, new VertexElement[]{new VertexElement("POSITION_CUBE", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0), new VertexElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, sizeof(float) * 3), new VertexElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, sizeof(float) * 6)}, 0), VertexBufferLayout.New(1, new VertexElement[]{new VertexElement("POSITION_INSTANCE", SharpDX.DXGI.Format.R32_SInt)}, 1)); // Create instance buffer for every VoxelInfo _voxelTypeRenderingData = new VoxelTypeInstanceData[TypeInformation.GetNumTypes()-1]; for (int i = 0; i < _voxelTypeRenderingData.Length; ++i) _voxelTypeRenderingData[i] = new VoxelTypeInstanceData(graphicsDevice, (VoxelType)(i+1)); LoadTextures(contentManager); // load shader EffectCompilerFlags compilerFlags = EffectCompilerFlags.None; #if DEBUG compilerFlags |= EffectCompilerFlags.Debug; #endif var voxelShaderCompileResult = EffectCompiler.CompileFromFile("Content/voxel.fx", compilerFlags); if (voxelShaderCompileResult.HasErrors) { System.Console.WriteLine(voxelShaderCompileResult.Logger.Messages); System.Diagnostics.Debugger.Break(); } _voxelEffect = new SharpDX.Toolkit.Graphics.Effect(graphicsDevice, voxelShaderCompileResult.EffectData); // setup states var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default(); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.Back; _backfaceCullingState = RasterizerState.New(graphicsDevice, "CullModeBack", rasterizerStateDesc); rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None; _noneCullingState = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc); var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default(); depthStencilStateDesc.IsDepthEnabled = true; _depthStencilStateState = DepthStencilState.New(graphicsDevice, "NormalZBufferUse", depthStencilStateDesc); var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default(); samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Mirror; samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Mirror; samplerStateDesc.Filter = SharpDX.Direct3D11.Filter.MinMagMipPoint; _pointSamplerState = SamplerState.New(graphicsDevice, "PointSampler", samplerStateDesc); _voxelEffect.Parameters["PointSampler"].SetResource(_pointSamplerState); var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default(); _blendStateOpaque = BlendState.New(graphicsDevice, "Opaque", blendStateDesc); blendStateDesc.RenderTarget[0].IsBlendEnabled = true; blendStateDesc.RenderTarget[0].SourceBlend = SharpDX.Direct3D11.BlendOption.SourceAlpha; blendStateDesc.RenderTarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha; blendStateDesc.RenderTarget[0].BlendOperation = SharpDX.Direct3D11.BlendOperation.Add; _blendStateTransparent = BlendState.New(graphicsDevice, "AlphaBlend", blendStateDesc); // vertexbuffer for a single instance _singleInstanceBuffer = Buffer.Vertex.New<Int32>(graphicsDevice, 1, SharpDX.Direct3D11.ResourceUsage.Dynamic); }
//////////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////////// public DxMeshEntity(Game renderer, AdnMeshData meshData) { Id = meshData.Id; IsSelected = false; IsPreselected = false; _renderer = renderer; _colors = new List<float[]>(); foreach (var color in meshData.Color) { _colors.Add(ConvertClr(color)); } _vertices = new List<AdnPoint>(); List<VertexPositionNormalTexture> vertList = new List<VertexPositionNormalTexture>(); for (int i = 0; i < meshData.VertexIndices.Length; ++i) { int vIdx = meshData.VertexIndices[i]; int nIdx = meshData.NormalIndices[i]; _vertices.Add( new AdnPoint( meshData.VertexCoords[3 * vIdx], meshData.VertexCoords[3 * vIdx + 1], meshData.VertexCoords[3 * vIdx + 2])); vertList.Add( new VertexPositionNormalTexture( new Vector3( meshData.VertexCoords[3 * vIdx], meshData.VertexCoords[3 * vIdx + 1], meshData.VertexCoords[3 * vIdx + 2]), new Vector3( meshData.Normals[3 * nIdx], meshData.Normals[3 * nIdx + 1], meshData.Normals[3 * nIdx + 2]), new Vector2())); } _boundingBox = new AdnBoundingBox(_vertices); _vertexBuffer = SharpDX.Toolkit.Graphics.Buffer.Vertex.New( _renderer.GraphicsDevice, vertList.ToArray()); _inputLayout = VertexInputLayout.FromBuffer(0, _vertexBuffer); }
protected override void LoadContent() { _basicEffect = ToDisposeContent(new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, View = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY), Projection = Matrix.PerspectiveFovLH(MathUtil.PiOverFour, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f), World = Matrix.Identity }); _vertices = ToDisposeContent(SharpDX.Toolkit.Graphics.Buffer.Vertex.New( GraphicsDevice, new[] { new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), // Front new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), // BACK new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Orange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), // Top new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), // Bottom new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.OrangeRed), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), // Left new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), // Right new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.DarkOrange), new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.DarkOrange) })); ToDisposeContent(_vertices); _font = Content.Load<SpriteFont>("Font"); _spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice)); _inputLayout = VertexInputLayout.FromBuffer(0, _vertices); SetScaling(1f); base.LoadContent(); }