summaryrefslogtreecommitdiffstats
path: root/o3d/samples/o3djs
diff options
context:
space:
mode:
authorgman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-21 18:10:42 +0000
committergman@google.com <gman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-21 18:10:42 +0000
commit219891e8bc41b05c51a81dc40ff1ed340691f2d7 (patch)
tree8565d1be2e0ba1719b50ce5ad4f93d8eac76ae9d /o3d/samples/o3djs
parentbabaa4e9c446721c92aff4f18e22c9314bc397f9 (diff)
downloadchromium_src-219891e8bc41b05c51a81dc40ff1ed340691f2d7.zip
chromium_src-219891e8bc41b05c51a81dc40ff1ed340691f2d7.tar.gz
chromium_src-219891e8bc41b05c51a81dc40ff1ed340691f2d7.tar.bz2
Add IO functions for Bitmap and change other utilites
to use them. Review URL: http://codereview.chromium.org/174236 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23982 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'o3d/samples/o3djs')
-rw-r--r--o3d/samples/o3djs/io.js124
-rw-r--r--o3d/samples/o3djs/loader.js43
2 files changed, 142 insertions, 25 deletions
diff --git a/o3d/samples/o3djs/io.js b/o3d/samples/o3djs/io.js
index 9d8f302..d101373 100644
--- a/o3d/samples/o3djs/io.js
+++ b/o3d/samples/o3djs/io.js
@@ -520,6 +520,93 @@ o3djs.io.loadArchiveAdvanced = function(pack,
};
/**
+ * Loads RawData.
+ *
+ * RawData is loaded asynchronously.
+ *
+ * @param {!o3d.Pack} pack Pack to create the request in.
+ * @param {string} url URL of raw data to load.
+ * @param {!function(!o3d.FileRequest, o3d.RawData, *): void} callback Callback
+ * when RawData is loaded. It will be passed the FileRequest, a RawData and
+ * an exception on error or null on success. The RawData is associated with
+ * the request so it will stay in memory until you free with request with
+ * pack.removeObject(request).
+ * @return {!o3djs.io.LoadInfo} A LoadInfo to track progress.
+ * @see o3djs.io.loadTexture
+ * @see o3djs.io.loadBitmaps
+ * @see o3djs.loader.createLoader
+ */
+o3djs.io.loadRawData = function(pack, url, callback) {
+ var request = pack.createFileRequest('RAWDATA');
+ var loadInfo = o3djs.io.createLoadInfo(
+ /** @type {!o3d.FileRequest} */ (request),
+ false);
+ request.open('GET', url, true);
+ /**
+ * @ignore
+ */
+ request.onreadystatechange = function() {
+ if (request.done) {
+ var data = request.data;
+ var success = request.success;
+ var exception = request.error;
+ loadInfo.finish();
+ if (!success && !exception) {
+ exception = 'unknown error loading RawData: ' + url;
+ }
+ callback(request, data, success ? null : exception);
+ }
+ };
+ request.send();
+ return loadInfo;
+};
+
+/**
+ * Loads bitmaps.
+ *
+ * Bitmaps are loaded asynchronously.
+ *
+ * Example:
+ * <pre>
+ * var loadInfo = o3djs.io.loadBitamps(pack,
+ * 'http://google.com/someimage.jpg',
+ * callback);
+ *
+ * function callback(bitmaps, exception) {
+ * if (!exception) {
+ * o3djs.texture.createTextureFromBitmaps(g_pack, bitmaps, true);
+ * } else {
+ * alert(exception);
+ * }
+ * }
+ * </pre>
+ *
+ *
+ * @param {!o3d.Pack} pack Pack to load texture into.
+ * @param {string} url URL of image to load.
+ * @param {!function(!Array.<!o3d.Bitmap>, *): void} callback Callback when
+ * image is loaded. It will be passed an array of bitmaps and an exception
+ * on error or null on success.
+ * @param {boolean} opt_generateMips Generate Mips. Default = true.
+ * @return {!o3djs.io.LoadInfo} A LoadInfo to track progress.
+ * @see o3djs.io.loadTexture
+ * @see o3djs.loader.createLoader
+ */
+o3djs.io.loadBitmaps = function(pack, url, callback, opt_generateMips) {
+ if (typeof opt_generateMips === 'undefined') {
+ opt_generateMips = true;
+ }
+ return o3djs.io.loadRawData(pack, url, function(request, rawData, exception) {
+ var bitmaps = [];
+ if (!exception) {
+ bitmaps = pack.createBitmapsFromRawData(rawData);
+ pack.removeObject(request);
+ }
+ callback(bitmaps, exception);
+ });
+};
+
+/**
* Loads a texture.
*
* Textures are loaded asynchronously.
@@ -545,36 +632,23 @@ o3djs.io.loadArchiveAdvanced = function(pack,
* @param {!function(o3d.Texture, *): void} callback Callback when
* texture is loaded. It will be passed the texture and an exception on
* error or null on success.
+ * @param {boolean} opt_generateMips Generate Mips. Default = true.
* @return {!o3djs.io.LoadInfo} A LoadInfo to track progress.
+ * @see o3djs.io.loadBitmaps
* @see o3djs.loader.createLoader
*/
-o3djs.io.loadTexture = function(pack, url, callback) {
- var request = pack.createFileRequest('RAWDATA');
- var loadInfo = o3djs.io.createLoadInfo(
- /** @type {!o3d.FileRequest} */ (request),
- false);
- request.open('GET', url, true);
- /**
- * @ignore
- */
- request.onreadystatechange = function() {
- if (request.done) {
- var rawData = /** @type {!o3d.RawData} */ request.data;
- var success = request.success;
- var exception = request.error;
- var texture = null;
- if (success) {
- texture = o3djs.texture.createTextureFromRawData(pack, rawData, true);
- }
- loadInfo.finish();
+o3djs.io.loadTexture = function(pack, url, callback, opt_generateMips) {
+ function onLoaded(request, rawData, exception) {
+ var texture = null;
+ if (!exception) {
+ texture = o3djs.texture.createTextureFromRawData(
+ pack, rawData, opt_generateMips);
pack.removeObject(request);
- if (!success && !exception) {
- exception = 'unknown error loading texture';
- }
- callback(texture, success ? null : exception);
}
+ callback(texture, exception);
};
- request.send();
- return loadInfo;
+
+ return o3djs.io.loadRawData(pack, url, onLoaded);
};
+
diff --git a/o3d/samples/o3djs/loader.js b/o3d/samples/o3djs/loader.js
index 6a8760b2..fa38e2d 100644
--- a/o3d/samples/o3djs/loader.js
+++ b/o3d/samples/o3djs/loader.js
@@ -111,6 +111,49 @@ o3djs.loader.Loader.prototype.loadTexture = function(pack,
};
/**
+ * Loads a RawData.
+ * @param {!o3d.Pack} pack Pack to load texture into.
+ * @param {string} url URL of image file to load.
+ * @param {!function(!o3d.FileRequest, o3d.RawData, *): void} onLoaded Callback
+ * when RawData is loaded. It will be passed the request, a RawData and an
+ * exception which is null on success. The RawData is associated with
+ * the request so it will stay in memory until you free with request with
+ * pack.removeObject(request).
+ */
+o3djs.loader.Loader.prototype.loadRawData = function(pack,
+ url,
+ onLoaded) {
+ var that = this; // so the function below can see "this".
+ ++this.count_;
+ var loadInfo = o3djs.io.loadRawData(
+ pack, url, function(request, rawData, exception) {
+ onLoaded(request, rawData, exception);
+ that.countDown_();
+ });
+ this.loadInfo.addChild(loadInfo);
+};
+
+/**
+ * Loads bitmaps.
+ * @param {!o3d.Pack} pack Pack to load texture into.
+ * @param {string} url URL of image file to load.
+ * @param {!function(!Array.<!o3d.Bitmap>, *): void} onBitmapsLoaded Callback
+ * when bitmaps are loaded. It will be passed an array of bitmaps and an
+ * exception which is null on success.
+ */
+o3djs.loader.Loader.prototype.loadBitmaps = function(pack,
+ url,
+ onBitmapsLoaded) {
+ var that = this; // so the function below can see "this".
+ ++this.count_;
+ var loadInfo = o3djs.io.loadBitmaps(pack, url, function(bitmaps, exception) {
+ onBitmapsLoaded(bitmaps, exception);
+ that.countDown_();
+ });
+ this.loadInfo.addChild(loadInfo);
+};
+
+/**
* Loads a 3d scene.
* @param {!o3d.Client} client An O3D client object.
* @param {!o3d.Pack} pack Pack to load texture into.