PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ruhlamat_website/o3djs/lineprimitives.js

http://demoasp.googlecode.com/
JavaScript | 396 lines | 188 code | 37 blank | 171 comment | 15 complexity | 432c90fe320240ec575b9264faa947f4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. /*
  2. * Copyright 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @fileoverview This file contains various functions to help
  33. * create line primitives for o3d applications.
  34. *
  35. * Note: This library is only a sample. It is not meant to be some official
  36. * library. It is provided only as example code.
  37. *
  38. */
  39. o3djs.provide('o3djs.lineprimitives');
  40. o3djs.require('o3djs.math');
  41. o3djs.require('o3djs.primitives');
  42. /**
  43. * Defines a namespace for o3djs.lineprimitives.
  44. * @namespace
  45. */
  46. o3djs.lineprimitives = o3djs.lineprimitives || {};
  47. /**
  48. * A LineVertexInfo is a specialization of VertexInfoBase for line based
  49. * geometry.
  50. * @constructor
  51. * @extends {o3djs.primitives.VertexInfoBase}
  52. */
  53. o3djs.lineprimitives.LineVertexInfo = function() {
  54. o3djs.primitives.VertexInfoBase.call(this);
  55. }
  56. o3djs.base.inherit(o3djs.lineprimitives.LineVertexInfo,
  57. o3djs.primitives.VertexInfoBase);
  58. /**
  59. * Returns the number of lines represented by the LineVertexInfo.
  60. * @return {number} The number of lines represented by LineVertexInfo.
  61. */
  62. o3djs.lineprimitives.LineVertexInfo.prototype.numLines = function() {
  63. return this.indices.length / 2;
  64. };
  65. /**
  66. * Adds a line.
  67. * @param {number} index1 The index of the first vertex of the line.
  68. * @param {number} index2 The index of the second vertex of the line.
  69. */
  70. o3djs.lineprimitives.LineVertexInfo.prototype.addLine = function(
  71. index1, index2) {
  72. this.indices.push(index1, index2);
  73. };
  74. /**
  75. * Gets the vertex indices of the triangle at the given triangle index.
  76. * @param {number} triangleIndex The index of the triangle.
  77. * @return {!Array.<number>} An array of three triangle indices.
  78. */
  79. o3djs.lineprimitives.LineVertexInfo.prototype.getLine = function(
  80. triangleIndex) {
  81. var indexIndex = triangleIndex * 3;
  82. return [this.indices[indexIndex + 0],
  83. this.indices[indexIndex + 1],
  84. this.indices[indexIndex + 2]];
  85. };
  86. /**
  87. * Sets the vertex indices of the line at the given line index.
  88. * @param {number} lineIndex The index of the line.
  89. * @param {number} index1 The index of the first vertex of the line.
  90. * @param {number} index2 The index of the second vertex of the line.
  91. */
  92. o3djs.lineprimitives.LineVertexInfo.prototype.setLine = function(
  93. lineIndex, index1, index2) {
  94. var indexIndex = lineIndex * 2;
  95. this.indices[indexIndex + 0] = index1;
  96. this.indices[indexIndex + 1] = index2;
  97. };
  98. /**
  99. * Creates a shape from a LineVertexInfo
  100. * @param {!o3d.Pack} pack Pack to create objects in.
  101. * @param {!o3d.Material} material to use.
  102. * @return {!o3d.Shape} The created shape.
  103. */
  104. o3djs.lineprimitives.LineVertexInfo.prototype.createShape = function(
  105. pack,
  106. material) {
  107. return this.createShapeByType(
  108. pack, material, o3djs.base.o3d.Primitive.LINELIST);
  109. };
  110. /**
  111. * Creates a new LineVertexInfo.
  112. * @return {!o3djs.lineprimitives.LineVertexInfo} The new LineVertexInfo.
  113. */
  114. o3djs.lineprimitives.createLineVertexInfo = function() {
  115. return new o3djs.lineprimitives.LineVertexInfo();
  116. };
  117. /**
  118. * Creates the vertices and indices for a cube of lines.
  119. * The cube will be created around the origin (-size / 2, size / 2).
  120. * The created cube has a position stream only and can therefore only be used
  121. * with appropriate shaders.
  122. *
  123. * @param {number} size Width, height and depth of the cube.
  124. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  125. * the vertices.
  126. * @return {!o3djs.lineprimitives.LineVertexInfo} The created cube vertices.
  127. */
  128. o3djs.lineprimitives.createLineCubeVertices = function(size, opt_matrix) {
  129. var k = size / 2;
  130. var vertices = [
  131. [-k, -k, -k],
  132. [+k, -k, -k],
  133. [-k, +k, -k],
  134. [+k, +k, -k],
  135. [-k, -k, +k],
  136. [+k, -k, +k],
  137. [-k, +k, +k],
  138. [+k, +k, +k]
  139. ];
  140. var indices = [
  141. [0, 1],
  142. [1, 3],
  143. [3, 2],
  144. [2, 0],
  145. [4, 5],
  146. [5, 7],
  147. [7, 6],
  148. [6, 4],
  149. [0, 4],
  150. [1, 5],
  151. [2, 6],
  152. [3, 7]
  153. ];
  154. var vertexInfo = o3djs.lineprimitives.createLineVertexInfo();
  155. var positionStream = vertexInfo.addStream(
  156. 3, o3djs.base.o3d.Stream.POSITION);
  157. for (var v = 0; v < vertices.length; ++v) {
  158. positionStream.addElementVector(vertices[v]);
  159. }
  160. for (var i = 0; i < indices.length; ++i) {
  161. vertexInfo.addLine(indices[i][0], indices[i][1]);
  162. }
  163. if (opt_matrix) {
  164. vertexInfo.reorient(opt_matrix);
  165. }
  166. return vertexInfo;
  167. };
  168. /**
  169. * Creates a cube of lines.
  170. * The cube will be created around the origin (-size / 2, size / 2).
  171. * The created cube has a position stream only and can therefore only be used
  172. * with appropriate shaders.
  173. *
  174. * @param {!o3d.Pack} pack Pack to create cube elements in.
  175. * @param {!o3d.Material} material Material to use.
  176. * @param {number} size Width, height and depth of the cube.
  177. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  178. * the vertices.
  179. * @return {!o3d.Shape} The created cube.
  180. */
  181. o3djs.lineprimitives.createLineCube = function(
  182. pack,
  183. material,
  184. size,
  185. opt_matrix) {
  186. var vertexInfo =
  187. o3djs.lineprimitives.createLineCubeVertices(size, opt_matrix);
  188. return vertexInfo.createShape(pack, material);
  189. };
  190. /**
  191. * Creates sphere vertices.
  192. * The created sphere has a position stream only and can therefore only be
  193. * used with appropriate shaders.
  194. *
  195. * @param {number} radius radius of the sphere.
  196. * @param {number} subdivisionsAxis number of steps around the sphere.
  197. * @param {number} subdivisionsHeight number of steps vertically on the sphere.
  198. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  199. * the vertices.
  200. * @return {!o3djs.lineprimitives.LineVertexInfo} The created sphere vertices.
  201. */
  202. o3djs.lineprimitives.createLineSphereVertices = function(
  203. radius,
  204. subdivisionsAxis,
  205. subdivisionsHeight,
  206. opt_matrix) {
  207. if (subdivisionsAxis <= 0 || subdivisionsHeight <= 0) {
  208. throw Error('subdivisionAxis and subdivisionHeight must be > 0');
  209. }
  210. // We are going to generate our sphere by iterating through its
  211. // spherical coordinates and generating 1 quad for each quad on a
  212. // ring of the sphere.
  213. var vertexInfo = o3djs.lineprimitives.createLineVertexInfo();
  214. var positionStream = vertexInfo.addStream(
  215. 3, o3djs.base.o3d.Stream.POSITION);
  216. // Generate the individual vertices in our vertex buffer.
  217. for (var y = 0; y <= subdivisionsHeight; y++) {
  218. for (var x = 0; x <= subdivisionsAxis; x++) {
  219. // Generate a vertex based on its spherical coordinates
  220. var u = x / subdivisionsAxis
  221. var v = y / subdivisionsHeight;
  222. var theta = 2 * Math.PI * u;
  223. var phi = Math.PI * v;
  224. var sinTheta = Math.sin(theta);
  225. var cosTheta = Math.cos(theta);
  226. var sinPhi = Math.sin(phi);
  227. var cosPhi = Math.cos(phi);
  228. var ux = cosTheta * sinPhi;
  229. var uy = cosPhi;
  230. var uz = sinTheta * sinPhi;
  231. positionStream.addElement(radius * ux, radius * uy, radius * uz);
  232. }
  233. }
  234. var numVertsAround = subdivisionsAxis + 1;
  235. for (var x = 0; x < subdivisionsAxis; x++) {
  236. for (var y = 0; y < subdivisionsHeight; y++) {
  237. // Make 2 lines per quad.
  238. vertexInfo.addLine(
  239. (y + 0) * numVertsAround + x,
  240. (y + 0) * numVertsAround + x + 1);
  241. vertexInfo.addLine(
  242. (y + 0) * numVertsAround + x,
  243. (y + 1) * numVertsAround + x);
  244. }
  245. }
  246. if (opt_matrix) {
  247. vertexInfo.reorient(opt_matrix);
  248. }
  249. return vertexInfo;
  250. };
  251. /**
  252. * Creates a sphere.
  253. * The created sphere has a position stream only and can therefore only be
  254. * used with appropriate shaders.
  255. *
  256. * @param {!o3d.Pack} pack Pack to create sphere elements in.
  257. * @param {!o3d.Material} material Material to use.
  258. * @param {number} radius radius of the sphere.
  259. * @param {number} subdivisionsAxis number of steps around the sphere.
  260. * @param {number} subdivisionsHeight number of steps vertically on the sphere.
  261. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  262. * the vertices.
  263. * @return {!o3d.Shape} The created sphere.
  264. *
  265. * @see o3d.Pack
  266. * @see o3d.Shape
  267. */
  268. o3djs.lineprimitives.createLineSphere = function(
  269. pack,
  270. material,
  271. radius,
  272. subdivisionsAxis,
  273. subdivisionsHeight,
  274. opt_matrix) {
  275. var vertexInfo = o3djs.lineprimitives.createLineSphereVertices(
  276. radius,
  277. subdivisionsAxis,
  278. subdivisionsHeight,
  279. opt_matrix);
  280. return vertexInfo.createShape(pack, material);
  281. };
  282. /**
  283. * Creates ring vertices.
  284. * The ring is a circle in the XZ plane, centered at the origin.
  285. * The created ring has position, normal, and 1-D texcoord streams.
  286. * The normals point outwards from the center of the ring.
  287. * The texture coordinates are based on angle about the center.
  288. *
  289. * @param {number} radius Radius of the ring.
  290. * @param {number} subdivisions Number of steps around the ring.
  291. * @param {number} maxTexCoord 1-D texture coordinates will range from 0 to
  292. * this value, based on angle about the center.
  293. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  294. * the vertices.
  295. * @return {!o3djs.lineprimitives.LineVertexInfo} The created ring vertices.
  296. */
  297. o3djs.lineprimitives.createLineRingVertices = function(
  298. radius,
  299. subdivisions,
  300. maxTexCoord,
  301. opt_matrix) {
  302. if (subdivisions < 3) {
  303. throw Error('subdivisions must be >= 3');
  304. }
  305. var vertexInfo = o3djs.lineprimitives.createLineVertexInfo();
  306. var positionStream = vertexInfo.addStream(
  307. 3, o3djs.base.o3d.Stream.POSITION);
  308. var normalStream = vertexInfo.addStream(
  309. 3, o3djs.base.o3d.Stream.NORMAL);
  310. var texCoordStream = vertexInfo.addStream(
  311. 1, o3djs.base.o3d.Stream.TEXCOORD, 0);
  312. // Generate the individual vertices in our vertex buffer.
  313. for (var i = 0; i <= subdivisions; i++) {
  314. var theta = 2 * Math.PI * i / subdivisions;
  315. positionStream.addElement(radius * Math.cos(theta), 0,
  316. radius * Math.sin(theta));
  317. normalStream.addElement(Math.cos(theta), 0, Math.sin(theta));
  318. texCoordStream.addElement(maxTexCoord * i / subdivisions);
  319. }
  320. // Connect the vertices by simple lines.
  321. for (var i = 0; i < subdivisions; i++) {
  322. vertexInfo.addLine(i, i+1);
  323. }
  324. if (opt_matrix) {
  325. vertexInfo.reorient(opt_matrix);
  326. }
  327. return vertexInfo;
  328. };
  329. /**
  330. * Creates a ring.
  331. * The ring is a circle in the XZ plane, centered at the origin.
  332. * The created ring has position, normal, and 1-D texcoord streams.
  333. * The normals point outwards from the center of the ring.
  334. * The texture coordinates are based on angle about the center.
  335. *
  336. * @param {!o3d.Pack} pack Pack to create ring elements in.
  337. * @param {!o3d.Material} material Material to use.
  338. * @param {number} radius Radius of the ring.
  339. * @param {number} subdivisions Number of steps around the ring.
  340. * @param {number} maxTexCoord 1-D texture coordinates will range from 0 to
  341. * this value, based on angle about the center.
  342. * @param {!o3djs.math.Matrix4} opt_matrix A matrix by which to multiply all
  343. * the vertices.
  344. * @return {!o3d.Shape} The created ring.
  345. */
  346. o3djs.lineprimitives.createLineRing = function(
  347. pack,
  348. material,
  349. radius,
  350. subdivisions,
  351. maxTexCoord,
  352. opt_matrix) {
  353. var vertexInfo = o3djs.lineprimitives.createLineRingVertices(
  354. radius,
  355. subdivisions,
  356. maxTexCoord,
  357. opt_matrix);
  358. return vertexInfo.createShape(pack, material);
  359. };