annotate webGL/dandy/resources/utils3d.js~ @ 7:4343c1feedb5

upload boss.js xmlAlpha file
author NOBUYASU Oshiro
date Mon, 05 Jul 2010 17:19:51 +0900
parents 10344afb38a6
children 4684e849866c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
1 /*
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
3 *
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
4 * Redistribution and use in source and binary forms, with or without
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
5 * modification, are permitted provided that the following conditions
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
6 * are met:
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
7 * 1. Redistributions of source code must retain the above copyright
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
8 * notice, this list of conditions and the following disclaimer.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
9 * 2. Redistributions in binary form must reproduce the above copyright
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer in the
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
11 * documentation and/or other materials provided with the distribution.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
12 *
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
24 */
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
25
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
26 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
27 // initWebGL
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
28 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
29 // Initialize the Canvas element with the passed name as a WebGL object and return the
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
30 // WebGLRenderingContext.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
31 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
32 // Load shaders with the passed names and create a program with them. Return this program
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
33 // in the 'program' property of the returned context.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
34 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
35 // For each string in the passed attribs array, bind an attrib with that name at that index.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
36 // Once the attribs are bound, link the program and then use it.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
37 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
38 // Set the clear color to the passed array (4 values) and set the clear depth to the passed value.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
39 // Enable depth testing and blending with a blend func of (SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
40 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
41 // A console function is added to the context: console(string). This can be replaced
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
42 // by the caller. By default, it maps to the window.console() function on WebKit and to
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
43 // an empty function on other browsers.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
44 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
45 function initWebGL(canvasName, vshader, fshader, attribs, clearColor, clearDepth)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
46 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
47 var canvas = document.getElementById(canvasName);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
48 var gl = canvas.getContext("experimental-webgl");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
49 if (!gl) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
50 alert("No WebGL context found");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
51 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
52 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
53
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
54 // Add a console
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
55 gl.console = ("console" in window) ? window.console : { log: function() { } };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
56
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
57 // create our shaders
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
58 var vertexShader = loadShader(gl, vshader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
59 var fragmentShader = loadShader(gl, fshader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
60
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
61 if (!vertexShader || !fragmentShader)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
62 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
63
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
64 // Create the program object
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
65 gl.program = gl.createProgram();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
66
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
67 if (!gl.program)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
68 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
69
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
70 // Attach our two shaders to the program
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
71 gl.attachShader (gl.program, vertexShader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
72 gl.attachShader (gl.program, fragmentShader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
73
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
74 // Bind attributes
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
75 for (var i in attribs)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
76 gl.bindAttribLocation (gl.program, i, attribs[i]);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
77
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
78 // Link the program
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
79 gl.linkProgram(gl.program);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
80
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
81 // Check the link status
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
82 var linked = gl.getProgramParameter(gl.program, gl.LINK_STATUS);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
83 if (!linked) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
84 // something went wrong with the link
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
85 var error = gl.getProgramInfoLog (gl.program);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
86 gl.console.log("Error in program linking:"+error);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
87
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
88 gl.deleteProgram(gl.program);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
89 gl.deleteProgram(fragmentShader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
90 gl.deleteProgram(vertexShader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
91
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
92 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
93 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
94
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
95 gl.useProgram(gl.program);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
96
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
97 gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
98 gl.clearDepth(clearDepth);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
99
7
4343c1feedb5 upload boss.js xmlAlpha file
NOBUYASU Oshiro
parents: 3
diff changeset
100 // gl.enable(gl.DEPTH_TEST);
3
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
101 gl.enable(gl.BLEND);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
102 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
103
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
104 return gl;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
105 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
106
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
107 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
108 // loadShader
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
109 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
110 // 'shaderId' is the id of a <script> element containing the shader source string.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
111 // Load this shader and return the WebGLShader object corresponding to it.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
112 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
113 function loadShader(ctx, shaderId)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
114 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
115 var shaderScript = document.getElementById(shaderId);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
116 if (!shaderScript) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
117 ctx.console.log("*** Error: shader script '"+shaderId+"' not found");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
118 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
119 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
120
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
121 if (shaderScript.type == "x-shader/x-vertex")
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
122 var shaderType = ctx.VERTEX_SHADER;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
123 else if (shaderScript.type == "x-shader/x-fragment")
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
124 var shaderType = ctx.FRAGMENT_SHADER;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
125 else {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
126 ctx.console.log("*** Error: shader script '"+shaderId+"' of undefined type '"+shaderScript.type+"'");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
127 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
128 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
129
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
130 // Create the shader object
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
131 var shader = ctx.createShader(shaderType);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
132 if (shader == null) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
133 ctx.console.log("*** Error: unable to create shader '"+shaderId+"'");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
134 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
135 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
136
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
137 // Load the shader source
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
138 ctx.shaderSource(shader, shaderScript.text);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
139
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
140 // Compile the shader
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
141 ctx.compileShader(shader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
142
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
143 // Check the compile status
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
144 var compiled = ctx.getShaderParameter(shader, ctx.COMPILE_STATUS);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
145 if (!compiled) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
146 // Something went wrong during compilation; get the error
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
147 var error = ctx.getShaderInfoLog(shader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
148 ctx.console.log("*** Error compiling shader '"+shaderId+"':"+error);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
149 ctx.deleteShader(shader);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
150 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
151 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
152
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
153 return shader;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
154 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
155
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
156 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
157 // makeBox
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
158 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
159 // Create a box with vertices, normals and texCoords. Create VBOs for each as well as the index array.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
160 // Return an object with the following properties:
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
161 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
162 // normalObject WebGLBuffer object for normals
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
163 // texCoordObject WebGLBuffer object for texCoords
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
164 // vertexObject WebGLBuffer object for vertices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
165 // indexObject WebGLBuffer object for indices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
166 // numIndices The number of indices in the indexObject
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
167 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
168 function makeBox(ctx)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
169 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
170 // box
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
171 // v6----- v5
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
172 // /| /|
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
173 // v1------v0|
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
174 // | | | |
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
175 // | |v7---|-|v4
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
176 // |/ |/
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
177 // v2------v3
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
178 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
179 // vertex coords array
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
180 var vertices = new WebGLFloatArray(
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
181 [ 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0-v1-v2-v3 front
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
182 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0-v3-v4-v5 right
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
183 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0-v5-v6-v1 top
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
184 -1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1-v6-v7-v2 left
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
185 -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7-v4-v3-v2 bottom
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
186 1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 ] // v4-v7-v6-v5 back
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
187 );
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
188
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
189 // normal array
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
190 var normals = new WebGLFloatArray(
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
191 [ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2-v3 front
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
192 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
193 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
194 -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7-v2 left
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
195 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3-v2 bottom
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
196 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 ] // v4-v7-v6-v5 back
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
197 );
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
198
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
199
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
200 // texCoord array
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
201 var texCoords = new WebGLFloatArray(
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
202 [ 1, 1, 0, 1, 0, 0, 1, 0, // v0-v1-v2-v3 front
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
203 0, 1, 0, 0, 1, 0, 1, 1, // v0-v3-v4-v5 right
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
204 1, 0, 1, 1, 0, 1, 0, 0, // v0-v5-v6-v1 top
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
205 1, 1, 0, 1, 0, 0, 1, 0, // v1-v6-v7-v2 left
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
206 0, 0, 1, 0, 1, 1, 0, 1, // v7-v4-v3-v2 bottom
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
207 0, 0, 1, 0, 1, 1, 0, 1 ] // v4-v7-v6-v5 back
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
208 );
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
209
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
210 // index array
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
211 var indices = new WebGLUnsignedByteArray(
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
212 [ 0, 1, 2, 0, 2, 3, // front
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
213 4, 5, 6, 4, 6, 7, // right
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
214 8, 9,10, 8,10,11, // top
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
215 12,13,14, 12,14,15, // left
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
216 16,17,18, 16,18,19, // bottom
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
217 20,21,22, 20,22,23 ] // back
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
218 );
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
219
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
220 var retval = { };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
221
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
222 retval.normalObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
223 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
224 ctx.bufferData(ctx.ARRAY_BUFFER, normals, ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
225
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
226 retval.texCoordObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
227 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
228 ctx.bufferData(ctx.ARRAY_BUFFER, texCoords, ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
229
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
230 retval.vertexObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
231 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
232 ctx.bufferData(ctx.ARRAY_BUFFER, vertices, ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
233
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
234 ctx.bindBuffer(ctx.ARRAY_BUFFER, null);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
235
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
236 retval.indexObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
237 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
238 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, indices, ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
239 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, null);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
240
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
241 retval.numIndices = indices.length;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
242
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
243 return retval;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
244 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
245
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
246 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
247 // makeSphere
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
248 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
249 // Create a sphere with the passed number of latitude and longitude bands and the passed radius.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
250 // Sphere has vertices, normals and texCoords. Create VBOs for each as well as the index array.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
251 // Return an object with the following properties:
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
252 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
253 // normalObject WebGLBuffer object for normals
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
254 // texCoordObject WebGLBuffer object for texCoords
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
255 // vertexObject WebGLBuffer object for vertices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
256 // indexObject WebGLBuffer object for indices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
257 // numIndices The number of indices in the indexObject
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
258 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
259 function makeSphere(ctx, radius, lats, longs)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
260 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
261 var geometryData = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
262 var normalData = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
263 var texCoordData = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
264 var indexData = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
265
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
266 for (var latNumber = 0; latNumber <= lats; ++latNumber) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
267 for (var longNumber = 0; longNumber <= longs; ++longNumber) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
268 var theta = latNumber * Math.PI / lats;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
269 var phi = longNumber * 2 * Math.PI / longs;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
270 var sinTheta = Math.sin(theta);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
271 var sinPhi = Math.sin(phi);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
272 var cosTheta = Math.cos(theta);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
273 var cosPhi = Math.cos(phi);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
274
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
275 var x = cosPhi * sinTheta;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
276 var y = cosTheta;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
277 var z = sinPhi * sinTheta;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
278 var u = 1-(longNumber/longs);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
279 var v = latNumber/lats;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
280
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
281 normalData.push(x);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
282 normalData.push(y);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
283 normalData.push(z);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
284 texCoordData.push(u);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
285 texCoordData.push(v);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
286 geometryData.push(radius * x);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
287 geometryData.push(radius * y);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
288 geometryData.push(radius * z);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
289 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
290 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
291
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
292 for (var latNumber = 0; latNumber < lats; ++latNumber) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
293 for (var longNumber = 0; longNumber < longs; ++longNumber) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
294 var first = (latNumber * (longs+1)) + longNumber;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
295 var second = first + longs + 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
296 indexData.push(first);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
297 indexData.push(second);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
298 indexData.push(first+1);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
299
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
300 indexData.push(second);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
301 indexData.push(second+1);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
302 indexData.push(first+1);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
303 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
304 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
305
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
306 var retval = { };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
307
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
308 retval.normalObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
309 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
310 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(normalData), ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
311
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
312 retval.texCoordObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
313 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
314 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(texCoordData), ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
315
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
316 retval.vertexObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
317 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
318 ctx.bufferData(ctx.ARRAY_BUFFER, new WebGLFloatArray(geometryData), ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
319
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
320 retval.numIndices = indexData.length;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
321 retval.indexObject = ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
322 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
323 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(indexData), ctx.STREAM_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
324
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
325 return retval;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
326 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
327
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
328 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
329 // loadObj
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
330 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
331 // Load a .obj file from the passed URL. Return an object with a 'loaded' property set to false.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
332 // When the object load is complete, the 'loaded' property becomes true and the following
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
333 // properties are set:
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
334 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
335 // normalObject WebGLBuffer object for normals
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
336 // texCoordObject WebGLBuffer object for texCoords
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
337 // vertexObject WebGLBuffer object for vertices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
338 // indexObject WebGLBuffer object for indices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
339 // numIndices The number of indices in the indexObject
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
340 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
341 function loadObj(ctx, url)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
342 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
343 var obj = { loaded : false };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
344 obj.ctx = ctx;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
345 var req = new XMLHttpRequest();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
346 req.obj = obj;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
347 req.onreadystatechange = function () { processLoadObj(req) };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
348 req.open("GET", url, true);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
349 req.send(null);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
350 return obj;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
351 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
352
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
353 function processLoadObj(req)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
354 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
355 req.obj.ctx.console.log("req="+req)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
356 // only if req shows "complete"
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
357 if (req.readyState == 4) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
358 doLoadObj(req.obj, req.responseText);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
359 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
360 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
361
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
362 function doLoadObj(obj, text)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
363 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
364 vertexArray = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
365 normalArray = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
366 textureArray = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
367 indexArray = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
368
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
369 var vertex = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
370 var normal = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
371 var texture = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
372 var facemap = { };
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
373 var index = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
374
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
375 var lines = text.split("\n");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
376 for (var lineIndex in lines) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
377 var line = lines[lineIndex].replace(/[ \t]+/g, " ").replace(/\s\s*$/, "");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
378
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
379 // ignore comments
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
380 if (line[0] == "#")
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
381 continue;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
382
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
383 var array = line.split(" ");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
384 if (array[0] == "v") {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
385 // vertex
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
386 vertex.push(parseFloat(array[1]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
387 vertex.push(parseFloat(array[2]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
388 vertex.push(parseFloat(array[3]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
389 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
390 else if (array[0] == "vt") {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
391 // normal
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
392 texture.push(parseFloat(array[1]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
393 texture.push(parseFloat(array[2]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
394 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
395 else if (array[0] == "vn") {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
396 // normal
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
397 normal.push(parseFloat(array[1]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
398 normal.push(parseFloat(array[2]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
399 normal.push(parseFloat(array[3]));
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
400 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
401 else if (array[0] == "f") {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
402 // face
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
403 if (array.length != 4) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
404 obj.ctx.console.log("*** Error: face '"+line+"' not handled");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
405 continue;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
406 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
407
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
408 for (var i = 1; i < 4; ++i) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
409 if (!(array[i] in facemap)) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
410 // add a new entry to the map and arrays
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
411 var f = array[i].split("/");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
412 var vtx, nor, tex;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
413
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
414 if (f.length == 1) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
415 vtx = parseInt(f[0]) - 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
416 nor = vtx;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
417 tex = vtx;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
418 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
419 else if (f.length = 3) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
420 vtx = parseInt(f[0]) - 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
421 tex = parseInt(f[1]) - 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
422 nor = parseInt(f[2]) - 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
423 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
424 else {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
425 obj.ctx.console.log("*** Error: did not understand face '"+array[i]+"'");
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
426 return null;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
427 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
428
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
429 // do the vertices
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
430 var x = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
431 var y = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
432 var z = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
433 if (vtx * 3 + 2 < vertex.length) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
434 x = vertex[vtx*3];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
435 y = vertex[vtx*3+1];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
436 z = vertex[vtx*3+2];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
437 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
438 vertexArray.push(x);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
439 vertexArray.push(y);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
440 vertexArray.push(z);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
441
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
442 // do the textures
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
443 x = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
444 y = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
445 if (tex * 2 + 1 < texture.length) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
446 x = texture[tex*2];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
447 y = texture[tex*2+1];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
448 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
449 textureArray.push(x);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
450 textureArray.push(y);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
451
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
452 // do the normals
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
453 x = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
454 y = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
455 z = 1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
456 if (nor * 3 + 2 < normal.length) {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
457 x = normal[nor*3];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
458 y = normal[nor*3+1];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
459 z = normal[nor*3+2];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
460 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
461 normalArray.push(x);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
462 normalArray.push(y);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
463 normalArray.push(z);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
464
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
465 facemap[array[i]] = index++;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
466 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
467
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
468 indexArray.push(facemap[array[i]]);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
469 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
470 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
471 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
472
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
473 // set the VBOs
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
474 obj.normalObject = obj.ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
475 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.normalObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
476 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new WebGLFloatArray(normalArray), obj.ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
477
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
478 obj.texCoordObject = obj.ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
479 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.texCoordObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
480 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new WebGLFloatArray(textureArray), obj.ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
481
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
482 obj.vertexObject = obj.ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
483 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.vertexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
484 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new WebGLFloatArray(vertexArray), obj.ctx.STATIC_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
485
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
486 obj.numIndices = indexArray.length;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
487 obj.indexObject = obj.ctx.createBuffer();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
488 obj.ctx.bindBuffer(obj.ctx.ELEMENT_ARRAY_BUFFER, obj.indexObject);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
489 obj.ctx.bufferData(obj.ctx.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(indexArray), obj.ctx.STREAM_DRAW);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
490
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
491 obj.loaded = true;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
492 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
493
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
494 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
495 // loadImageTexture
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
496 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
497 // Load the image at the passed url, place it in a new WebGLTexture object and return the WebGLTexture.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
498 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
499 function loadImageTexture(ctx, url)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
500 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
501 var texture = ctx.createTexture();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
502 texture.image = new Image();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
503 texture.image.onload = function() { doLoadImageTexture(ctx, texture.image, texture) }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
504 texture.image.src = url;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
505 // alert("texture.image.src:"+texture.image.src);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
506 return texture;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
507 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
508
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
509 function doLoadImageTexture(ctx, image, texture)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
510 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
511 ctx.bindTexture(ctx.TEXTURE_2D, texture);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
512 ctx.texImage2D(ctx.TEXTURE_2D, 0, image);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
513 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MAG_FILTER, ctx.LINEAR);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
514 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MIN_FILTER, ctx.LINEAR);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
515 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_S, ctx.CLAMP_TO_EDGE);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
516 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_T, ctx.CLAMP_TO_EDGE);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
517 //ctx.generateMipmap(ctx.TEXTURE_2D)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
518 ctx.bindTexture(ctx.TEXTURE_2D, null);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
519 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
520
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
521 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
522 // Framerate object
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
523 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
524 // This object keeps track of framerate and displays it as the innerHTML text of the
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
525 // HTML element with the passed id. Once created you call snapshot at the end
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
526 // of every rendering cycle. Every 500ms the framerate is updated in the HTML element.
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
527 //
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
528 Framerate = function(id)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
529 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
530 this.numFramerates = 10;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
531 this.framerateUpdateInterval = 500;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
532 this.id = id;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
533
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
534 this.renderTime = -1;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
535 this.framerates = [ ];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
536 self = this;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
537 var fr = function() { self.updateFramerate() }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
538 setInterval(fr, this.framerateUpdateInterval);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
539 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
540
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
541 Framerate.prototype.updateFramerate = function()
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
542 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
543 var tot = 0;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
544 for (var i = 0; i < this.framerates.length; ++i)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
545 tot += this.framerates[i];
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
546
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
547 var framerate = tot / this.framerates.length;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
548 framerate = Math.round(framerate);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
549 document.getElementById(this.id).innerHTML = "Framerate:"+framerate+"fps";
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
550 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
551
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
552 Framerate.prototype.snapshot = function()
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
553 {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
554 if (this.renderTime < 0)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
555 this.renderTime = new Date().getTime();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
556 else {
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
557 var newTime = new Date().getTime();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
558 var t = newTime - this.renderTime;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
559 var framerate = 1000/t;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
560 this.framerates.push(framerate);
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
561 while (this.framerates.length > this.numFramerates)
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
562 this.framerates.shift();
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
563 this.renderTime = newTime;
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
564 }
10344afb38a6 upload dandy
NOBUYASU Oshiro
parents:
diff changeset
565 }