Skip to content

Commit

Permalink
Ambiguous Utils changed for V8GLUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
philogb committed Oct 23, 2009
1 parent dcae47c commit 0c07674
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 24 deletions.
101 changes: 97 additions & 4 deletions examples/gles/metaballs/metaballs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
MIT License
*/

var Config = {
gridDim: 64,
shift1: 6,
shift2: 12,
ballCount: 128,

GP_TYPE_META: 0x1,
GP_TYPE_BORDER: 0x2,
GP_TYPE_TEXT: 0x4
};

var MetaBalls = new Class({
balls: null,
grid: null,

initialize: function(count) {
initialize: function(grid) {
this.grid = grid;
var count = Config.ballCount;
var b = this.balls = new Array(count);
for(var i=0; i < count; i++) {
//add metaobject
Expand All @@ -41,13 +55,73 @@ var MetaBalls = new Class({
'b': 0
});
}
},

update: function(from, to) {
var balls = this.balls;
var dim = Config.gridDim;
var points = this.grid.points;
var shift1 = Config.shift1, shift2 = Config.shift2;
var meta = Config.GP_TYPE_META;

while (from < num) {
var ballsFrom = balls[from];
var influenceRange = 1 + (ballsFrom.b >> 0);
var pos = ballsFrom.pos;
var posx = (pos.x >> 0);
var posy = (pos.y >> 0);
var posz = (pos.z >> 0);
for (var z = posz-influenceRange; z < posz+influenceRange; ++z) {
if (z < 0 || z >= dim)
continue;

for (var y = posy-influenceRange; y < posy+influenceRange; ++y) {
if (y < 0 || y >= dim)
continue;

for (var x = posx-influenceRange; x < posx+influenceRange; ++x) {
if (x < 0 || x >= dim)
continue;

var dist = {
'x': pos.x - x,
'y': pos.y - y,
'z': pos.z - z
};
var dist2 = dist.x * dist.x + dist.y * dist.y + dist.z * dist.z;
var range2 = ballsFrom.b * ballsFrom.b;

if (range2 > dist2) {
var gp = points[(z<<shift2) + (y<<shift1) + x];
gp.value += ballsFrom.a *
(1.0f -
0.4444444f * dist2 * dist2 * dist2 / (range2 * range2 * range2) +
1.8888888f * dist2 * dist2 / (range2 * range2) -
2.4444444f * dist2 / range2);

gp.flags |= meta;
}
}
}
}
++from;
}
}
});

var MetaGrid = new Class({
points: null,
points: null,

dim: null,
dimSq: null,
dimCb: null,

initialize: function(count) {
initialize: function() {
var count = Config.gridDim;
this.dim = count;
this.dimSq = count * count;
this.dimCb = count * count * count;

var p = this.points = new Array(count);
for(var i=0; i < count; i++) {
//add gridpoint
Expand All @@ -56,5 +130,24 @@ var MetaGrid = new Class({
'flags': 0
});
}
}
},

move: function(fac) {
var movecount = this.dimCb;
var dimSq = this.dimSq;
var points = this.points;

while (movecount--) {
if (((movecount >> 12) & 0x3f) > 0) {
var p = points[movecount];
var pdiff = points[movecount - dimSq];

p.value = fac * pdiff.value;
p.flags = pdiff.flags;
} else {
points[movecount].value = 0;
}
}
}

});
2 changes: 1 addition & 1 deletion examples/gles/shaders/m3d-vshader.sl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
attribute vec4 pos;

void main() {
gl_Position = vec4(pos.x * float(370), pos.y * float(240), 0.0, 1.0);
gl_Position = vec4(pos.x * float(370), pos.y * float(240), 0.0, 1.0);
}
4 changes: 2 additions & 2 deletions glesbindings/glesbind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ Handle<Value> GLESglTexImage2DFileCallback(const Arguments& args) {
String::Utf8Value value(args[0]);
char* filepath_str = *value;

char* filename = Utils::getRealPath(filepath_str);
char* filename = V8GLUtils::getRealPath(filepath_str);

//take care of relative/absolute paths.
Image* img = loadPNG(filename);
Expand Down Expand Up @@ -1534,7 +1534,7 @@ Handle<Value> GLESglShaderSourceFileCallback(const Arguments& args) {
return v8::Undefined();

char* filepath_str = *filepath_ascii;
char* filename = Utils::getRealPath(filepath_str);
char* filename = V8GLUtils::getRealPath(filepath_str);

std::ifstream in_file(filename);

Expand Down
4 changes: 2 additions & 2 deletions glesbindings/glescustom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ Handle<Value> GLESglTexImage2DFileCallback(const Arguments& args) {
String::Utf8Value value(args[0]);
char* filepath_str = *value;

char* filename = Utils::getRealPath(filepath_str);
char* filename = V8GLUtils::getRealPath(filepath_str);

//take care of relative/absolute paths.
Image* img = loadPNG(filename);
Expand Down Expand Up @@ -1510,7 +1510,7 @@ Handle<Value> GLESglShaderSourceFileCallback(const Arguments& args) {
return v8::Undefined();

char* filepath_str = *filepath_ascii;
char* filename = Utils::getRealPath(filepath_str);
char* filename = V8GLUtils::getRealPath(filepath_str);

std::ifstream in_file(filename);

Expand Down
20 changes: 10 additions & 10 deletions utils.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#include "utils.h"
#include <string>

namespace Utils {
namespace V8GLUtils {
char separator = '/';
char* root_path;

void setRootPath(char* program_path, char* jsfile_path) {
//Set the root_path for opening shader files with
//relative paths
//take path from executable
char* pch = strrchr(program_path, Utils::separator);
char* pch = strrchr(program_path, V8GLUtils::separator);
int last_index = pch - program_path +1;
char* tmp_exec_path = new char[last_index +1];
strncpy(tmp_exec_path, program_path, last_index);
tmp_exec_path[last_index] = '\0';

//take relative path from javascript file
char* p1ch = strrchr(jsfile_path, Utils::separator);
char* p1ch = strrchr(jsfile_path, V8GLUtils::separator);
int last_index1 = p1ch - jsfile_path +1;
char* tmp_js_path = new char[last_index1 +1];
strncpy(tmp_js_path, jsfile_path, last_index1);
tmp_js_path[last_index1] = '\0';

Utils::root_path = new char[last_index + last_index1 +1];
V8GLUtils::root_path = new char[last_index + last_index1 +1];

strcpy(Utils::root_path, tmp_exec_path);
strcat(Utils::root_path, tmp_js_path);
strcpy(V8GLUtils::root_path, tmp_exec_path);
strcat(V8GLUtils::root_path, tmp_js_path);

delete[] tmp_exec_path;
delete[] tmp_js_path;
}

char* getRootPath(void) {
return Utils::root_path;
return V8GLUtils::root_path;
}

char* getRealPath(char* filepath_str) {
//read the file source
char* filename = NULL;
if(filepath_str[0] != Utils::separator) {
filename = new char[strlen(Utils::root_path) + strlen(filepath_str) +1];
strcpy(filename, Utils::root_path);
if(filepath_str[0] != V8GLUtils::separator) {
filename = new char[strlen(V8GLUtils::root_path) + strlen(filepath_str) +1];
strcpy(filename, V8GLUtils::root_path);
strcat(filename, filepath_str);
} else {
filename = new char[strlen(filepath_str) +1];
Expand Down
6 changes: 3 additions & 3 deletions utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef UTILS_H_
#define UTILS_H_
#ifndef V8GLUTILS_H_
#define V8GLUTILS_H_

namespace Utils {
namespace V8GLUtils {
void setRootPath(char* program_path, char* jsfile_path);

char* getRootPath(void);
Expand Down
4 changes: 2 additions & 2 deletions v8-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Handle<Value> load(const Arguments& args) {
//get argument
String::Utf8Value value0(args[i]);
char* arg0 = *value0;
string str(Utils::getRealPath(arg0));
string str(V8GLUtils::getRealPath(arg0));
if(!exec(str)) {
fprintf(stderr, "Error reading '%s'.\n", arg0);
return v8::Undefined();
Expand Down Expand Up @@ -153,7 +153,7 @@ bool V8GL::initialize(int* pargc, char** argv, string scriptname) {
GlesFactory::self_ = Persistent<Object>::New(Gles->NewInstance());

//Set (only once) the absolute path for the .js file being executed.
Utils::setRootPath(argv[0], argv[1]);
V8GLUtils::setRootPath(argv[0], argv[1]);

// Compile and run the script
if (!executeScript(scriptname))
Expand Down

0 comments on commit 0c07674

Please sign in to comment.