private void InitParticles() { //per instance attributes var rnd = new Random(12); float Rnd01() => (float)rnd.NextDouble(); float RndCoord() => (Rnd01() - 0.5f) * 2.0f; for (int i = 0; i < instanceCount; ++i) { instancePositions[i] = new Vector3(RndCoord(), RndCoord(), RndCoord()); } geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instancePosition"), instancePositions, true); #if SOLUTION float RndVelocity() => (Rnd01() - 0.5f) * 0.01f; for (int i = 0; i < instanceCount; ++i) { instancePositions[i] = new Vector3(RndCoord(), RndCoord(), RndCoord()); instanceVelocity[i] = new Vector3(RndVelocity(), RndVelocity(), RndVelocity()); instanceColor[i] = new Vector3(0.5f) + instancePositions[i] * 0.5f; instanceRotation[i] = Rnd01() * MathHelper.TWO_PI; } geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instanceColor"), instanceColor, true); #endif }
private void UpdateGeometry(IShaderProgram shaderProgram) { geometry = new VAO(PrimitiveType.Points); //generate position array on CPU var rnd = new Random(12); float Rnd01() => (float)rnd.NextDouble(); float RndCoord() => (Rnd01() - 0.5f) * 2.0f; var positions = new Vector2[pointCount]; for (int i = 0; i < pointCount; ++i) { positions[i] = new Vector2(RndCoord(), RndCoord()); } //copy positions to GPU geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "in_position"), positions, VertexAttribPointerType.Float, 2); //generate velocity arrray on CPU float RndSpeed() => (Rnd01() - 0.5f) * 0.1f; var velocities = new Vector2[pointCount]; for (int i = 0; i < pointCount; ++i) { velocities[i] = new Vector2(RndSpeed(), RndSpeed()) * 10.0f; } //copy velocities to GPU geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "in_velocity"), velocities, VertexAttribPointerType.Float, 2); }
public Deferred(IContentLoader contentLoader, Dictionary <Enums.EntityType, DefaultMesh> meshes) { _deferredProgram = contentLoader.Load <IShaderProgram>("deferred.*"); foreach (var meshContainer in meshes) { VAO geometry = VAOLoader.FromMesh(meshContainer.Value, _deferredProgram); if (meshContainer.Value is TBNMesh mesh) { var loc = _deferredProgram.GetResourceLocation(ShaderResourceType.Attribute, TBNMesh.TangentName); geometry.SetAttribute(loc, mesh.Tangent.ToArray(), VertexAttribPointerType.Float, 3); loc = _deferredProgram.GetResourceLocation(ShaderResourceType.Attribute, TBNMesh.BitangentName); geometry.SetAttribute(loc, mesh.Bitangent.ToArray(), VertexAttribPointerType.Float, 3); } _geometries.Add(meshContainer.Key, geometry); } _defaultMap = contentLoader.Load <ITexture2D>("Nvidia.png"); _projectilesGenerationNvidia = new ProjectileGeneration(contentLoader, meshes[Enums.EntityType.NvidiaParticle], Enums.EntityType.NvidiaParticle); _projectilesGenerationRadeon = new ProjectileGeneration(contentLoader, meshes[Enums.EntityType.RadeonParticle], Enums.EntityType.RadeonParticle); _addProjectilesNvidia = new AddWithDepthTest(contentLoader); _addProjectilesRadeon = new AddWithDepthTest(contentLoader); _tesselation = new Tesselation(contentLoader); _addTesselation = new AddWithDepthTest(contentLoader); }
private void UpdateAttributes(IShaderProgram shaderProgram) { //per instance attributes var rnd = new Random(12); float Rnd01() => (float)rnd.NextDouble(); float RndCoord() => (Rnd01() - 0.5f) * 8.0f; var instancePositions = new Vector3[particleCount]; for (int i = 0; i < particleCount; ++i) { instancePositions[i] = new Vector3(RndCoord(), RndCoord(), RndCoord()); } geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instancePosition"), instancePositions, true); float RndSpeed() => (Rnd01() - 0.5f); var instanceSpeeds = new Vector3[particleCount]; for (int i = 0; i < particleCount; ++i) { instanceSpeeds[i] = new Vector3(RndSpeed(), RndSpeed(), RndSpeed()); } geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instanceSpeed"), instanceSpeeds, true); }
public void UpdateTransforms(Dictionary <Enums.EntityType, Matrix4x4[]> transforms) { foreach (var type in _geometriesDepth.Keys) { _geometriesDepth[type].SetAttribute(_depthShader.GetResourceLocation(ShaderResourceType.Attribute, "transform"), transforms[type], true); } foreach (var type in _geometriesShadow.Keys) { _geometriesShadow[type].SetAttribute(_shadowShader.GetResourceLocation(ShaderResourceType.Attribute, "transform"), transforms[type], true); } }
public void Draw(IRenderState renderState, ITexture2D depth, float mipmapLevel = 0) { _geometries[_entity.Type].SetAttribute(_environmentMapProgram.GetResourceLocation(ShaderResourceType.Attribute, "transform"), new[] { _entity.Transform }, true); GL.ClearColor(Color.FromArgb(0, 0, 0, 0)); _outputSurface.Activate(); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); _environmentMapProgram.Activate(); _environmentMapProgram.ActivateTexture("cubeMap", 0, _cubeFbo.Texture); _environmentMapProgram.ActivateTexture("depth", 1, depth); _environmentMapProgram.Uniform("camera", _camera); Matrix4x4.Invert(_camera.Matrix, out var invert); _environmentMapProgram.Uniform("camPos", invert.Translation / invert.M44); _environmentMapProgram.Uniform("mipmapLevel", mipmapLevel); _geometries[_entity.Type].Draw(); _environmentMapProgram.DeactivateTexture(0, _cubeFbo.Texture); _environmentMapProgram.DeactivateTexture(1, depth); _environmentMapProgram.Deactivate(); _outputSurface.Deactivate(); GL.ClearColor(Color.FromArgb(0, 0, 0, 1)); }
public void UpdateTransforms(Dictionary <Enums.EntityType, Matrix4x4[]> transforms) { foreach (var type in _geometries.Keys) { _geometries[type].SetAttribute(_deferredProgram.GetResourceLocation(ShaderResourceType.Attribute, "transform"), transforms[type], true); } }
public View(IContentLoader contentLoader, IRenderContext renderContext) { renderContext.RenderState.Set(new DepthTest(true)); //renderContext.RenderState.Set(new FaceCullingModeState(FaceCullingMode.BACK_SIDE)); shader = contentLoader.Load <IShaderProgram>("shader.*"); var fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\Cameras\glTF-Embedded\Cameras.gltf"; fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\BrainStem\glTF\BrainStem.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\BoxAnimated\glTF\BoxAnimated.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\RiggedSimple\glTF\RiggedSimple.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\RiggedFigure\glTF\RiggedFigure.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\Monster\glTF\Monster.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\CesiumMan\glTF\CesiumMan.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\2CylinderEngine\glTF\2CylinderEngine.gltf"; //fileName = @"D:\Daten\downloads\gits\glTF-Sample-Models\2.0\OrientationTest\glTF-Embedded\OrientationTest.gltf"; //fileName = @"D:\Daten\downloads\_cg\gltf models\ferris_wheel_animated\scene.gltf"; //fileName = @"D:\Daten\downloads\_cg\gltf models\izzy_-_animated_female_character_free_download\scene.gltf"; //fileName = @"D:\Daten\downloads\_cg\gltf models\littlest_tokyo\scene.gltf"; var directory = Path.GetDirectoryName(fileName); //using (var stream = contentLoader.Load<Stream>("AnimatedTriangle.gltf")) //using (var stream = contentLoader.Load<Stream>("Box.gltf")) using (var stream = File.OpenRead(fileName)) { byte[] LoadFile(string externalFile) => File.ReadAllBytes(Path.Combine(directory, externalFile)); int UniformLoc(string name) => shader.GetResourceLocation(ShaderResourceType.Uniform, name); int AttributeLoc(string name) { var attributeName = name.ToLowerInvariant(); var location = shader.GetResourceLocation(ShaderResourceType.Attribute, attributeName); return(location); } model = new GltfModelRendererGL(stream, LoadFile, UniformLoc, AttributeLoc); } locJoints = shader.GetResourceLocation(ShaderResourceType.Uniform, "u_jointMat"); Cameras = model.Cameras; time = new Stopwatch(); time.Start(); }
public void Render(IEnumerable <IBody> bodies, float time, Transformation3D camera) { GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); if (shaderProgram is null) { return; } var instancePositions = new List <Vector3>(); var instanceScale = new List <float>(); foreach (var body in bodies) { instancePositions.Add(body.Location); instanceScale.Add((float)Math.Pow(body.Mass, 0.33f)); } geometryBody.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instancePosition"), instancePositions.ToArray(), VertexAttribPointerType.Float, 3, true); geometryBody.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "instanceScale"), instanceScale.ToArray(), VertexAttribPointerType.Float, 1, true); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); shaderProgram.Activate(); shaderProgram.Uniform("time", time); shaderProgram.Uniform("camera", camera.CalcLocalToWorldColumnMajorMatrix()); geometryBody.Draw(instancePositions.Count); shaderProgram.Deactivate(); floorShaderProgram.Activate(); floorShaderProgram.Uniform("time", time); floorShaderProgram.Uniform("floorColor", new Vector4(0, .5f, .5f, 1f)); floorShaderProgram.Uniform("camera", camera.CalcLocalToWorldColumnMajorMatrix()); floor.Draw(); floorShaderProgram.Uniform("floorColor", new Vector4(0, 0, 1, 0.5f)); waterCube.Draw(); floorShaderProgram.Deactivate(); GL.Disable(EnableCap.Blend); GL.Disable(EnableCap.DepthTest); }
/// <summary> /// Creates a VertexArrayObject from a mesh expecting the MeshAttribute names as shader variable names for the attributes /// </summary> /// <param name="mesh">From which to load positions, indices, normals, texture coordinates</param> /// <param name="shaderProgram">Used for the attribute location bindings</param> /// <returns>A vertex array object</returns> public static VAO FromMesh(DefaultMesh mesh, IShaderProgram shaderProgram) { var vao = new VAO(PrimitiveType.Triangles); if (mesh.Position.Count > 0) { var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.PositionName); vao.SetAttribute(loc, mesh.Position.ToArray(), VertexAttribPointerType.Float, 3); } if (mesh.Normal.Count > 0) { var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.NormalName); vao.SetAttribute(loc, mesh.Normal.ToArray(), VertexAttribPointerType.Float, 3); } if (mesh.TexCoord.Count > 0) { var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, DefaultMesh.TexCoordName); vao.SetAttribute(loc, mesh.TexCoord.ToArray(), VertexAttribPointerType.Float, 2); } vao.SetIndex(mesh.IDs.ToArray()); return(vao); }
public Deferred(IContentLoader contentLoader, Dictionary <Enums.EntityType, DefaultMesh> meshes) { _deferredProgram = contentLoader.Load <IShaderProgram>("deferred.*"); foreach (var meshContainer in meshes) { VAO geometry = VAOLoader.FromMesh(meshContainer.Value, _deferredProgram); if (meshContainer.Value is TBNMesh mesh) { var loc = _deferredProgram.GetResourceLocation(ShaderResourceType.Attribute, TBNMesh.TangentName); geometry.SetAttribute(loc, mesh.Tangent.ToArray(), VertexAttribPointerType.Float, 3); loc = _deferredProgram.GetResourceLocation(ShaderResourceType.Attribute, TBNMesh.BitangentName); geometry.SetAttribute(loc, mesh.Bitangent.ToArray(), VertexAttribPointerType.Float, 3); } _geometries.Add(meshContainer.Key, geometry); } _defaultMap = contentLoader.Load <ITexture2D>("mercury.jpg"); }
/// <summary> /// Creates a VertexArrayObject from a mesh expecting the MeshAttribute names as shader variable names for the attributes /// </summary> /// <param name="mesh">From which to load positions, indices, normals, texture coordinates</param> /// <param name="shaderProgram">Used for the attribute location bindings</param> /// <returns>A vertex array object</returns> public static VAO FromMesh(Mesh mesh, IShaderProgram shaderProgram) { var vao = new VAO(PrimitiveType.Triangles); foreach (var attributeName in mesh.AttributeNames) { var loc = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, attributeName); var attribute = mesh.GetAttribute(attributeName); var array = attribute.ToArray(); // copy vao.SetAttribute(loc, array, attribute.BaseType, attribute.BaseTypeCount); } vao.SetIndex(mesh.IDs.ToArray()); return(vao); }
private Action CreateDrawCall(MeshPrimitive primitive, IShaderProgram shaderProgram) { Action drawCommand = null; var idVAO = GL.GenVertexArray(); var primitiveType = (PrimitiveType)primitive.Mode; GL.BindVertexArray(idVAO); if (primitive.Indices.HasValue) { var accessor = gltf.Accessors[primitive.Indices.Value]; if (accessor.BufferView.HasValue) { glBuffers[accessor.BufferView.Value].Activate(); drawCommand = () => GL.DrawElements(primitiveType, accessor.Count, (DrawElementsType)accessor.ComponentType, accessor.ByteOffset); } } foreach (var attribute in primitive.Attributes) { var accessor = gltf.Accessors[attribute.Value]; if (accessor.BufferView.HasValue) { var uniformName = attribute.Key.ToLowerInvariant(); var bindingID = shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, uniformName); if (-1 != bindingID) { glBuffers[accessor.BufferView.Value].Activate(); var bufferView = gltf.BufferViews[accessor.BufferView.Value]; var elementBytes = bufferView.ByteStride ?? 0; var baseTypeCount = mappingTypeToBaseTypeCount[accessor.Type]; GL.VertexAttribPointer(bindingID, baseTypeCount, (VertexAttribPointerType)accessor.ComponentType, accessor.Normalized, elementBytes, accessor.ByteOffset); GL.EnableVertexAttribArray(bindingID); } } } GL.BindVertexArray(0); //GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); //GL.BindBuffer(BufferTarget.ArrayBuffer, 0); if (drawCommand is null) { drawCommand = () => GL.DrawArrays(primitiveType, 0, 3); } return(() => { GL.BindVertexArray(idVAO); drawCommand(); GL.BindVertexArray(0); }); }
public MainVisual(IRenderState renderState, IContentLoader contentLoader) { renderState.Set(new ClearColorState(1, 1, 1, 1)); renderState.Set(BoolState <IDepthState> .Enabled); renderState.Set(BoolState <IBackfaceCullingState> .Enabled); envMap = contentLoader.Load <ITexture2D>("beach"); envMap.WrapFunction = TextureWrapFunction.MirroredRepeat; envMap.Filter = TextureFilterMode.Linear; sphereTex = contentLoader.Load <ITexture2D>("hatefield1"); sphereTex.WrapFunction = TextureWrapFunction.MirroredRepeat; sphereTex.Filter = TextureFilterMode.Linear; shaderProgram = contentLoader.Load <IShaderProgram>("envMapping.*"); var sphere = Meshes.CreateSphere(1, 4); var envSphere = sphere.SwitchTriangleMeshWinding(); var refSphere = Meshes.CreateSphere(.1f, 4).Transform(new Translation3D(new Vector3(-.1f, 0, 0))); var refractSphere = Meshes.CreateSphere(.1f, 4).Transform(new Translation3D(new Vector3(.1f, 0, 0))); geometry = VAOLoader.FromMesh(envSphere, shaderProgram); geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "specularity"), new[] { 0.0f }, VertexAttribPointerType.Float, 1, true); geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "translucency"), new[] { 0.0f }, VertexAttribPointerType.Float, 1, true); geometry.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "refractionIndex"), new[] { 1.0f }, VertexAttribPointerType.Float, 1, true); geometryRef = VAOLoader.FromMesh(refSphere, shaderProgram); geometryRef.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "specularity"), new[] { .2f }, VertexAttribPointerType.Float, 1, true); geometryRef.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "translucency"), new[] { 0.0f }, VertexAttribPointerType.Float, 1, true); geometryRef.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "refractionIndex"), new[] { 1.0f }, VertexAttribPointerType.Float, 1, true); geometryRefract = VAOLoader.FromMesh(refractSphere, shaderProgram); geometryRefract.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "specularity"), new[] { .0f }, VertexAttribPointerType.Float, 1, true); geometryRefract.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "translucency"), new[] { 1.0f }, VertexAttribPointerType.Float, 1, true); geometryRefract.SetAttribute(shaderProgram.GetResourceLocation(ShaderResourceType.Attribute, "refractionIndex"), new[] { 1.6f }, VertexAttribPointerType.Float, 1, true); }
public void CreateMaps(float time) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); mapFBO.Activate(); waterMapShader.Activate(); waterMapShader.Uniform("time", time); waterMapShader.Uniform("numberOfWaves", numberOfWaves); int buffer = waterMapShader.GetResourceLocation(ShaderResourceType.RWBuffer, "WavesBuffer"); waveBuffer.ActivateBind(buffer); //Activate Textures of FBO int textAmount = mapFBO.Textures.Count; //Number of Texture Channels of FBO for (int i = 0; i < textAmount; i++) { GL.ActiveTexture(TextureUnit.Texture0 + i); mapFBO.Textures[i].Activate(); } DrawBuffersEnum[] buffers = new DrawBuffersEnum[textAmount]; for (int i = 0; i < textAmount; i++) { buffers[i] = DrawBuffersEnum.ColorAttachment0 + i; } GL.DrawBuffers(textAmount, buffers); GL.DrawArrays(PrimitiveType.Quads, 0, 4); waveBuffer.Deactivate(); for (int i = textAmount - 1; i >= 0; i--) { GL.ActiveTexture(TextureUnit.Texture0 + i); mapFBO.Textures[i].Deactivate(); } waterMapShader.Deactivate(); mapFBO.Deactivate(); }
public void Draw(ITransformation camera, ITexture2D materialColor, ITexture2D normals, ITexture2D position, ITexture2D shadowSurface, ITexture2D intensity, List <LightSource> lightSources) { if (lightSources.Count > _lightArraySizeInShader) { throw new ArgumentException("A maximum of " + _lightArraySizeInShader + " light sources is possible. See shader 'deferredLighting.glsl' for details."); } _outputSurface.Activate(); GL.Clear(ClearBufferMask.ColorBufferBit); _shader.Activate(); Matrix4x4.Invert(camera.Matrix, out var invert); _shader.Uniform("camPos", invert.Translation / invert.M44); _shader.Uniform("hemColorTop", new Vector3(0.9f, 0.9f, 1.0f)); _shader.Uniform("hemColorBottom", new Vector3(41.0f / 255.0f, 49.0f / 255.0f, 51.0f / 255.0f)); _shader.ActivateTexture("materialColor", 0, materialColor); _shader.ActivateTexture("normals", 1, normals); _shader.ActivateTexture("position", 2, position); _shader.ActivateTexture("shadowSurface", 3, shadowSurface); _shader.ActivateTexture("intensity", 4, intensity); var bufferObject = LightSourcesToBufferObject(lightSources); var loc = _shader.GetResourceLocation(ShaderResourceType.RWBuffer, "Lights"); bufferObject.ActivateBind(loc); GL.DrawArrays(PrimitiveType.Quads, 0, 4); _shader.DeactivateTexture(4, intensity); _shader.DeactivateTexture(3, shadowSurface); _shader.DeactivateTexture(2, position); _shader.DeactivateTexture(1, normals); _shader.DeactivateTexture(0, materialColor); _shader.Deactivate(); _outputSurface.Deactivate(); }
public void SetInstancePositions(Vector3[] positions) { deferredMesh.SetAttribute(deferredShader.GetResourceLocation(Zenseless.HLGL.ShaderResourceType.Attribute, "instancePosition"), positions, true); lightViewMesh.SetAttribute(lightViewShader.GetResourceLocation(Zenseless.HLGL.ShaderResourceType.Attribute, "instancePosition"), positions, true); shadowMapMesh.SetAttribute(shadowMapShader.GetResourceLocation(Zenseless.HLGL.ShaderResourceType.Attribute, "instancePosition"), positions, true); }
/// <summary> /// Set color Uniform on active shader. The correct shader has to be activated first! /// </summary> /// <param name="shaderProgram">The shader program.</param> /// <param name="name">The uniform variable name.</param> /// <param name="color">The color.</param> public static void Uniform(this IShaderProgram shaderProgram, string name, Color color) { GL.ProgramUniform4(shaderProgram.ProgramID, shaderProgram.GetResourceLocation(ShaderResourceType.Uniform, name), color); }
public void UpdateTransforms(Dictionary <Enums.EntityType, Matrix4x4[]> transforms) { _trianglesGeometry.SetAttribute(_projectileGenerationProgram.GetResourceLocation(ShaderResourceType.Attribute, "transform"), transforms[_triangleType], true); }