Code Monkey home page Code Monkey logo

swiftgl's Introduction

SwiftGL OpenGL

MacOS:

brew install --HEAD glfw3

swift build -Xswiftc -I/usr/local/include -Xlinker -L/usr/local/lib

Ubuntu:

sudo apt install libglfw3-dev

swift build

To use, include dependency in your Package.swift:

let package = Package(
    dependencies: [
        .package(url: "https://github.com/GothStar/SwiftGL.git", .branch("master"))
    ]
)

Then import SwiftGL in your swift file.

Getting Started

You can't use OpenGL until you can call its functions. The SwiftGL OpenGL loader imports all the functions up to OpenGL 4.6. Platform differences are abstracted away. There's nothing to initialize and no C code. 100% easy. 100% Swift.

If you've used OpenGL in Swift before you probably used commands like this.

glDepthMask(GLboolean(GL_FALSE))
glClear(GLbitfield(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT))
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GLint(GL_MIRRORED_REPEAT))

You have to cast everything. Not fun. This is a result of how Swift translates C header files. Because SwiftGL loader is specialized for Swift, all that casting is a thing of the past.

glDepthMask(false)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT)

Every OpenGL command is also available with argument labels. This may make your code easier to read. It also makes it difficult to put values in the wrong position and sometimes catches copy-and-paste mistakes.

glClear(mask: GL_COLOR_BUFFER_BIT)
glTexParameteri(target: GL_TEXTURE_2D, pname: GL_TEXTURE_WRAP_S, param: GL_MIRRORED_REPEAT)
glViewport(x: 0, y: 0, width: 800, height: 600)

The SwiftGL OpenGL loader provides you with direct calls to the OpenGL functions. There's no translation layer required to provide the syntactical sugar. Because Swift has first-class support for working with C, there's no performance penalty for crossing languages.

Regenerate OpenGL loading code

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

swiftgl's People

Contributors

ae9rb avatar cwimberger avatar gothstar avatar jkolb avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gergelydaniel

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.