diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/renderUtils.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/renderUtils.js | 386 |
1 files changed, 386 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/renderUtils.js b/js/helper-classes/RDGE/src/core/script/renderUtils.js new file mode 100644 index 00000000..baa38560 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/renderUtils.js | |||
@@ -0,0 +1,386 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | renderUtils = {} | ||
8 | |||
9 | /* | ||
10 | * Creates an indexed box primitive | ||
11 | * @return a rdge primitive | ||
12 | */ | ||
13 | renderUtils.createBox = function() | ||
14 | { | ||
15 | var renderer = g_Engine.getContext().renderer; | ||
16 | |||
17 | var coords= | ||
18 | [ 1,1,1, -1,1,1, -1,-1,1, 1,-1,1, // front | ||
19 | 1,1,1, 1,-1,1, 1,-1,-1, 1,1,-1, // right | ||
20 | 1,1,1, 1,1,-1, -1,1,-1, -1,1,1, // top | ||
21 | -1,1,1, -1,1,-1, -1,-1,-1, -1,-1,1, // left | ||
22 | -1,-1,-1, 1,-1,-1, 1,-1,1, -1,-1,1, // bottom | ||
23 | 1,-1,-1, -1,-1,-1, -1,1,-1, 1,1,-1]; // back | ||
24 | |||
25 | var normals= | ||
26 | [ 0,0,1, 0,0,1, 0,0,1, 0,0,1, // front | ||
27 | 1,0,0, 1,0,0, 1,0,0, 1,0,0, // right | ||
28 | 0,1,0, 0,1,0, 0,1,0, 0,1,0, // top | ||
29 | -1,0,0, -1,0,0, -1,0,0, -1,0,0, // left | ||
30 | 0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, // bottom | ||
31 | 0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1]; // back | ||
32 | |||
33 | |||
34 | |||
35 | var uvs= | ||
36 | [ 1,1, 0,1, 0,0, 1,0, // front | ||
37 | 0,1, 0,0, 1,0, 1,1, // right | ||
38 | 1,0, 1,1, 0,1, 0,0, // top | ||
39 | 1,1, 0,1, 0,0, 1,0, // left | ||
40 | 0,0, 1,0, 1,1, 0,1, // bottom | ||
41 | 0,0, 1,0, 1,1, 0,1]; // back | ||
42 | |||
43 | var indices= | ||
44 | [ 0,1,2,0,2,3, // front | ||
45 | 4,5,6,4,6,7, // right | ||
46 | 8,9,10,8,10,11, // top | ||
47 | 12,13,14,12,14,15, // left | ||
48 | 16,17,18,16,18,19, // bottom | ||
49 | 20,21,22,20,22,23]; // back | ||
50 | |||
51 | |||
52 | var prim = new rdgePrimitiveDefinition(); | ||
53 | |||
54 | prim.vertexDefinition= | ||
55 | { | ||
56 | "vert":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, | ||
57 | "a_pos":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, | ||
58 | |||
59 | "normal":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, | ||
60 | "a_nrm":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, | ||
61 | |||
62 | "texcoord":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC}, | ||
63 | "a_uv":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC} | ||
64 | }; | ||
65 | |||
66 | prim.bufferStreams= | ||
67 | [ | ||
68 | coords, | ||
69 | normals, | ||
70 | uvs | ||
71 | ]; | ||
72 | |||
73 | prim.streamUsage= | ||
74 | [ | ||
75 | rdgeConstants.BUFFER_STATIC, | ||
76 | rdgeConstants.BUFFER_STATIC, | ||
77 | rdgeConstants.BUFFER_STATIC | ||
78 | ]; | ||
79 | |||
80 | prim.indexUsage = rdgeConstants.BUFFER_STREAM; | ||
81 | prim.indexBuffer = indices; | ||
82 | |||
83 | prim.type = rdgeConstants.TRIANGLES; | ||
84 | |||
85 | renderer.createPrimitive(prim); | ||
86 | |||
87 | return prim; | ||
88 | } | ||
89 | |||
90 | // | ||
91 | // makeSphere | ||
92 | // | ||
93 | // Create a sphere with the passed number of latitude and longitude bands and the passed radius. | ||
94 | // Sphere has vertices, normals and texCoords. Create VBOs for each as well as the index array. | ||
95 | // Return an object with the following properties: | ||
96 | // | ||
97 | // normalObject WebGLBuffer object for normals | ||
98 | // texCoordObject WebGLBuffer object for texCoords | ||
99 | // vertexObject WebGLBuffer object for vertices | ||
100 | // indexObject WebGLBuffer object for indices | ||
101 | // numIndices The number of indices in the indexObject | ||
102 | // | ||
103 | function makeSphere(ctx, radius, lats, longs) | ||
104 | { | ||
105 | var geometryData = [ ]; | ||
106 | var normalData = [ ]; | ||
107 | var texCoordData = [ ]; | ||
108 | var indexData = [ ]; | ||
109 | |||
110 | for (var latNumber = 0; latNumber <= lats; ++latNumber) { | ||
111 | for (var longNumber = 0; longNumber <= longs; ++longNumber) { | ||
112 | var theta = latNumber * Math.PI / lats; | ||
113 | var phi = longNumber * 2 * Math.PI / longs; | ||
114 | var sinTheta = Math.sin(theta); | ||
115 | var sinPhi = Math.sin(phi); | ||
116 | var cosTheta = Math.cos(theta); | ||
117 | var cosPhi = Math.cos(phi); | ||
118 | |||
119 | var x = cosPhi * sinTheta; | ||
120 | var y = cosTheta; | ||
121 | var z = sinPhi * sinTheta; | ||
122 | var u = 1-(longNumber/longs); | ||
123 | var v = latNumber/lats; | ||
124 | |||
125 | normalData.push(x); | ||
126 | normalData.push(y); | ||
127 | normalData.push(z); | ||
128 | texCoordData.push(u); | ||
129 | texCoordData.push(v); | ||
130 | geometryData.push(radius * x); | ||
131 | geometryData.push(radius * y); | ||
132 | geometryData.push(radius * z); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | for (var latNumber = 0; latNumber < lats; ++latNumber) { | ||
137 | for (var longNumber = 0; longNumber < longs; ++longNumber) { | ||
138 | var first = (latNumber * (longs+1)) + longNumber; | ||
139 | var second = first + longs + 1; | ||
140 | indexData.push(first); | ||
141 | indexData.push(second); | ||
142 | indexData.push(first+1); | ||
143 | |||
144 | indexData.push(second); | ||
145 | indexData.push(second+1); | ||
146 | indexData.push(first+1); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | var retval = { }; | ||
151 | |||
152 | retval.normalObject = ctx.createBuffer(); | ||
153 | ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject); | ||
154 | ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(normalData), ctx.STATIC_DRAW); | ||
155 | |||
156 | retval.texCoordObject = ctx.createBuffer(); | ||
157 | ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject); | ||
158 | ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(texCoordData), ctx.STATIC_DRAW); | ||
159 | |||
160 | retval.vertexObject = ctx.createBuffer(); | ||
161 | ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject); | ||
162 | ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(geometryData), ctx.STATIC_DRAW); | ||
163 | |||
164 | retval.numIndices = indexData.length; | ||
165 | retval.indexObject = ctx.createBuffer(); | ||
166 | ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject); | ||
167 | ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), ctx.STREAM_DRAW); | ||
168 | |||
169 | return retval; | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Creates a plane as a grid of triangles/quads, orients the plane according the the plane normal | ||
174 | * note: the center of the plane is always assumed to be the origin. | ||
175 | */ | ||
176 | function createPlane( numCols, numRows, width, height, uTileCount, vTileCount, planeNormal ) | ||
177 | { | ||
178 | var renderer = g_Engine.getContext().renderer; | ||
179 | |||
180 | var pn = [0, 1, 0]; | ||
181 | if(!planeNormal) | ||
182 | pn = planeNormal; | ||
183 | |||
184 | var coords = new Array( numCols*numRows*3 ); | ||
185 | var normals = new Array( numCols*numRows*3 ); | ||
186 | var uvs = new Array( numCols*numRows*2 ); | ||
187 | var indices = new Array( numCols*numRows ); | ||
188 | |||
189 | // setup the vertices's in a grid and on the plane | ||
190 | var coordIdx = 0; | ||
191 | var uvIdx = 0; | ||
192 | |||
193 | for(var row = 0; row < numRows; ++row) | ||
194 | { | ||
195 | for(var col = 0; col < numCols; ++col) | ||
196 | { | ||
197 | coords[coordIdx] = col*width - (numCols - 1)*width*0.5; | ||
198 | coords[coordIdx + 1] = 0; | ||
199 | coords[coordIdx + 2] = row*height - (numRows - 1)*height*0.5; | ||
200 | |||
201 | normals[coordIdx] = planeNormal[0]; | ||
202 | normals[coordIdx + 1] = planeNormal[1]; | ||
203 | normals[coordIdx + 2] = planeNormal[2]; | ||
204 | |||
205 | uvs[uvIdx] = col/numCols * uTileCount; | ||
206 | uvs[uvIdx+1] = row/numRows * vTileCount; | ||
207 | |||
208 | coordIdx += 3; | ||
209 | uvIdx += 2; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | // take the grid of vertices's and create triangles | ||