aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/renderUtils.js
blob: e83cb4c05e6f8bf5df3650a4140366d7837d399c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Motorola Mobility LLC nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */

var RDGE = RDGE || {};
RDGE.renderUtils = RDGE.renderUtils || {};

/*
*	Creates an indexed box primitive
*  @return a rdge primitive
*/
RDGE.renderUtils.createBox = function () {
    var renderer = RDGE.globals.engine.getContext().renderer;

    var coords =
		[1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 	// front
				1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 	// right
				1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 	// top
				-1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, // left
				-1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, // bottom
				1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1]; // back

    var normals =
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 	// front
            1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 	// right
			0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 	// top
			-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 	// left
            0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0,     // bottom
			0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1];    // back

    var uvs =
        [1, 1, 0, 1, 0, 0, 1, 0,    // front
			0, 1, 0, 0, 1, 0, 1, 1,    // right
			1, 0, 1, 1, 0, 1, 0, 0,    // top
			1, 1, 0, 1, 0, 0, 1, 0,    // left
			0, 0, 1, 0, 1, 1, 0, 1,    // bottom
			0, 0, 1, 0, 1, 1, 0, 1];   // back

    var indices =
        [0, 1, 2, 0, 2, 3, 	// front
			4, 5, 6, 4, 6, 7, 	// right
			8, 9, 10, 8, 10, 11, 	// top
			12, 13, 14, 12, 14, 15, // left
			16, 17, 18, 16, 18, 19,  // bottom
			20, 21, 22, 20, 22, 23]; // back


    var prim = new RDGE.rdgePrimitiveDefinition();

    prim.vertexDefinition =
            {
                "vert": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_pos": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },

                "normal": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_nrm": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },

                "texcoord": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_uv": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC }
            };

    prim.bufferStreams =
            [
				coords,
				normals,
				uvs
            ];

    prim.streamUsage =
            [
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC
            ];

    prim.indexUsage = RDGE.rdgeConstants.BUFFER_STREAM;
    prim.indexBuffer = indices;

    prim.type = RDGE.rdgeConstants.TRIANGLES;

    renderer.createPrimitive(prim);

    return prim;
};

//
// makeSphere
//
// Create a sphere with the passed number of latitude and longitude bands and the passed radius.
// Sphere has vertices, normals and texCoords. Create VBOs for each as well as the index array.
// Return an object with the following properties:
//
//  normalObject        WebGLBuffer object for normals
//  texCoordObject      WebGLBuffer object for texCoords
//  vertexObject        WebGLBuffer object for vertices
//  indexObject         WebGLBuffer object for indices
//  numIndices          The number of indices in the indexObject
//
RDGE.renderUtils.makeSphere = function (ctx, radius, lats, longs) {
    var geometryData = [];
    var normalData = [];
    var texCoordData = [];
    var indexData = [];

    for (var latNumber = 0; latNumber <= lats; ++latNumber) {
        for (var longNumber = 0; longNumber <= longs; ++longNumber) {
            var theta = latNumber * Math.PI / lats;
            var phi = longNumber * 2 * Math.PI / longs;
            var sinTheta = Math.sin(theta);
            var sinPhi = Math.sin(phi);
            var cosTheta = Math.cos(theta);
            var cosPhi = Math.cos(phi);

            var x = cosPhi * sinTheta;
            var y = cosTheta;
            var z = sinPhi * sinTheta;
            var u = 1 - (longNumber / longs);
            var v = latNumber / lats;

            normalData.push(x);
            normalData.push(y);
            normalData.push(z);
            texCoordData.push(u);
            texCoordData.push(v);
            geometryData.push(radius * x);
            geometryData.push(radius * y);
            geometryData.push(radius * z);
        }
    }

    for (var latNumber = 0; latNumber < lats; ++latNumber) {
        for (var longNumber = 0; longNumber < longs; ++longNumber) {
            var first = (latNumber * (longs + 1)) + longNumber;
            var second = first + longs + 1;
            indexData.push(first);
            indexData.push(second);
            indexData.push(first + 1);

            indexData.push(second);
            indexData.push(second + 1);
            indexData.push(first + 1);
        }
    }

    var retval = {};

    retval.normalObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(normalData), ctx.STATIC_DRAW);

    retval.texCoordObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(texCoordData), ctx.STATIC_DRAW);

    retval.vertexObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
    ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(geometryData), ctx.STATIC_DRAW);

    retval.numIndices = indexData.length;
    retval.indexObject = ctx.createBuffer();
    ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
    ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), ctx.STREAM_DRAW);

    return retval;
};

/*
*	Creates a plane as a grid of triangles/quads, orients the plane according the the plane normal
*	note: the center of the plane is always assumed to be the origin.
*/
RDGE.renderUtils.createPlane = function (numCols, numRows, width, height, uTileCount, vTileCount, planeNormal) {
    var renderer = RDGE.globals.engine.getContext().renderer;

    var pn = [0, 1, 0];
    if (!planeNormal)
        pn = planeNormal;

    var coords = new Array(numCols * numRows * 3);
    var normals = new Array(numCols * numRows * 3);
    var uvs = new Array(numCols * numRows * 2);
    var indices = new Array(numCols * numRows);

    // setup the vertices's in a grid and on the plane
    var coordIdx = 0;
    var uvIdx = 0;

    for (var row = 0; row < numRows; ++row) {
        for (var col = 0; col < numCols; ++col) {
            coords[coordIdx] = col * width - (numCols - 1) * width * 0.5;
            coords[coordIdx + 1] = 0;
            coords[coordIdx + 2] = row * height - (numRows - 1) * height * 0.5;

            normals[coordIdx] = planeNormal[0];
            normals[coordIdx + 1] = planeNormal[1];
            normals[coordIdx + 2] = planeNormal[2];

            uvs[uvIdx] = col / numCols * uTileCount;
            uvs[uvIdx + 1] = row / numRows * vTileCount;

            coordIdx += 3;
            uvIdx += 2;
        }
    }

    // take the grid of vertices's and create triangles
    var k = 0;
    for (var row = 0; row < numRows; ++row) {
        for (var col = 0; col < numCols; ++col) {
            // layout both triangles of the quad
            indices[k + 2] = row * numCols + col;
            indices[k + 1] = row * numCols + (col + 1);
            indices[k] = (row + 1) * numCols + col;

            indices[k + 5] = (row + 1) * numCols + col;
            indices[k + 4] = row * numCols + (col + 1);
            indices[k + 3] = (row + 1) * numCols + (col + 1);

            k += 6;
        }
    }

    // reorient to plane normal

    var prim = new RDGE.rdgePrimitiveDefinition();

    prim.vertexDefinition =
            {
                "vert": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_pos": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },

                "normal": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_nrm": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },

                "texcoord": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_uv": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC }
            };

    prim.bufferStreams =
            [
				coords,
				normals,
				uvs
            ];

    prim.streamUsage =
            [
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC
            ];

    prim.indexUsage = RDGE.rdgeConstants.BUFFER_STREAM;
    prim.indexBuffer = indices;

    prim.type = RDGE.rdgeConstants.TRIANGLES;

    renderer.createPrimitive(prim);

    return prim;
};

// creates a cubic volume of points
RDGE.renderUtils.createCubeVolume = function (numCols_x, numLayers_y, numRows_z, x_interval, y_interval, z_interval, optPointsOut) {
    var renderer = RDGE.globals.engine.getContext().renderer;

    var coords = new Array(numCols_x * numRows_z * numLayers_y * 3);
    var indices = new Array(numCols_x * numRows_z * numLayers_y);

    var layerSize = numCols_x * numRows_z * 3;

    var coordIdx = 0;
    var idx = 0;

    for (var layer = 0; layer < numLayers_y; ++layer) {
        for (var row = 0; row < numRows_z; ++row) {
            for (var col = 0; col < numCols_x; ++col) {
                coords[coordIdx] = col * x_interval - (numCols_x - 1) * x_interval * 0.5;
                coords[coordIdx + 1] = layer * y_interval - (numLayers_y - 1) * y_interval * 0.5;
                coords[coordIdx + 2] = row * z_interval - (numRows_z - 1) * z_interval * 0.5;

                coordIdx += 3;

                indices.push(idx++);
            }
        }
    }

    if (optPointsOut) {
        optPointsOut = coords.slice();
    }

    var prim = new RDGE.rdgePrimitiveDefinition();

    prim.vertexDefinition =
            {
                "a_pos": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC }
            };

    prim.bufferStreams =
            [
				coords
            ];

    prim.streamUsage =
            [
				RDGE.rdgeConstants.BUFFER_DYNAMIC
            ];

    prim.indexUsage = RDGE.rdgeConstants.BUFFER_STREAM;
    prim.indexBuffer = indices;

    prim.type = RDGE.rdgeConstants.POINTS;

    prim.useDoubleBuffer = true;

    renderer.createPrimitive(prim);

    return prim;
};

RDGE.renderUtils.createScreenAlignedQuad = function () {
    var renderer = RDGE.globals.engine.getContext().renderer;

    //  Screen aligned quad
    var coords = [
				  -1.0, 1.0, 0.0,
				  1.0, 1.0, 0.0,
				  -1.0, -1.0, 0.0,

				  -1.0, -1.0, 0.0,
				  1.0, 1.0, 0.0,
				  1.0, -1.0, 0.0
				  ];

    var uvs = [0.0, 0.0,
                0.0, 1.0,
                1.0, 1.0,
                1.0, 1.0,
                1.0, 0.0,
                0.0, 0.0];

    var prim = new RDGE.rdgePrimitiveDefinition();

    prim.vertexDefinition =
            {
                "vert": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_pos": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },

                "texcoord": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_uv": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC }
            };

    prim.bufferStreams =
            [
				coords,
				uvs
            ];

    prim.streamUsage =
            [
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC
            ];

    prim.type = RDGE.rdgeConstants.TRIANGLES;

    renderer.createPrimitive(prim);

    return prim;
};