Skip to content
This repository

A Toolkit for creating Desktop Hardware Accelerated Graphics with JS.

branch: master

Merge pull request #13 from cscott/c58afc4c408d75cc5580d165a2279ff40e…

…4454b6

Another batch of core Gles binding improvements.
latest commit 157c113914
nicolas garcia belmonte authored December 09, 2011
Octocat-spinner-32 examples Add missing gles/lib/matrix.js file. November 29, 2011
Octocat-spinner-32 glbindings Removing handleScopes for Glu(t) and Gl callbacks October 27, 2009
Octocat-spinner-32 glesbindings Accept TypedArray argument to Gles.texImage2D(). December 08, 2011
Octocat-spinner-32 glubindings Removing handleScopes for Glu(t) and Gl callbacks October 27, 2009
Octocat-spinner-32 glutbindings Workaround missing GLUT functions on non-Mac OS X platforms. November 16, 2011
Octocat-spinner-32 v8-typed-array Hook up v8-typed-array to v8-gl. December 08, 2011
Octocat-spinner-32 Makefile Hook up v8-typed-array to v8-gl. December 08, 2011
Octocat-spinner-32 Makefile.Darwin Pull out platform specific Makefile details into seperate files. July 13, 2009
Octocat-spinner-32 Makefile.Linux Pull out platform specific Makefile details into seperate files. July 13, 2009
Octocat-spinner-32 README.mkd Point to Plask. June 21, 2011
Octocat-spinner-32 imageloader.cpp Quiet some compiler warnings about &&/|| precedence. November 16, 2011
Octocat-spinner-32 imageloader.h Added a load function to load js files. Clean up. October 16, 2009
Octocat-spinner-32 main.cpp Added a load function to load js files. Clean up. October 16, 2009
Octocat-spinner-32 utils.cpp Set root path to reflect the current javascript file when using load(). December 08, 2011
Octocat-spinner-32 utils.h Set root path to reflect the current javascript file when using load(). December 08, 2011
Octocat-spinner-32 v8-gl.cpp Add 'read' built-in to get file contents as a string. December 08, 2011
Octocat-spinner-32 v8-gl.h Nicer exception reporting (filename, linenumber, position). December 04, 2011
README.mkd

V8-GL

A JavaScript Toolkit for creating Desktop Hardware Accelerated Graphics with JS.

A V8-GL example code

Note

If you're interested in this project you might also want to check out Plask, a JavaScript multimedia programming environment by Dean McNamee.

Description

V8-GL intends to provide bindings for creating 2D-3D hardware accelerated graphics on the Desktop with JavaScript.

Although OpenGL bindings are included in this library, this project main goal is to provide a more rich and easy-to-use toolkit for making 2D-3D graphics.

I set a few goals for this project, the first of those is to provide complete OpenGL bindings for V8. Other goals will be explained at my blog in due time.

Status

No releases yet. OpenGL 2.1 bindings are 75% complete. The repo already has some functional examples. See the example readme section for a complete working example.

Requirements

  • I use Mac OS X, but you can try to build your examples with Linux or Windows if you want. I guess that the only differences between Mac and Linux might be a couple of lines in the Makefile.
  • OpenGL 2.1+
  • V8 and its requirements

Download

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

$ git clone git://github.com/philogb/v8-gl.git

Then checkout V8 source

$ cd v8-gl
$ svn co http://v8.googlecode.com/svn/trunk/ v8

Build V8

$ cd v8
$ scons mode=release

Make the project

$ cd ..
$ make

You'll probably get some warnings (but no errors), so everything should be ok. Finally, run some example JS code

$ ./v8-gl examples/example2.js

Example

Here's an example of a rotating Icosahedron with some lighting and colors.

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

//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.InitDisplayMode(Glut.DOUBLE | Glut.RGB | Glut.DEPTH);
    Glut.InitWindowSize(400, 400); //Set the window size
    //Create the window
    Glut.CreateWindow("OpenGL on V8 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);
    });
    //Set timeout callback
    Glut.TimerFunc(25, function() {
        angle += 2.0;
        if (angle > 360) angle -= 360;
        Glut.PostRedisplay();
        Glut.TimerFunc(25, arguments.callee, 0);
    }, 0);
    //Start the main loop.
    Glut.MainLoop();
})();

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.
Something went wrong with that request. Please try again.