Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add three.js and implement the missing methods

  • Loading branch information...
commit b59bfe379744bd955bd8f5c4daf61ccc06b1c680 1 parent 71fc98e
Elijah Insua authored
83 example/threejs/getting-started.js
... ... @@ -0,0 +1,83 @@
  1 +var window = this;
  2 +var $ = function() {};
  3 +console.error = console.log;
  4 +var document = {
  5 + createElement : function() {
  6 + return {
  7 + getContext : function() {
  8 + return new WebGLRenderingContext();
  9 + }
  10 +
  11 + };
  12 + }
  13 +};
  14 +var navigator = {};
  15 +
  16 +var WebGLRenderingContext = require('../lib/webgl.js').WebGLRenderingContext;
  17 +
  18 +var THREE = require('../example/threejs/three.js');
  19 +
  20 +THREE.WebGLRenderer.prototype = {};
  21 +// set the scene size
  22 +var WIDTH = 400,
  23 + HEIGHT = 300;
  24 +
  25 +// set some camera attributes
  26 +var VIEW_ANGLE = 45,
  27 + ASPECT = WIDTH / HEIGHT,
  28 + NEAR = 0.1,
  29 + FAR = 10000;
  30 +
  31 +// get the DOM element to attach to
  32 +// - assume we've got jQuery to hand
  33 +//var $container = $('#container');
  34 +
  35 +// create a WebGL renderer, camera
  36 +// and a scene
  37 +var renderer = new THREE.WebGLRenderer();
  38 +var camera = new THREE.Camera( VIEW_ANGLE,
  39 + ASPECT,
  40 + NEAR,
  41 + FAR );
  42 +var scene = new THREE.Scene();
  43 +
  44 +// the camera starts at 0,0,0 so pull it back
  45 +camera.position.z = 300;
  46 +
  47 +// start the renderer
  48 +renderer.setSize(WIDTH, HEIGHT);
  49 +
  50 +// attach the render-supplied DOM element
  51 +//$container.append(renderer.domElement);
  52 +
  53 +// create the sphere's material
  54 +var sphereMaterial = new THREE.MeshLambertMaterial(
  55 +{
  56 + color: 0xCC0000
  57 +});
  58 +
  59 +// set up the sphere vars
  60 +var radius = 50, segments = 16, rings = 16;
  61 +
  62 +// create a new mesh with sphere geometry -
  63 +// we will cover the sphereMaterial next!
  64 +var sphere = new THREE.Mesh(
  65 + new THREE.SphereGeometry(radius, segments, rings),
  66 + sphereMaterial);
  67 +
  68 +// add the sphere to the scene
  69 +scene.addChild(sphere);
  70 +
  71 +// create a point light
  72 +var pointLight = new THREE.PointLight( 0xFFFFFF );
  73 +
  74 +// set its position
  75 +pointLight.position.x = 10;
  76 +pointLight.position.y = 50;
  77 +pointLight.position.z = 130;
  78 +
  79 +// add to the scene
  80 +scene.addLight(pointLight);
  81 +
  82 +// draw!
  83 +renderer.render(scene, camera);
27,249 example/threejs/three.js
27,249 additions, 0 deletions not shown
194 src/js/spidermonkey.cc
@@ -98,13 +98,27 @@ JSBool webgl_rendering_context_blendColor(JSContext *cx, uintN argc, jsval *argv
98 98 }
99 99
100 100 JSBool webgl_rendering_context_blendEquation(JSContext *cx, uintN argc, jsval *argv) {
101   - JS_ReportError(cx, "method not implemented");
102   - return JS_FALSE;
  101 + GLuint mode;
  102 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &mode)) {
  103 + return JS_FALSE;
  104 + }
  105 +
  106 + glBlendEquation(mode);
  107 +
  108 + return JS_TRUE;
103 109 }
104 110
105 111 JSBool webgl_rendering_context_blendEquationSeparate(JSContext *cx, uintN argc, jsval *argv) {
106   - JS_ReportError(cx, "method not implemented");
107   - return JS_FALSE;
  112 +
  113 + GLuint modeRGB;
  114 + GLenum modeAlpha;
  115 +
  116 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uu", &modeRGB, &modeAlpha)) {
  117 + return JS_FALSE;
  118 + }
  119 +
  120 + glBlendEquationSeparate(modeRGB, modeAlpha);
  121 + return JS_TRUE;
108 122 }
109 123
110 124 JSBool webgl_rendering_context_blendFunc(JSContext *cx, uintN argc, jsval *argv) {
@@ -121,8 +135,17 @@ JSBool webgl_rendering_context_blendFunc(JSContext *cx, uintN argc, jsval *argv)
121 135 }
122 136
123 137 JSBool webgl_rendering_context_blendFuncSeparate(JSContext *cx, uintN argc, jsval *argv) {
124   - JS_ReportError(cx, "method not implemented");
125   - return JS_FALSE;
  138 + GLuint srcRGB;
  139 + GLuint dstRGB;
  140 + GLenum srcAlpha;
  141 + GLenum dstAlpha;
  142 +
  143 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uuuu", &srcRGB, &dstRGB, &srcAlpha, &dstAlpha)) {
  144 + return JS_FALSE;
  145 + }
  146 +
  147 + glBlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
  148 + return JS_TRUE;
126 149 }
127 150
128 151
@@ -281,8 +304,14 @@ JSBool webgl_rendering_context_createTexture(JSContext *cx, uintN argc, jsval *a
281 304
282 305
283 306 JSBool webgl_rendering_context_cullFace(JSContext *cx, uintN argc, jsval *argv) {
284   - JS_ReportError(cx, "method not implemented");
285   - return JS_FALSE;
  307 + GLuint mode;
  308 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &mode)) {
  309 + return JS_FALSE;
  310 + }
  311 +
  312 + glCullFace(mode);
  313 +
  314 + return JS_TRUE;
286 315 }
287 316
288 317
@@ -318,8 +347,15 @@ JSBool webgl_rendering_context_deleteTexture(JSContext *cx, uintN argc, jsval *a
318 347
319 348
320 349 JSBool webgl_rendering_context_depthFunc(JSContext *cx, uintN argc, jsval *argv) {
321   - JS_ReportError(cx, "method not implemented");
322   - return JS_FALSE;
  350 +
  351 + GLuint func;
  352 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &func)) {
  353 + return JS_FALSE;
  354 + }
  355 +
  356 + glDepthFunc(func);
  357 +
  358 + return JS_TRUE;
323 359 }
324 360
325 361 JSBool webgl_rendering_context_depthMask(JSContext *cx, uintN argc, jsval *argv) {
@@ -338,8 +374,15 @@ JSBool webgl_rendering_context_detachShader(JSContext *cx, uintN argc, jsval *ar
338 374 }
339 375
340 376 JSBool webgl_rendering_context_disable(JSContext *cx, uintN argc, jsval *argv) {
341   - JS_ReportError(cx, "method not implemented");
342   - return JS_FALSE;
  377 + GLenum cap;
  378 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &cap)) {
  379 + JS_ReportError(cx, "Error in blendFunc");
  380 + return JS_FALSE;
  381 + }
  382 +
  383 + glDisable(cap);
  384 +
  385 + return JS_TRUE;
343 386 }
344 387
345 388 JSBool webgl_rendering_context_disableVertexAttribArray(JSContext *cx, uintN argc, jsval *argv) {
@@ -432,8 +475,14 @@ JSBool webgl_rendering_context_framebufferTexture2D(JSContext *cx, uintN argc, j
432 475 }
433 476
434 477 JSBool webgl_rendering_context_frontFace(JSContext *cx, uintN argc, jsval *argv) {
435   - JS_ReportError(cx, "method not implemented");
436   - return JS_FALSE;
  478 + GLuint mode;
  479 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &mode)) {
  480 + return JS_FALSE;
  481 + }
  482 +
  483 + glFrontFace(mode);
  484 +
  485 + return JS_TRUE;
437 486 }
438 487
439 488
@@ -477,8 +526,26 @@ JSBool webgl_rendering_context_getAttribLocation(JSContext *cx, uintN argc, jsva
477 526
478 527
479 528 JSBool webgl_rendering_context_getParameter(JSContext *cx, uintN argc, jsval *argv) {
480   - JS_ReportError(cx, "method not implemented");
481   - return JS_FALSE;
  529 +
  530 + GLenum name;
  531 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "u", &name)) {
  532 + return JS_FALSE;
  533 + }
  534 +
  535 + const char *value = (const char *)glGetString(name);
  536 +
  537 + if (value == 0) {
  538 + // DANGER!
  539 + JSString *js_value = JS_NewStringCopyN(cx, "", 0);
  540 + JS_SET_RVAL(cx, argv, STRING_TO_JSVAL(js_value));
  541 +
  542 + } else {
  543 + // DANGER!
  544 + JSString *js_value = JS_NewStringCopyN(cx, value, strlen(value));
  545 + JS_SET_RVAL(cx, argv, STRING_TO_JSVAL(js_value));
  546 + }
  547 +
  548 + return JS_TRUE;
482 549 }
483 550
484 551 JSBool webgl_rendering_context_getBufferParameter(JSContext *cx, uintN argc, jsval *argv) {
@@ -554,8 +621,16 @@ JSBool webgl_rendering_context_getRenderbufferParameter(JSContext *cx, uintN arg
554 621 }
555 622
556 623 JSBool webgl_rendering_context_getShaderParameter(JSContext *cx, uintN argc, jsval *argv) {
557   - JS_ReportError(cx, "method not implemented");
558   - return JS_FALSE;
  624 + GLuint shader;
  625 + GLenum pname;
  626 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uu", &shader, &pname)) {
  627 + return JS_FALSE;
  628 + }
  629 +
  630 + GLint param;
  631 + glGetShaderiv(shader, pname, &param);
  632 + JS_SET_RVAL(cx, argv, INT_TO_JSVAL(param));
  633 + return JS_TRUE;
559 634 }
560 635
561 636 JSBool webgl_rendering_context_getShaderInfoLog(JSContext *cx, uintN argc, jsval *argv) {
@@ -855,8 +930,15 @@ JSBool webgl_rendering_context_texSubImage2D(JSContext *cx, uintN argc, jsval *a
855 930
856 931
857 932 JSBool webgl_rendering_context_uniform1f(JSContext *cx, uintN argc, jsval *argv) {
858   - JS_ReportError(cx, "method not implemented");
859   - return JS_FALSE;
  933 + GLuint location;
  934 + jsdouble x;
  935 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ud", &location, &x)) {
  936 + return JS_FALSE;
  937 + }
  938 +
  939 + glUniform1f(location, x);
  940 +
  941 + return JS_TRUE;
860 942 }
861 943
862 944 JSBool webgl_rendering_context_uniform1fv(JSContext *cx, uintN argc, jsval *argv) {
@@ -882,8 +964,15 @@ JSBool webgl_rendering_context_uniform1iv(JSContext *cx, uintN argc, jsval *argv
882 964 }
883 965
884 966 JSBool webgl_rendering_context_uniform2f(JSContext *cx, uintN argc, jsval *argv) {
885   - JS_ReportError(cx, "method not implemented");
886   - return JS_FALSE;
  967 + GLuint location;
  968 + jsdouble x, y;
  969 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "udd", &location, &x, &y)) {
  970 + return JS_FALSE;
  971 + }
  972 +
  973 + glUniform2f(location, x, y);
  974 +
  975 + return JS_TRUE;
887 976 }
888 977
889 978 JSBool webgl_rendering_context_uniform2fv(JSContext *cx, uintN argc, jsval *argv) {
@@ -902,13 +991,34 @@ JSBool webgl_rendering_context_uniform2iv(JSContext *cx, uintN argc, jsval *argv
902 991 }
903 992
904 993 JSBool webgl_rendering_context_uniform3f(JSContext *cx, uintN argc, jsval *argv) {
905   - JS_ReportError(cx, "method not implemented");
906   - return JS_FALSE;
  994 + GLuint location;
  995 + jsdouble x, y, z;
  996 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uddd", &location, &x, &y, &z)) {
  997 + return JS_FALSE;
  998 + }
  999 +
  1000 + glUniform3f(location, x, y, z);
  1001 +
  1002 + return JS_TRUE;
907 1003 }
908 1004
909 1005 JSBool webgl_rendering_context_uniform3fv(JSContext *cx, uintN argc, jsval *argv) {
910   - JS_ReportError(cx, "method not implemented");
911   - return JS_FALSE;
  1006 + GLenum location;
  1007 + JSObject *array;
  1008 +
  1009 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uo", &location, &array)) {
  1010 + return JS_FALSE;
  1011 + }
  1012 +
  1013 + // TODO: convert it into a typed array?
  1014 + if (!js_IsTypedArray(array)) {
  1015 + return JS_FALSE;
  1016 + }
  1017 +
  1018 + js::TypedArray *tarray = js::TypedArray::fromJSObject(array);
  1019 + glUniform3fv(location, tarray->length, (GLfloat *)tarray->data);
  1020 +
  1021 + return JS_TRUE;
912 1022 }
913 1023
914 1024 JSBool webgl_rendering_context_uniform3i(JSContext *cx, uintN argc, jsval *argv) {
@@ -922,8 +1032,15 @@ JSBool webgl_rendering_context_uniform3iv(JSContext *cx, uintN argc, jsval *argv
922 1032 }
923 1033
924 1034 JSBool webgl_rendering_context_uniform4f(JSContext *cx, uintN argc, jsval *argv) {
925   - JS_ReportError(cx, "method not implemented");
926   - return JS_FALSE;
  1035 + GLuint location;
  1036 + jsdouble x, y, z, w;
  1037 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "uddd", &location, &x, &y, &z, &w)) {
  1038 + return JS_FALSE;
  1039 + }
  1040 +
  1041 + glUniform4f(location, x, y, z, w);
  1042 +
  1043 + return JS_TRUE;
927 1044 }
928 1045
929 1046 JSBool webgl_rendering_context_uniform4fv(JSContext *cx, uintN argc, jsval *argv) {
@@ -953,8 +1070,25 @@ JSBool webgl_rendering_context_uniformMatrix3fv(JSContext *cx, uintN argc, jsval
953 1070 }
954 1071
955 1072 JSBool webgl_rendering_context_uniformMatrix4fv(JSContext *cx, uintN argc, jsval *argv) {
956   - JS_ReportError(cx, "method not implemented");
957   - return JS_FALSE;
  1073 + GLenum location;
  1074 +
  1075 + // TODO: this is currently doing nothing.
  1076 + GLboolean transpose;
  1077 + JSObject *array;
  1078 +
  1079 + if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ubo", &location, &transpose, &array)) {
  1080 + return JS_FALSE;
  1081 + }
  1082 +
  1083 + // TODO: convert it into a typed array?
  1084 + if (!js_IsTypedArray(array)) {
  1085 + return JS_FALSE;
  1086 + }
  1087 +
  1088 + js::TypedArray *tarray = js::TypedArray::fromJSObject(array);
  1089 + glUniform4fv(location, tarray->length, (GLfloat *)tarray->data);
  1090 +
  1091 + return JS_TRUE;
958 1092 }
959 1093
960 1094

0 comments on commit b59bfe3

Please sign in to comment.
Something went wrong with that request. Please try again.