Giter Site home page Giter Site logo

math's Introduction

SwiftGL Math Library

SwiftGL is libraries for graphics programming that's 100% Swift.

To use, include dependency in your Package.swift:

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/SwiftGL/Math.git", majorVersion: 2)
    ]
)

Then import SGLMath in your swift file.

Getting Started

The SwiftGL math library is an implementation of every data type and operator in the GLSL 4.5 specification. It includes every operator in section 5.1 and every function in sections 8.1 to 8.7. You can even swizzle.

Swift syntax and GLSL syntax are naturally very similar. With the same types and functions available in both you may not even notice when you switch between them. That's exactly the point of this library. Everything you know and use in GLSL will now work in Swift.

var myVec = vec4(1, 2, 3, 4)
myVec.ab = vec2(99, 98)
print(myVec) //=> (1, 2, 98, 99)

Arrays and structures of SwiftGL math types can be passed directly to OpenGL. The binary formats for all types are an exact match.

let vertices = [vec3](count: 100, repeatedValue: vec3())
glBufferData(target: GL_ARRAY_BUFFER, size: strideof(vec3) * vertices.count,
             data: vertices, usage: GL_STATIC_DRAW)

You may find that you have a type with the wrong scalar. The Swift thing to do is construct a new one from the old one. That also happens to be how you do it in GLSL. From section 5.1 of the spec: "There is no typecast operator; constructors are used instead."

let f = Float(3.1)
let d = Double(f)
let v = vec3()
let dv = dvec3(v)

The glm library for C++ is one of the most used math libraries for OpenGL. Like SwiftGL, it implements the GLSL specification. It also provides additional functions to support things like quaternions and cameras. The SwiftGL math module contains matrix transformations compatible with glm.

let projectionMatrix = mat4.perspective(fov, aspect, clip.near, clip.far)

Regenerate Swizzle.swift

From the root directory of the package execute: ./.build/x86_64-unknown-linux/debug/swizgen

math's People

Contributors

jkolb avatar ctreffs avatar ae9rb avatar db47h avatar hamstudio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.