Skip to content

paddybyers/node-gl

 
 

Repository files navigation

node-gl

V8-GL is a JavaScript Toolkit for creating Desktop Hardware Accelerated Graphics with JS. It is a javascript binding to the GL and GLUT libraries, together with a launcher that can run GL applications written as javascript programs.

This node-gl project is an early attempt to get that same functionality integrated as a node.js module.

Please see V8-GL for information on that project.

My current thought is that this should migrate to WebGL support but that is TBD right now.

These notes will be expanded as the work develops.

A contributions are welcomed.

example2.js - GL on node

Download

Since there are no releases yet, you can clone the repo from github

$ git clone git://github.com/paddybyers/node-gl.git

There's a build.gyp file there, but no attempt has been made yet to get that to build on anything other than Mac.

You can run some example JS code

$ node examples/example2.js

Limitations

NOTE that so far this simply integrates GLUT, so if Glut.mainLoop() is called, the Glut loop is entered and the node event loop no longer services events. Instead the application can call Glut.checkLoop() which will just execute the body of the Glut event loop once, and will then return to the node event loop. Note also that there needs to be a call to Glut.idleFunc(function(){}) so that checkLoop() returns when the event loop is idle.

To be more node-friendly, this API should really be turned into an EventEmitter.

Example

Its the same example of a rotating Icosahedron with some lighting and colours, unashamedly stolen from V8-GL.

//Add array iteration method
Array.prototype.each = function(f) {
    var len = this.length;
    for ( var i = 0; i < len; i++) f(this[i]);
};

//load module
var Gl = require('node-gl').Gl;
var Glu = require('node-gl').Glu;
var Glut = require('node-gl').Glut;

//Initializes 3D rendering
function initRendering() {
    "DEPTH_TEST COLOR_MATERIAL LIGHTING LIGHT0 NORMALIZE COLOR_MATERIAL"
        .split(" ").each(function(elem) {
        Gl.Enable(Gl[elem]);
    });
}

//global angle variable
var angle = 0;

//Draws the 3D scene
function drawScene() {
    //Set global color and drawing properties
    Gl.Clear(Gl.COLOR_BUFFER_BIT | Gl.DEPTH_BUFFER_BIT);
    Gl.MatrixMode(Gl.MODELVIEW); 
    Gl.LoadIdentity();
    Gl.Translatef(0.0, 0.0, -5.0);
    //Set diffuse and positioned lights
    Gl.LightModelfv(Gl.LIGHT_MODEL_AMBIENT, [0.3, 0.3, 0.3, 1.0]);
    Gl.Lightfv(Gl.LIGHT0, Gl.DIFFUSE, [0.4, 0.4, 0.4, 1.0]);
    Gl.Lightfv(Gl.LIGHT0, Gl.POSITION, [5.0, 5.0, 5.0, 1.0]);
    //Rotate and plot Icosahedron
    Gl.Rotatef(angle, 1.0, 1.0, 1.0);
    Gl.Color3f(0.5, 0.0, 0.8);
    Glut.SolidIcosahedron(2.5);
    //Render
    Glut.SwapBuffers(); 
}

(function() {
    //Initialize Glut
    Glut.Init();
    Glut.idleFunc(function(){});
    Glut.InitDisplayMode(Glut.DOUBLE | Glut.RGB | Glut.DEPTH);
    Glut.InitWindowSize(400, 400); //Set the window size
    //Create the window
    Glut.CreateWindow("OpenGL on node baby!");
    initRendering();
    //Set drawing callback
    Glut.DisplayFunc(drawScene);
    //Set resize window callback
    Glut.ReshapeFunc(function(w, h) {
        var gl = { 'Viewport': [0, 0, w, h], 'MatrixMode': [Gl.PROJECTION], 'LoadIdentity': [] };
        for (var i in gl) Gl[i].apply(this, gl[i]);
        Glu.Perspective(45.0, w / h, 1.0, 200.0);
    });
    //Per-frame callback
    var updateFrame = function() {
        angle += 2.0;
        if (angle > 360) angle -= 360;
        Glut.postRedisplay();
        setTimeout(updateFrame, 25);
        Glut.checkLoop();
    };
    //Start the main loop.
    updateFrame();
})();

License

BSD License.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the organization nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY Nicolas Garcia Belmonte ``AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL Nicolas Garcia Belmonte BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

Integrating the v8-gl binding as a node module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 72.8%
  • C 19.1%
  • JavaScript 4.5%
  • Python 3.6%