Permalink
Browse files

extremely close to having three.js example (http://aerotwist.com/lab/…

  • Loading branch information...
1 parent 6e53b07 commit 80eb5c1137ae7eb567d7d38b3b37a274a401ceb9 @tmpvar committed Aug 30, 2011
Showing with 118 additions and 56 deletions.
  1. +2 −2 example/js/main.cc
  2. +65 −0 example/threejs/cube.js
  3. +6 −4 example/threejs/getting-started.js
  4. +2 −14 example/threejs/three.js
  5. +35 −21 lib/webgl.js
  6. +2 −0 src/arch/wrapper.h
  7. +6 −15 src/js/spidermonkey.cc
View
@@ -404,7 +404,7 @@ int main(int argc, char **argv)
char *script = getFileContents(filename);
/* Create a JS runtime. You always need at least one runtime per process. */
- rt = JS_NewRuntime(8 * 1024 * 1024);
+ rt = JS_NewRuntime(67108864);
if (rt == NULL)
c_exit(EXIT_FAILURE);
@@ -443,7 +443,7 @@ int main(int argc, char **argv)
ok = JS_EvaluateScript(cx, global, script, strlen(script),
filename, lineno, &rval);
- cout << "eval script" << ok << endl;
+ cout << "script result: " << ok << endl;
delete [] script;
if (!ok) {
View
@@ -0,0 +1,65 @@
+var WebGLRenderingContext = require('../lib/webgl.js').WebGLRenderingContext;
+var window = this;
+var $ = function() {};
+console.error = console.log;
+var document = {
+ createElement : function() {
+ return {
+ getContext : function() {
+ return new WebGLRenderingContext();
+ },
+ setTransform : function() {}
+
+ };
+ }
+};
+
+var navigator = {};
+var THREE = require('../example/threejs/three.js');
+var w = 300, h = 300;
+var container, stats;
+var camera, scene, renderer;
+var cube, plane;
+var targetRotation = 0;
+var targetRotationOnMouseDown = 0;
+var mouseX = 0;
+var mouseXOnMouseDown = 0;
+var windowHalfX = 150;
+var windowHalfY = 150;
+
+camera = new THREE.Camera( 70, w / h, 1, 1000 );
+camera.position.y = 150;
+camera.position.z = 500;
+camera.target.position.y = 150;
+
+scene = new THREE.Scene();
+
+
+var sphereMaterial = new THREE.MeshLambertMaterial(
+{
+ color: 0xCC0000
+});
+
+// set up the sphere vars
+var radius = 50, segments = 16, rings = 16;
+
+// create a new mesh with sphere geometry -
+// we will cover the sphereMaterial next!
+var sphere = new THREE.Mesh(
+ new THREE.PlaneGeometry(10, 10, 2, 2),
+ sphereMaterial);
+
+// add the sphere to the scene
+scene.addChild(sphere);
+
+// Cube
+sphere.position.y = 150;
+
+
+renderer = new THREE.WebGLRenderer();
+renderer.setSize( w, h );
+var a = 1000;
+while (a--) {
+ renderer.render(scene, camera);
+ webgl_rendering_context_flush();
+}
@@ -12,7 +12,9 @@ var document = {
};
}
};
-var navigator = {};
+var navigator = {
+ userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1'
+};
var WebGLRenderingContext = require('../lib/webgl.js').WebGLRenderingContext;
@@ -85,14 +87,14 @@ function render() {
}
// draw!
-var a = 1000;
+var a = 30;
while(a--) {
try {
render();
} catch (e) {
console.log(e.stack);
fail();
}
- console.log('-------------------------------------------------------------\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n');
+ webgl_rendering_context_flush();
+ webgl_rendering_context_finish();
}
-console.log("HERE");
View
@@ -8825,7 +8825,7 @@ THREE.WebGLRenderer = function ( parameters ) {
drawCalls: 0
};
-
+ window.programs = _programs;
this.maxMorphTargets = 8;
this.domElement = _canvas;
this.autoClear = true;
@@ -11449,15 +11449,11 @@ console.log("setProgram");
};
function renderBuffer( camera, lights, fog, material, geometryGroup, object ) {
-console.log('renderBUFFER', material.opacity);
if ( material.opacity == 0 ) return;
var program, attributes, linewidth, primitives, a, attribute;
-console.log('renderBUFFER');
program = setProgram( camera, lights, fog, material, object );
-console.log('renderBUFFER');
attributes = program.attributes;
-console.log('renderBUFFER');
// vertices
if ( !material.morphTargets && attributes.position >= 0 ) {
@@ -11569,7 +11565,7 @@ console.log('renderBUFFER');
if ( attributes.uv2 >= 0 ) {
- if ( geometryGroup.__webglUV2Buffer ) {
+ if ( geometryGroup.__webglUV2Buffer && false) {
_gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
_gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
@@ -12366,20 +12362,12 @@ console.log('renderBUFFER');
setObjectFaces( object );
for ( i = 0; i < opaque.count; i ++ ) {
-
material = opaque.list[ i ];
-console.log("HERE");
setDepthTest( material.depthTest );
- console.log("HERE");
setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- console.log("HERE");
renderBuffer( camera, lights, fog, material, buffer, object );
- console.log("HERE");
-
}
-
}
-
}
// opaque pass (immediate simulator)
View
@@ -22,19 +22,22 @@ interface WebGLActiveInfo {
//readonly attribute HTMLCanvasElement canvas;
var e = function(name, result) {
var ex = (new Error()).stack.split('\n')[2];
- console.log("\n");
var e = gl.getError()
if (e) {
console.log(name, '\nFAILURE!', e, ex);
- fail();
+ var ok = [gl.INVALID_VALUE, gl.INVALID_ENUM];
+ if (ok.indexOf(e) > -1) {
+ return result;
+ } else {
+ fail();
+ }
} else if (result === -1) {
console.log(name, '\nFAILURE! result code was -1 (' + ex +')');
- return null;
} else if (result && typeof result !== 'function') {
- console.log(name, '\nOK (' + JSON.stringify(result, null, ' ') + ')');
+ //console.log(name, '\nOK (' + JSON.stringify(result, null, ' ') + ')');
} else {
- console.log(name, '\nOK', (new Error()).stack.split('\n')[2]);
+ //console.log(name, '\nOK', (new Error()).stack.split('\n')[2]);
}
return result;
};
@@ -47,6 +50,19 @@ interface WebGLActiveInfo {
}
};
+ var convertArguments = function(a) {
+ var args = Array.prototype.slice.call(a);
+ args.forEach(function(arg, index) {
+ var type = typeof arg;
+ if (type === 'object') {
+ args[index] = JSON.stringify(arg, null, ' ');
+ } else if (type === 'string') {
+ args[index] = "'" + arg + "'";
+ }
+ });
+ return args;
+ }
+
var methods = [
"activeTexture","attachShader","bindAttribLocation","bindBuffer","bindFramebuffer",
"bindRenderbuffer","bindTexture","blendColor","blendEquation","blendEquationSeparate","blendFunc",
@@ -80,34 +96,26 @@ interface WebGLActiveInfo {
if (!func) {
that.notImplemented++;
func = function() {
- return global['webgl_rendering_context_' + method].apply(this, arguments);
+ return e(method + '(' + convertArguments(arguments).join(', ') + ')', global['webgl_rendering_context_' + method].apply(this, arguments));
}
}
that[method] = function() {
console.log(method)
try {
- var args = Array.prototype.slice.call(arguments);
- args.forEach(function(arg, index) {
- var type = typeof arg;
- if (type === 'object') {
- args[index] = JSON.stringify(arg, null, ' ');
- } else if (type === 'string') {
- args[index] = "'" + arg + "'";
- }
- });
+ var args = convertArguments(arguments);
+
return e(method + '(' + args.join(', ') + ')', func.apply(this, arguments));
} catch (e) {
console.log(method, e);
fail();
}
};
});
- this.getError = function() {
- return 0;
- };
};
WebGLRenderingContext.prototype = {
+ getError : webgl_rendering_context_getError,
+
/* ClearBufferMask */
DEPTH_BUFFER_BIT : 0x00000100,
STENCIL_BUFFER_BIT : 0x00000400,
@@ -584,11 +592,10 @@ WebGLRenderingContext.prototype = {
return program.getAttribLocation(attr);
},
enableVertexAttribArray : function enableVertexAttribArray (attr) {
- webgl_rendering_context_enableVertexAttribArray(attr)
+ webgl_rendering_context_enableVertexAttribArray(attr);
},
disableVertexAttribArray : webgl_rendering_context_disableVertexAttribArray,
vertexAttribPointer : function vertexAttribPointer (attr, size, type, normalized, stride, offset) {
- console.log(attr, size, type, normalized, stride, offset);
webgl_rendering_context_vertexAttribPointer(attr, size, type, normalized, stride, offset);
},
getUniformLocation : function getUniformLocation (program, name) {
@@ -648,6 +655,7 @@ WebGLRenderingContext.prototype = {
drawArrays : function drawArrays (mode, first, count) {
webgl_rendering_context_drawArrays(mode, first, count);
},
+ drawElements : webgl_rendering_context_drawElements,
/***************************
* Buffers *
@@ -658,12 +666,18 @@ WebGLRenderingContext.prototype = {
createBuffer : function createBuffer () {
return new WebGLBuffer();
},
+
+ boundBuffers : null,
bindBuffer : function bindBuffer (target, buffer) {
buffer.bind(target);
},
bufferData : function bufferData (target, array, usage) {
+ if (!array){
+ throw new Error(array + ' is not a valid buffer!');
+ }
+
usage = usage || this.STATIC_DRAW;
- webgl_rendering_context_bufferData(target, array, usage);
+ webgl_rendering_context_bufferData(target, array , usage);
},
/***************************
View
@@ -4,6 +4,8 @@
#include <assert.h>
using namespace std;
+ #define BUFFER_OFFSET(i) ((char*)NULL + (i))
+
#ifdef __APPLE__
#include "../../deps/glfw/include/GL/glfw.h"
#include <OpenGL/OpenGL.h>
View
@@ -150,8 +150,6 @@ JSBool webgl_rendering_context_blendFuncSeparate(JSContext *cx, uintN argc, jsva
JSBool webgl_rendering_context_bufferData(JSContext *cx, uintN argc, jsval *argv) {
-
-
GLenum target;
JSObject *array;
GLenum usage;
@@ -160,11 +158,6 @@ JSBool webgl_rendering_context_bufferData(JSContext *cx, uintN argc, jsval *argv
return JS_FALSE;
}
- // TODO: convert it into a typed array?
- if (!js_IsTypedArray(array)) {
- return JS_FALSE;
- }
-
js::TypedArray *tarray = js::TypedArray::fromJSObject(array);
glBufferData(target, tarray->byteLength, tarray->data, usage);
@@ -415,14 +408,15 @@ JSBool webgl_rendering_context_drawArrays(JSContext *cx, uintN argc, jsval *argv
JSBool webgl_rendering_context_drawElements(JSContext *cx, uintN argc, jsval *argv) {
GLenum mode;
- GLint first;
GLsizei count;
+ GLenum type;
+ GLint offset;
- if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uii", &mode, &first, &count)) {
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uiui", &mode, &count, &type, &offset)) {
return JS_FALSE;
}
- //glDrawElements(mode, first, count);
+ glDrawElements(mode, count, type, (void *)offset);
return JS_TRUE;
}
@@ -517,9 +511,7 @@ JSBool webgl_rendering_context_getAttribLocation(JSContext *cx, uintN argc, jsva
}
const char *attr = JS_EncodeString(cx, js_attr);
-
GLint ret = glGetAttribLocation(program, attr);
- cout << "attrib-:" << attr << " result:" << ret << " " << glGetError() << endl;
JS_free(cx, (void *)attr);
JS_SET_RVAL(cx, argv, INT_TO_JSVAL(ret));
@@ -858,7 +850,7 @@ JSBool webgl_rendering_context_texImage2D(JSContext *cx, uintN argc, jsval *argv
JSBool webgl_rendering_context_texImage2D_Image(JSContext *cx, uintN argc, jsval *argv) {
-
+ cout << "gere" << endl;
GLenum target;
GLint level;
GLint internalFormat;
@@ -1168,8 +1160,7 @@ JSBool webgl_rendering_context_vertexAttribPointer(JSContext *cx, uintN argc, js
GLenum type;
JSBool normalized;
GLsizei stride;
- // TODO: how do you calculate a pointer here?
- int offset;
+ GLint offset;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uiubii", &attr, &size, &type, &normalized, &stride, &offset)) {
return JS_FALSE;

0 comments on commit 80eb5c1

Please sign in to comment.