├── TrippyGL_logo.png ├── images ├── img_conways.png ├── img_fractal.png ├── img_terrain.png ├── img_lighting.png └── img_bouncyballs.png ├── TrippyTests ├── IndexedRendering │ ├── indices.png │ ├── fs.glsl │ ├── vs.glsl │ ├── Program.cs │ ├── IndexedRendering.csproj │ ├── SimpleVertex.cs │ └── IndexedRendering.cs ├── TextureBatcherTest │ ├── ball.png │ ├── diamond.png │ ├── particles.png │ ├── rectangle.png │ ├── Program.cs │ ├── TextureBatcherTest.csproj │ ├── Diamond.cs │ ├── Particle.cs │ ├── Ball.cs │ └── FontLoadHelper.cs ├── SimpleCubemap │ ├── cubemap │ │ ├── back.png │ │ ├── left.png │ │ ├── top.png │ │ ├── bottom.png │ │ ├── front.png │ │ └── right.png │ ├── fs.glsl │ ├── Program.cs │ ├── vs.glsl │ ├── SimpleCubemap.csproj │ └── SimpleCubemap.cs ├── TexturedTriangles │ ├── texture.png │ ├── satellite.png │ ├── Program.cs │ └── TexturedTriangles.csproj ├── SimpleShader3D │ ├── lamp_texture.png │ ├── Program.cs │ ├── skyVs.glsl │ ├── skyFs.glsl │ └── SimpleShader3D.csproj ├── InstancedCubes │ ├── fs.glsl │ ├── Program.cs │ ├── vs.glsl │ └── InstancedCubes.csproj ├── ComplexVertexFormats │ ├── fs1.glsl │ ├── Program.cs │ ├── ComplexVertexFormats.csproj │ ├── vs1.glsl │ ├── ComplexVertexFormats.cs │ └── ComplexVertex.cs ├── SimpleCube │ ├── Program.cs │ ├── SimpleCube.csproj │ └── SimpleCube.cs ├── GameOfLifeSim │ ├── Program.cs │ ├── sim_vs.glsl │ ├── GameOfLifeSim.csproj │ └── sim_fs.glsl ├── ShaderFractals │ ├── Program.cs │ ├── vs.glsl │ ├── fs.glsl │ └── ShaderFractals.csproj ├── SimpleTriangle │ ├── Program.cs │ ├── SimpleTriangle.csproj │ └── SimpleTriangle.cs └── TrippyTestBase │ └── TrippyTestBase.csproj ├── TrippyDemos └── TerrainMaker │ ├── data │ ├── distortMap.png │ ├── normalMap.png │ ├── skyVs.glsl │ ├── postprocessVs.glsl │ ├── terrainFs.glsl │ ├── waterVs.glsl │ ├── underwaterFs.glsl │ ├── skyFs.glsl │ ├── terrainVs.glsl │ └── waterFs.glsl │ ├── Program.cs │ ├── TerrainVertex.cs │ ├── TerrainChunk.cs │ ├── TerrainChunkData.cs │ ├── TerrainMaker.csproj │ ├── GeneratorSeed.cs │ └── NoiseGenerator.cs ├── TrippyGL ├── Enums │ ├── DebugSeverity.cs │ ├── PolygonFace.cs │ ├── TextureMagFilter.cs │ ├── BlitFramebufferFilter.cs │ ├── CullingMode.cs │ ├── DebugSource.cs │ ├── TextureWrapMode.cs │ ├── ElementType.cs │ ├── ClearBuffers.cs │ ├── CubemapFace.cs │ ├── DebugType.cs │ ├── DepthFunction.cs │ ├── TextureMinFilter.cs │ ├── BlendingMode.cs │ ├── StencilFunction.cs │ ├── StencilOperation.cs │ ├── DepthStencilFormat.cs │ ├── AttributeBaseType.cs │ ├── PrimitiveType.cs │ ├── RenderbufferFormat.cs │ ├── FramebufferAttachmentPoint.cs │ ├── ReadPixelsFormat.cs │ ├── BufferTarget.cs │ ├── BlendingFactor.cs │ ├── TextureImageFormat.cs │ ├── TextureType.cs │ ├── BufferUsage.cs │ ├── BatcherBeginMode.cs │ ├── UniformType.cs │ └── AttributeType.cs ├── IVertex.cs ├── TextureMultisamplable.cs ├── TrippyGL.csproj ├── SpecifiedShaderAttrib.cs ├── VertexNormal.cs ├── VertexTexture.cs ├── VertexDataBufferSubset.cs ├── MonospaceTextureFont.cs ├── VertexColor.cs ├── VertexPosition.cs ├── VertexNormalColor.cs ├── VertexNormalTexture.cs ├── GraphicsResource.cs ├── GeometryShaderData.cs ├── ShaderBlockUniform.cs ├── ActiveVertexAttrib.cs ├── SpacedTextureFont.cs ├── VertexColorTexture.cs ├── VertexNormalColorTexture.cs ├── Exceptions.cs ├── DirectionalLight.cs ├── BufferObject.cs ├── RenderbufferObject.cs └── FramebufferAttachments.cs ├── TrippyGL.Fonts ├── Exceptions.cs ├── TrippyGL.Fonts.csproj └── IGlyphSource.cs ├── .editorconfig ├── LICENSE ├── TrippyGL.ImageSharp ├── ImageUtils.cs ├── TrippyGL.ImageSharp.csproj └── Texture2DArrayExtensions.cs ├── README.md ├── TrippyGL.Fonts.Extensions ├── TrippyGL.Fonts.Extensions.csproj ├── TextureFontDataExtensions.cs ├── TextureFontExtensions.cs └── TrippyFontFileExtensions.cs └── TrippyGL.Fonts.Building ├── TrippyGL.Fonts.Building.csproj └── FontBuilderExtensions.cs /TrippyGL_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyGL_logo.png -------------------------------------------------------------------------------- /images/img_conways.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/images/img_conways.png -------------------------------------------------------------------------------- /images/img_fractal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/images/img_fractal.png -------------------------------------------------------------------------------- /images/img_terrain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/images/img_terrain.png -------------------------------------------------------------------------------- /images/img_lighting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/images/img_lighting.png -------------------------------------------------------------------------------- /images/img_bouncyballs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/images/img_bouncyballs.png -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/indices.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/IndexedRendering/indices.png -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TextureBatcherTest/ball.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/back.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/left.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/top.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/top.png -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/diamond.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TextureBatcherTest/diamond.png -------------------------------------------------------------------------------- /TrippyTests/TexturedTriangles/texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TexturedTriangles/texture.png -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/distortMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyDemos/TerrainMaker/data/distortMap.png -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/normalMap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyDemos/TerrainMaker/data/normalMap.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/bottom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/bottom.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/front.png -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/cubemap/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleCubemap/cubemap/right.png -------------------------------------------------------------------------------- /TrippyTests/SimpleShader3D/lamp_texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/SimpleShader3D/lamp_texture.png -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/particles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TextureBatcherTest/particles.png -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/rectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TextureBatcherTest/rectangle.png -------------------------------------------------------------------------------- /TrippyTests/TexturedTriangles/satellite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SilkCommunity/TrippyGL/HEAD/TrippyTests/TexturedTriangles/satellite.png -------------------------------------------------------------------------------- /TrippyTests/InstancedCubes/fs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | in vec4 fColor; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = fColor; 9 | } -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/fs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform vec4 color; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = color; 9 | } -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/fs1.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | in vec4 fColor; 4 | 5 | out vec4 FragColor; 6 | 7 | void main() { 8 | FragColor = fColor; 9 | } -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/vs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 Projection; 4 | 5 | in vec2 vPosition; 6 | 7 | void main() { 8 | gl_Position = Projection * vec4(vPosition, 0.0, 1.0); 9 | } -------------------------------------------------------------------------------- /TrippyTests/SimpleCube/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleCube 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new SimpleCube().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/fs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform samplerCube cubemap; 4 | 5 | in vec3 fPosition; 6 | 7 | out vec4 FragColor; 8 | 9 | void main() { 10 | FragColor = texture(cubemap, fPosition); 11 | } -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/Program.cs: -------------------------------------------------------------------------------- 1 | namespace TerrainMaker 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new TerrainMaker().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/GameOfLifeSim/Program.cs: -------------------------------------------------------------------------------- 1 | namespace GameOfLifeSim 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new GameOfLifeSim().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/InstancedCubes/Program.cs: -------------------------------------------------------------------------------- 1 | namespace InstancedCubes 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new InstancedCubes().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/ShaderFractals/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ShaderFractals 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new ShaderFractals().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleCubemap 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new SimpleCubemap().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/SimpleShader3D/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleShader3D 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new SimpleShader3D().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/SimpleTriangle/Program.cs: -------------------------------------------------------------------------------- 1 | namespace SimpleTriangle 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new SimpleTriangle().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/Program.cs: -------------------------------------------------------------------------------- 1 | namespace IndexedRendering 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new IndexedRendering().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/TexturedTriangles/Program.cs: -------------------------------------------------------------------------------- 1 | namespace TexturedTriangles 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new TexturedTriangles().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/Program.cs: -------------------------------------------------------------------------------- 1 | namespace TextureBatcherTest 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new TextureBatcherTest().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ComplexVertexFormats 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | new ComplexVertexFormats().Run(); 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /TrippyGL/Enums/DebugSeverity.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | public enum DebugSeverity 4 | { 5 | DontCare = 4352, 6 | Notification = 33387, 7 | High = 37190, 8 | Medium = 37191, 9 | Low = 37192 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TrippyTests/GameOfLifeSim/sim_vs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | in vec3 vPosition; 4 | in vec2 vTexCoords; 5 | 6 | out vec2 fTexCoords; 7 | 8 | void main() { 9 | gl_Position = vec4(vPosition, 1.0); 10 | fTexCoords = vec2(vTexCoords.x, 1.0 - vTexCoords.y); 11 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/PolygonFace.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the faces of a polygon. 5 | /// 6 | public enum PolygonFace 7 | { 8 | Clockwise = 2304, 9 | CounterClockwise = 2305 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/skyVs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 Projection; 4 | uniform mat4 View; 5 | 6 | in vec3 vPosition; 7 | 8 | out vec3 fPosition; 9 | 10 | void main() { 11 | fPosition = vPosition; 12 | gl_Position = Projection * View * vec4(vPosition, 1.0); 13 | } -------------------------------------------------------------------------------- /TrippyTests/SimpleShader3D/skyVs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 View; 4 | uniform mat4 Projection; 5 | 6 | in vec3 vPosition; 7 | 8 | out vec3 fPosition; 9 | 10 | void main() { 11 | gl_Position = Projection * View * vec4(vPosition, 1.0); 12 | fPosition = vPosition; 13 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/TextureMagFilter.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the magnifying filters that can be used when sampling a texture. 5 | /// 6 | public enum TextureMagFilter 7 | { 8 | Nearest = 9728, 9 | Linear = 9729 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/vs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 World; 4 | uniform mat4 View; 5 | uniform mat4 Projection; 6 | 7 | in vec3 vPosition; 8 | 9 | out vec3 fPosition; 10 | 11 | void main() { 12 | fPosition = vPosition; 13 | gl_Position = Projection * View * World * vec4(vPosition, 1.0); 14 | } -------------------------------------------------------------------------------- /TrippyTests/ShaderFractals/vs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat3x2 Transform; 4 | uniform mat4 Projection; 5 | 6 | in vec3 vPosition; 7 | 8 | out vec2 fCoords; 9 | 10 | void main() { 11 | gl_Position = Projection * vec4(vPosition, 1.0); 12 | fCoords = (Transform * vec3(vPosition.xy, 1.0)).xy; 13 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/BlitFramebufferFilter.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the interpolation filters that can be used when blitting framebuffers. 5 | /// 6 | public enum BlitFramebufferFilter 7 | { 8 | Nearest = 9728, 9 | Linear = 9729 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TrippyGL/Enums/CullingMode.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the modes for which polygon culling can be enabled. 5 | /// 6 | public enum CullingMode 7 | { 8 | CullFront = 1028, 9 | CullBack = 1029, 10 | CullFrontAndBack = 1032 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TrippyGL/Enums/DebugSource.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | public enum DebugSource 4 | { 5 | DontCare = 4352, 6 | Api = 33350, 7 | WindowSystem = 33351, 8 | ShaderCompiler = 33352, 9 | ThirdParty = 33353, 10 | Application = 33354, 11 | Other = 33355 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TrippyGL/Enums/TextureWrapMode.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the modes in which texture coordinate wrapping can be handled. 5 | /// 6 | public enum TextureWrapMode 7 | { 8 | Repeat = 10497, 9 | ClampToEdge = 33071, 10 | MirroredRepeat = 33648 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TrippyGL/Enums/ElementType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the different types of elements that can be used for indexed drawing. 5 | /// 6 | public enum ElementType 7 | { 8 | UnsignedByte = 5121, 9 | UnsignedShort = 5123, 10 | UnsignedInt = 5125 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/postprocessVs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 Projection; 4 | 5 | in vec3 vPosition; 6 | in vec4 vColor; 7 | in vec2 vTexCoords; 8 | 9 | out vec4 fColor; 10 | out vec2 fTexCoords; 11 | 12 | void main() { 13 | gl_Position = Projection * vec4(vPosition, 1.0); 14 | fColor = vColor; 15 | fTexCoords = vTexCoords; 16 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/ClearBuffers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// Specifies the buffers that can be targeted by a clear operation. 7 | /// 8 | [Flags] 9 | public enum ClearBuffers : uint 10 | { 11 | Depth = 256, 12 | Stencil = 1024, 13 | Color = 16384, 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/terrainFs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform vec3 cameraPos; 4 | uniform float nearFog; 5 | uniform float farFog; 6 | 7 | in vec3 fPosition; 8 | in vec3 fNormal; 9 | in vec4 fColor; 10 | 11 | out vec4 FragColor; 12 | 13 | void main() { 14 | FragColor = fColor; 15 | FragColor.a = 1.0 - max((distance(cameraPos, fPosition) - nearFog) / (farFog - nearFog), 0.0); 16 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/CubemapFace.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the faces of a . 5 | /// 6 | public enum CubemapFace 7 | { 8 | PositiveX = 34069, 9 | NegativeX = 34070, 10 | PositiveY = 34071, 11 | NegativeY = 34072, 12 | PositiveZ = 34073, 13 | NegativeZ = 34074 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TrippyGL.Fonts/Exceptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL.Fonts 4 | { 5 | public class FontLoadingException : Exception 6 | { 7 | public FontLoadingException() : base() { } 8 | 9 | public FontLoadingException(string? message) : base(message) { } 10 | 11 | public FontLoadingException(string? message, Exception? innerException) : base(message, innerException) { } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TrippyGL/Enums/DebugType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | public enum DebugType 4 | { 5 | DontCare = 4352, 6 | Error = 33356, 7 | DeprecatedBehavior = 33357, 8 | UndefinedBehavior = 33358, 9 | Portability = 33359, 10 | Performance = 33360, 11 | Other = 33361, 12 | Marker = 33384, 13 | PushGroup = 33385, 14 | PopGroup = 33386 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TrippyGL/Enums/DepthFunction.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the functions that can be used to perform depth testing. 5 | /// 6 | public enum DepthFunction 7 | { 8 | Never = 512, 9 | Less = 513, 10 | Equal = 514, 11 | LessOrEqual = 515, 12 | Greater = 516, 13 | NotEqual = 517, 14 | GreaterOrEqual = 518, 15 | Always = 519 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TrippyTests/InstancedCubes/vs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 World; 4 | uniform mat4 View; 5 | uniform mat4 Projection; 6 | 7 | in vec3 vPosition; 8 | in vec4 vColor; 9 | in vec4 vOffset; 10 | 11 | out vec4 fColor; 12 | 13 | void main() { 14 | fColor = vColor; 15 | 16 | vec4 worldPosition = World * vec4(vPosition * vOffset.w, 1.0); 17 | worldPosition.xyz += vOffset.xyz; 18 | gl_Position = Projection * View * worldPosition; 19 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/TextureMinFilter.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the minifying filters that can be used when sampling a texture. 5 | /// 6 | public enum TextureMinFilter 7 | { 8 | Nearest = 9728, 9 | Linear = 9729, 10 | NearestMipmapNearest = 9984, 11 | LinearMipmapNearest = 9985, 12 | NearestMipmapLinear = 9986, 13 | LinearMipmapLinear = 9987 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /TrippyGL/Enums/BlendingMode.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the blending equation modes that can be used in a . 5 | /// 6 | public enum BlendingMode 7 | { 8 | FuncAdd = 32774, 9 | Min = 32775, 10 | Max = 32776, 11 | FuncSubtract = 32778, 12 | FuncReverseSubtract = 32779, 13 | AlphaMin = 33568, 14 | AlphaMax = 33569 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TrippyGL/Enums/StencilFunction.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the different comparisons that can be used in a stencil test. 5 | /// 6 | public enum StencilFunction 7 | { 8 | Never = 512, 9 | Less = 513, 10 | Equal = 514, 11 | LessOrEqual = 515, 12 | Greater = 516, 13 | NotEqual = 517, 14 | GreaterOrEqual = 518, 15 | Always = 519 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TrippyGL/Enums/StencilOperation.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the operations that can be performed after comparison during a stencil test. 5 | /// 6 | public enum StencilOperation 7 | { 8 | Zero = 0, 9 | Invert = 5386, 10 | Keep = 7680, 11 | Replace = 7681, 12 | Increment = 7682, 13 | Decrement = 7683, 14 | IncrementWrap = 34055, 15 | DecrementWrap = 34056 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCube/SimpleCube.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /TrippyTests/SimpleTriangle/SimpleTriangle.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /TrippyGL/Enums/DepthStencilFormat.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies depth and/or stencil formats a can have. 5 | /// 6 | public enum DepthStencilFormat 7 | { 8 | None = 0, 9 | Depth24Stencil8 = 35056, 10 | Depth32fStencil8 = 36013, 11 | Depth16 = 33189, 12 | Depth24 = 33190, 13 | Depth32f = 36012, 14 | Stencil8 = 36168 //not a recommended format though, better to use Depth24Stencil8 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /TrippyTests/TrippyTestBase/TrippyTestBase.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | annotations 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /TrippyGL/Enums/AttributeBaseType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the base data types a vertex attribute can be composed of. 5 | /// 6 | public enum AttributeBaseType 7 | { 8 | Byte = 5120, 9 | UnsignedByte = 5121, 10 | Short = 5122, 11 | UnsignedShort = 5123, 12 | Int = 5124, 13 | UnsignedInt = 5125, 14 | Float = 5126, 15 | Double = 5130, 16 | HalfFloat = 5131, 17 | Fixed = 5132, 18 | //Int64Arb = 5134, 19 | //UnsignedInt64Arb = 5135, 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /TrippyGL/Enums/PrimitiveType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the types of primitives that can be rendered. 5 | /// 6 | public enum PrimitiveType 7 | { 8 | Points = 0, 9 | Lines = 1, 10 | LineLoop = 2, 11 | LineStrip = 3, 12 | Triangles = 4, 13 | TriangleStrip = 5, 14 | TriangleFan = 6, 15 | Quads = 7, 16 | LinesAdjacency = 10, 17 | LineStripAdjacency = 11, 18 | TrianglesAdjacency = 12, 19 | TriangleStripAdjacency = 13, 20 | Patches = 14, 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /TrippyTests/ShaderFractals/fs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | const int loops = 300; 3 | 4 | uniform vec2 c; 5 | 6 | in vec2 fCoords; 7 | 8 | out vec4 FragColor; 9 | 10 | vec2 CMult(vec2 a, vec2 b) { 11 | return vec2(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x); 12 | } 13 | 14 | vec3 Jul(vec2 v) { 15 | for (int i=0; i 2.0 || v.y > 2.0) 18 | return vec3(float(i)/100.0, float(i)/150.0, float(i)/50.0); 19 | } 20 | 21 | return vec3(0, 0, 0); 22 | } 23 | 24 | void main() { 25 | FragColor = vec4(Jul(fCoords), 1.0); 26 | } -------------------------------------------------------------------------------- /TrippyTests/InstancedCubes/InstancedCubes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/IndexedRendering.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/ComplexVertexFormats.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/waterVs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 View; 4 | uniform mat4 Projection; 5 | 6 | uniform vec3 cameraPos; 7 | 8 | in vec3 vPosition; 9 | 10 | out vec3 fPosition; 11 | out vec4 clipSpace; 12 | out vec2 distortCoords; 13 | out vec3 toCameraVector; 14 | out float waterDepth; 15 | out float aboveWater; 16 | 17 | void main() { 18 | fPosition = vPosition; 19 | distortCoords = (vPosition.xz * 0.5) * 0.04; 20 | clipSpace = Projection * View * vec4(vPosition.x, 0.0, vPosition.z, 1.0); 21 | toCameraVector = cameraPos - vPosition; 22 | waterDepth = -vPosition.y; 23 | aboveWater = sign(cameraPos.y); 24 | gl_Position = clipSpace; 25 | } -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/underwaterFs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform sampler2D textureSamp; 4 | uniform sampler2D depthSamp; 5 | 6 | uniform float nearPlane; 7 | uniform float farPlane; 8 | 9 | uniform float maxDistance; 10 | 11 | uniform vec3 waterColor; 12 | 13 | in vec4 fColor; 14 | in vec2 fTexCoords; 15 | 16 | out vec4 FragColor; 17 | 18 | float depthToDistance(in float depth) { 19 | return 2.0 * nearPlane * farPlane / (farPlane + nearPlane - (2.0 * depth - 1.0) * (farPlane - nearPlane)); 20 | } 21 | 22 | void main() { 23 | float dist = depthToDistance(texture(depthSamp, fTexCoords).x); 24 | FragColor = mix(texture(textureSamp, fTexCoords), vec4(waterColor, 1.0), min(dist / maxDistance, 1.0)); 25 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/RenderbufferFormat.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies formats a 's storage can have. 5 | /// 6 | public enum RenderbufferFormat 7 | { 8 | Color4b = 32856, 9 | 10 | Float = 33326, 11 | Float2 = 33328, 12 | Float4 = 34836, 13 | 14 | Int = 33333, 15 | Int2 = 33339, 16 | Int4 = 36226, 17 | 18 | UnsignedInt = 33334, 19 | UnsignedInt2 = 33340, 20 | UnsignedInt4 = 36208, 21 | 22 | Depth16 = 33189, 23 | Depth24 = 33190, 24 | Depth32f = 36012, 25 | Depth24Stencil8 = 35056, 26 | Depth32fStencil8 = 36013, 27 | Stencil8 = 36168, 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TrippyTests/GameOfLifeSim/GameOfLifeSim.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | PreserveNewest 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | PreserveNewest 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/vs1.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform mat4 Projection; 4 | 5 | in float X; 6 | in float Y; 7 | in float Z; 8 | 9 | in float colorR; 10 | in float colorG; 11 | in float colorB; 12 | 13 | in mat4 matrix1; 14 | 15 | in float nothing0; 16 | in float sixtyFour; 17 | in float sixtyThree; 18 | in vec4 oneTwoThreeFour; 19 | in int alwaysZero; 20 | in float alsoZero; 21 | 22 | out vec4 fColor; 23 | 24 | void main() { 25 | gl_Position = Projection * matrix1 * vec4(X + float(alwaysZero), Y + alsoZero, Z, oneTwoThreeFour.x); 26 | fColor = vec4( 27 | colorR * (nothing0 + 1.0), 28 | colorG / 2048.0 * (oneTwoThreeFour.z - oneTwoThreeFour.y), 29 | colorB * (sixtyFour - sixtyThree), 30 | oneTwoThreeFour.w - oneTwoThreeFour.z 31 | ); 32 | } -------------------------------------------------------------------------------- /TrippyGL/Enums/FramebufferAttachmentPoint.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the attachment points on a . 5 | /// 6 | public enum FramebufferAttachmentPoint 7 | { 8 | Color0 = 36064, 9 | Color1 = 36065, 10 | Color2 = 36066, 11 | Color3 = 36067, 12 | Color4 = 36068, 13 | Color5 = 36069, 14 | Color6 = 36070, 15 | Color7 = 36071, 16 | Color8 = 36072, 17 | Color9 = 36073, 18 | Color10 = 36074, 19 | Color11 = 36075, 20 | Color12 = 36076, 21 | Color13 = 36077, 22 | Color14 = 36078, 23 | Color15 = 36079, 24 | Depth = 36096, 25 | Stencil = 36128, 26 | DepthStencil = 33306 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TrippyGL/Enums/ReadPixelsFormat.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies formats for a read pixels operation on a . 5 | /// 6 | public enum ReadPixelsFormat 7 | { 8 | // Allowed values in glReadPixels: 9 | // GL_RED, GL_GREEN, GL_BLUE, GL_RGB, GL_BGR, GL_RGBA, and GL_BGRA, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL 10 | // Source: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glReadPixels.xhtml 11 | 12 | Red = 6403, 13 | Green = 6404, 14 | Blue = 6405, 15 | Rgb = 6407, 16 | Bgr = 32992, 17 | Rgba = 6408, 18 | Bgra = 32993, 19 | DepthComponent = 6402, 20 | StencilIndex = 6401, 21 | DepthStencil = 34041 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /TrippyGL/Enums/BufferTarget.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the buffer binding points at which a can be bound. 5 | /// 6 | public enum BufferTarget 7 | { 8 | ParameterBuffer = 33006, 9 | ArrayBuffer = 34962, 10 | ElementArrayBuffer = 34963, 11 | PixelPackBuffer = 35051, 12 | PixelUnpackBuffer = 35052, 13 | UniformBuffer = 35345, 14 | TextureBuffer = 35882, 15 | TransformFeedbackBuffer = 35982, 16 | CopyReadBuffer = 36662, 17 | CopyWriteBuffer = 36663, 18 | DrawIndirectBuffer = 36671, 19 | ShaderStorageBuffer = 37074, 20 | DispatchIndirectBuffer = 37102, 21 | QueryBuffer = 37266, 22 | AtomicCounterBuffer = 37568 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/SimpleVertex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | using TrippyGL; 4 | 5 | namespace IndexedRendering 6 | { 7 | /// 8 | /// A simple vertex type with only one vertex attribute, a float vec2. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | struct SimpleVertex : IVertex 12 | { 13 | public float X, Y; 14 | 15 | public SimpleVertex(float x, float y) 16 | { 17 | X = x; 18 | Y = y; 19 | } 20 | 21 | public int AttribDescriptionCount => 1; 22 | 23 | public void WriteAttribDescriptions(Span descriptions) 24 | { 25 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec2); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/skyFs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform vec3 sunDirection; 4 | uniform vec3 sunColor; 5 | 6 | in vec3 fPosition; 7 | 8 | out vec4 FragColor; 9 | 10 | const float Pi = 3.1415926535897932; 11 | 12 | void main() { 13 | vec3 vec = normalize(fPosition); 14 | float rotX = asin(vec.y); 15 | float rotY = acos(clamp(vec.x / cos(rotX), -1, 1)); 16 | rotY *= sign(vec.z); 17 | 18 | float texCoord = rotX / Pi + 0.5; 19 | vec3 color = texCoord > 0.5 ? 20 | mix(vec3(0.42, 0.76, 0.73), vec3(0.056, 0.27, 0.63), (texCoord-0.5)*2.0) 21 | : mix(vec3(0.75, 0.95, 1.0), vec3(0.42, 0.76, 0.73), texCoord*2.0); 22 | 23 | float sunStrenght = pow(clamp(dot(sunDirection, vec)+0.005, 0, 1), 32.0); 24 | FragColor.rgb = mix(color, sunColor, sunStrenght); 25 | FragColor.a = 1.0; 26 | } -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/terrainVs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform vec3 cameraPos; 4 | uniform vec3 sunDirection; 5 | 6 | uniform mat4 View; 7 | uniform mat4 Projection; 8 | 9 | in vec3 vPosition; 10 | in vec3 vNormal; 11 | in vec4 vColor; 12 | in vec2 vLightingConfig; 13 | 14 | out vec3 fPosition; 15 | out vec3 fNormal; 16 | out vec4 fColor; 17 | 18 | void main() { 19 | 20 | float d = 1.0; //max(dot(sunDirection, vNormal)*0.2+0.8, 0.775f); 21 | vec3 r = reflect(-sunDirection, vNormal); 22 | 23 | float specular = max(dot(r, normalize(cameraPos - vPosition)), 0); 24 | d += vLightingConfig.x * pow(specular, vLightingConfig.y); 25 | 26 | fPosition = vPosition; 27 | fNormal = vNormal; 28 | fColor = vec4(vColor.rgb * d, vColor.a); 29 | 30 | gl_Position = Projection * View * vec4(vPosition, 1.0); 31 | } -------------------------------------------------------------------------------- /TrippyTests/TexturedTriangles/TexturedTriangles.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /TrippyGL/Enums/BlendingFactor.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the possible blending factors that can be used in a blending equation. 5 | /// 6 | public enum BlendingFactor 7 | { 8 | Zero = 0, 9 | One = 1, 10 | SrcColor = 768, 11 | OneMinusSrcColor = 769, 12 | SrcAlpha = 770, 13 | OneMinusSrcAlpha = 771, 14 | DstAlpha = 772, 15 | OneMinusDstAlpha = 773, 16 | DstColor = 774, 17 | OneMinusDstColor = 775, 18 | SrcAlphaSaturate = 776, 19 | ConstantColor = 32769, 20 | OneMinusConstantColor = 32770, 21 | ConstantAlpha = 32771, 22 | OneMinusConstantAlpha = 32772, 23 | Src1Alpha = 34185, 24 | Src1Color = 35065, 25 | OneMinusSrc1Color = 35066, 26 | OneMinusSrc1Alpha = 35067 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /TrippyGL/IVertex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// This interface is used by different classes that need vertex specification (such as 7 | /// , ) to be able to handle vertex specification 8 | /// work in a more convenient way. 9 | /// All vertex structs that properly implement this interface can be easily used with these classes. 10 | /// 11 | public interface IVertex 12 | { 13 | /// The amount of attribute descriptions this vertex type has. 14 | int AttribDescriptionCount { get; } 15 | 16 | /// 17 | /// Gets the vertex attrib descriptors by writting them into a . 18 | /// The must have a length of . 19 | /// 20 | void WriteAttribDescriptions(Span descriptions); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.cs] 2 | 3 | # CA1051: Do not declare visible instance fields 4 | dotnet_diagnostic.CA1051.severity = none 5 | 6 | # CA2225: Operator overloads have named alternates 7 | dotnet_diagnostic.CA2225.severity = none 8 | 9 | # CA1067: Override Object.Equals(object) when implementing IEquatable 10 | dotnet_diagnostic.CA1067.severity = none 11 | 12 | # CA1305: Specify IFormatProvider 13 | dotnet_diagnostic.CA1305.severity = none 14 | 15 | # CA1303: Do not pass literals as localized parameters 16 | dotnet_diagnostic.CA1303.severity = none 17 | 18 | # CA1720: Identifier contains type name 19 | dotnet_diagnostic.CA1720.severity = none 20 | 21 | # CA1044: Properties should not be write only 22 | dotnet_diagnostic.CA1044.severity = none 23 | 24 | # CA2208: Instantiate argument exceptions correctly 25 | dotnet_diagnostic.CA2208.severity = none 26 | 27 | # CA5394: Do not use insecure randomness 28 | dotnet_diagnostic.CA5394.severity = none 29 | 30 | # CA1028: Enum Storage should be Int32 31 | dotnet_diagnostic.CA1028.severity = none 32 | -------------------------------------------------------------------------------- /TrippyTests/ShaderFractals/ShaderFractals.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | true 10 | 11 | 12 | 13 | true 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | PreserveNewest 24 | 25 | 26 | PreserveNewest 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /TrippyGL/Enums/TextureImageFormat.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies formats a 's image can have. 5 | /// 6 | public enum TextureImageFormat 7 | { 8 | // These are organized in such away so the base type (float, int, uint) 9 | // is distinguisable by dividing by 32 and the remainder indicates the amount of components 10 | // (amount of components: Color4b has 4, Vector2 has 2, Vector3i has 3, etc) 11 | // This is done in TrippyUtils.GetTextureFormatEnums() 12 | 13 | Color4b = 5, 14 | 15 | Float = 33, 16 | Float2 = 34, 17 | Float3 = 35, 18 | Float4 = 36, 19 | Depth16 = 37, 20 | Depth24 = 38, 21 | Depth32f = 39, 22 | 23 | Int = 65, 24 | Int2 = 66, 25 | Int3 = 67, 26 | Int4 = 68, 27 | 28 | UnsignedInt = 97, 29 | UnsignedInt2 = 98, 30 | UnsignedInt3 = 99, 31 | UnsignedInt4 = 100, 32 | 33 | Depth24Stencil8 = 129, 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Thomas Mizrahi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /TrippyTests/SimpleShader3D/skyFs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform float time; 4 | uniform vec3 sunDirection; 5 | uniform vec3 sunColor; 6 | 7 | in vec3 fPosition; 8 | 9 | out vec4 FragColor; 10 | 11 | float rand(float x, float y) { 12 | return fract(sin(dot(vec2(x, y), vec2(46.49791, -77.58153))) * 839.441956); 13 | } 14 | 15 | float waves(float rx, float ry) { 16 | return (sin(rx*2.0 - ry + time*1.1 + 1.3) * 0.5 + 0.5) 17 | * (sin(ry*3.0 - rx + time*0.5 + 2.7) * 0.5 + 0.5); 18 | } 19 | 20 | void main() { 21 | vec3 norm = normalize(fPosition); 22 | float rotX = asin(norm.y); 23 | float rotY = acos(clamp(norm.x / cos(rotX), -1, 1)); 24 | rotY *= sign(norm.z); 25 | 26 | float rx = rotX + sin(rotY*3.0 + time*4.4 - 0.7) * 0.2; 27 | float ry = rotY + sin(rotX*2.0 - time*2.3 + 1.5) * 0.2; 28 | 29 | float w = waves(rx, ry); 30 | 31 | float p = pow(min(w+0.3, 1), 3)*0.15; 32 | float s = sqrt(w); 33 | vec3 skyMix = vec3(p + s*0.2, p, p + s); 34 | skyMix.xyz += rand(rotX, rotY)*0.1; 35 | 36 | float sunStrenght = max(pow(dot(norm, sunDirection)+0.01, 64), 0); 37 | 38 | FragColor = vec4(skyMix * (1.0-clamp(sunStrenght, -0.2, 0.2)) + sunColor * sunStrenght, 1.0); 39 | } -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/TextureBatcherTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | PreserveNewest 25 | 26 | 27 | PreserveNewest 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/TerrainVertex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | using TrippyGL; 5 | 6 | namespace TerrainMaker 7 | { 8 | [StructLayout(LayoutKind.Sequential)] 9 | struct TerrainVertex : IVertex 10 | { 11 | public Vector3 Position; 12 | public Vector3 Normal; 13 | public Color4b Color; 14 | public Vector2 LightingConfig; 15 | 16 | public TerrainVertex(Vector3 position, Vector3 normal, Color4b color, Vector2 lightingConfig) 17 | { 18 | Position = position; 19 | Normal = normal; 20 | Color = color; 21 | LightingConfig = lightingConfig; 22 | } 23 | 24 | public int AttribDescriptionCount => 4; 25 | 26 | public void WriteAttribDescriptions(Span descriptions) 27 | { 28 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 29 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec3); 30 | descriptions[2] = new VertexAttribDescription(AttributeType.FloatVec4, true, AttributeBaseType.UnsignedByte); 31 | descriptions[3] = new VertexAttribDescription(AttributeType.FloatVec2); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TrippyTests/SimpleShader3D/SimpleShader3D.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | PreserveNewest 25 | 26 | 27 | PreserveNewest 28 | 29 | 30 | PreserveNewest 31 | 32 | 33 | PreserveNewest 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /TrippyTests/GameOfLifeSim/sim_fs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform sampler2D previous; 4 | uniform vec2 pixelDelta; 5 | 6 | in vec2 fTexCoords; 7 | 8 | out vec4 FragColor; 9 | 10 | void main() { 11 | vec4 prev = texture(previous, fTexCoords); 12 | 13 | vec4 sum = texture(previous, fTexCoords + vec2(-pixelDelta.x, -pixelDelta.y)); 14 | sum += texture(previous, fTexCoords + vec2(0, -pixelDelta.y)); 15 | sum += texture(previous, fTexCoords + vec2(pixelDelta.x, -pixelDelta.y)); 16 | sum += texture(previous, fTexCoords + vec2(-pixelDelta.x, 0)); 17 | sum += texture(previous, fTexCoords + vec2(pixelDelta.x, 0)); 18 | sum += texture(previous, fTexCoords + vec2(-pixelDelta.x, pixelDelta.y)); 19 | sum += texture(previous, fTexCoords + vec2(0, pixelDelta.y)); 20 | sum += texture(previous, fTexCoords + vec2(pixelDelta.x, pixelDelta.y)); 21 | 22 | // Single color mode 23 | /*vec3 next = mix( 24 | vec3(step(2.8, sum.x) * step(sum.x, 3.2)), 25 | vec3(step(1.9, sum.x) * step(sum.x, 3.1)), 26 | step(0.5, prev.x) 27 | );*/ 28 | 29 | // Multi color mode 30 | vec3 next = mix( 31 | vec3(step(vec3(2.8), sum.xyz) * step(sum.xyz, vec3(3.2))), 32 | vec3(step(vec3(1.9), sum.xyz) * step(sum.xyz, vec3(3.1))), 33 | step(0.5, prev.xyz) 34 | ); 35 | 36 | FragColor = vec4(next, 1.0); 37 | } -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/TerrainChunk.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using TrippyGL; 3 | 4 | namespace TerrainMaker 5 | { 6 | class TerrainChunk : IDisposable 7 | { 8 | public readonly int GridX; 9 | public readonly int GridY; 10 | 11 | public VertexBuffer TerrainBuffer { get; private set; } 12 | public VertexBuffer UnderwaterBuffer { get; private set; } 13 | 14 | public TerrainChunk(GraphicsDevice graphicsDevice, in TerrainChunkData chunkData) 15 | { 16 | GridX = chunkData.GridX; 17 | GridY = chunkData.GridY; 18 | if (chunkData.TerrainVertexCount > 0) 19 | TerrainBuffer = new VertexBuffer(graphicsDevice, chunkData.TerrainMesh, BufferUsage.StaticDraw); 20 | 21 | if (chunkData.UnderwaterVertexCount > 0) 22 | UnderwaterBuffer = new VertexBuffer(graphicsDevice, chunkData.UnderwaterMesh, BufferUsage.StaticDraw); 23 | } 24 | 25 | public void Dispose() 26 | { 27 | if (!TerrainBuffer.IsEmpty) TerrainBuffer.Dispose(); 28 | if (!UnderwaterBuffer.IsEmpty) UnderwaterBuffer.Dispose(); 29 | } 30 | 31 | public override string ToString() 32 | { 33 | return "Chunk (" + GridX + ", " + GridY + ")"; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TrippyGL/Enums/TextureType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the types of textures. 5 | /// 6 | public enum TextureType 7 | { 8 | Texture1D = 3552, 9 | Texture2D = 3553, 10 | //ProxyTexture1D = 32867, 11 | //ProxyTexture2D = 32868, 12 | Texture3D = 32879, 13 | //ProxyTexture3D = 32880, 14 | //DetailTexture2DSgis = 32917, 15 | //Texture4DSgis = 33076, 16 | //ProxyTexture4DSgis = 33077, 17 | //TextureRectangle = 34037, 18 | //ProxyTextureRectangle = 34039, 19 | TextureCubeMap = 34067, 20 | //TextureCubeMapPositiveX = 34069, 21 | //TextureCubeMapNegativeX = 34070, 22 | //TextureCubeMapPositiveY = 34071, 23 | //TextureCubeMapNegativeY = 34072, 24 | //TextureCubeMapPositiveZ = 34073, 25 | //TextureCubeMapNegativeZ = 34074, 26 | //ProxyTextureCubeMap = 34075, 27 | Texture1DArray = 35864, 28 | //ProxyTexture1DArray = 35865, 29 | Texture2DArray = 35866, 30 | //ProxyTexture2DArray = 35867, 31 | TextureCubeMapArray = 36873, 32 | //ProxyTextureCubeMapArray = 36875, 33 | Texture2DMultisample = 37120, 34 | //ProxyTexture2DMultisample = 37121, 35 | Texture2DMultisampleArray = 37122, 36 | //ProxyTexture2DMultisampleArray = 37123 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/TerrainChunkData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TerrainMaker 4 | { 5 | readonly struct TerrainChunkData 6 | { 7 | public readonly int GridX; 8 | public readonly int GridY; 9 | 10 | private readonly TerrainVertex[] terrainMesh; 11 | private readonly TerrainVertex[] underwaterMesh; 12 | 13 | public readonly int TerrainVertexCount; 14 | public readonly int UnderwaterVertexCount; 15 | 16 | public ReadOnlySpan TerrainMesh => new ReadOnlySpan(terrainMesh, 0, TerrainVertexCount); 17 | public ReadOnlySpan UnderwaterMesh => new ReadOnlySpan(underwaterMesh, 0, UnderwaterVertexCount); 18 | 19 | public TerrainChunkData(int gridX, int gridY, TerrainVertex[] terrainMesh, int terrainVertexCount, 20 | TerrainVertex[] underwaterMesh, int underwaterVertexCount) 21 | { 22 | GridX = gridX; 23 | GridY = gridY; 24 | this.terrainMesh = terrainMesh; 25 | this.underwaterMesh = underwaterMesh; 26 | TerrainVertexCount = terrainVertexCount; 27 | UnderwaterVertexCount = underwaterVertexCount; 28 | } 29 | 30 | public void RetrieveBackingArrays(out TerrainVertex[] arr1, out TerrainVertex[] arr2) 31 | { 32 | arr1 = terrainMesh; 33 | arr2 = underwaterMesh; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/SimpleCubemap.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | PreserveNewest 19 | 20 | 21 | PreserveNewest 22 | 23 | 24 | PreserveNewest 25 | 26 | 27 | PreserveNewest 28 | 29 | 30 | PreserveNewest 31 | 32 | 33 | PreserveNewest 34 | 35 | 36 | PreserveNewest 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /TrippyGL/TextureMultisamplable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// An abstract type that supports multisampling. 7 | /// 8 | public abstract class TextureMultisamplable : Texture 9 | { 10 | /// The amount of samples this has. 11 | public uint Samples { get; private set; } 12 | 13 | internal TextureMultisamplable(GraphicsDevice graphicsDevice, TextureType type, uint samples, TextureImageFormat imageFormat) 14 | : base(graphicsDevice, type, imageFormat) 15 | { 16 | ValidateSampleCount(samples); 17 | Samples = samples; 18 | } 19 | 20 | private void ValidateSampleCount(uint samples) 21 | { 22 | if (samples < 0 || samples > GraphicsDevice.MaxSamples) 23 | throw new ArgumentOutOfRangeException(nameof(samples), samples, nameof(samples) + " must be in the range [0, " + nameof(GraphicsDevice.MaxSamples) + "]"); 24 | } 25 | 26 | protected void ValidateNotMultisampledPixelAccess() 27 | { 28 | if (Samples != 0) 29 | throw new InvalidOperationException("You can't read/write the pixels of a multisampled texture"); 30 | } 31 | 32 | protected void ValidateNotMultisampledWrapStates() 33 | { 34 | if (Samples != 0) 35 | throw new InvalidOperationException("You can't change a multisampled texture's sampler states"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TrippyGL.ImageSharp/ImageUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SixLabors.ImageSharp.Formats; 3 | 4 | namespace TrippyGL.ImageSharp 5 | { 6 | /// 7 | /// Contains helper image-related methods used across the library. 8 | /// 9 | public static class ImageUtils 10 | { 11 | internal const string ImageSizeMustMatchTextureSizeError = "The size of the image must match the size of the texture"; 12 | 13 | internal const string TextureFormatMustBeColor4bError = "The texture's format must be Color4b (RGBA)"; 14 | 15 | /// 16 | /// Gets an appropiate for the given . 17 | /// 18 | public static IImageFormat GetFormatFor(SaveImageFormat imageFormat) 19 | { 20 | return imageFormat switch 21 | { 22 | SaveImageFormat.Png => SixLabors.ImageSharp.Formats.Png.PngFormat.Instance, 23 | SaveImageFormat.Jpeg => SixLabors.ImageSharp.Formats.Jpeg.JpegFormat.Instance, 24 | SaveImageFormat.Bmp => SixLabors.ImageSharp.Formats.Bmp.BmpFormat.Instance, 25 | SaveImageFormat.Gif => SixLabors.ImageSharp.Formats.Gif.GifFormat.Instance, 26 | _ => throw new ArgumentException("Invalid " + nameof(SaveImageFormat), nameof(imageFormat)), 27 | }; 28 | } 29 | } 30 | 31 | /// 32 | /// Specifies image file formats. 33 | /// 34 | public enum SaveImageFormat 35 | { 36 | Png, Jpeg, Bmp, Gif 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/TerrainMaker.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | PreserveNewest 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | PreserveNewest 29 | 30 | 31 | PreserveNewest 32 | 33 | 34 | PreserveNewest 35 | 36 | 37 | PreserveNewest 38 | 39 | 40 | PreserveNewest 41 | 42 | 43 | PreserveNewest 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TrippyGL 2 | [![NuGet](https://img.shields.io/nuget/v/TrippyGL)](https://nuget.org/packages/TrippyGL) 3 | 4 | A simple and lightweight, yet highly versatile OpenGL graphics library built on top of [Silk.NET](https://github.com/Ultz/Silk.NET/). 5 | 6 | TrippyGL is built for .NET 6.0! 7 | 8 | Targets OpenGL 3.3 and up. 9 | 10 | The TrippyGL.ImageSharp project provides integration with [ImageSharp](https://github.com/SixLabors/ImageSharp) for easy texture loading and saving. 11 | 12 | For windowing and input, the [Silk.NET.Windowing](https://www.nuget.org/packages/Silk.NET.Windowing) and [Silk.NET.Input](https://www.nuget.org/packages/Silk.NET.Input) packages can be used. 13 | 14 | ## Getting Started 15 | [The wiki](https://github.com/SilkCommunity/TrippyGL/wiki) contains explanatory articles and guides to help you get started! I recommend first reading [Getting Started: Opening a Window](https://github.com/SilkCommunity/TrippyGL/wiki/Getting-Started:-Opening-a-Window). After that, if you're after drawing textures check out [Getting Started: Drawing a Texture](https://github.com/SilkCommunity/TrippyGL/wiki/Getting-Started:-Drawing-a-Texture). Or if you're after drawing triangles, check out [Hello Triangle: The easy way](https://github.com/SilkCommunity/TrippyGL/wiki/Hello-Triangle:-The-easy-way). 16 | 17 | ## Need Help? 18 | Feel free to come ask questions over at the [TrippyGL Discord server](https://discord.gg/3j5Q4zN)! 19 | 20 | ## Gallery 21 | ![](https://raw.githubusercontent.com/SilkCommunity/TrippyGL/master/images/img_terrain.png) 22 | 23 | ![](https://raw.githubusercontent.com/SilkCommunity/TrippyGL/master/images/img_lighting.png) 24 | 25 | ![](https://raw.githubusercontent.com/SilkCommunity/TrippyGL/master/images/img_fractal.png) 26 | 27 | ![](https://raw.githubusercontent.com/SilkCommunity/TrippyGL/master/images/img_conways.png) 28 | 29 | ![](https://raw.githubusercontent.com/SilkCommunity/TrippyGL/master/images/img_bouncyballs.png) 30 | -------------------------------------------------------------------------------- /TrippyGL.Fonts/TrippyGL.Fonts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | true 6 | 1.2.2 7 | ThomasMiz 8 | MIT 9 | https://github.com/SilkCommunity/TrippyGL 10 | https://github.com/SilkCommunity/TrippyGL 11 | TrippyGL_logo.png 12 | TrippyGL is a highly versatile, yet lightweight and simple to use OpenGL graphics library that runs on .NET Core. 13 | 14 | This package provides basic font creating/loading/saving functionality. 15 | OpenGL;graphics;gamedev;desktopgl;fonts;text 16 | Changelog from 1.2.1: 17 | - Upgraded all dependencies 18 | git 19 | enable 20 | README.md 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | all 30 | runtime; build; native; contentfiles; analyzers; buildtransitive 31 | 32 | 33 | 34 | 35 | 36 | 37 | True 38 | \ 39 | 40 | 41 | True 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /TrippyTests/SimpleTriangle/SimpleTriangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using Silk.NET.Maths; 4 | using TrippyGL; 5 | using TrippyTestBase; 6 | 7 | namespace SimpleTriangle 8 | { 9 | // A simple project that opens an OpenGL window and renders a centered colored triangle. 10 | 11 | class SimpleTriangle : TestBase 12 | { 13 | VertexBuffer vertexBuffer; 14 | SimpleShaderProgram shaderProgram; 15 | 16 | protected override void OnLoad() 17 | { 18 | Span vertexData = stackalloc VertexColor[] 19 | { 20 | new VertexColor(new Vector3(-0.5f, -0.5f, 0), new Color4b(255, 0, 0, 255)), 21 | new VertexColor(new Vector3(0, 0.5f, 0), new Color4b(0, 255, 0, 255)), 22 | new VertexColor(new Vector3(0.5f, -0.5f, 0), new Color4b(0, 0, 255, 255)), 23 | }; 24 | 25 | vertexBuffer = new VertexBuffer(graphicsDevice, vertexData, BufferUsage.StaticDraw); 26 | shaderProgram = SimpleShaderProgram.Create(graphicsDevice); 27 | 28 | graphicsDevice.ClearColor = new Vector4(0, 0, 0, 1); 29 | graphicsDevice.BlendingEnabled = false; 30 | graphicsDevice.DepthTestingEnabled = false; 31 | } 32 | 33 | protected override void OnRender(double dt) 34 | { 35 | graphicsDevice.Clear(ClearBuffers.Color); 36 | 37 | graphicsDevice.VertexArray = vertexBuffer; 38 | graphicsDevice.ShaderProgram = shaderProgram; 39 | graphicsDevice.DrawArrays(PrimitiveType.Triangles, 0, 3); 40 | } 41 | 42 | protected override void OnResized(Vector2D size) 43 | { 44 | if (size.X == 0 || size.Y == 0) 45 | return; 46 | 47 | graphicsDevice.SetViewport(0, 0, (uint)size.X, (uint)size.Y); 48 | } 49 | 50 | protected override void OnUnload() 51 | { 52 | vertexBuffer.Dispose(); 53 | shaderProgram.Dispose(); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /TrippyGL/TrippyGL.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | MIT 6 | https://github.com/SilkCommunity/TrippyGL 7 | TrippyGL_logo.png 8 | https://github.com/SilkCommunity/TrippyGL 9 | true 10 | 1.2.2 11 | ThomasMiz 12 | A highly versatile, yet lightweight and simple to use OpenGL graphics library that runs on .NET Core 13 | OpenGL;graphics;gamedev;desktopgl; 14 | Changelog from 1.2.1: 15 | - Upgraded all dependencies 16 | git 17 | enable 18 | True 19 | False 20 | README.md 21 | 22 | 23 | 24 | true 25 | 26 | 27 | 28 | 29 | true 30 | 31 | 32 | 33 | 34 | all 35 | runtime; build; native; contentfiles; analyzers; buildtransitive 36 | 37 | 38 | 39 | 40 | 41 | 42 | True 43 | \ 44 | 45 | 46 | True 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TrippyGL/Enums/BufferUsage.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies values that hint on how a will be used. 5 | /// 6 | /// 7 | /// Explanations and descriptions for these can be found in the khronos OpenGL wiki: 8 | /// https://www.khronos.org/opengl/wiki/Buffer_Object#Buffer_Object_Usage 9 | /// 10 | public enum BufferUsage 11 | { 12 | /// 13 | /// Optimizes the buffer for it's data to be set every use, or almost every use, and used for drawing. 14 | /// 15 | StreamDraw = 35040, 16 | 17 | /// 18 | /// Optimizes the buffer for it's data to be set every use, or almost every use, and used for reading back. 19 | /// 20 | StreamRead = 35041, 21 | 22 | /// 23 | /// Optimizes the buffer for it's data to be set every use, or almost every use, and used for copying within GPU memory. 24 | /// 25 | StreamCopy = 35042, 26 | 27 | /// 28 | /// Optimizes the buffer for it's data to be set once and used for drawing. 29 | /// 30 | StaticDraw = 35044, 31 | 32 | /// 33 | /// Optimizes the buffer for it's data to be set once and used for reading back. 34 | /// 35 | StaticRead = 35045, 36 | 37 | /// 38 | /// Optimizes the buffer for it's data to be set once and used for copying within GPU memory. 39 | /// 40 | StaticCopy = 35046, 41 | 42 | /// 43 | /// Optimizes the buffer for it's data to be set often and used for drawing. 44 | /// 45 | DynamicDraw = 35048, 46 | 47 | /// 48 | /// Optimizes the buffer for it's data to be set often and used for reading back. 49 | /// 50 | DynamicRead = 35049, 51 | 52 | /// 53 | /// Optimizes the buffer for it's data to be set often and used for copying within GPU memory. 54 | /// 55 | DynamicCopy = 35050 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /TrippyGL.Fonts.Extensions/TrippyGL.Fonts.Extensions.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | https://github.com/SilkCommunity/TrippyGL 6 | https://github.com/SilkCommunity/TrippyGL 7 | TrippyGL_logo.png 8 | MIT 9 | ThomasMiz 10 | 1.2.2 11 | true 12 | TrippyGL is a highly versatile, yet lightweight and simple to use OpenGL graphics library that runs on .NET Core. 13 | 14 | This package provides integration between TrippyGL.Fonts and the main TrippyGL package. 15 | OpenGL;graphics;gamedev;desktopgl;fonts;text 16 | git 17 | enable 18 | Changelog from 1.2.1: 19 | - Upgraded all dependencies 20 | README.md 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | True 36 | \ 37 | 38 | 39 | True 40 | 41 | 42 | 43 | 44 | 45 | 46 | all 47 | runtime; build; native; contentfiles; analyzers; buildtransitive 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TrippyGL.ImageSharp/TrippyGL.ImageSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 1.2.2 6 | true 7 | ThomasMiz 8 | MIT 9 | https://github.com/SilkCommunity/TrippyGL 10 | https://github.com/SilkCommunity/TrippyGL 11 | TrippyGL_logo.png 12 | TrippyGL is a highly versatile, yet lightweight and simple to use OpenGL graphics library that runs on .NET Core. 13 | 14 | This package provides integration with the ImageSharp library, allowing loading and saving textures from files or images. 15 | OpenGL;graphics;gamedev;desktopgl;images;ImageSharp;textures; 16 | git 17 | enable 18 | Changelog from 1.2.1: 19 | - Upgraded all dependencies 20 | True 21 | README.md 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | all 31 | runtime; build; native; contentfiles; analyzers; buildtransitive 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | True 43 | \ 44 | 45 | 46 | True 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TrippyGL.Fonts.Extensions/TextureFontDataExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL.Fonts.Extensions 2 | { 3 | /// 4 | /// Provides extension methods for . 5 | /// 6 | public static class TextureFontDataExtensions 7 | { 8 | /// 9 | /// Creates a from the and a texture. 10 | /// 11 | public static TextureFont CreateFont(this TextureFontData fontData, Texture2D texture) 12 | { 13 | bool isMonospace = true; 14 | for (int i = 1; i < fontData.Advances!.Length && isMonospace; i++) 15 | isMonospace = fontData.Advances[i] != fontData.Advances[0]; 16 | 17 | if (fontData.KerningOffsets != null) 18 | { 19 | float[] advances; 20 | 21 | if (isMonospace && fontData.Advances.Length != fontData.CharCount) 22 | { 23 | advances = new float[fontData.CharCount]; 24 | for (int i = 0; i < advances.Length; i++) 25 | advances[i] = fontData.Advances[0]; 26 | } 27 | else 28 | advances = fontData.Advances; 29 | 30 | return new KerningTextureFont(texture, fontData.Size, fontData.FirstChar, fontData.LastChar, 31 | fontData.RenderOffsets!, fontData.SourceRectangles!, fontData.KerningOffsets, advances, 32 | fontData.Ascender, fontData.Descender, fontData.LineGap, fontData.Name!); 33 | } 34 | 35 | if (isMonospace) 36 | { 37 | return new MonospaceTextureFont(texture, fontData.Size, fontData.FirstChar, fontData.LastChar, 38 | fontData.RenderOffsets!, fontData.SourceRectangles!, fontData.Advances[0], fontData.Ascender, 39 | fontData.Descender, fontData.LineGap, fontData.Name!); 40 | } 41 | 42 | return new SpacedTextureFont(texture, fontData.Size, fontData.FirstChar, fontData.LastChar, 43 | fontData.RenderOffsets!, fontData.SourceRectangles!, fontData.Advances, fontData.Ascender, 44 | fontData.Descender, fontData.LineGap, fontData.Name!); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /TrippyGL.Fonts.Building/TrippyGL.Fonts.Building.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | https://github.com/SilkCommunity/TrippyGL 6 | MIT 7 | https://github.com/SilkCommunity/TrippyGL 8 | TrippyGL_logo.png 9 | 1.2.2 10 | ThomasMiz 11 | true 12 | TrippyGL is a highly versatile, yet lightweight and simple to use OpenGL graphics library that runs on .NET Core. 13 | 14 | This package provides integration with the SixLabors.Fonts library for the TrippyGL.Fonts package, allowing the creation of TrippyGL fonts from common font file formats, such as as TrueType. 15 | OpenGL;graphics;gamedev;desktopgl;fonts;text 16 | git 17 | enable 18 | Changelog from 1.2.1: 19 | - Upgraded all dependencies 20 | README.md 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | all 30 | runtime; build; native; contentfiles; analyzers; buildtransitive 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | True 39 | \ 40 | 41 | 42 | True 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TrippyGL/Enums/BatcherBeginMode.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies options on how a handles drawing textures. 5 | /// 6 | public enum BatcherBeginMode 7 | { 8 | // IMPORTANT NOTE: 9 | // The values are set specifically so those that require batch items to be sorted have the 10 | // least significant bit set to 1. This way, in order to know whether sorting is needed we 11 | // can just do (beginMode & 1) == 1 12 | 13 | /// 14 | /// Textures are drawn when End() is called in order of draw call, batching together where possible. 15 | /// 16 | Deferred = 0, 17 | 18 | /// 19 | /// Textures are drawn in order of draw call but the batcher doesn't wait until End() to flush all the calls. 20 | /// If the same texture is drawn consecutively the Draw()-s will still be batched into a single draw call. 21 | /// 22 | OnTheFly = 2, 23 | 24 | /// 25 | /// Each texture is drawn in it's own individual draw call, immediately, during Draw(). 26 | /// 27 | Immediate = 4, 28 | 29 | /// 30 | /// Textures are drawn when End() is called, but first sorted by texture. This uses the least amount of draw 31 | /// calls, but doesn't retain order (depth testing can be used for ordering). 32 | /// 33 | SortByTexture = 1, 34 | 35 | /// 36 | /// Textures are drawn when End() is called, but first sorted by depth in back-to-front order. 37 | /// This means items with higher depth get drawn before items with lower depth. 38 | /// Textures with the same depth aren't guaranteed to retain the order in which they were Draw()-n. 39 | /// 40 | SortBackToFront = 3, 41 | 42 | /// 43 | /// Textures are drawn when End() is called, but first sorted by depth in front-to-back order. 44 | /// This means items with lower depth get drawn before items with higher depth. 45 | /// Textures with the same depth aren't guaranteed to retain the order in which they were Draw()-n. 46 | /// 47 | SortFrontToBack = 5, 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/Diamond.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using TrippyGL; 4 | using TrippyGL.Utils; 5 | 6 | namespace TextureBatcherTest 7 | { 8 | class Diamond 9 | { 10 | public static Texture2D texture; 11 | 12 | Vector2[] trail; 13 | Vector2 position; 14 | Vector2 velocity; 15 | 16 | public Diamond() 17 | { 18 | trail = new Vector2[5]; 19 | Reset(); 20 | } 21 | 22 | public void Update(float dtSeconds) 23 | { 24 | position += dtSeconds * velocity; 25 | 26 | if (velocity.X < 0) 27 | { 28 | if (position.X <= 0) 29 | velocity.X = -velocity.X; 30 | } 31 | else 32 | { 33 | if (position.X + texture.Width >= TextureBatcherTest.MaxX) 34 | velocity.X = -velocity.X; 35 | } 36 | 37 | if (velocity.Y < 0) 38 | { 39 | if (position.Y <= 0) 40 | velocity.Y = -velocity.Y; 41 | } 42 | else 43 | { 44 | if (position.Y + texture.Height >= TextureBatcherTest.MaxY) 45 | velocity.Y = -velocity.Y; 46 | } 47 | } 48 | 49 | public void Draw(TextureBatcher textureBatcher) 50 | { 51 | for (int i = trail.Length - 1; i >= 0; i--) 52 | { 53 | byte alpha = (byte)(127 - i * 127 / trail.Length); 54 | textureBatcher.Draw(texture, trail[i], new Color4b(255, 255, 255, alpha)); 55 | } 56 | 57 | textureBatcher.Draw(texture, position); 58 | 59 | for (int i = trail.Length - 1; i != 0; i--) 60 | trail[i] = trail[i - 1]; 61 | trail[0] = position; 62 | } 63 | 64 | public void Reset() 65 | { 66 | Random random = TextureBatcherTest.random; 67 | 68 | position.X = random.NextFloat(TextureBatcherTest.MaxX - texture.Width); 69 | position.Y = random.NextFloat(TextureBatcherTest.MaxY - texture.Height); 70 | 71 | const float spd = 400; 72 | velocity.X = random.NextFloat(-spd, spd); 73 | velocity.Y = random.NextFloat(-spd, spd); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /TrippyGL/SpecifiedShaderAttrib.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// Used by for specifying vertex attributes. 7 | /// 8 | public readonly struct SpecifiedShaderAttrib : IEquatable 9 | { 10 | /// The shader attribute's type. 11 | public readonly AttributeType AttribType; 12 | /// 13 | /// The name with which the shader attribute is declared in the shader. 14 | /// Settings this to null specifies an attribute which occupies indices, but is not used by the shader program. 15 | /// 16 | public readonly string? Name; 17 | 18 | /// 19 | /// Creates a with the given values. 20 | /// 21 | public SpecifiedShaderAttrib(string? name, AttributeType type) 22 | { 23 | AttribType = type; 24 | Name = name; 25 | } 26 | 27 | public static bool operator ==(SpecifiedShaderAttrib left, SpecifiedShaderAttrib right) => left.Equals(right); 28 | public static bool operator !=(SpecifiedShaderAttrib left, SpecifiedShaderAttrib right) => !left.Equals(right); 29 | 30 | /// 31 | /// Whether this and another have matching data. 32 | /// 33 | public bool Matches(in ActiveVertexAttrib activeAttrib) 34 | { 35 | return AttribType == activeAttrib.AttribType 36 | && Name == activeAttrib.Name; 37 | } 38 | 39 | public override int GetHashCode() 40 | { 41 | return HashCode.Combine(AttribType, Name); 42 | } 43 | 44 | public bool Equals(SpecifiedShaderAttrib shaderAttrib) 45 | { 46 | return AttribType == shaderAttrib.AttribType && Name == shaderAttrib.Name; 47 | } 48 | 49 | public override bool Equals(object? obj) 50 | { 51 | if (obj is SpecifiedShaderAttrib shaderAttrib) 52 | return Equals(shaderAttrib); 53 | return false; 54 | } 55 | 56 | public override string ToString() 57 | { 58 | return string.Concat(AttribType.ToString(), " ", Name); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /TrippyGL/VertexNormal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with a Position and Normal. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexNormal : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 3) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's normal. 20 | public Vector3 Normal; 21 | 22 | /// 23 | /// Creates a with the specified position and normal. 24 | /// 25 | public VertexNormal(Vector3 position, Vector3 normal) 26 | { 27 | Position = position; 28 | Normal = normal; 29 | } 30 | 31 | public static bool operator ==(VertexNormal left, VertexNormal right) => left.Equals(right); 32 | public static bool operator !=(VertexNormal left, VertexNormal right) => !left.Equals(right); 33 | 34 | public int AttribDescriptionCount => 2; 35 | 36 | public void WriteAttribDescriptions(Span descriptions) 37 | { 38 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 39 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec3); 40 | } 41 | 42 | public override string ToString() 43 | { 44 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", Normal.X.ToString(), ", ", Normal.Y.ToString(), ", ", Normal.Z.ToString(), ")"); 45 | } 46 | 47 | public override int GetHashCode() 48 | { 49 | return HashCode.Combine(Position, Normal); 50 | } 51 | 52 | public bool Equals(VertexNormal other) 53 | { 54 | return Position == other.Position 55 | && Normal == other.Normal; 56 | } 57 | 58 | public override bool Equals(object? obj) 59 | { 60 | if (obj is VertexNormal vertexNormal) 61 | return Equals(vertexNormal); 62 | return false; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /TrippyGL/VertexTexture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position and TexCoords. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexTexture : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 2) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's texture coordinates. 20 | public Vector2 TexCoords; 21 | 22 | /// 23 | /// Creates a with the specified position and texture coordinates. 24 | /// 25 | public VertexTexture(Vector3 position, Vector2 texCoords) 26 | { 27 | Position = position; 28 | TexCoords = texCoords; 29 | } 30 | 31 | public static bool operator ==(VertexTexture left, VertexTexture right) => left.Equals(right); 32 | public static bool operator !=(VertexTexture left, VertexTexture right) => !left.Equals(right); 33 | 34 | public int AttribDescriptionCount => 2; 35 | 36 | public void WriteAttribDescriptions(Span descriptions) 37 | { 38 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 39 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec2); 40 | } 41 | 42 | public override string ToString() 43 | { 44 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", TexCoords.X.ToString(), ", ", TexCoords.Y.ToString(), ")"); 45 | } 46 | 47 | public override int GetHashCode() 48 | { 49 | return HashCode.Combine(Position, TexCoords); 50 | } 51 | 52 | public bool Equals(VertexTexture other) 53 | { 54 | return Position == other.Position 55 | && TexCoords == other.TexCoords; 56 | } 57 | 58 | public override bool Equals(object? obj) 59 | { 60 | if (obj is VertexTexture vertexTexture) 61 | return Equals(vertexTexture); 62 | return false; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /TrippyGL/VertexDataBufferSubset.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// A whose purpose is to store vertex data. 7 | /// 8 | /// The type of struct (element) this will manage. 9 | public sealed class VertexDataBufferSubset : DataBufferSubset where T : unmanaged 10 | { 11 | /// 12 | /// Creates a with the given 13 | /// and target, offset into the buffer in bytes, storage length in elements and optional initial data. 14 | /// 15 | /// The this subset will belong to. 16 | /// The offset into the 's storage where this subset begins. 17 | /// The length of this subset measured in elements. 18 | /// A containing the initial data to set to the subset, or empty. 19 | /// The offset into the subset's storage at which to start writting the initial data. 20 | public VertexDataBufferSubset(BufferObject bufferObject, uint storageOffsetBytes, uint storageLength, ReadOnlySpan data = default, uint dataWriteOffset = 0) 21 | : base(bufferObject, BufferTarget.ArrayBuffer, storageOffsetBytes, storageLength, data, dataWriteOffset) 22 | { 23 | 24 | } 25 | 26 | /// 27 | /// Creates a with the given 28 | /// and target, with the subset covering the entire buffer's storage and optional initial data. 29 | /// 30 | /// The this subset will belong to. 31 | /// A containing the initial data to set to the subset, or empty. 32 | /// The offset into the subset's storage at which to start writting the initial data. 33 | public VertexDataBufferSubset(BufferObject bufferObject, ReadOnlySpan data = default, uint dataWriteOffset = 0) 34 | : base(bufferObject, BufferTarget.ArrayBuffer, data, dataWriteOffset) 35 | { 36 | 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /TrippyGL/MonospaceTextureFont.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using System.Numerics; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// A where all the characters have the same advance value. 9 | /// 10 | public sealed class MonospaceTextureFont : TextureFont 11 | { 12 | /// The advance value for any character in this font. 13 | public readonly float Advance; 14 | 15 | /// 16 | /// Creates a . 17 | /// 18 | /// 19 | /// Any array passed to this method will NOT be copied. The provided instance will be used instead. 20 | /// Holding on to a reference to these arrays and modifying them afterwards can have unexpected 21 | /// behavior. 22 | /// 23 | public MonospaceTextureFont(Texture2D texture, float size, char firstChar, char lastChar, Vector2[] renderOffsets, 24 | Rectangle[] sources, float advance, float ascender, float descender, float lineGap, string name) 25 | : base(texture, size, firstChar, lastChar, renderOffsets, sources, ascender, descender, lineGap, name) 26 | { 27 | Advance = advance; 28 | } 29 | 30 | public override float GetAdvance(char character) 31 | { 32 | return Advance; 33 | } 34 | 35 | public override Vector2 GetKerning(char fromChar, char toChar) 36 | { 37 | return default; 38 | } 39 | 40 | public override Vector2 Measure(ReadOnlySpan text) 41 | { 42 | if (text.IsEmpty) 43 | return default; 44 | 45 | int lineCount = 1; 46 | int maxCharsPerLine = 0; 47 | int charsInLine = 0; 48 | for (int i = 0; i < text.Length; i++) 49 | { 50 | if (text[i] == NewlineIndicator) 51 | { 52 | if (charsInLine > maxCharsPerLine) 53 | maxCharsPerLine = charsInLine; 54 | lineCount++; 55 | charsInLine = 0; 56 | } 57 | else 58 | charsInLine++; 59 | } 60 | 61 | return new Vector2(Advance * Math.Max(charsInLine, maxCharsPerLine), lineCount * LineAdvance); 62 | } 63 | 64 | public override Vector2 MeasureLine(ReadOnlySpan text) 65 | { 66 | return new Vector2(text.Length * Advance, LineAdvance); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/Particle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using System.Numerics; 4 | using TrippyGL; 5 | using TrippyGL.Utils; 6 | 7 | namespace TextureBatcherTest 8 | { 9 | class Particle 10 | { 11 | public static Texture2D texture; 12 | public static readonly Rectangle[] sources = new Rectangle[] 13 | { 14 | new Rectangle(0, 0, 3, 3), 15 | new Rectangle(3, 0, 4, 4), 16 | new Rectangle(7, 0, 4, 4), 17 | new Rectangle(5, 4, 5, 5), 18 | new Rectangle(0, 4, 5, 5), 19 | new Rectangle(11, 0, 7, 7), 20 | new Rectangle(18, 0, 9, 9), 21 | }; 22 | 23 | Color4b color; 24 | Rectangle source; 25 | public Vector2 position; 26 | Vector2 velocity; 27 | float rotation; 28 | float rotSpeed; 29 | 30 | public Particle(Vector2 position) 31 | { 32 | Random r = TextureBatcherTest.random; 33 | source = sources[TextureBatcherTest.random.Next(sources.Length)]; 34 | this.position = position; 35 | const float spd = 300; 36 | velocity = new Vector2(r.NextFloat(-spd, spd), r.NextFloat(-spd, spd)); 37 | color = r.NextColor4bFullAlpha(); 38 | rotation = r.NextFloat(MathF.PI * 2); 39 | rotSpeed = r.NextFloat(-10, 10); 40 | } 41 | 42 | public Particle() 43 | { 44 | Random r = TextureBatcherTest.random; 45 | source = sources[TextureBatcherTest.random.Next(sources.Length)]; 46 | position = new Vector2(r.NextFloat(TextureBatcherTest.MaxX), r.NextFloat(TextureBatcherTest.MaxY)); 47 | const float spd = 300; 48 | velocity = new Vector2(r.NextFloat(-spd, spd), r.NextFloat(-spd, spd)); 49 | color = r.NextColor4bFullAlpha(); 50 | rotation = r.NextFloat(MathF.PI * 2); 51 | rotSpeed = r.NextFloat(-10, 10); 52 | } 53 | 54 | public void Update(float dtSeconds) 55 | { 56 | position += velocity * dtSeconds; 57 | rotation += rotSpeed * dtSeconds; 58 | } 59 | 60 | public void Draw(TextureBatcher textureBatcher) 61 | { 62 | textureBatcher.Draw(texture, position, source, color, 5f, rotation, new Vector2(source.Width, source.Height) / 2f); 63 | } 64 | 65 | public bool IsOffscreen() 66 | { 67 | return position.X < -100 || position.X - 100 > TextureBatcherTest.MaxX 68 | || position.Y < -100 || position.Y - 100 > TextureBatcherTest.MaxY; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/data/waterFs.glsl: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | uniform sampler2D distortMap; 4 | uniform sampler2D normalsMap; 5 | 6 | uniform sampler2D reflectSamp; 7 | uniform sampler2D refractSamp; 8 | uniform sampler2D depthSamp; 9 | 10 | uniform float nearPlane; 11 | uniform float farPlane; 12 | 13 | uniform vec3 sunlightDir; 14 | uniform vec3 sunlightColor; 15 | 16 | uniform vec3 cameraPos; 17 | uniform float nearFog; 18 | uniform float farFog; 19 | 20 | uniform vec2 distortOffset; 21 | 22 | in vec3 fPosition; 23 | in vec4 clipSpace; 24 | in vec2 distortCoords; 25 | in vec3 toCameraVector; 26 | in float waterDepth; 27 | in float aboveWater; 28 | 29 | out vec4 FragColor; 30 | 31 | const float waveStrenght = 0.02; 32 | const float shineDamper = 64.0; 33 | const float reflectivity = 0.7; 34 | 35 | float depthToDistance(in float depth) { 36 | return 2.0 * nearPlane * farPlane / (farPlane + nearPlane - (2.0 * depth - 1.0) * (farPlane - nearPlane)); 37 | } 38 | 39 | vec3 calcSpecularReflection(in vec2 distortedTexCoords, in vec3 toCamVec) { 40 | vec4 normalColor = texture(normalsMap, distortedTexCoords); 41 | vec3 waterNormal = normalize(vec3(normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 - 1.0)); 42 | 43 | vec3 reflectedLight = aboveWater > 0 ? reflect(sunlightDir, waterNormal) : refract(sunlightDir, -waterNormal, 0.9); 44 | float specular = max(dot(reflectedLight, toCamVec), 0.0); 45 | specular = pow(specular, shineDamper); 46 | return sunlightColor * (specular * reflectivity); 47 | } 48 | 49 | void main() { 50 | float depthyness = clamp((waterDepth-1.0) / 12.0, 0, 1); 51 | vec2 fragCoords = (clipSpace.xy / clipSpace.w) * 0.5 + 0.5; 52 | 53 | vec2 distortedTexCoords = texture(distortMap, vec2(distortCoords.x + distortOffset.x, distortCoords.y)).xy * 0.1; 54 | distortedTexCoords = distortCoords + vec2(distortedTexCoords.x, distortedTexCoords.y + distortOffset.y); 55 | vec2 distort = (texture(distortMap, distortedTexCoords).xy * 2.0 - 1.0) * waveStrenght * depthyness; 56 | 57 | vec4 reflectColor = texture(reflectSamp, vec2(fragCoords.x, 1.0 - fragCoords.y) + distort); 58 | vec4 refractColor = texture(refractSamp, fragCoords + distort); 59 | float camWaterDepth = depthToDistance(texture(depthSamp, fragCoords + distort).x) - depthToDistance(gl_FragCoord.z); 60 | 61 | vec3 toCamVec = normalize(toCameraVector); 62 | 63 | float refractFactor = max(pow(abs(toCamVec.y), 1.1) - (aboveWater*0.5+0.5) * camWaterDepth/92.0, 0); 64 | FragColor = mix(reflectColor, refractColor, refractFactor); 65 | 66 | FragColor.xyz += calcSpecularReflection(distortedTexCoords, toCamVec) * depthyness; 67 | FragColor.a = 1.0 - max((distance(cameraPos, fPosition) - nearFog) / (farFog - nearFog), 0.0); 68 | } -------------------------------------------------------------------------------- /TrippyGL.Fonts.Extensions/TextureFontExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | 3 | namespace TrippyGL.Fonts.Extensions 4 | { 5 | /// 6 | /// Provides extension methods for . 7 | /// 8 | public static class TextureFontExtensions 9 | { 10 | /// 11 | /// Creates an array of -s by loading them from a trippy font file. 12 | /// 13 | /// The the -s will use. 14 | /// The file on disk where the trippy font file is located. 15 | /// Whether to generate mipmaps for the texture's font. 16 | public static TextureFont[] FromFile(GraphicsDevice graphicsDevice, string file, bool generateMipmaps = false) 17 | { 18 | using TrippyFontFile fontFile = TrippyFontFile.FromFile(file); 19 | return fontFile.CreateFonts(graphicsDevice, generateMipmaps); 20 | } 21 | 22 | /// 23 | /// Creates an array of -s by loading them from a trippy font file. 24 | /// 25 | /// The the -s will use. 26 | /// The stream where the trippy font file is located. 27 | /// Whether to generate mipmaps for the texture's font. 28 | public static TextureFont[] FromStream(GraphicsDevice graphicsDevice, Stream stream, bool generateMipmaps = false) 29 | { 30 | using TrippyFontFile fontFile = TrippyFontFile.FromStream(stream); 31 | return fontFile.CreateFonts(graphicsDevice, generateMipmaps); 32 | } 33 | 34 | /// 35 | /// Creates an array of -s by loading them from a trippy font file. 36 | /// 37 | /// The the -s will use. 38 | /// The stream where the trippy font file is located. 39 | /// Whether to generate mipmaps for the texture's font. 40 | public static TextureFont[] FromStream(GraphicsDevice graphicsDevice, BinaryReader streamReader, bool generateMipmaps = false) 41 | { 42 | using TrippyFontFile fontFile = TrippyFontFile.FromStream(streamReader); 43 | return fontFile.CreateFonts(graphicsDevice, generateMipmaps); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /TrippyGL/VertexColor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position and Color. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexColor : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 1) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's color. 20 | public Color4b Color; 21 | 22 | /// 23 | /// Creates a with the specified position and color. 24 | /// 25 | public VertexColor(Vector3 position, Color4b color) 26 | { 27 | Position = position; 28 | Color = color; 29 | } 30 | 31 | /// 32 | /// Creates a with the specified position and white color. 33 | /// 34 | public VertexColor(Vector3 position) 35 | { 36 | Position = position; 37 | Color = new Color4b(255, 255, 255, 255); 38 | } 39 | 40 | public static bool operator ==(VertexColor left, VertexColor right) => left.Equals(right); 41 | public static bool operator !=(VertexColor left, VertexColor right) => !left.Equals(right); 42 | 43 | public int AttribDescriptionCount => 2; 44 | 45 | public void WriteAttribDescriptions(Span descriptions) 46 | { 47 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 48 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec4, true, AttributeBaseType.UnsignedByte); 49 | } 50 | 51 | public override string ToString() 52 | { 53 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), "), (", Color.R.ToString(), ", ", Color.G.ToString(), ", ", Color.B.ToString(), ", ", Color.A.ToString(), ")"); 54 | } 55 | 56 | public override int GetHashCode() 57 | { 58 | return HashCode.Combine(Position, Color); 59 | } 60 | 61 | public bool Equals(VertexColor other) 62 | { 63 | return Position == other.Position 64 | && Color == other.Color; 65 | } 66 | 67 | public override bool Equals(object? obj) 68 | { 69 | if (obj is VertexColor vertexColor) 70 | return Equals(vertexColor); 71 | return false; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /TrippyGL.Fonts.Extensions/TrippyFontFileExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using TrippyGL.ImageSharp; 3 | 4 | namespace TrippyGL.Fonts.Extensions 5 | { 6 | /// 7 | /// Provides extension methods for . 8 | /// 9 | public static class TrippyFontFileExtensions 10 | { 11 | /// 12 | /// Creates an array of -s from the . 13 | /// 14 | /// The to create fonts from. 15 | /// The the -s will use. 16 | /// Whether to generate mipmaps for the texture's font. 17 | public static TextureFont[] CreateFonts(this TrippyFontFile font, GraphicsDevice graphicsDevice, bool generateMipmaps = false) 18 | { 19 | if (graphicsDevice == null) 20 | throw new ArgumentNullException(nameof(graphicsDevice)); 21 | 22 | if (font == null) 23 | throw new ArgumentNullException(nameof(font)); 24 | 25 | font.ThrowIfAnyNull(); 26 | 27 | Texture2D texture = new Texture2D(graphicsDevice, (uint)font.Image.Width, (uint)font.Image.Height, generateMipmaps); 28 | try 29 | { 30 | TextureFont[] textureFonts = new TextureFont[font.FontDatas.Length]; 31 | for (int i = 0; i < textureFonts.Length; i++) 32 | textureFonts[i] = font.FontDatas[i].CreateFont(texture); 33 | 34 | texture.SetData(font.Image); 35 | return textureFonts; 36 | } 37 | catch 38 | { 39 | texture.Dispose(); 40 | throw; 41 | } 42 | } 43 | 44 | /// 45 | /// Creates a single from the . 46 | /// 47 | /// The to create fonts from. 48 | /// The the will use. 49 | /// Whether to generate mipmaps for the texture's font. 50 | public static TextureFont CreateFont(this TrippyFontFile font, GraphicsDevice graphicsDevice, bool generateMipmaps = false) 51 | { 52 | if (font.FontDatas.Length > 1) 53 | throw new FontLoadingException("Called " + nameof(CreateFont) + "() on a " + nameof(TrippyFontFile) + " with multiple fonts! Try " + nameof(CreateFonts) + "() instead."); 54 | 55 | return font.CreateFonts(graphicsDevice, generateMipmaps)[0]; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/Ball.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using TrippyGL; 4 | using TrippyGL.Utils; 5 | 6 | namespace TextureBatcherTest 7 | { 8 | class Ball 9 | { 10 | public static Texture2D texture; 11 | 12 | Vector2[] trail; 13 | Vector2 position; 14 | Vector2 velocity; 15 | float startY; 16 | float minheight; 17 | float scale; 18 | 19 | public Ball() 20 | { 21 | trail = new Vector2[5]; 22 | Reset(); 23 | } 24 | 25 | public void Update(float dtSeconds) 26 | { 27 | const float gravity = 2560; 28 | velocity.Y += gravity * dtSeconds; 29 | 30 | position += velocity * dtSeconds; 31 | 32 | if (velocity.X < 0) 33 | { 34 | if (position.X <= 0) 35 | velocity.X = -velocity.X; 36 | } 37 | else 38 | { 39 | if (position.X + texture.Width * scale >= TextureBatcherTest.MaxX) 40 | velocity.X = -velocity.X; 41 | } 42 | 43 | if (velocity.Y > 0) 44 | { 45 | if (position.Y + texture.Height * scale > TextureBatcherTest.MaxY) 46 | { 47 | velocity.Y = -Math.Abs(velocity.Y * (minheight < startY ? 0.993f : 1.05f)); 48 | position.Y = TextureBatcherTest.MaxY - texture.Height * scale; 49 | minheight = position.Y; 50 | } 51 | else 52 | minheight = Math.Min(minheight, position.Y); 53 | } 54 | } 55 | 56 | public void Draw(TextureBatcher textureBatcher) 57 | { 58 | for (int i = trail.Length - 1; i >= 0; i--) 59 | { 60 | byte alpha = (byte)(127 - i * 127 / trail.Length); 61 | textureBatcher.Draw(texture, trail[i], null, new Color4b(255, 255, 255, alpha), scale, 0f); 62 | } 63 | 64 | textureBatcher.Draw(texture, position, null, Color4b.White, scale, 0f); 65 | 66 | for (int i = trail.Length - 1; i != 0; i--) 67 | trail[i] = trail[i - 1]; 68 | trail[0] = position; 69 | } 70 | 71 | public void Reset() 72 | { 73 | Random random = TextureBatcherTest.random; 74 | position.X = random.NextFloat(TextureBatcherTest.MaxX - texture.Width); 75 | position.Y = random.NextFloat(TextureBatcherTest.MaxY / 8, TextureBatcherTest.MaxY * 0.75f); 76 | scale = random.NextFloat(0.6f, 1.4f); 77 | 78 | velocity.X = random.NextFloat(-300, 300); 79 | velocity.Y = random.NextFloat(40); 80 | 81 | startY = position.Y; 82 | minheight = TextureBatcherTest.MaxY; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /TrippyGL/VertexPosition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with only a Position. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexPosition : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = 3 * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// 20 | /// Creates a with the specified position. 21 | /// 22 | /// The vertex position. 23 | public VertexPosition(Vector3 position) 24 | { 25 | Position = position; 26 | } 27 | 28 | /// 29 | /// Creates a with the specified position. 30 | /// 31 | /// The X component of the position. 32 | /// The Y component of the position. 33 | /// The Z component of the position. 34 | public VertexPosition(float x, float y, float z) 35 | { 36 | Position = new Vector3(x, y, z); 37 | } 38 | 39 | public static bool operator ==(VertexPosition left, VertexPosition right) => left.Equals(right); 40 | public static bool operator !=(VertexPosition left, VertexPosition right) => !left.Equals(right); 41 | 42 | public static implicit operator Vector3(VertexPosition vertexPosition) => vertexPosition.Position; 43 | public static implicit operator VertexPosition(Vector3 position) => new VertexPosition(position); 44 | 45 | public int AttribDescriptionCount => 1; 46 | 47 | public void WriteAttribDescriptions(Span descriptions) 48 | { 49 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 50 | } 51 | 52 | public override string ToString() 53 | { 54 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ")"); 55 | } 56 | 57 | public override int GetHashCode() 58 | { 59 | return HashCode.Combine(Position); 60 | } 61 | 62 | public bool Equals(VertexPosition other) 63 | { 64 | return Position == other.Position; 65 | } 66 | 67 | public override bool Equals(object? obj) 68 | { 69 | if (obj is VertexPosition vertexPosition) 70 | return Equals(vertexPosition); 71 | return false; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/GeneratorSeed.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using TrippyGL.Utils; 4 | 5 | namespace TerrainMaker 6 | { 7 | class GeneratorSeed 8 | { 9 | public readonly NoiseSeed HeightSeed; 10 | public readonly NoiseSeed HumiditySeed; 11 | public readonly NoiseSeed VegetationSeed; 12 | public readonly NoiseSeed ExtraSeed1; 13 | 14 | public GeneratorSeed(in NoiseSeed heightSeed, in NoiseSeed humiditySeed, in NoiseSeed vegetationSeed, in NoiseSeed extraSeed1) 15 | { 16 | HeightSeed = heightSeed; 17 | HumiditySeed = humiditySeed; 18 | VegetationSeed = vegetationSeed; 19 | ExtraSeed1 = extraSeed1; 20 | } 21 | 22 | public GeneratorSeed(Random random) 23 | { 24 | HeightSeed = new NoiseSeed(random); 25 | HumiditySeed = new NoiseSeed(random); 26 | VegetationSeed = new NoiseSeed(random); 27 | ExtraSeed1 = new NoiseSeed(random); 28 | } 29 | 30 | public GeneratorSeed(int seed) : this(new Random(seed)) { } 31 | 32 | public static GeneratorSeed Default 33 | { 34 | get 35 | { 36 | return new GeneratorSeed( 37 | new NoiseSeed(new Vector2(52.9258f, 76.3911f), 49164.7641f), 38 | new NoiseSeed(new Vector2(66.7943f, 33.1674f), 69761.6413f), 39 | new NoiseSeed(new Vector2(37.8254f, 53.2556f), 51338.1952f), 40 | new NoiseSeed(new Vector2(61.5137f, 49.4915f), 43913.5758f) 41 | ); 42 | } 43 | } 44 | } 45 | 46 | readonly struct NoiseSeed 47 | { 48 | //public static readonly NoiseSeed HeightSeed = new NoiseSeed(new Vector2(52.9258f, 76.3911f), 49164.7641f); 49 | //public static readonly NoiseSeed HumiditySeed = new NoiseSeed(new Vector2(66.7943f, 33.1674f), 69761.6413f); 50 | //public static readonly NoiseSeed VegetationSeed = new NoiseSeed(new Vector2(37.8254f, 53.2556f), 51338.1952f); 51 | //public static readonly NoiseSeed ExtraSeed1 = new NoiseSeed(new Vector2(61.5137f, 49.4915f), 43913.5758f); 52 | //public static readonly NoiseSeed ExtraSeed2 = new NoiseSeed(new Vector2(42.3791f, 54.3171f), 56123.2499f); 53 | //public static readonly NoiseSeed ExtraSeed3 = new NoiseSeed(new Vector2(49.2583f, 69.4277f), 57912.5311f); 54 | 55 | public readonly Vector2 DotSeed; 56 | public readonly float RandMultiplier; 57 | 58 | public NoiseSeed(Vector2 dotSeed, float randMult) 59 | { 60 | DotSeed = dotSeed; 61 | RandMultiplier = randMult; 62 | } 63 | 64 | public NoiseSeed(Random r) 65 | { 66 | DotSeed = new Vector2(r.NextFloat(40, 77), r.NextFloat(40, 77)); 67 | RandMultiplier = r.NextFloat(40000, 70000); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /TrippyGL/VertexNormalColor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position, Normal and Color. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexNormalColor : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 3 + 1) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's normal. 20 | public Vector3 Normal; 21 | 22 | /// The vertex's color. 23 | public Color4b Color; 24 | 25 | /// 26 | /// Creates a with the specified position, normal and color. 27 | /// 28 | public VertexNormalColor(Vector3 position, Vector3 normal, Color4b color) 29 | { 30 | Position = position; 31 | Normal = normal; 32 | Color = color; 33 | } 34 | 35 | public static bool operator ==(VertexNormalColor left, VertexNormalColor right) => left.Equals(right); 36 | public static bool operator !=(VertexNormalColor left, VertexNormalColor right) => !left.Equals(right); 37 | 38 | public int AttribDescriptionCount => 3; 39 | 40 | public void WriteAttribDescriptions(Span descriptions) 41 | { 42 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 43 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec3); 44 | descriptions[2] = new VertexAttribDescription(AttributeType.FloatVec4, true, AttributeBaseType.UnsignedByte); 45 | } 46 | 47 | public override string ToString() 48 | { 49 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", Normal.X.ToString(), ", ", Normal.Y.ToString(), ", ", Normal.Z.ToString(), ") (", Color.R.ToString(), ", ", Color.G.ToString(), ", ", Color.B.ToString(), ", ", Color.A.ToString(), ")"); 50 | } 51 | 52 | public override int GetHashCode() 53 | { 54 | return HashCode.Combine(Position, Normal, Color); 55 | } 56 | 57 | public bool Equals(VertexNormalColor other) 58 | { 59 | return Position == other.Position 60 | && Normal == other.Normal 61 | && Color == other.Color; 62 | } 63 | 64 | public override bool Equals(object? obj) 65 | { 66 | if (obj is VertexNormalColor vertexNormalColor) 67 | return Equals(vertexNormalColor); 68 | return false; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TrippyGL/VertexNormalTexture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position, Normal and TexCoords. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexNormalTexture : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 3 + 2) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's normal. 20 | public Vector3 Normal; 21 | 22 | /// The vertex's texture coordinates. 23 | public Vector2 TexCoords; 24 | 25 | /// 26 | /// Creates a with the specified position, normal and texture coordinates. 27 | /// 28 | public VertexNormalTexture(Vector3 position, Vector3 normal, Vector2 texCoords) 29 | { 30 | Position = position; 31 | Normal = normal; 32 | TexCoords = texCoords; 33 | } 34 | 35 | public static bool operator ==(VertexNormalTexture left, VertexNormalTexture right) => left.Equals(right); 36 | public static bool operator !=(VertexNormalTexture left, VertexNormalTexture right) => !left.Equals(right); 37 | 38 | public int AttribDescriptionCount => 3; 39 | 40 | public void WriteAttribDescriptions(Span descriptions) 41 | { 42 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 43 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec3); 44 | descriptions[2] = new VertexAttribDescription(AttributeType.FloatVec2); 45 | } 46 | 47 | public override string ToString() 48 | { 49 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", Normal.X.ToString(), ", ", Normal.Y.ToString(), ", ", Normal.Z.ToString(), ") (", TexCoords.X.ToString(), ", ", TexCoords.Y.ToString(), ")"); 50 | } 51 | 52 | public override int GetHashCode() 53 | { 54 | return HashCode.Combine(Position, Normal, TexCoords); 55 | } 56 | 57 | public bool Equals(VertexNormalTexture other) 58 | { 59 | return Position == other.Position 60 | && Normal == other.Normal 61 | && TexCoords == other.TexCoords; 62 | } 63 | 64 | public override bool Equals(object? obj) 65 | { 66 | if (obj is VertexNormalTexture vertexNormalTexture) 67 | return Equals(vertexNormalTexture); 68 | return false; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TrippyGL/Enums/UniformType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the types of shader uniforms. 5 | /// 6 | public enum UniformType 7 | { 8 | Int = 5124, 9 | UnsignedInt = 5125, 10 | Float = 5126, 11 | Double = 5130, 12 | FloatVec2 = 35664, 13 | FloatVec3 = 35665, 14 | FloatVec4 = 35666, 15 | IntVec2 = 35667, 16 | IntVec3 = 35668, 17 | IntVec4 = 35669, 18 | Bool = 35670, 19 | BoolVec2 = 35671, 20 | BoolVec3 = 35672, 21 | BoolVec4 = 35673, 22 | FloatMat2 = 35674, 23 | FloatMat3 = 35675, 24 | FloatMat4 = 35676, 25 | Sampler1D = 35677, 26 | Sampler2D = 35678, 27 | Sampler3D = 35679, 28 | SamplerCube = 35680, 29 | Sampler1DShadow = 35681, 30 | Sampler2DShadow = 35682, 31 | Sampler2DRect = 35683, 32 | Sampler2DRectShadow = 35684, 33 | FloatMat2x3 = 35685, 34 | FloatMat2x4 = 35686, 35 | FloatMat3x2 = 35687, 36 | FloatMat3x4 = 35688, 37 | FloatMat4x2 = 35689, 38 | FloatMat4x3 = 35690, 39 | Sampler1DArray = 36288, 40 | Sampler2DArray = 36289, 41 | SamplerBuffer = 36290, 42 | Sampler1DArrayShadow = 36291, 43 | Sampler2DArrayShadow = 36292, 44 | SamplerCubeShadow = 36293, 45 | UnsignedIntVec2 = 36294, 46 | UnsignedIntVec3 = 36295, 47 | UnsignedIntVec4 = 36296, 48 | IntSampler1D = 36297, 49 | IntSampler2D = 36298, 50 | IntSampler3D = 36299, 51 | IntSamplerCube = 36300, 52 | IntSampler2DRect = 36301, 53 | IntSampler1DArray = 36302, 54 | IntSampler2DArray = 36303, 55 | IntSamplerBuffer = 36304, 56 | UnsignedIntSampler1D = 36305, 57 | UnsignedIntSampler2D = 36306, 58 | UnsignedIntSampler3D = 36307, 59 | UnsignedIntSamplerCube = 36308, 60 | UnsignedIntSampler2DRect = 36309, 61 | UnsignedIntSampler1DArray = 36310, 62 | UnsignedIntSampler2DArray = 36311, 63 | UnsignedIntSamplerBuffer = 36312, 64 | DoubleMat2 = 36678, 65 | DoubleMat3 = 36679, 66 | DoubleMat4 = 36680, 67 | DoubleMat2x3 = 36681, 68 | DoubleMat2x4 = 36682, 69 | DoubleMat3x2 = 36683, 70 | DoubleMat3x4 = 36684, 71 | DoubleMat4x2 = 36685, 72 | DoubleMat4x3 = 36686, 73 | DoubleVec2 = 36860, 74 | DoubleVec3 = 36861, 75 | DoubleVec4 = 36862, 76 | SamplerCubeMapArray = 36876, 77 | SamplerCubeMapArrayShadow = 36877, 78 | IntSamplerCubeMapArray = 36878, 79 | UnsignedIntSamplerCubeMapArray = 36879, 80 | Sampler2DMultisample = 37128, 81 | IntSampler2DMultisample = 37129, 82 | UnsignedIntSampler2DMultisample = 37130, 83 | Sampler2DMultisampleArray = 37131, 84 | IntSampler2DMultisampleArray = 37132, 85 | UnsignedIntSampler2DMultisampleArray = 37133 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /TrippyGL/GraphicsResource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Silk.NET.OpenGL; 3 | 4 | #pragma warning disable CA1816 // Dispose methods should call SuppressFinalize 5 | 6 | namespace TrippyGL 7 | { 8 | /// 9 | /// Base class for any graphics resource. 10 | /// These include , , , etc. 11 | /// 12 | public abstract class GraphicsResource : IDisposable 13 | { 14 | /// The that manages this . 15 | public GraphicsDevice GraphicsDevice { get; internal set; } 16 | 17 | /// Gets this 's GL object. 18 | internal GL GL => GraphicsDevice!.GL; 19 | 20 | /// Whether this has been disposed. 21 | public bool IsDisposed => GraphicsDevice == null; 22 | 23 | /// 24 | /// Creates a that uses the specified . 25 | /// 26 | internal GraphicsResource(GraphicsDevice graphicsDevice) 27 | { 28 | GraphicsDevice = graphicsDevice ?? throw new ArgumentNullException(nameof(graphicsDevice)); 29 | graphicsDevice.OnResourceAdded(this); 30 | } 31 | 32 | ~GraphicsResource() 33 | { 34 | if (GraphicsDevice != null && !GraphicsDevice.IsDisposed) 35 | Dispose(false); 36 | } 37 | 38 | /// 39 | /// Disposes this , deleting and releasing the resources it uses. 40 | /// Resources override this method to implement their disposing code. 41 | /// 42 | /// Whether the call to this function happened because of a call to or by the destructor. 43 | protected abstract void Dispose(bool isManualDispose); 44 | 45 | /// 46 | /// Disposes this without notifying . 47 | /// This function is only called by . 48 | /// 49 | internal void DisposeByGraphicsDevice() 50 | { 51 | if (!IsDisposed) 52 | { 53 | Dispose(true); 54 | GC.SuppressFinalize(this); 55 | GraphicsDevice = null!; 56 | } 57 | } 58 | 59 | /// 60 | /// Disposes this . It cannot be used after it's been disposed. 61 | /// 62 | public void Dispose() 63 | { 64 | if (!IsDisposed) 65 | { 66 | Dispose(true); 67 | GC.SuppressFinalize(this); 68 | GraphicsDevice!.OnResourceRemoved(this); 69 | GraphicsDevice = null!; 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /TrippyTests/TextureBatcherTest/FontLoadHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using SixLabors.Fonts; 4 | using TrippyGL; 5 | using TrippyGL.Fonts; 6 | using TrippyGL.Fonts.Building; 7 | using TrippyGL.Fonts.Extensions; 8 | 9 | namespace TextureBatcherTest 10 | { 11 | static class FontLoadHelper 12 | { 13 | public const string FontFileName = "font.tglf"; 14 | 15 | public static void LoadOrCreateFonts(GraphicsDevice graphicsDevice, out TextureFont ComicSans48, out TextureFont ArialItalic36) 16 | { 17 | try 18 | { 19 | // Tries to load the font files from the file font.tglf. Will fail if the file is corrupted or doesn't exist. 20 | TextureFont[] fonts = TextureFontExtensions.FromFile(graphicsDevice, FontFileName); 21 | ComicSans48 = fonts[0]; 22 | ArialItalic36 = fonts[1]; 23 | } 24 | catch 25 | { 26 | Console.WriteLine(FontFileName + " file not found, creating the fonts..."); 27 | 28 | // Type of file that can store information about multiple TextureFonts (as long as they share a single Texture2D). 29 | TrippyFontFile fontFile; 30 | try 31 | { 32 | Font comicSans48 = SystemFonts.CreateFont("Comic Sans MS", 48); 33 | Font arialItalic36 = SystemFonts.CreateFont("Arial", 36, FontStyle.Italic); 34 | fontFile = FontBuilderExtensions.CreateFontFile(new Font[] { comicSans48, arialItalic36 }); 35 | } 36 | catch (Exception e) 37 | { 38 | Console.ForegroundColor = ConsoleColor.Red; 39 | Console.WriteLine("Failed to create font files! Are these system fonts installed? \"Comic Sans MS\" and \"Arial Italic\""); 40 | Console.ResetColor(); 41 | Console.WriteLine(e.Message); 42 | Console.ReadKey(); 43 | throw new FileNotFoundException("System Fonts: \"Comic Sans MS\" and \"Arial Italic\""); 44 | } 45 | 46 | // Create and load up the fonts for rendering. 47 | TextureFont[] fonts = fontFile.CreateFonts(graphicsDevice); 48 | ComicSans48 = fonts[0]; 49 | ArialItalic36 = fonts[1]; 50 | 51 | // Users are not expected to write code like this, but rather create their TrippyFontFile and ship the 52 | // resulting ".tglf" file as an asset. You can then easily load them up using TextureFontExtensions.FromFile() 53 | 54 | try 55 | { 56 | fontFile.WriteToFile(FontFileName); 57 | Console.WriteLine("Written created fonts to " + FontFileName + " for future reuse."); 58 | } 59 | catch 60 | { 61 | Console.WriteLine("Failed to save fonts to " + FontFileName + ". They will be recreated rather than reused next time."); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /TrippyGL/GeometryShaderData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Silk.NET.OpenGL; 3 | 4 | namespace TrippyGL 5 | { 6 | /// 7 | /// Stores data about a Geometry Shader. 8 | /// 9 | public readonly struct GeometryShaderData : IEquatable 10 | { 11 | /// The the geometry shader takes as input. 12 | public readonly PrimitiveType GeometryInputType; 13 | 14 | /// The the geometry shader takes as output. 15 | public readonly PrimitiveType GeometryOutputType; 16 | 17 | /// The amount of invocations the geometry shader will do. 18 | public readonly int GeometryShaderInvocations; 19 | 20 | /// The maximum amount of vertices the geometry shader can output. 21 | public readonly int GeometryVerticesOut; 22 | 23 | internal GeometryShaderData(GL gl, uint programHandle) 24 | { 25 | gl.GetProgram(programHandle, ProgramPropertyARB.GeometryInputType, out int tmp); 26 | GeometryInputType = (PrimitiveType)tmp; 27 | 28 | gl.GetProgram(programHandle, ProgramPropertyARB.GeometryOutputType, out tmp); 29 | GeometryOutputType = (PrimitiveType)tmp; 30 | 31 | gl.GetProgram(programHandle, GLEnum.GeometryShaderInvocations, out tmp); 32 | GeometryShaderInvocations = tmp; 33 | 34 | gl.GetProgram(programHandle, ProgramPropertyARB.GeometryVerticesOut, out tmp); 35 | GeometryVerticesOut = tmp; 36 | } 37 | 38 | public static bool operator ==(GeometryShaderData left, GeometryShaderData right) => left.Equals(right); 39 | public static bool operator !=(GeometryShaderData left, GeometryShaderData right) => !left.Equals(right); 40 | 41 | public override string ToString() 42 | { 43 | return string.Concat( 44 | nameof(GeometryInputType) + "=", GeometryInputType.ToString(), 45 | nameof(GeometryOutputType) + "=", GeometryOutputType.ToString(), 46 | nameof(GeometryShaderInvocations) + "=", GeometryShaderInvocations.ToString(), 47 | nameof(GeometryVerticesOut) + "=", GeometryVerticesOut.ToString() 48 | ); 49 | } 50 | 51 | public override int GetHashCode() 52 | { 53 | return HashCode.Combine(GeometryInputType, GeometryOutputType, GeometryShaderInvocations, GeometryVerticesOut); 54 | } 55 | 56 | public bool Equals(GeometryShaderData other) 57 | { 58 | return GeometryInputType == other.GeometryInputType 59 | && GeometryOutputType == other.GeometryOutputType 60 | && GeometryShaderInvocations == other.GeometryShaderInvocations 61 | && GeometryVerticesOut == other.GeometryVerticesOut; 62 | } 63 | 64 | public override bool Equals(object? obj) 65 | { 66 | if (obj is GeometryShaderData geometryShaderData) 67 | return Equals(geometryShaderData); 68 | return false; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TrippyGL/ShaderBlockUniform.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// A buffer-backed block of uniforms. That is, a block of uniforms that get their value from a . 7 | /// 8 | public sealed class ShaderBlockUniform 9 | { 10 | /// The name with which this is declared in the shader. 11 | public readonly string Name; 12 | 13 | /// The that owns this uniform. 14 | public readonly ShaderProgram OwnerProgram; 15 | 16 | /// The binding index in the uniform buffer target from which this uniform reads buffer data. 17 | public readonly uint BindingIndex; 18 | 19 | /// The amount of active uniforms this uniform block contains. 20 | public readonly int ActiveUniformCount; 21 | 22 | private UniformBufferSubset? uniformSource; 23 | private uint uniformBindOffsetBytes, uniformBindLengthBytes; 24 | 25 | internal ShaderBlockUniform(ShaderProgram owner, uint bindingIndex, string name, int activeUniformCount) 26 | { 27 | OwnerProgram = owner; 28 | BindingIndex = bindingIndex; 29 | Name = name; 30 | ActiveUniformCount = activeUniformCount; 31 | } 32 | 33 | /// 34 | /// Sets the buffer containing the values for this uniform. 35 | /// 36 | /// The buffer from which the values will be read. 37 | /// The index of the element in the buffer subset whose value should be used. 38 | public void SetValue(UniformBufferSubset buffer, uint elementIndex = 0) 39 | { 40 | if (buffer == null) 41 | throw new ArgumentNullException(nameof(buffer)); 42 | 43 | if (elementIndex < 0 || elementIndex > buffer.StorageLength) 44 | throw new ArgumentOutOfRangeException(nameof(elementIndex), nameof(elementIndex) + " must be in the range [0, " + nameof(buffer.StorageLength) + ")"); 45 | 46 | uniformSource = buffer; 47 | buffer.GetOffsetAndStorageLengthForIndex(elementIndex, out uniformBindOffsetBytes, out uniformBindLengthBytes); 48 | } 49 | 50 | /// 51 | /// This is called by to ensure the buffer backing 52 | /// this uniform block is bound to the right index and range on the uniform buffer target. 53 | /// 54 | internal void ApplyUniformValue() 55 | { 56 | uniformSource?.BindBufferRange(BindingIndex, uniformBindOffsetBytes, uniformBindLengthBytes); 57 | } 58 | 59 | public override string ToString() 60 | { 61 | return string.Concat( 62 | nameof(Name) + "=\"", Name, "\"", 63 | ", " + nameof(BindingIndex) + "=", BindingIndex.ToString(), 64 | ", " + nameof(ActiveUniformCount) + "=", ActiveUniformCount.ToString() 65 | ); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/ComplexVertexFormats.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using System.Numerics; 4 | using Silk.NET.Maths; 5 | using TrippyGL; 6 | using TrippyTestBase; 7 | 8 | namespace ComplexVertexFormats 9 | { 10 | // Renders two triangles on a black background using a highly unusual vertex format. 11 | // You should see two right triangles that look like a rectangle got split up diagonally. 12 | // The bottom-left and top-right vertices should be red, the top-left vertices should be 13 | // blue and the bottom-right vertices should be green. 14 | 15 | // The ComplexVertex type requires 16 vertex attrib indices, so if your GPU has less than 16 | // that this will fail when creating the VertexArray. 17 | // My GTX 765m supports no more than 16 vertex attrib indices in case you're wondering. 18 | 19 | class ComplexVertexFormats : TestBase 20 | { 21 | VertexBuffer vertexBuffer; 22 | ShaderProgram shaderProgram; 23 | 24 | protected override void OnLoad() 25 | { 26 | Span vertices = stackalloc ComplexVertex[] 27 | { 28 | new ComplexVertex(new Vector3(-0.6f, -0.6f, 0), new Color4b(255, 0, 0, 255)), 29 | new ComplexVertex(new Vector3(0.4f, -0.6f, 0), new Color4b(0, 255, 0, 255)), 30 | new ComplexVertex(new Vector3(-0.6f, 0.4f, 0), new Color4b(0, 0, 255, 255)), 31 | 32 | new ComplexVertex(new Vector3(0.6f, -0.4f, 0), new Color4b(0, 255, 0, 255)), 33 | new ComplexVertex(new Vector3(-0.4f, 0.6f, 0), new Color4b(0, 0, 255, 255)), 34 | new ComplexVertex(new Vector3(0.6f, 0.6f, 0), new Color4b(255, 0, 0, 255)), 35 | }; 36 | 37 | vertexBuffer = new VertexBuffer(graphicsDevice, vertices, BufferUsage.StaticDraw); 38 | shaderProgram = ShaderProgram.FromFiles(graphicsDevice, "vs1.glsl", "fs1.glsl", new string[] { "sixtyThree", "X", "nothing0", "colorR", "matrix1", "colorG", "sixtyFour", "Y", "colorB", "Z", "oneTwoThreeFour", "alwaysZero", "alsoZero" }); 39 | 40 | shaderProgram.Uniforms["Projection"].SetValueMat4(Matrix4x4.Identity); 41 | 42 | graphicsDevice.BlendingEnabled = false; 43 | graphicsDevice.DepthTestingEnabled = false; 44 | } 45 | 46 | protected override void OnRender(double dt) 47 | { 48 | graphicsDevice.ClearColor = new Vector4(0, 0, 0, 1); 49 | graphicsDevice.Clear(ClearBuffers.Color); 50 | 51 | graphicsDevice.VertexArray = vertexBuffer; 52 | graphicsDevice.ShaderProgram = shaderProgram; 53 | graphicsDevice.DrawArrays(PrimitiveType.Triangles, 0, vertexBuffer.StorageLength); 54 | } 55 | 56 | protected override void OnResized(Vector2D size) 57 | { 58 | if (size.X == 0 || size.Y == 0) 59 | return; 60 | 61 | graphicsDevice.SetViewport(0, 0, (uint)size.X, (uint)size.Y); 62 | } 63 | 64 | protected override void OnUnload() 65 | { 66 | vertexBuffer.Dispose(); 67 | shaderProgram.Dispose(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /TrippyGL/ActiveVertexAttrib.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// Stores data about an active vertex attribute from a . 7 | /// 8 | public readonly struct ActiveVertexAttrib : IEquatable 9 | { 10 | /// The attribute's location in the shader. 11 | public readonly int Location; 12 | 13 | /// The name with which the attribute is declared. 14 | public readonly string Name; 15 | 16 | /// The size of the attribute, measured in -s. 17 | public readonly int Size; 18 | 19 | /// The type of the attribute declared in the shader. 20 | public readonly AttributeType AttribType; 21 | 22 | /// 23 | /// Creates an and queries the attribute 24 | /// data from a specified attrib index in a . 25 | /// 26 | /// The that owns the shader program. 27 | /// The handle of the shader program. 28 | /// The attribute index to query the data from. 29 | internal ActiveVertexAttrib(GraphicsDevice graphicsDevice, uint programHandle, uint attribIndex) 30 | { 31 | // We use the glGetActiveAttrib that easily turns the required Name parameters into a string 32 | // Then we query the size and type and then the location separately 33 | Name = graphicsDevice.GL.GetActiveAttrib(programHandle, attribIndex, out Size, out Silk.NET.OpenGL.AttributeType attribType); 34 | AttribType = (AttributeType)attribType; 35 | Location = graphicsDevice.GL.GetAttribLocation(programHandle, Name); 36 | } 37 | 38 | public static bool operator ==(ActiveVertexAttrib left, ActiveVertexAttrib right) => left.Equals(right); 39 | public static bool operator !=(ActiveVertexAttrib left, ActiveVertexAttrib right) => !left.Equals(right); 40 | 41 | public override string ToString() 42 | { 43 | return string.Concat( 44 | nameof(Location) + "=", Location.ToString(), 45 | ", " + nameof(Name) + "=\"", Name, "\"", 46 | ", " + nameof(Size) + "=", Size.ToString(), 47 | ", " + nameof(AttribType) + "=", AttribType.ToString() 48 | ); 49 | } 50 | 51 | public override int GetHashCode() 52 | { 53 | return HashCode.Combine(Location, Size, AttribType, Name); 54 | } 55 | 56 | public bool Equals(ActiveVertexAttrib other) 57 | { 58 | return Location == other.Location 59 | && Size == other.Size 60 | && AttribType == other.AttribType 61 | && Name == other.Name; 62 | } 63 | 64 | public override bool Equals(object? obj) 65 | { 66 | if (obj is ActiveVertexAttrib activeVertexAttrib) 67 | return Equals(activeVertexAttrib); 68 | return false; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /TrippyGL/SpacedTextureFont.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using System.Numerics; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// A where characters can have different advance values but no kerning. 9 | /// 10 | public sealed class SpacedTextureFont : TextureFont 11 | { 12 | private readonly float[] advances; 13 | 14 | /// The advance values for the characters in this font. 15 | public ReadOnlySpan Advances => new ReadOnlySpan(advances); 16 | 17 | /// 18 | /// Creates a . 19 | /// 20 | /// 21 | /// Any array passed to this method will NOT be copied. The provided instance will be used instead. 22 | /// Holding on to a reference to these arrays and modifying them afterwards can have unexpected 23 | /// behavior. 24 | /// 25 | public SpacedTextureFont(Texture2D texture, float size, char firstChar, char lastChar, Vector2[] renderOffsets, 26 | Rectangle[] sources, float[] advances, float ascender, float descender, float lineGap, string name) 27 | : base(texture, size, firstChar, lastChar, renderOffsets, sources, ascender, descender, lineGap, name) 28 | { 29 | this.advances = advances ?? throw new ArgumentNullException(nameof(advances)); 30 | if (advances.Length != CharCount) 31 | throw new ArgumentException("The length of the " + nameof(advances) + " array must match the amount of characters.", nameof(advances)); 32 | } 33 | 34 | public override float GetAdvance(char character) 35 | { 36 | ValidateCharAvailable(character); 37 | return advances[character - FirstChar]; 38 | } 39 | 40 | public override Vector2 GetKerning(char fromChar, char toChar) 41 | { 42 | return default; 43 | } 44 | 45 | public override Vector2 Measure(ReadOnlySpan text) 46 | { 47 | if (text.IsEmpty) 48 | return default; 49 | 50 | int lineCount = 1; 51 | float maxLineWidth = 0; 52 | float lineWidth = 0; 53 | 54 | for (int i = 0; i < text.Length; i++) 55 | { 56 | char c = text[i]; 57 | if (c == NewlineIndicator) 58 | { 59 | if (lineWidth > maxLineWidth) 60 | maxLineWidth = lineWidth; 61 | lineWidth = 0; 62 | lineCount++; 63 | } 64 | else 65 | lineWidth += advances[c - FirstChar]; 66 | } 67 | 68 | return new Vector2(Math.Max(lineWidth, maxLineWidth), lineCount * LineAdvance); 69 | } 70 | 71 | public override Vector2 MeasureLine(ReadOnlySpan text) 72 | { 73 | if (text.IsEmpty) 74 | return default; 75 | 76 | float lineWidth = advances[text[0] - FirstChar]; 77 | for (int i = 1; i < text.Length && text[i] != NewlineIndicator; i++) 78 | lineWidth += advances[text[i] - FirstChar]; 79 | 80 | return new Vector2(lineWidth, LineAdvance); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /TrippyGL/VertexColorTexture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position, Color and TexCoords. 9 | /// 10 | [StructLayout(LayoutKind.Sequential)] 11 | public struct VertexColorTexture : IVertex, IEquatable 12 | { 13 | /// The size of a measured in bytes. 14 | public const int SizeInBytes = (3 + 1 + 2) * 4; 15 | 16 | /// The vertex's position. 17 | public Vector3 Position; 18 | 19 | /// The vertex's color. 20 | public Color4b Color; 21 | 22 | /// The vertex's texture coordinates. 23 | public Vector2 TexCoords; 24 | 25 | /// 26 | /// Creates a with the specified position, color and texture coordinates. 27 | /// 28 | public VertexColorTexture(Vector3 position, Color4b color, Vector2 texCoords) 29 | { 30 | Position = position; 31 | Color = color; 32 | TexCoords = texCoords; 33 | } 34 | 35 | /// 36 | /// Creates a with the specified position and texture coordinates, and white color. 37 | /// 38 | public VertexColorTexture(Vector3 position, Vector2 texCoords) 39 | { 40 | Position = position; 41 | Color = new Color4b(255, 255, 255, 255); 42 | TexCoords = texCoords; 43 | } 44 | 45 | public static bool operator ==(VertexColorTexture left, VertexColorTexture right) => left.Equals(right); 46 | public static bool operator !=(VertexColorTexture left, VertexColorTexture right) => !left.Equals(right); 47 | 48 | public int AttribDescriptionCount => 3; 49 | 50 | public void WriteAttribDescriptions(Span descriptions) 51 | { 52 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 53 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec4, true, AttributeBaseType.UnsignedByte); 54 | descriptions[2] = new VertexAttribDescription(AttributeType.FloatVec2); 55 | } 56 | 57 | public override string ToString() 58 | { 59 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", Color.R.ToString(), ", ", Color.G.ToString(), ", ", Color.B.ToString(), ", ", Color.A.ToString(), ") (", TexCoords.X.ToString(), ", ", TexCoords.Y.ToString(), ")"); 60 | } 61 | 62 | public override int GetHashCode() 63 | { 64 | return HashCode.Combine(Position, Color, TexCoords); 65 | } 66 | 67 | public bool Equals(VertexColorTexture other) 68 | { 69 | return Position == other.Position 70 | && Color == other.Color 71 | && TexCoords == other.TexCoords; 72 | } 73 | 74 | public override bool Equals(object? obj) 75 | { 76 | if (obj is VertexColorTexture vertexColorTexture) 77 | return Equals(vertexColorTexture); 78 | return false; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /TrippyGL.Fonts.Building/FontBuilderExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using SixLabors.Fonts; 4 | using SixLabors.ImageSharp; 5 | 6 | namespace TrippyGL.Fonts.Building 7 | { 8 | /// 9 | /// Provides extension methods that allow building a instance 10 | /// from font objects or font files. 11 | /// 12 | public static class FontBuilderExtensions 13 | { 14 | /// 15 | /// Creates a holding information for multiple fonts. 16 | /// 17 | /// All the fonts have the same character range. 18 | public static TrippyFontFile CreateFontFile(ReadOnlySpan fonts, char firstChar = ' ', char lastChar = '~', Color? backgroundColor = null) 19 | { 20 | IGlyphSource[] glyphSources = new IGlyphSource[fonts.Length]; 21 | for (int i = 0; i < fonts.Length; i++) 22 | glyphSources[i] = new FontGlyphSource(fonts[i], firstChar, lastChar); 23 | return FontBuilder.CreateFontFile(glyphSources, backgroundColor); 24 | } 25 | 26 | /// 27 | /// Creates a holding information for a single font. 28 | /// 29 | public static TrippyFontFile CreateFontFile(Font font, char firstChar = ' ', char lastChar = '~', Color? backgroundColor = null) 30 | { 31 | return FontBuilder.CreateFontFile(new FontGlyphSource(font, firstChar, lastChar), backgroundColor); 32 | } 33 | 34 | /// 35 | /// Creates a holding information for a single font. 36 | /// 37 | public static TrippyFontFile CreateFontFile(string fontFile, float size, char firstChar = ' ', char lastChar = '~', Color? backgroundColor = null) 38 | { 39 | using FileStream fileStream = new FileStream(fontFile, FileMode.Open, FileAccess.Read, FileShare.Read); 40 | return CreateFontFile(fileStream, size, firstChar, lastChar, backgroundColor); 41 | } 42 | 43 | /// 44 | /// Creates a holding information for a single font. 45 | /// 46 | public static TrippyFontFile CreateFontFile(Stream fontStream, float size, char firstChar = ' ', char lastChar = '~', Color? backgroundColor = null) 47 | { 48 | return FontBuilder.CreateFontFile(new FontGlyphSource(new FontCollection().Add(fontStream).CreateFont(size), firstChar, lastChar), backgroundColor); 49 | } 50 | 51 | /// 52 | /// Creates a holding information for multiple fonts with the same size. 53 | /// 54 | /// All the fonts have the same character range. 55 | public static TrippyFontFile CreateFontFile(ReadOnlySpan fontFiles, float size, char firstChar = ' ', char lastChar = '~', Color? backgroundColor = null) 56 | { 57 | IGlyphSource[] glyphSources = new IGlyphSource[fontFiles.Length]; 58 | for (int i = 0; i < glyphSources.Length; i++) 59 | glyphSources[i] = new FontGlyphSource(new FontCollection().Add(fontFiles[i]).CreateFont(size), firstChar, lastChar); 60 | return FontBuilder.CreateFontFile(glyphSources, backgroundColor); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /TrippyGL/VertexNormalColorTexture.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace TrippyGL 6 | { 7 | /// 8 | /// Represents a vertex with Position, Normal, 9 | /// Color and TexCoords. 10 | /// 11 | [StructLayout(LayoutKind.Sequential)] 12 | public struct VertexNormalColorTexture : IVertex, IEquatable 13 | { 14 | /// The size of a measured in bytes. 15 | public const int SizeInBytes = (3 + 3 + 1 + 2) * 4; 16 | 17 | /// The vertex's position. 18 | public Vector3 Position; 19 | 20 | /// The vertex's normal. 21 | public Vector3 Normal; 22 | 23 | /// The vertex's color. 24 | public Color4b Color; 25 | 26 | /// The vertex's texture coordinates. 27 | public Vector2 TexCoords; 28 | 29 | /// 30 | /// Creates a with the specified position, normal, color and texture coordinates. 31 | /// 32 | public VertexNormalColorTexture(Vector3 position, Vector3 normal, Color4b color, Vector2 texCoords) 33 | { 34 | Position = position; 35 | Normal = normal; 36 | Color = color; 37 | TexCoords = texCoords; 38 | } 39 | 40 | public static bool operator ==(VertexNormalColorTexture left, VertexNormalColorTexture right) => left.Equals(right); 41 | public static bool operator !=(VertexNormalColorTexture left, VertexNormalColorTexture right) => !left.Equals(right); 42 | 43 | public int AttribDescriptionCount => 4; 44 | 45 | public void WriteAttribDescriptions(Span descriptions) 46 | { 47 | descriptions[0] = new VertexAttribDescription(AttributeType.FloatVec3); 48 | descriptions[1] = new VertexAttribDescription(AttributeType.FloatVec3); 49 | descriptions[2] = new VertexAttribDescription(AttributeType.FloatVec4, true, AttributeBaseType.UnsignedByte); 50 | descriptions[3] = new VertexAttribDescription(AttributeType.FloatVec2); 51 | } 52 | 53 | public override string ToString() 54 | { 55 | return string.Concat("(", Position.X.ToString(), ", ", Position.Y.ToString(), ", ", Position.Z.ToString(), ") (", Normal.X.ToString(), ", ", Normal.Y.ToString(), ", ", Normal.Z.ToString(), ") (", Color.R.ToString(), ", ", Color.G.ToString(), ", ", Color.B.ToString(), ", ", Color.A.ToString(), ") (", TexCoords.X.ToString(), ", ", TexCoords.Y.ToString(), ")"); 56 | } 57 | 58 | public override int GetHashCode() 59 | { 60 | return HashCode.Combine(Position, Normal, Color, TexCoords); 61 | } 62 | 63 | public bool Equals(VertexNormalColorTexture other) 64 | { 65 | return Position == other.Position 66 | && Normal == other.Normal 67 | && Color == other.Color 68 | && TexCoords == other.TexCoords; 69 | } 70 | 71 | public override bool Equals(object? obj) 72 | { 73 | if (obj is VertexNormalColorTexture vertexNormalColorTexture) 74 | return Equals(vertexNormalColorTexture); 75 | return false; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /TrippyGL.ImageSharp/Texture2DArrayExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using Silk.NET.OpenGL; 4 | using SixLabors.ImageSharp; 5 | using SixLabors.ImageSharp.PixelFormats; 6 | 7 | namespace TrippyGL.ImageSharp 8 | { 9 | /// 10 | /// Provides extension methods for 11 | /// 12 | public static class Texture2DArrayExtensions 13 | { 14 | /// 15 | /// Sets the data of one of the 's images from an . 16 | /// 17 | /// The to set data for. 18 | /// The array layer to set the data for. 19 | /// The image with the pixel data to set. 20 | public static void SetData(this Texture2DArray texture, int depthLevel, Image image) 21 | { 22 | if (texture == null) 23 | throw new ArgumentNullException(nameof(texture)); 24 | 25 | if (image == null) 26 | throw new ArgumentNullException(nameof(image)); 27 | 28 | if (texture.ImageFormat != TextureImageFormat.Color4b) 29 | throw new InvalidOperationException(nameof(TextureCubemap.ImageFormat) + " must be " + nameof(TextureImageFormat.Color4b) + " in order to do this"); 30 | 31 | if (image.Width != texture.Width || image.Height != texture.Height) 32 | throw new InvalidOperationException("The size of the image must match the size of the " + nameof(Texture2DArray)); 33 | 34 | if (image.DangerousTryGetSinglePixelMemory(out Memory pixels)) 35 | { 36 | texture.SetData(pixels.Span, depthLevel, PixelFormat.Rgba); 37 | } 38 | else 39 | { 40 | image.ProcessPixelRows(accessor => 41 | { 42 | for (int yi = 0; yi < accessor.Height; yi++) 43 | texture.SetData(accessor.GetRowSpan(yi), 0, yi, depthLevel, (uint)accessor.Width, 1, 1, PixelFormat.Rgba); 44 | }); 45 | } 46 | } 47 | 48 | /// 49 | /// Sets the data of one of the 's images from a stream. 50 | /// 51 | /// The to set data for. 52 | /// The array layer to set the data for. 53 | /// The stream from which to load an image. 54 | public static void SetData(this Texture2DArray texture, int depthLevel, Stream stream) 55 | { 56 | using Image image = Image.Load(stream); 57 | SetData(texture, depthLevel, image); 58 | } 59 | 60 | /// 61 | /// Sets the data of one of the 's images from a file. 62 | /// 63 | /// The to set data for. 64 | /// The array layer to set the data for. 65 | /// The file containing the image with the pixel data to set. 66 | public static void SetData(this Texture2DArray texture, int depthLevel, string file) 67 | { 68 | using Image image = Image.Load(file); 69 | SetData(texture, depthLevel, image); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCube/SimpleCube.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Numerics; 4 | using Silk.NET.Maths; 5 | using TrippyGL; 6 | using TrippyTestBase; 7 | 8 | namespace SimpleCube 9 | { 10 | // Draws a 3D rotating colored cube in front of a static camera. 11 | 12 | class SimpleCube : TestBase 13 | { 14 | Stopwatch stopwatch; 15 | 16 | VertexBuffer vertexBuffer; 17 | SimpleShaderProgram shaderProgram; 18 | 19 | public SimpleCube() : base(null, 24) { } 20 | 21 | protected override void OnLoad() 22 | { 23 | Span cubeBufferData = stackalloc VertexColor[] { 24 | new VertexColor(new Vector3(-0.5f, -0.5f, -0.5f), Color4b.LightBlue),//4 25 | new VertexColor(new Vector3(-0.5f, -0.5f, 0.5f), Color4b.Lime),//3 26 | new VertexColor(new Vector3(-0.5f, 0.5f, -0.5f), Color4b.White),//7 27 | new VertexColor(new Vector3(-0.5f, 0.5f, 0.5f), Color4b.Black),//8 28 | new VertexColor(new Vector3(0.5f, 0.5f, 0.5f), Color4b.Blue),//5 29 | new VertexColor(new Vector3(-0.5f, -0.5f, 0.5f), Color4b.Lime),//3 30 | new VertexColor(new Vector3(0.5f, -0.5f, 0.5f), Color4b.Red),//1 31 | new VertexColor(new Vector3(-0.5f, -0.5f, -0.5f), Color4b.LightBlue),//4 32 | new VertexColor(new Vector3(0.5f, -0.5f, -0.5f), Color4b.Yellow),//2 33 | new VertexColor(new Vector3(-0.5f, 0.5f, -0.5f), Color4b.White),//7 34 | new VertexColor(new Vector3(0.5f, 0.5f, -0.5f), Color4b.Pink),//6 35 | new VertexColor(new Vector3(0.5f, 0.5f, 0.5f), Color4b.Blue),//5 36 | new VertexColor(new Vector3(0.5f, -0.5f, -0.5f), Color4b.Yellow),//2 37 | new VertexColor(new Vector3(0.5f, -0.5f, 0.5f), Color4b.Red),//1 38 | }; 39 | 40 | vertexBuffer = new VertexBuffer(graphicsDevice, cubeBufferData, BufferUsage.StaticCopy); 41 | 42 | shaderProgram = SimpleShaderProgram.Create(graphicsDevice); 43 | 44 | shaderProgram.View = Matrix4x4.CreateLookAt(new Vector3(0, 1.0f, -1.5f), Vector3.Zero, Vector3.UnitY); 45 | 46 | graphicsDevice.DepthState = DepthState.Default; 47 | graphicsDevice.BlendState = BlendState.Opaque; 48 | 49 | stopwatch = Stopwatch.StartNew(); 50 | } 51 | 52 | protected override void OnRender(double dt) 53 | { 54 | graphicsDevice.ClearDepth = 1f; 55 | graphicsDevice.ClearColor = Vector4.Zero; 56 | graphicsDevice.Clear(ClearBuffers.Color | ClearBuffers.Depth); 57 | 58 | shaderProgram.World = Matrix4x4.CreateRotationY(2 * (float)stopwatch.Elapsed.TotalSeconds); 59 | graphicsDevice.ShaderProgram = shaderProgram; 60 | graphicsDevice.VertexArray = vertexBuffer; 61 | 62 | graphicsDevice.DrawArrays(PrimitiveType.TriangleStrip, 0, vertexBuffer.StorageLength); 63 | } 64 | 65 | protected override void OnResized(Vector2D size) 66 | { 67 | if (size.X == 0 || size.Y == 0) 68 | return; 69 | 70 | graphicsDevice.SetViewport(0, 0, (uint)size.X, (uint)size.Y); 71 | shaderProgram.Projection = Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, size.X / (float)size.Y, 0.01f, 100f); 72 | } 73 | 74 | protected override void OnUnload() 75 | { 76 | vertexBuffer.Dispose(); 77 | shaderProgram.Dispose(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /TrippyGL/Exceptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Silk.NET.OpenGL; 3 | 4 | namespace TrippyGL 5 | { 6 | /// 7 | /// An exception thrown when a shader didn't compile properly. 8 | /// 9 | public class ShaderCompilationException : Exception 10 | { 11 | public ShaderCompilationException() : base() { } 12 | 13 | public ShaderCompilationException(string? infoLog) : this(infoLog, null) { } 14 | 15 | public ShaderCompilationException(string? infoLog, Exception? innerException) 16 | : base(string.Concat("Shader didn't compile properly: ", Environment.NewLine, infoLog), innerException) { } 17 | 18 | public ShaderCompilationException(ShaderType shaderType, string? infoLog) : this(shaderType, infoLog, null) { } 19 | 20 | public ShaderCompilationException(ShaderType shaderType, string? infoLog, Exception? innerException) 21 | : base(string.Concat(shaderType.ToString(), " didn't compile properly: ", Environment.NewLine, infoLog), innerException) { } 22 | } 23 | 24 | /// 25 | /// An exception thrown when a didn't link properly. 26 | /// 27 | public class ProgramLinkException : Exception 28 | { 29 | public ProgramLinkException() : base() { } 30 | 31 | public ProgramLinkException(string? infoLog) : this(infoLog, null) { } 32 | 33 | public ProgramLinkException(string? infoLog, Exception? innerException) 34 | : base(string.Concat("Program didn't link properly: ", Environment.NewLine, infoLog), innerException) { } 35 | } 36 | 37 | /// 38 | /// An exception thrown when a fails to be updated. 39 | /// 40 | public class FramebufferException : Exception 41 | { 42 | public FramebufferException() : base() { } 43 | 44 | public FramebufferException(string? message) : base(message) { } 45 | 46 | public FramebufferException(string? message, Exception? innerException) : base(message, innerException) { } 47 | } 48 | 49 | /// 50 | /// An exception thrown when a blit operation is invalid. 51 | /// 52 | public class InvalidBlitException : Exception 53 | { 54 | public InvalidBlitException() : base() { } 55 | 56 | public InvalidBlitException(string? message) : base(message) { } 57 | 58 | public InvalidBlitException(string? message, Exception? innerException) : base(message, innerException) { } 59 | } 60 | 61 | /// 62 | /// An exception thrown when a can't attach a resource to an attachment point. 63 | /// 64 | public class InvalidFramebufferAttachmentException : Exception 65 | { 66 | public InvalidFramebufferAttachmentException() : base() { } 67 | 68 | public InvalidFramebufferAttachmentException(string? message) : base(message) { } 69 | 70 | public InvalidFramebufferAttachmentException(string? message, Exception? innerException) : base(message, innerException) { } 71 | } 72 | 73 | /// 74 | /// An exception thrown when a buffer copy operation fails. 75 | /// 76 | public class BufferCopyException : Exception 77 | { 78 | public BufferCopyException() : base() { } 79 | 80 | public BufferCopyException(string? message) : base(message) { } 81 | 82 | public BufferCopyException(string? message, Exception? innerException) : base(message, innerException) { } 83 | } 84 | 85 | /// 86 | /// An exception thrown when an error ocurres while loading an OBJ file. 87 | /// 88 | public class ObjLoaderException : Exception 89 | { 90 | public ObjLoaderException() : base() { } 91 | 92 | public ObjLoaderException(string? message) : base(message) { } 93 | 94 | public ObjLoaderException(string? message, Exception? innerException) : base(message, innerException) { } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /TrippyTests/SimpleCubemap/SimpleCubemap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using Silk.NET.Maths; 4 | using TrippyGL; 5 | using TrippyGL.ImageSharp; 6 | using TrippyTestBase; 7 | 8 | namespace SimpleCubemap 9 | { 10 | // Loads the images in the cubemap folder into a TextureCubemap and displays it as a skybox. 11 | // The camera can be moved to look around by moving the mouse while holding the left button. 12 | // A gamepad also works for moving the camera. (the cubemap's images are somewhat low-res) 13 | 14 | class SimpleCubemap : TestBase 15 | { 16 | TextureCubemap cubemap; 17 | ShaderProgram shaderProgram; 18 | ShaderUniform viewUniform; 19 | VertexBuffer vertexBuffer; 20 | 21 | InputManager3D inputManager; 22 | 23 | protected override void OnLoad() 24 | { 25 | inputManager = new InputManager3D(InputContext); 26 | 27 | cubemap = TextureCubemapExtensions.FromFiles( 28 | graphicsDevice, 29 | "cubemap/back.png", "cubemap/front.png", 30 | "cubemap/bottom.png", "cubemap/top.png", 31 | "cubemap/left.png", "cubemap/right.png" 32 | ); 33 | cubemap.SetTextureFilters(TextureMinFilter.Linear, TextureMagFilter.Linear); 34 | 35 | shaderProgram = ShaderProgram.FromFiles(graphicsDevice, "vs.glsl", "fs.glsl", "vPosition"); 36 | 37 | shaderProgram.Uniforms["cubemap"].SetValueTexture(cubemap); 38 | shaderProgram.Uniforms["World"].SetValueMat4(Matrix4x4.Identity); 39 | viewUniform = shaderProgram.Uniforms["View"]; 40 | viewUniform.SetValueMat4(Matrix4x4.Identity); 41 | 42 | Span vertexData = stackalloc VertexPosition[] 43 | { 44 | new Vector3(-0.5f, -0.5f, -0.5f),//4 45 | new Vector3(-0.5f, -0.5f, 0.5f),//3 46 | new Vector3(-0.5f, 0.5f, -0.5f),//7 47 | new Vector3(-0.5f, 0.5f, 0.5f),//8 48 | new Vector3(0.5f, 0.5f, 0.5f),//5 49 | new Vector3(-0.5f, -0.5f, 0.5f),//3 50 | new Vector3(0.5f, -0.5f, 0.5f),//1 51 | new Vector3(-0.5f, -0.5f, -0.5f),//4 52 | new Vector3(0.5f, -0.5f, -0.5f),//2 53 | new Vector3(-0.5f, 0.5f, -0.5f),//7 54 | new Vector3(0.5f, 0.5f, -0.5f),//6 55 | new Vector3(0.5f, 0.5f, 0.5f),//5 56 | new Vector3(0.5f, -0.5f, -0.5f),//2 57 | new Vector3(0.5f, -0.5f, 0.5f),//1 58 | }; 59 | 60 | vertexBuffer = new VertexBuffer(graphicsDevice, vertexData, BufferUsage.StaticDraw); 61 | 62 | graphicsDevice.DepthTestingEnabled = false; 63 | graphicsDevice.BlendingEnabled = false; 64 | } 65 | 66 | protected override void OnUpdate(double dt) 67 | { 68 | base.OnUpdate(dt); 69 | } 70 | 71 | protected override void OnRender(double dt) 72 | { 73 | inputManager.Update((float)dt); 74 | 75 | graphicsDevice.ShaderProgram = shaderProgram; 76 | viewUniform.SetValueMat4(inputManager.CalculateViewMatrixNoTranslation()); 77 | graphicsDevice.VertexArray = vertexBuffer; 78 | graphicsDevice.DrawArrays(PrimitiveType.TriangleStrip, 0, vertexBuffer.StorageLength); 79 | } 80 | 81 | protected override void OnResized(Vector2D size) 82 | { 83 | if (size.X == 0 || size.Y == 0) 84 | return; 85 | 86 | graphicsDevice.SetViewport(0, 0, (uint)size.X, (uint)size.Y); 87 | shaderProgram.Uniforms["Projection"].SetValueMat4(Matrix4x4.CreatePerspectiveFieldOfView(MathF.PI / 2f, size.X / (float)size.Y, 0.01f, 10f)); 88 | } 89 | 90 | protected override void OnUnload() 91 | { 92 | vertexBuffer.Dispose(); 93 | shaderProgram.Dispose(); 94 | cubemap.Dispose(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /TrippyGL/DirectionalLight.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | 4 | namespace TrippyGL 5 | { 6 | /// 7 | /// Represents a directional light on a shader and provides functionality 8 | /// to change it's diffuse/specular colors and direction. 9 | /// 10 | public sealed class DirectionalLight 11 | { 12 | internal static string IncorrectUniformMessage = "The provided uniforms must be the correct type."; 13 | 14 | /// The uniform for setting this light's direction. 15 | private readonly ShaderUniform directionUniform; 16 | 17 | /// The uniform for setting this light's diffuse color. 18 | private readonly ShaderUniform diffuseColorUniform; 19 | 20 | /// The uniform for setting this light's specular color. 21 | private readonly ShaderUniform specularColorUniform; 22 | 23 | /// The last known value of this light's direction. 24 | private Vector3 direction; 25 | 26 | /// The last known value of this light's diffuse color. 27 | private Vector3 diffuseColor; 28 | 29 | /// The last known value of this light's specular color. 30 | private Vector3 specularColor; 31 | 32 | /// 33 | /// Gets or sets this light's direction. 34 | /// 35 | public Vector3 Direction 36 | { 37 | get => direction; 38 | set 39 | { 40 | direction = Vector3.Normalize(value); 41 | directionUniform.SetValueVec3(direction); 42 | } 43 | } 44 | 45 | /// 46 | /// Gets or sets this light's diffuse color. 47 | /// 48 | public Vector3 DiffuseColor 49 | { 50 | get => diffuseColor; 51 | set 52 | { 53 | diffuseColorUniform.SetValueVec3(value); 54 | diffuseColor = value; 55 | } 56 | } 57 | 58 | /// 59 | /// Gets or sets this light's specular color 60 | /// 61 | public Vector3 SpecularColor 62 | { 63 | get => specularColor; 64 | set 65 | { 66 | specularColorUniform.SetValueVec3(value); 67 | specularColor = value; 68 | } 69 | } 70 | 71 | /// 72 | /// Creates a with the specified -s. 73 | /// 74 | /// The uniform for setting this light's direction. Must be of type . 75 | /// The uniform for setting this light's diffuse color. Must be of type . 76 | /// The uniform for setting this light's specular color. Must be of type . 77 | public DirectionalLight(ShaderUniform directionUniform, ShaderUniform diffuseColorUniform, ShaderUniform specularColorUniform) 78 | { 79 | if (directionUniform.UniformType != UniformType.FloatVec3) 80 | throw new ArgumentException(IncorrectUniformMessage, nameof(directionUniform)); 81 | if (diffuseColorUniform.UniformType != UniformType.FloatVec3) 82 | throw new ArgumentException(IncorrectUniformMessage, nameof(diffuseColorUniform)); 83 | if (specularColorUniform.UniformType != UniformType.FloatVec3) 84 | throw new ArgumentException(IncorrectUniformMessage, nameof(specularColorUniform)); 85 | 86 | this.directionUniform = directionUniform; 87 | this.diffuseColorUniform = diffuseColorUniform; 88 | this.specularColorUniform = specularColorUniform; 89 | 90 | Direction = new Vector3(0, -1, 0); 91 | DiffuseColor = new Vector3(1, 1, 1); 92 | SpecularColor = new Vector3(1, 1, 1); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /TrippyDemos/TerrainMaker/NoiseGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using TrippyGL.Utils; 4 | 5 | namespace TerrainMaker 6 | { 7 | static class NoiseGenerator 8 | { 9 | public static float Random(in Vector2 position, in NoiseSeed seed) 10 | { 11 | float r = MathF.Sin(Vector2.Dot(position, seed.DotSeed)) * seed.RandMultiplier; 12 | return r - MathF.Floor(r); 13 | } 14 | 15 | public static float Noise(in Vector2 position, in NoiseSeed seed) 16 | { 17 | Vector2 floor = new Vector2(MathF.Floor(position.X), MathF.Floor(position.Y)); 18 | Vector2 fract = position - floor; 19 | 20 | return TrippyMath.Lerp( 21 | TrippyMath.Lerp(Random(floor, seed), Random(floor + Vector2.UnitX, seed), fract.X), 22 | TrippyMath.Lerp(Random(floor + Vector2.UnitY, seed), Random(floor + Vector2.One, seed), fract.X), 23 | fract.Y 24 | ); 25 | } 26 | 27 | public static float Perlin(in Vector2 position, in NoiseSeed seed1, in NoiseSeed seed2) 28 | { 29 | Vector2 floor = new Vector2(MathF.Floor(position.X), MathF.Floor(position.Y)); 30 | Vector2 fract = position - floor; 31 | 32 | Vector2 blvec = new Vector2(Random(floor, seed1), Random(floor, seed2)) * 2f - Vector2.One; 33 | Vector2 brvec = new Vector2(Random(floor + Vector2.UnitX, seed1), Random(floor + Vector2.UnitX, seed2)) * 2f - Vector2.One; 34 | Vector2 tlvec = new Vector2(Random(floor + Vector2.UnitY, seed1), Random(floor + Vector2.UnitY, seed2)) * 2f - Vector2.One; 35 | Vector2 trvec = new Vector2(Random(floor + Vector2.One, seed1), Random(floor + Vector2.One, seed2)) * 2f - Vector2.One; 36 | 37 | Vector2 bldiff = fract; 38 | Vector2 trdiff = fract - Vector2.One; 39 | Vector2 brdiff = new Vector2(trdiff.X, bldiff.Y); 40 | Vector2 tldiff = new Vector2(bldiff.X, trdiff.Y); 41 | 42 | float dbl = Vector2.Dot(blvec, bldiff); 43 | float dbr = Vector2.Dot(brvec, brdiff); 44 | float dtl = Vector2.Dot(tlvec, tldiff); 45 | float dtr = Vector2.Dot(trvec, trdiff); 46 | 47 | return TrippyMath.SmootherStep(TrippyMath.SmootherStep(dbl, dbr, fract.X), TrippyMath.SmootherStep(dtl, dtr, fract.X), fract.Y) * 0.5f + 0.5f; 48 | } 49 | 50 | public static float FractalNoise(in Vector2 position, int loops, in NoiseSeed seed) 51 | { 52 | float amp = 0.5f; 53 | float freq = 1; 54 | float v = 0; 55 | for (int i = 0; i < loops; i++) 56 | { 57 | v += Noise(position * freq, seed) * amp; 58 | amp /= 2; 59 | freq *= 2; 60 | } 61 | return v; 62 | } 63 | 64 | public static void GenPoint(GeneratorSeed seed, in Vector2 position, out float humidity, out float vegetation) 65 | { 66 | humidity = FractalNoise(position / 64f, 9, seed.HumiditySeed); 67 | 68 | vegetation = Noise(position / 32f, seed.VegetationSeed); 69 | } 70 | 71 | public static float GenHeight(GeneratorSeed seed, in Vector2 position) 72 | { 73 | GenPoint(seed, position, out float humidity, out float vegetation); 74 | 75 | float height = FractalNoise(position / 200f, 12, seed.HeightSeed) * 128f - 56.25f; 76 | if (height > 0) 77 | { 78 | float m = Math.Clamp(MathF.Pow(height / 50f, 2), 0, 1); 79 | float a = FractalNoise(position / 32f, 8, seed.HeightSeed) * (3.1f - vegetation * 3.1f); 80 | a += Perlin(position / 32f, seed.HeightSeed, seed.ExtraSeed1) * (2.5f + vegetation); 81 | height += m * 38 * a; 82 | } 83 | else 84 | { 85 | float m = -height / 10f; 86 | float a = FractalNoise(position / 32f, 10, seed.HeightSeed); 87 | height -= MathF.Pow(a, 8) * m * 100; 88 | } 89 | 90 | return height; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /TrippyTests/IndexedRendering/IndexedRendering.cs: -------------------------------------------------------------------------------- 1 | using System.Numerics; 2 | using Silk.NET.Input; 3 | using Silk.NET.Maths; 4 | using TrippyGL; 5 | using TrippyTestBase; 6 | 7 | namespace IndexedRendering 8 | { 9 | // This test makes a 7 segment display using indexed rendering. 10 | // The number displayed can be changed by pressing a number on the keyboard. 11 | 12 | // All the vertices for the display are stored in a buffer and indices are used to define 13 | // the order in which they are taken. By using different indices, we can draw the different numbers. 14 | 15 | class IndexedRendering : TestBase 16 | { 17 | VertexBuffer vertexBuffer; 18 | ShaderProgram shaderProgram; 19 | 20 | /// The location in the index buffer where the index data for each number starts. 21 | int[] indicesStart; 22 | /// The number the 7 segment display is currently rendering. 23 | int currentSelectedIndex = 0; 24 | 25 | protected override void OnLoad() 26 | { 27 | SimpleVertex[] vertexData = Indices.Vertices; 28 | 29 | // We create a VertexBuffer with just enough vertex storage for all the vertices 30 | // and enough index storage for all the indices, and give it vertexData as initial vertex data. 31 | vertexBuffer = new VertexBuffer(graphicsDevice, (uint)vertexData.Length, (uint)Indices.TotalIndicesLength, ElementType.UnsignedByte, BufferUsage.StaticDraw, vertexData); 32 | 33 | // We will store the location in the index subset where each number's indices start in this array 34 | indicesStart = new int[Indices.AllNumbersIndices.Length]; 35 | 36 | // We will copy all the data from all the number's indices over to the index buffer subset. 37 | // We copy them in order (that is, Number0 followed by Number1 followed by Number2 etc) 38 | // and in indicesStart we store in which location of the subset the indices of each number start. 39 | int indexStart = 0; 40 | for (int i = 0; i < Indices.AllNumbersIndices.Length; i++) 41 | { 42 | indicesStart[i] = indexStart; 43 | vertexBuffer.IndexSubset.SetData(Indices.AllNumbersIndices[i], (uint)indexStart); 44 | indexStart += Indices.AllNumbersIndices[i].Length; 45 | } 46 | 47 | shaderProgram = ShaderProgram.FromFiles(graphicsDevice, "vs.glsl", "fs.glsl", "vPosition"); 48 | 49 | shaderProgram.Uniforms["color"].SetValueVec4(1f, 0f, 0f, 1f); 50 | 51 | graphicsDevice.BlendingEnabled = false; 52 | graphicsDevice.DepthTestingEnabled = false; 53 | } 54 | 55 | protected override void OnRender(double dt) 56 | { 57 | graphicsDevice.ClearColor = new Vector4(0, 0, 0, 1); 58 | graphicsDevice.Clear(ClearBuffers.Color); 59 | 60 | graphicsDevice.VertexArray = vertexBuffer; 61 | graphicsDevice.ShaderProgram = shaderProgram; 62 | graphicsDevice.DrawElements(PrimitiveType.Triangles, indicesStart[currentSelectedIndex], (uint)Indices.AllNumbersIndices[currentSelectedIndex].Length); 63 | } 64 | 65 | protected override void OnKeyDown(IKeyboard sender, Key key, int idk) 66 | { 67 | if (key >= Key.Number0 && key <= Key.Number9) 68 | currentSelectedIndex = key - Key.Number0; 69 | else if (key >= Key.Keypad0 && key <= Key.Keypad9) 70 | currentSelectedIndex = key - Key.Keypad0; 71 | } 72 | 73 | protected override void OnResized(Vector2D size) 74 | { 75 | if (size.X == 0 || size.Y == 0) 76 | return; 77 | 78 | graphicsDevice.SetViewport(0, 0, (uint)size.X, (uint)size.Y); 79 | shaderProgram.Uniforms["Projection"].SetValueMat4(Matrix4x4.CreateOrthographic(size.X / (float)size.Y * 2f, 2f, 0.1f, 10f)); 80 | } 81 | 82 | protected override void OnUnload() 83 | { 84 | vertexBuffer.Dispose(); 85 | shaderProgram.Dispose(); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /TrippyGL.Fonts/IGlyphSource.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | using System.Numerics; 3 | using SixLabors.ImageSharp; 4 | using SixLabors.ImageSharp.PixelFormats; 5 | 6 | namespace TrippyGL.Fonts 7 | { 8 | /// 9 | /// Defines methods and properties used by for building 10 | /// instances. 11 | /// 12 | public interface IGlyphSource 13 | { 14 | /// The first character in the range the font will contain. 15 | public char FirstChar { get; } 16 | 17 | /// The last (inclusive) character in the range the font will contain. 18 | public char LastChar { get; } 19 | 20 | /// The size of the font, typically measured in pixels. 21 | public float Size { get; } 22 | 23 | /// The name of the font. Can be null, but no longer than . 24 | public string Name { get; } 25 | 26 | /// The distance between the baseline and the highest glyph's highest point. Typically positive. 27 | public float Ascender { get; } 28 | 29 | /// The distance between the baseline and the lowest glyph's lowest point. Typically negative. 30 | public float Descender { get; } 31 | 32 | /// The distance between the lowest point of a line and the highest point of the next line. 33 | public float LineGap { get; } 34 | 35 | /// 36 | /// Gets the advances for all glyphs. 37 | /// 38 | /// 39 | /// Whether the font is spaced. If this returned false, then the font is monospace and advances 40 | /// will be an array of length one, whose only element is the advance for all characters. 41 | /// 42 | /// 43 | /// The implementation shouldn't hold a reference to the returned array. Users of this interface 44 | /// are allowed to use the same array instance without having to copy the data to a new location. 45 | /// 46 | public bool GetAdvances([NotNullWhen(true)] out float[]? advances); 47 | 48 | /// 49 | /// Tries to get kerning for all glyphs. 50 | /// 51 | /// 52 | /// Whether the font has kerning. If false, no kerning is used and kerningOffsets should be ignored. 53 | /// 54 | /// 55 | /// The implementation shouldn't hold a reference to the returned array. Users of this interface 56 | /// are allowed to use the same array instance without having to copy the data to a new location. 57 | /// 58 | public bool TryGetKerning([NotNullWhen(true)] out Vector2[,]? kerningOffsets); 59 | 60 | /// 61 | /// Gets the render offsets for all glyphs. 62 | /// 63 | /// 64 | /// The implementation shouldn't hold a reference to the returned array. Users of this interface 65 | /// are allowed to use the same array instance without having to copy the data to a new location. 66 | /// 67 | public Vector2[] GetRenderOffsets(); 68 | 69 | /// 70 | /// Gets the size in pixels of the area needed for drawing a specific character. 71 | /// 72 | /// 73 | /// A size of zero means a glyph does not need drawing (for example, a space character). 74 | /// 75 | public System.Drawing.Point GetGlyphSize(int charCode); 76 | 77 | /// 78 | /// Draws a character into the given image, at the specified location. 79 | /// 80 | /// 81 | /// This operation shouldn't touch anything outside the area defined by the given location 82 | /// and the size provided for the same character in . 83 | /// 84 | public void DrawGlyphToImage(int charCode, System.Drawing.Point location, Image image); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /TrippyGL/BufferObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Silk.NET.OpenGL; 3 | 4 | namespace TrippyGL 5 | { 6 | /// 7 | /// Owns storage on the GPU's memory that can be used for various purposes though -s. 8 | /// 9 | public sealed class BufferObject : GraphicsResource 10 | { 11 | /// The handle for the GL Buffer Object. 12 | public readonly uint Handle; 13 | 14 | /// The usage hint applied for this . 15 | public BufferUsage UsageHint { get; private set; } 16 | 17 | /// The length of this 's storage, measured in bytes. 18 | public uint StorageLengthInBytes { get; private set; } 19 | 20 | /// 21 | /// Creates a with a specified length and usage hint. 22 | /// 23 | /// The this resource will use. 24 | /// The desired size of the 's storage measured in bytes. 25 | /// Used by the graphics driver to optimize performance. 26 | public BufferObject(GraphicsDevice graphicsDevice, uint sizeInBytes, BufferUsage usageHint) : base(graphicsDevice) 27 | { 28 | if (usageHint == 0) 29 | throw new ArgumentException(nameof(usageHint) + " is not a valid " + nameof(BufferUsage) + " value"); 30 | 31 | Handle = GL.GenBuffer(); 32 | RecreateStorage(sizeInBytes, usageHint); 33 | } 34 | 35 | /// 36 | /// Recreate this 's storage with a new size and usage hint. 37 | /// The contents of the new storage are undefined after this operation. 38 | /// 39 | /// The new size for the measured in bytes. 40 | /// The new usage hint for the , or 0 to keep the previous hint. 41 | public unsafe void RecreateStorage(uint sizeInBytes, BufferUsage usageHint = 0) 42 | { 43 | // We check that the parameters are valid, then store them 44 | ValidateBufferSize(sizeInBytes); 45 | 46 | if (usageHint != 0) 47 | { 48 | ValidateBufferUsage(usageHint); 49 | UsageHint = usageHint; 50 | } 51 | 52 | StorageLengthInBytes = sizeInBytes; 53 | 54 | // We then bind this buffer and specify it's storage to OpenGL 55 | GraphicsDevice.BindBufferObject(this); 56 | GL.BufferData((GLEnum)GraphicsDevice.DefaultBufferTarget, sizeInBytes, (void*)0, (GLEnum)UsageHint); 57 | } 58 | 59 | protected override void Dispose(bool isManualDispose) 60 | { 61 | GL.DeleteBuffer(Handle); 62 | } 63 | 64 | public override string ToString() 65 | { 66 | return string.Concat( 67 | nameof(Handle) + "=", Handle.ToString(), 68 | ", " + nameof(StorageLengthInBytes) + "=", StorageLengthInBytes.ToString(), 69 | ", " + nameof(UsageHint) + "=", UsageHint.ToString() 70 | ); 71 | } 72 | 73 | /// 74 | /// Checks that the buffer size in bytes parameter is valid and throws an exception if it's not. 75 | /// 76 | private static void ValidateBufferSize(uint sizeInBytes) 77 | { 78 | if (sizeInBytes <= 0) 79 | throw new ArgumentOutOfRangeException(nameof(sizeInBytes), sizeInBytes, nameof(sizeInBytes) + " must be greater than 0"); 80 | } 81 | 82 | /// 83 | /// Checks that the usage hint parameter is valid and throws an exception if it's not. 84 | /// 85 | private static void ValidateBufferUsage(BufferUsage usageHint) 86 | { 87 | if (!Enum.IsDefined(usageHint)) 88 | throw new ArgumentException(nameof(usageHint) + " is not a valid " + nameof(BufferUsage) + " value", nameof(usageHint)); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /TrippyTests/ComplexVertexFormats/ComplexVertex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Numerics; 3 | using System.Runtime.InteropServices; 4 | using TrippyGL; 5 | 6 | namespace ComplexVertexFormats 7 | { 8 | [StructLayout(LayoutKind.Sequential)] 9 | struct ComplexVertex : IVertex 10 | { 11 | float padding0; 12 | short padding1; 13 | byte padding2; 14 | sbyte sixtyThree; 15 | float X; 16 | short padding3; 17 | byte nothing0; 18 | byte colorR; 19 | Matrix4x4 matrix1; 20 | ushort colorG; 21 | short sixtyFour; 22 | float Y; 23 | short padding4; 24 | byte colorB; 25 | Matrix3x2 padding5; 26 | byte padding6; 27 | float Z; 28 | Vector4 oneTwoThreeFour; 29 | int alwaysZero; 30 | byte padding7; 31 | short padding8; 32 | byte alsoZero; 33 | Matrix4x4 padding9; 34 | byte padding10; 35 | 36 | public ComplexVertex(Vector3 position, Color4b color) 37 | { 38 | X = position.X; 39 | Y = position.Y; 40 | Z = position.Z; 41 | 42 | colorR = color.R; 43 | colorG = (ushort)(color.G * 2048 / 255); 44 | colorB = color.B; 45 | 46 | matrix1 = Matrix4x4.Identity; 47 | 48 | sixtyThree = 63; 49 | sixtyFour = 64; 50 | oneTwoThreeFour = new Vector4(1, 2, 3, 4); 51 | alwaysZero = 0; 52 | alsoZero = 0; 53 | 54 | nothing0 = default; 55 | 56 | padding0 = default; 57 | padding1 = default; 58 | padding2 = default; 59 | padding3 = default; 60 | padding4 = default; 61 | padding5 = default; 62 | padding6 = default; 63 | padding7 = default; 64 | padding8 = default; 65 | padding9 = default; 66 | padding10 = default; 67 | } 68 | 69 | public int AttribDescriptionCount => 20; 70 | 71 | public void WriteAttribDescriptions(Span descriptions) 72 | { 73 | descriptions[0] = new VertexAttribDescription(7); // padding0-2 74 | descriptions[1] = new VertexAttribDescription(AttributeType.Float, false, AttributeBaseType.Byte); // sixtyThree 75 | descriptions[2] = new VertexAttribDescription(AttributeType.Float); // X 76 | descriptions[3] = VertexAttribDescription.CreatePadding(AttributeBaseType.Short, 1); // padding3 77 | descriptions[4] = new VertexAttribDescription(AttributeType.Float, true, AttributeBaseType.UnsignedByte); // nothing0 78 | descriptions[5] = new VertexAttribDescription(AttributeType.Float, true, AttributeBaseType.UnsignedByte); //colorR 79 | descriptions[6] = new VertexAttribDescription(AttributeType.FloatMat4); // mat1 80 | descriptions[7] = new VertexAttribDescription(AttributeType.Float, false, AttributeBaseType.UnsignedShort); // colorG 81 | descriptions[8] = new VertexAttribDescription(AttributeType.Float, false, AttributeBaseType.Short); // sixtyFour 82 | descriptions[9] = new VertexAttribDescription(AttributeType.Float); // Y 83 | descriptions[10] = VertexAttribDescription.CreatePadding(AttributeBaseType.Short, 1); // padding4 84 | descriptions[11] = new VertexAttribDescription(AttributeType.Float, true, AttributeBaseType.UnsignedByte); // colorB 85 | descriptions[12] = new VertexAttribDescription(3 * 2 * 4 + 2); // padding 5-6 86 | descriptions[13] = new VertexAttribDescription(AttributeType.Float); // Z 87 | descriptions[14] = new VertexAttribDescription(AttributeType.FloatVec4); // oneTwoThreeFour 88 | descriptions[15] = new VertexAttribDescription(AttributeType.Int); // alwaysZero 89 | descriptions[16] = VertexAttribDescription.CreatePadding(AttributeBaseType.UnsignedByte, 1); // padding7 90 | descriptions[17] = VertexAttribDescription.CreatePadding(AttributeBaseType.Short, 1); // padding8 91 | descriptions[18] = new VertexAttribDescription(AttributeType.Float, true, AttributeBaseType.UnsignedByte); // alsoZero 92 | descriptions[19] = new VertexAttribDescription(65); // padding9-10 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /TrippyGL/RenderbufferObject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Silk.NET.OpenGL; 3 | 4 | namespace TrippyGL 5 | { 6 | /// 7 | /// A buffer optimized to be rendered to. The only way to use a 8 | /// is to attach it to a . 9 | /// 10 | public sealed class RenderbufferObject : GraphicsResource 11 | { 12 | /// The handle for the GL Renderbuffer Object. 13 | public readonly uint Handle; 14 | 15 | /// The width of this . 16 | public readonly uint Width; 17 | 18 | /// The height of this . 19 | public readonly uint Height; 20 | 21 | /// The amount of samples this has. 22 | public readonly uint Samples; 23 | 24 | /// The format for this . 25 | public readonly RenderbufferFormat Format; 26 | 27 | /// Gets whether the format of this is depth-only. 28 | public bool IsDepthOnly => Format == RenderbufferFormat.Depth16 || Format == RenderbufferFormat.Depth24 || Format == RenderbufferFormat.Depth32f; 29 | 30 | /// Gets whether the format of this is stencil-only. 31 | public bool IsStencilOnly => Format == RenderbufferFormat.Stencil8; 32 | 33 | /// Gets whether the format of this is depth-stencil. 34 | public bool IsDepthStencil => Format == RenderbufferFormat.Depth24Stencil8 || Format == RenderbufferFormat.Depth32fStencil8; 35 | 36 | /// Gets whether the format of this is color-renderable. 37 | public bool IsColorRenderableFormat => !(IsDepthOnly || IsStencilOnly || IsDepthStencil); 38 | 39 | /// 40 | /// Creates a with the specified format. 41 | /// 42 | /// The this resource will use. 43 | /// The width for the . 44 | /// The height for the . 45 | /// The format for the 's storage. 46 | /// The amount of samples the will have. 47 | public RenderbufferObject(GraphicsDevice graphicsDevice, uint width, uint height, RenderbufferFormat format, uint samples = 0) 48 | : base(graphicsDevice) 49 | { 50 | if (!Enum.IsDefined(format)) 51 | throw new ArgumentException("Invalid renderbuffer format", nameof(format)); 52 | 53 | if (width <= 0 || width > graphicsDevice.MaxRenderbufferSize) 54 | throw new ArgumentOutOfRangeException(nameof(width), width, "Width must be in the range (0, " + nameof(graphicsDevice.MaxRenderbufferSize) + "]"); 55 | 56 | if (height <= 0 || height > graphicsDevice.MaxRenderbufferSize) 57 | throw new ArgumentOutOfRangeException(nameof(height), height, "Height must be in the range (0, " + nameof(graphicsDevice.MaxRenderbufferSize) + "]"); 58 | 59 | ValidateSampleCount(samples); 60 | 61 | Handle = GL.GenRenderbuffer(); 62 | Format = format; 63 | Width = width; 64 | Height = height; 65 | Samples = samples; 66 | graphicsDevice.ForceBindRenderbuffer(this); 67 | GL.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, Samples, (InternalFormat)format, Width, Height); 68 | } 69 | 70 | protected override void Dispose(bool isManualDispose) 71 | { 72 | GL.DeleteRenderbuffer(Handle); 73 | } 74 | 75 | internal void ValidateSampleCount(uint samples) 76 | { 77 | if (samples < 0 || samples > GraphicsDevice.MaxSamples) 78 | throw new ArgumentOutOfRangeException(nameof(samples), samples, "The sample count must be in the range [0, " + nameof(GraphicsDevice.MaxSamples) + "]"); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /TrippyGL/Enums/AttributeType.cs: -------------------------------------------------------------------------------- 1 | namespace TrippyGL 2 | { 3 | /// 4 | /// Specifies the types of vertex attributes. 5 | /// 6 | public enum AttributeType 7 | { 8 | Int = 5124, 9 | UnsignedInt = 5125, 10 | Float = 5126, 11 | Double = 5130, 12 | Int64Arb = 5134, 13 | UnsignedInt64Arb = 5135, 14 | FloatVec2 = 35664, 15 | FloatVec3 = 35665, 16 | FloatVec4 = 35666, 17 | IntVec2 = 35667, 18 | IntVec3 = 35668, 19 | IntVec4 = 35669, 20 | Bool = 35670, 21 | BoolVec2 = 35671, 22 | BoolVec3 = 35672, 23 | BoolVec4 = 35673, 24 | FloatMat2 = 35674, 25 | FloatMat3 = 35675, 26 | FloatMat4 = 35676, 27 | /*Sampler1D = 35677, 28 | Sampler2D = 35678, 29 | Sampler3D = 35679, 30 | SamplerCube = 35680, 31 | Sampler1DShadow = 35681, 32 | Sampler2DShadow = 35682, 33 | Sampler2DRect = 35683, 34 | Sampler2DRectShadow = 35684,*/ 35 | FloatMat2x3 = 35685, 36 | FloatMat2x4 = 35686, 37 | FloatMat3x2 = 35687, 38 | FloatMat3x4 = 35688, 39 | FloatMat4x2 = 35689, 40 | FloatMat4x3 = 35690, 41 | /*SamplerBuffer = 36290, 42 | Sampler1DArrayShadow = 36291, 43 | Sampler2DArrayShadow = 36292, 44 | SamplerCubeShadow = 36293,*/ 45 | UnsignedIntVec2 = 36294, 46 | UnsignedIntVec3 = 36295, 47 | UnsignedIntVec4 = 36296, 48 | /*IntSampler1D = 36297, 49 | IntSampler2D = 36298, 50 | IntSampler3D = 36299, 51 | IntSamplerCube = 36300, 52 | IntSampler2DRect = 36301, 53 | IntSampler1DArray = 36302, 54 | IntSampler2DArray = 36303, 55 | IntSamplerBuffer = 36304, 56 | UnsignedIntSampler1D = 36305, 57 | UnsignedIntSampler2D = 36306, 58 | UnsignedIntSampler3D = 36307, 59 | UnsignedIntSamplerCube = 36308, 60 | UnsignedIntSampler2DRect = 36309, 61 | UnsignedIntSampler1DArray = 36310, 62 | UnsignedIntSampler2DArray = 36311, 63 | UnsignedIntSamplerBuffer = 36312,*/ 64 | DoubleMat2 = 36678, 65 | DoubleMat3 = 36679, 66 | DoubleMat4 = 36680, 67 | DoubleMat2x3 = 36681, 68 | DoubleMat2x4 = 36682, 69 | DoubleMat3x2 = 36683, 70 | DoubleMat3x4 = 36684, 71 | DoubleMat4x2 = 36685, 72 | DoubleMat4x3 = 36686, 73 | Int64Vec2Arb = 36841, 74 | Int64Vec3Arb = 36842, 75 | Int64Vec4Arb = 36843, 76 | UnsignedInt64Vec2Arb = 36853, 77 | UnsignedInt64Vec3Arb = 36854, 78 | UnsignedInt64Vec4Arb = 36855, 79 | DoubleVec2 = 36860, 80 | DoubleVec3 = 36861, 81 | DoubleVec4 = 36862, 82 | /*SamplerCubeMapArray = 36876, 83 | SamplerCubeMapArrayShadow = 36877, 84 | IntSamplerCubeMapArray = 36878, 85 | UnsignedIntSamplerCubeMapArray = 36879, 86 | Image1D = 36940, 87 | Image2D = 36941, 88 | Image3D = 36942, 89 | Image2DRect = 36943, 90 | ImageCube = 36944, 91 | ImageBuffer = 36945, 92 | Image1DArray = 36946, 93 | Image2DArray = 36947, 94 | ImageCubeMapArray = 36948, 95 | Image2DMultisample = 36949, 96 | Image2DMultisampleArray = 36950, 97 | IntImage1D = 36951, 98 | IntImage2D = 36952, 99 | IntImage3D = 36953, 100 | IntImage2DRect = 36954, 101 | IntImageCube = 36955, 102 | IntImageBuffer = 36956, 103 | IntImage1DArray = 36957, 104 | IntImage2DArray = 36958, 105 | IntImageCubeMapArray = 36959, 106 | IntImage2DMultisample = 36960, 107 | IntImage2DMultisampleArray = 36961, 108 | UnsignedIntImage1D = 36962, 109 | UnsignedIntImage2D = 36963, 110 | UnsignedIntImage3D = 36964, 111 | UnsignedIntImage2DRect = 36965, 112 | UnsignedIntImageCube = 36966, 113 | UnsignedIntImageBuffer = 36967, 114 | UnsignedIntImage1DArray = 36968, 115 | UnsignedIntImage2DArray = 36969, 116 | UnsignedIntImageCubeMapArray = 36970, 117 | UnsignedIntImage2DMultisample = 36971, 118 | UnsignedIntImage2DMultisampleArray = 36972, 119 | Sampler2DMultisample = 37128, 120 | IntSampler2DMultisample = 37129, 121 | UnsignedIntSampler2DMultisample = 37130, 122 | Sampler2DMultisampleArray = 37131, 123 | IntSampler2DMultisampleArray = 37132, 124 | UnsignedIntSampler2DMultisampleArray = 37133*/ 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /TrippyGL/FramebufferAttachments.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TrippyGL 4 | { 5 | /// 6 | /// Represents an attachment of a to a . 7 | /// 8 | public readonly struct FramebufferTextureAttachment : IEquatable 9 | { 10 | /// The in this framebuffer attachment. 11 | public readonly Texture Texture; 12 | 13 | /// The attachment point to which this attachment is attached in a . 14 | public readonly FramebufferAttachmentPoint AttachmentPoint; 15 | 16 | /// 17 | /// Creates a . 18 | /// 19 | /// The to attach in this attachment. 20 | /// The attachment point to which this attachment attaches to. 21 | public FramebufferTextureAttachment(Texture texture, FramebufferAttachmentPoint attachmentPoint) 22 | { 23 | Texture = texture; 24 | AttachmentPoint = attachmentPoint; 25 | } 26 | 27 | public static bool operator ==(FramebufferTextureAttachment left, FramebufferTextureAttachment right) => left.Equals(right); 28 | public static bool operator !=(FramebufferTextureAttachment left, FramebufferTextureAttachment right) => !left.Equals(right); 29 | 30 | public override string ToString() 31 | { 32 | return string.Concat("Texture" + nameof(AttachmentPoint) + "=", AttachmentPoint.ToString()); 33 | } 34 | 35 | public override int GetHashCode() 36 | { 37 | return HashCode.Combine(Texture, AttachmentPoint); 38 | } 39 | 40 | public bool Equals(FramebufferTextureAttachment other) 41 | { 42 | return Texture == other.Texture && AttachmentPoint == other.AttachmentPoint; 43 | } 44 | 45 | public override bool Equals(object? obj) 46 | { 47 | if (obj is FramebufferTextureAttachment framebufferTextureAttachment) 48 | return Equals(framebufferTextureAttachment); 49 | return false; 50 | } 51 | } 52 | 53 | /// 54 | /// Represents an attachment of a to a . 55 | /// 56 | public readonly struct FramebufferRenderbufferAttachment : IEquatable 57 | { 58 | /// The in this framebuffer attachment. 59 | public readonly RenderbufferObject Renderbuffer; 60 | 61 | /// The attachment point to which this attachment is attached in a . 62 | public readonly FramebufferAttachmentPoint AttachmentPoint; 63 | 64 | /// 65 | /// Creates a . 66 | /// 67 | /// The to attach in this attachment. 68 | /// The attachment point to which this attachment attaches to. 69 | public FramebufferRenderbufferAttachment(RenderbufferObject renderbuffer, FramebufferAttachmentPoint attachmentPoint) 70 | { 71 | Renderbuffer = renderbuffer; 72 | AttachmentPoint = attachmentPoint; 73 | } 74 | 75 | public static bool operator ==(FramebufferRenderbufferAttachment left, FramebufferRenderbufferAttachment right) => left.Equals(right); 76 | public static bool operator !=(FramebufferRenderbufferAttachment left, FramebufferRenderbufferAttachment right) => !left.Equals(right); 77 | 78 | public override string ToString() 79 | { 80 | return string.Concat("Renderbuffer" + nameof(AttachmentPoint) + "=", AttachmentPoint.ToString()); 81 | } 82 | 83 | public override int GetHashCode() 84 | { 85 | return HashCode.Combine(Renderbuffer, AttachmentPoint); 86 | } 87 | 88 | public bool Equals(FramebufferRenderbufferAttachment other) 89 | { 90 | return Renderbuffer == other.Renderbuffer && AttachmentPoint == other.AttachmentPoint; 91 | } 92 | 93 | public override bool Equals(object? obj) 94 | { 95 | if (obj is FramebufferRenderbufferAttachment framebufferRenderbufferAttachment) 96 | return Equals(framebufferRenderbufferAttachment); 97 | return false; 98 | } 99 | } 100 | } 101 | --------------------------------------------------------------------------------