Skip to content

Commit

Permalink
adding files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolalysenko committed Jul 6, 2013
0 parents commit 861a371
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules/*
16 changes: 16 additions & 0 deletions .npmignore
@@ -0,0 +1,16 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules/*
example/*
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2013 Mikola Lysenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
89 changes: 89 additions & 0 deletions README.md
@@ -0,0 +1,89 @@
orbit-camera
============
Simple arcball camera built on top of gl-matrix

## Example

```javascript
var shell = require("gl-now")()
var createMesh = require("gl-mesh")
var glm = require("gl-matrix")
var mat4 = glm.mat4
var simple3DShader = require("simple-3d-shader")
var createOrbitCamera = require("orbit-camera")

var camera = createOrbitCamera([0, 10, 20],
[0, 3, 0],
[0, 1, 0])

var shader, mesh

shell.on("gl-init", function() {
shader = simple3DShader(shell.gl)
mesh = createMesh(shell.gl, require("bunny"))
})

shell.on("gl-render", function(t) {
shader.bind()

var scratch = mat4.create()
shader.uniforms.model = scratch
shader.uniforms.projection = mat4.perspective(scratch, Math.PI/4.0, shell.width/shell.height, 0.1, 1000.0)
shader.uniforms.view = camera.view(scratch)

mesh.bind(shader)
mesh.draw()
mesh.unbind()
})

shell.on("tick", function() {
if(shell.wasDown("mouse-left")) {
camera.rotate([shell.mouseX/shell.width-0.5, shell.mouseY/shell.height-0.5],
[shell.prevMouseX/shell.width-0.5, shell.prevMouseY/shell.height-0.5])
}
if(shell.wasDown("mouse-right")) {
camera.pan([10*(shell.mouseX-shell.prevMouseX)/shell.width,
10*(shell.mouseY - shell.prevMouseY)/shell.height])
}
if(shell.scroll[1]) {
camera.zoom(shell.scroll[1] * 0.1)
}
})
```

## Install

npm install orbit-camera

## API

```javascript
var createOrbitCamera = require("orbit-camera")
```

### `var camera = createOrbitCamera(eye, center, up)`
Creates an orbit camera looking at `center`. This has the same semantics as `gluLookAt`

* `eye` is the eye vector of the camera
* `center` is the target the camera is looking at
* `up` is the up direction for the camera

**Returns** A new orbit camera object

### `camera.lookAt(eye, center, up)`
Move the camera to look at the new position.

### `camera.pan(translation)`
Moves the center of the camera by `translation`. Note that translation must be an array of length either 2 or 3

### `camera.rotate(cur, prev)`
Applies a rotation to the camera. `cur` and `prev` are the state of the previous locations. These can be pairs of 2D arrays representing the mouse coordinates in distance relative to the center of the sceen.

### `camera.zoom(delta)`
Zooms in or out by some amount

### `camera.view([out])`
Returns the current view matrix associated to the camera

## Credits
(c) 2013 Mikola Lysenko. MIT License
49 changes: 49 additions & 0 deletions example/example.js
@@ -0,0 +1,49 @@
"use strict"

var shell = require("gl-now")()
var createMesh = require("gl-mesh")
var glm = require("gl-matrix")
var mat4 = glm.mat4
var simple3DShader = require("simple-3d-shader")
var createOrbitCamera = require("../orbit.js")

var camera = createOrbitCamera([0, 10, 20],
[0, 3, 0],
[0, 1, 0])

var shader, mesh

shell.on("gl-init", function() {
shader = simple3DShader(shell.gl)
mesh = createMesh(shell.gl, require("bunny"))
})

shell.on("gl-render", function(t) {
//Bind shader
shader.bind()

//Set camera parameters
var scratch = mat4.create()
shader.uniforms.model = scratch
shader.uniforms.projection = mat4.perspective(scratch, Math.PI/4.0, shell.width/shell.height, 0.1, 1000.0)
shader.uniforms.view = camera.view(scratch)

//Draw object
mesh.bind(shader)
mesh.draw()
mesh.unbind()
})

shell.on("tick", function() {
if(shell.wasDown("mouse-left")) {
camera.rotate([shell.mouseX/shell.width-0.5, shell.mouseY/shell.height-0.5],
[shell.prevMouseX/shell.width-0.5, shell.prevMouseY/shell.height-0.5])
}
if(shell.wasDown("mouse-right")) {
camera.pan([10*(shell.mouseX-shell.prevMouseX)/shell.width,
10*(shell.mouseY - shell.prevMouseY)/shell.height])
}
if(shell.scroll[1]) {
camera.zoom(shell.scroll[1] * 0.1)
}
})
133 changes: 133 additions & 0 deletions orbit.js
@@ -0,0 +1,133 @@
"use strict"

var glm = require("gl-matrix")
var vec3 = glm.vec3
var mat3 = glm.mat3
var mat4 = glm.mat4
var quat = glm.quat


//BEGIN: Missing gl-matrix functions from 2.0.0 (these are in 2.2.0)

function mat3FromMat4(out, a) {
out[0] = a[0]
out[1] = a[1]
out[2] = a[2]
out[3] = a[4]
out[4] = a[5]
out[5] = a[6]
out[6] = a[8]
out[7] = a[9]
out[8] = a[10]
return out
}

function quatFromMat3(out, m) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var fTrace = m[0] + m[4] + m[8]
var fRoot
if ( fTrace > 0.0 ) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0) // 2w
out[3] = 0.5 * fRoot
fRoot = 0.5/fRoot // 1/(4w)
out[0] = (m[7]-m[5])*fRoot
out[1] = (m[2]-m[6])*fRoot
out[2] = (m[3]-m[1])*fRoot
} else {
// |w| <= 1/2
var i = 0
if ( m[4] > m[0] )
i = 1
if ( m[8] > m[i*3+i] )
i = 2
var j = (i+1)%3
var k = (i+2)%3

fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0)
out[i] = 0.5 * fRoot
fRoot = 0.5 / fRoot
out[3] = (m[k*3+j] - m[j*3+k]) * fRoot
out[j] = (m[j*3+i] + m[i*3+j]) * fRoot
out[k] = (m[k*3+i] + m[i*3+k]) * fRoot
}
return out
}

//END MISSING gl-matrix stuff

//Scratch variables
var scratch0 = new Float32Array(16)
var scratch1 = new Float32Array(16)

function OrbitCamera(rotation, center, distance) {
this.rotation = rotation
this.center = center
this.distance = distance
}

var proto = OrbitCamera.prototype

proto.view = function(out) {
if(!out) {
out = mat4.create()
}
scratch1[0] = scratch1[1] = 0.0
scratch1[2] = -this.distance
mat4.fromRotationTranslation(out,
quat.conjugate(scratch0, this.rotation),
scratch1)
mat4.translate(out, out, vec3.negate(scratch0, this.center))
return out
}

proto.lookAt = function(eye, center, up) {
mat4.lookAt(scratch0, eye, center, up)
mat3FromMat4(scratch0, scratch0)
quatFromMat3(this.rotation, scratch0)
vec3.copy(this.center, center)
this.distance = vec3.distance(eye, center)
}

proto.pan = function(dpan) {
scratch0[0] = dpan[0]||0
scratch0[1] = dpan[1]||0
scratch0[2] = dpan[2]||0
vec3.transformQuat(scratch0, scratch0, this.rotation)
vec3.add(this.center, this.center, scratch0)
}

proto.zoom = function(d) {
this.distance += d
}

function quatFromVec(out, da) {
out[0] = -da[0]
out[1] = da[1]
out[2] = da[2] || Math.sqrt(1.0 - da[0]*da[0] - da[1]*da[1])
out[3] = 0.0
}

proto.rotate = function(da, db) {
quatFromVec(scratch0, da)
quatFromVec(scratch1, db)
quat.invert(scratch1, scratch1)
quat.multiply(scratch0, scratch0, scratch1)
if(quat.length(scratch0) < 1e-6) {
return
}
quat.multiply(this.rotation, this.rotation, scratch0)
quat.normalize(this.rotation, this.rotation)
}

function createOrbitCamera(eye, target, up) {
eye = eye || [0,0,-1]
target = target || [0,0,0]
up = up || [0,1,0]
var camera = new OrbitCamera(quat.create(), vec3.create(), 1.0)
camera.lookAt(eye, target, up)
return camera
}

module.exports = createOrbitCamera

0 comments on commit 861a371

Please sign in to comment.