Permalink
Browse files

Initial commit. Sample cube animation.

  • Loading branch information...
0 parents commit b5d753c249287572a2220722c6c22611f09339df @hross committed Oct 30, 2011
Showing with 494 additions and 0 deletions.
  1. +13 −0 README.md
  2. +131 −0 examples/animatecube.js
  3. +1 −0 opengl.js
  4. +18 −0 package.json
  5. +274 −0 src/opengl.cc
  6. +33 −0 src/opengl.h
  7. +24 −0 wscript
@@ -0,0 +1,13 @@
+# node-opengl (OpenGL bindings for node.js)
+
+## 0. Notes
+
+This package is a companion to the [node-sdl](https://github.com/creationx/node-sdl) package. It was built so the
+OpenGL bindings for SDL could remain separate from the SDL library.
+
+## 1. Installation
+
+This package depends on node-sdl and OpenGL/SDL libraries being present
+on the target system. The following should get SDL installed on Ubunutu:
+
+<pre>sudo apt-get install libsdl-mixer1.2-dev libsdl-image1.2-dev libsdl-ttf2.0-dev</pre>
@@ -0,0 +1,131 @@
+var SDL = require( '../../node-sdl/sdl' );
+var GL = require('../opengl');
+
+// initialize SDL video components
+SDL.init( SDL.INIT.VIDEO );
+//SDL.IMG.init( 0 );
+
+// set up OpenGL
+SDL.GL.setAttribute(SDL.GL.DOUBLEBUFFER, 1);
+SDL.GL.setAttribute(SDL.GL.DEPTH_SIZE, 16);
+SDL.GL.setAttribute(SDL.GL.RED_SIZE, 8);
+SDL.GL.setAttribute(SDL.GL.GREEN_SIZE, 8);
+SDL.GL.setAttribute(SDL.GL.BLUE_SIZE, 8);
+SDL.GL.setAttribute(SDL.GL.ALPHA_SIZE, 8);
+
+// create a window to draw in
+// SDL.SURFACE.OPENGL | SDL.SURFACE.FULLSCREEN
+var screen = SDL.setVideoMode( 640, 480, 32, SDL.SURFACE.OPENGL);
+
+initialize();
+
+function initialize() {
+ // set up light sources
+ GL.light(GL.LIGHT0, GL.DIFFUSE, [1,0,0,1]);
+ GL.light(GL.LIGHT0, GL.POSITION, [1,1,1,0]);
+ GL.enable(GL.LIGHT0);
+ GL.enable(GL.LIGHTING);
+
+ // use depth buffering for hidden surface elmination
+ GL.enable(GL.DEPTH_TEST);
+
+ // set up the view of the cube
+ GL.matrixMode(GL.PROJECTION);
+ GL.perspective(40, /* field of view in degrees */
+ 1, /*aspect ratio */
+ 1, /* z near */
+ 10); /* z var */
+
+
+ GL.matrixMode(GL.MODELVIEW);
+ GL.lookAt(0, 0, 5, /* eye is at 0,0,5 */
+ 0, 0, 0, /* center is at 0,0,0 */
+ 0, 1, 0); /* up is in +Y direction */
+
+ /* adjust cube position */
+ GL.translate(0, 0, -1);
+ GL.rotate(60, 1, 0, 0);
+ GL.rotate(-20, 0, 0, 1);
+}
+
+function redraw() {
+ // start with a clear screen
+ GL.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
+
+ // draw the box
+ drawBox();
+
+ // swap the buffers
+ SDL.GL.swapBuffers();
+}
+
+
+function drawBox() {
+
+ // rotate the cube each time we draw so it animates
+ GL.rotate(-5, 0, 0, 1);
+
+ GL.begin(GL.QUADS);
+
+ // top
+ GL.vertex3([0, 0, 0]);
+ GL.vertex3([0, 0, -1]);
+ GL.vertex3([-1, 0, -1]);
+ GL.vertex3([-1, 0, 0]);
+
+ // front
+ GL.vertex3([0, 0, 0]);
+ GL.vertex3([-1, 0, 0]);
+ GL.vertex3([-1, -1, 0]);
+ GL.vertex3([0, -1, 0]);
+
+ // right
+ GL.vertex3([0, 0, 0]);
+ GL.vertex3([0, -1, 0]);
+ GL.vertex3([0, -1, -1]);
+ GL.vertex3([0, 0, -1]);
+
+ // left
+ GL.vertex3([-1, 0, 0]);
+ GL.vertex3([-1, 0, -1]);
+ GL.vertex3([-1, -1, -1]);
+ GL.vertex3([-1, -1, 0]);
+
+ // bottom
+ GL.vertex3([0, 0, 0]);
+ GL.vertex3([0, -1, -1]);
+ GL.vertex3([-1, -1, -1]);
+ GL.vertex3([-1, -1, 0]);
+
+ // back
+ GL.vertex3([0, 0, 0]);
+ GL.vertex3([-1, 0, -1]);
+ GL.vertex3([-1, -1, -1]);
+ GL.vertex3([0, -1, -1]);
+
+ GL.end();
+}
+
+// quit
+function quit ( exitCode ) {
+ process.exit( exitCode );
+}
+
+/** SDL key event handlers **/
+SDL.events.on('QUIT', function(evt) {
+ console.log("Quit pushed.");
+ quit(0);
+});
+
+SDL.events.on('KEYDOWN', function (evt) {
+ console.log("Keydown: " + JSON.stringify(evt));
+
+ // Ctrl+Q
+ if (evt.sym && (113 == evt.sym) && evt.mod && (64 == evt.mod)) {
+ quit(0);
+ }
+});
+
+SDL.events.on( 'tick', function ( d ) {
+ redraw();
+});
@@ -0,0 +1 @@
+var GL = module.exports = require('./build/default/node-gl.node');
@@ -0,0 +1,18 @@
+{
+ "author": "Ross Brodbeck <hross@hross.com> (http://hross.net/)",
+ "name": "opengl",
+ "description": "OpenGL bindings for node",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/hross/node-opengl.git"
+ },
+ "main": "opengl.js",
+ "engines": {
+ "node": "0.4"
+ },
+ "dependencies": {
+ "sdl": ">= 0.1.0"
+ },
+ "devDependencies": {}
+}
Oops, something went wrong.

0 comments on commit b5d753c

Please sign in to comment.