-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create configurable camera #16
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { mat4, quat, vec3 } from 'gl-matrix'; | ||
|
||
/** | ||
* Represents the orientation of the camera in a scene, defined by a position for the camera and a | ||
* target for the camera to look at | ||
*/ | ||
export class Camera { | ||
private static readonly up: vec3 = vec3.fromValues(0, 1, 0); | ||
private static readonly defaultDirection: vec3 = vec3.fromValues(0, 0, -1); | ||
|
||
private position: vec3 = vec3.fromValues(0, 0, 0); | ||
private target: vec3 = vec3.copy(vec3.create(), Camera.defaultDirection); | ||
private transform: mat4 = mat4.create(); | ||
private dirty: boolean = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment for what |
||
|
||
/** | ||
* @param {vec3} position The world-space coordinate to move the camera to, preserving rotation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😍 |
||
*/ | ||
public moveTo(position: vec3) { | ||
const direction = vec3.subtract(vec3.create(), position, this.position); | ||
vec3.add(this.target, this.target, direction); | ||
vec3.copy(this.position, position); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} position The world-space coordinate to move the camera to, keeping the camera | ||
* rotated towards its previous target | ||
*/ | ||
public moveToWithFixedTarget(position: vec3) { | ||
vec3.copy(this.position, position); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} direction A world-space direction vector that will be added to the camera's | ||
* position, preserving its existing rotation | ||
*/ | ||
public moveBy(direction: vec3) { | ||
vec3.add(this.position, this.position, direction); | ||
vec3.add(this.target, this.target, direction); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} direction A world-space direction vector that will be added to the camera's | ||
* position, keeping the camera pointed towards its previous target | ||
*/ | ||
public moveByWithFixedTarget(direction: vec3) { | ||
vec3.add(this.position, this.position, direction); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} target A world-space coordinate that the camera will rotate to face | ||
*/ | ||
public lookAt(target: vec3) { | ||
vec3.copy(this.target, target); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} target A quaternion that will be applied to the camera's current rotation. | ||
*/ | ||
public rotate(rotation: quat) { | ||
const direction = vec3.subtract(vec3.create(), this.target, this.position); | ||
vec3.normalize(direction, direction); | ||
vec3.transformQuat(direction, direction, rotation); | ||
vec3.add(this.target, this.position, direction); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* @param {vec3} target A quaternion that will replace the camera's current rotation. | ||
*/ | ||
public setRotation(rotation: quat) { | ||
const direction = vec3.copy(vec3.create(), Camera.defaultDirection); | ||
vec3.transformQuat(direction, direction, rotation); | ||
vec3.add(this.target, this.position, direction); | ||
this.dirty = true; | ||
} | ||
|
||
/** | ||
* Using the current position and target of the camera, produces a transformation matrix to | ||
* bring world space coordinates into camera space | ||
*/ | ||
public getTransform(): mat4 { | ||
if (this.dirty) { | ||
this.updateTransform(); | ||
} | ||
|
||
return this.transform; | ||
} | ||
|
||
private updateTransform() { | ||
mat4.lookAt(this.transform, this.position, this.target, Camera.up); | ||
this.dirty = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Camera } from '../../src/renderer/Camera'; | ||
import '../glMatrix'; | ||
|
||
import { mat4, quat, vec3 } from 'gl-matrix'; | ||
|
||
describe('Camera', () => { | ||
it('defaults to an identity transformation', () => { | ||
expect(new Camera().getTransform()).toEqualMat4(mat4.create()); | ||
}); | ||
|
||
it('can be moved', () => { | ||
const camera = new Camera(); | ||
camera.lookAt(vec3.fromValues(0, 0, -1)); | ||
camera.moveTo(vec3.fromValues(0, 1, 1)); | ||
expect(mat4.getTranslation(vec3.create(), camera.getTransform())).toEqualVec3( | ||
vec3.fromValues(0, -1, -1) | ||
); | ||
expect(mat4.getRotation(quat.create(), camera.getTransform())).toEqualQuat(quat.create()); | ||
}); | ||
|
||
it('can be moved without changing the target', () => { | ||
const camera = new Camera(); | ||
camera.lookAt(vec3.fromValues(0, 0, -1)); | ||
camera.moveToWithFixedTarget(vec3.fromValues(0, 2, 1)); | ||
expect( | ||
vec3.transformMat4(vec3.create(), vec3.fromValues(0, 0, -1), camera.getTransform()) | ||
).toEqualVec3(vec3.fromValues(0, 0, -Math.sqrt(2) * 2)); | ||
}); | ||
|
||
it('can be moved incrementally', () => { | ||
const camera = new Camera(); | ||
camera.lookAt(vec3.fromValues(0, 0, -1)); | ||
camera.moveTo(vec3.fromValues(0, 1, 0)); | ||
camera.moveBy(vec3.fromValues(0, 0, 1)); | ||
expect(mat4.getTranslation(vec3.create(), camera.getTransform())).toEqualVec3( | ||
vec3.fromValues(0, -1, -1) | ||
); | ||
expect(mat4.getRotation(quat.create(), camera.getTransform())).toEqualQuat(quat.create()); | ||
}); | ||
|
||
it('can be moved incrementally without changing the target', () => { | ||
const camera = new Camera(); | ||
camera.lookAt(vec3.fromValues(0, 0, -1)); | ||
camera.moveToWithFixedTarget(vec3.fromValues(0, 1, 1)); | ||
camera.moveByWithFixedTarget(vec3.fromValues(0, 1, 0)); | ||
expect( | ||
vec3.transformMat4(vec3.create(), vec3.fromValues(0, 0, -1), camera.getTransform()) | ||
).toEqualVec3(vec3.fromValues(0, 0, -Math.sqrt(2) * 2)); | ||
}); | ||
|
||
it('can be rotated', () => { | ||
const camera = new Camera(); | ||
camera.setRotation(quat.fromEuler(quat.create(), 0, 90, 0)); | ||
expect( | ||
vec3.transformMat4(vec3.create(), vec3.fromValues(1, 0, 0), camera.getTransform()) | ||
).toEqualVec3(vec3.fromValues(0, 0, 1)); | ||
}); | ||
|
||
it('can be rotated incrementally', () => { | ||
const camera = new Camera(); | ||
camera.setRotation(quat.fromEuler(quat.create(), 0, 90, 0)); | ||
camera.rotate(quat.fromEuler(quat.create(), 0, 90, 0)); | ||
expect( | ||
vec3.transformMat4(vec3.create(), vec3.fromValues(0, 0, -1), camera.getTransform()) | ||
).toEqualVec3(vec3.fromValues(0, 0, 1)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { mat4, quat, vec3 } from 'gl-matrix'; | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R> { | ||
toEqualMat4(argument: mat4): { pass: boolean; message(): string }; | ||
toEqualVec3(argument: vec3): { pass: boolean; message(): string }; | ||
toEqualQuat(argument: quat): { pass: boolean; message(): string }; | ||
} | ||
} | ||
} | ||
|
||
expect.extend({ | ||
toEqualMat4(received: mat4, argument: mat4) { | ||
if (mat4.equals(received, argument)) { | ||
return { | ||
message: () => | ||
`expected ${mat4.str(received)} to not be equal to ${mat4.str(argument)}`, | ||
pass: true | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${mat4.str(received)} to be equal to ${mat4.str(argument)}`, | ||
pass: false | ||
}; | ||
} | ||
}, | ||
toEqualVec3(received: vec3, argument: vec3) { | ||
if (vec3.equals(received, argument)) { | ||
return { | ||
message: () => | ||
`expected ${vec3.str(received)} to not be equal to ${vec3.str(argument)}`, | ||
pass: true | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${vec3.str(received)} to be equal to ${vec3.str(argument)}`, | ||
pass: false | ||
}; | ||
} | ||
}, | ||
toEqualQuat(received: quat, argument: quat) { | ||
if (quat.equals(received, argument)) { | ||
return { | ||
message: () => | ||
`expected ${quat.str(received)} to not be equal to ${quat.str(argument)}`, | ||
pass: true | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected ${quat.str(received)} to be equal to ${quat.str(argument)}`, | ||
pass: false | ||
}; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a huge nit, but do you think you could use periods at the end of your comments to be consistent with the commenting I have in #11?