OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CubeModel.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "CubeModel.h"
22 
23 #include "Platform.h"
24 
25 #include <cstdlib>
26 
27 namespace MaliSDK
28 {
29  void CubeModel::getTriangleRepresentation(float scalingFactor, int* numberOfCoordinates, float** coordinates)
30  {
31  if (coordinates == NULL)
32  {
33  LOGE("Cannot use null pointer while calculating coordinates.");
34  return;
35  }
36 
37  /* 6 faces, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
38  const int numberOfCubeTriangleCoordinates = 6 * 2 * 3 * 3;
39  /* Index of an array we will put new point coordinates at. */
40  int currentIndex = 0;
41 
42  /* Allocate memory for result array. */
43  *coordinates = (float*) malloc (numberOfCubeTriangleCoordinates * sizeof(float));
44 
45  /* Is allocation successful?. */
46  if (*coordinates == NULL)
47  {
48  LOGE("Could not allocate memory for result array.");
49 
50  return;
51  }
52 
53  /* Example:
54  * Coordinates for cube points:
55  * A -1.0f, 1.0f, 1.0f
56  * B -1.0f, 1.0f, -1.0f
57  * C 1.0f, 1.0f, -1.0f
58  * D 1.0f, 1.0f, 1.0f
59  * E -1.0f, -1.0f, 1.0f
60  * F -1.0f, -1.0f, -1.0f
61  * G 1.0f, -1.0f, -1.0f
62  * H 1.0f, -1.0f, 1.0f
63  * Create 2 triangles for each face of the cube. Vertices are written in clockwise order.
64  * B ________ C
65  * / | / |
66  * A ......... D |
67  * . | . |
68  * . F|_ _.___ |G
69  * . / . /
70  * E ......... H
71  */
72 
73  /* Fill the array with coordinates. */
74  /* Top face. */
75  /*A*/
76  (*coordinates)[currentIndex] = -1.0f;
77  currentIndex++;
78  (*coordinates)[currentIndex] = 1.0f;
79  currentIndex++;
80  (*coordinates)[currentIndex] = 1.0f;
81  currentIndex++;
82  /*B*/
83  (*coordinates)[currentIndex] = -1.0f;
84  currentIndex++;
85  (*coordinates)[currentIndex] = 1.0f;
86  currentIndex++;
87  (*coordinates)[currentIndex] = -1.0f;
88  currentIndex++;
89  /*C*/
90  (*coordinates)[currentIndex] = 1.0f;
91  currentIndex++;
92  (*coordinates)[currentIndex] = 1.0f;
93  currentIndex++;
94  (*coordinates)[currentIndex] = -1.0f;
95  currentIndex++;
96 
97  /*A*/
98  (*coordinates)[currentIndex] = -1.0f;
99  currentIndex++;
100  (*coordinates)[currentIndex] = 1.0f;
101  currentIndex++;
102  (*coordinates)[currentIndex] = 1.0f;
103  currentIndex++;
104  /*C*/
105  (*coordinates)[currentIndex] = 1.0f;
106  currentIndex++;
107  (*coordinates)[currentIndex] = 1.0f;
108  currentIndex++;
109  (*coordinates)[currentIndex] = -1.0f;
110  currentIndex++;
111  /*D*/
112  (*coordinates)[currentIndex] = 1.0f;
113  currentIndex++;
114  (*coordinates)[currentIndex] = 1.0f;
115  currentIndex++;
116  (*coordinates)[currentIndex] = 1.0f;
117  currentIndex++;
118 
119  /* Bottom face. */
120  /*E*/
121  (*coordinates)[currentIndex] = -1.0f;
122  currentIndex++;
123  (*coordinates)[currentIndex] = -1.0f;
124  currentIndex++;
125  (*coordinates)[currentIndex] = 1.0f;
126  currentIndex++;
127  /*F*/
128  (*coordinates)[currentIndex] = -1.0f;
129  currentIndex++;
130  (*coordinates)[currentIndex] = -1.0f;
131  currentIndex++;
132  (*coordinates)[currentIndex] = -1.0f;
133  currentIndex++;
134  /*G*/
135  (*coordinates)[currentIndex] = 1.0f;
136  currentIndex++;
137  (*coordinates)[currentIndex] = -1.0f;
138  currentIndex++;
139  (*coordinates)[currentIndex] = -1.0f;
140  currentIndex++;
141 
142  /*E*/
143  (*coordinates)[currentIndex] = -1.0f;
144  currentIndex++;
145  (*coordinates)[currentIndex] = -1.0f;
146  currentIndex++;
147  (*coordinates)[currentIndex] = 1.0f;
148  currentIndex++;
149  /*G*/
150  (*coordinates)[currentIndex] = 1.0f;
151  currentIndex++;
152  (*coordinates)[currentIndex] = -1.0f;
153  currentIndex++;
154  (*coordinates)[currentIndex] = -1.0f;
155  currentIndex++;
156  /*H*/
157  (*coordinates)[currentIndex] = 1.0f;
158  currentIndex++;
159  (*coordinates)[currentIndex] = -1.0f;
160  currentIndex++;
161  (*coordinates)[currentIndex] = 1.0f;
162  currentIndex++;
163 
164  /* Back face. */
165  /*G*/
166  (*coordinates)[currentIndex] = 1.0f;
167  currentIndex++;
168  (*coordinates)[currentIndex] = -1.0f;
169  currentIndex++;
170  (*coordinates)[currentIndex] = -1.0f;
171  currentIndex++;
172  /*C*/
173  (*coordinates)[currentIndex] = 1.0f;
174  currentIndex++;
175  (*coordinates)[currentIndex] = 1.0f;
176  currentIndex++;
177  (*coordinates)[currentIndex] = -1.0f;
178  currentIndex++;
179  /*B*/
180  (*coordinates)[currentIndex] = -1.0f;
181  currentIndex++;
182  (*coordinates)[currentIndex] = 1.0f;
183  currentIndex++;
184  (*coordinates)[currentIndex] = -1.0f;
185  currentIndex++;
186 
187  /*G*/
188  (*coordinates)[currentIndex] = 1.0f;
189  currentIndex++;
190  (*coordinates)[currentIndex] = -1.0f;
191  currentIndex++;
192  (*coordinates)[currentIndex] = -1.0f;
193  currentIndex++;
194  /*B*/
195  (*coordinates)[currentIndex] = -1.0f;
196  currentIndex++;
197  (*coordinates)[currentIndex] = 1.0f;
198  currentIndex++;
199  (*coordinates)[currentIndex] = -1.0f;
200  currentIndex++;
201  /*F*/
202  (*coordinates)[currentIndex] = -1.0f;
203  currentIndex++;
204  (*coordinates)[currentIndex] = -1.0f;
205  currentIndex++;
206  (*coordinates)[currentIndex] = -1.0f;
207  currentIndex++;
208 
209  /* Front face. */
210  /*E*/
211  (*coordinates)[currentIndex] = -1.0f;
212  currentIndex++;
213  (*coordinates)[currentIndex] = -1.0f;
214  currentIndex++;
215  (*coordinates)[currentIndex] = 1.0f;
216  currentIndex++;
217  /*A*/
218  (*coordinates)[currentIndex] = -1.0f;
219  currentIndex++;
220  (*coordinates)[currentIndex] = 1.0f;
221  currentIndex++;
222  (*coordinates)[currentIndex] = 1.0f;
223  currentIndex++;
224  /*D*/
225  (*coordinates)[currentIndex] = 1.0f;
226  currentIndex++;
227  (*coordinates)[currentIndex] = 1.0f;
228  currentIndex++;
229  (*coordinates)[currentIndex] = 1.0f;
230  currentIndex++;
231 
232  /*E*/
233  (*coordinates)[currentIndex] = -1.0f;
234  currentIndex++;
235  (*coordinates)[currentIndex] = -1.0f;
236  currentIndex++;
237  (*coordinates)[currentIndex] = 1.0f;
238  currentIndex++;
239  /*D*/
240  (*coordinates)[currentIndex] = 1.0f;
241  currentIndex++;
242  (*coordinates)[currentIndex] = 1.0f;
243  currentIndex++;
244  (*coordinates)[currentIndex] = 1.0f;
245  currentIndex++;
246  /*H*/
247  (*coordinates)[currentIndex] = 1.0f;
248  currentIndex++;
249  (*coordinates)[currentIndex] = -1.0f;
250  currentIndex++;
251  (*coordinates)[currentIndex] = 1.0f;
252  currentIndex++;
253 
254  /* Right face. */
255  /*H*/
256  (*coordinates)[currentIndex] = 1.0f;
257  currentIndex++;
258  (*coordinates)[currentIndex] = -1.0f;
259  currentIndex++;
260  (*coordinates)[currentIndex] = 1.0f;
261  currentIndex++;
262  /*D*/
263  (*coordinates)[currentIndex] = 1.0f;
264  currentIndex++;
265  (*coordinates)[currentIndex] = 1.0f;
266  currentIndex++;
267  (*coordinates)[currentIndex] = 1.0f;
268  currentIndex++;
269  /*C*/
270  (*coordinates)[currentIndex] = 1.0f;
271  currentIndex++;
272  (*coordinates)[currentIndex] = 1.0f;
273  currentIndex++;
274  (*coordinates)[currentIndex] = -1.0f;
275  currentIndex++;
276 
277  /*H*/
278  (*coordinates)[currentIndex] = 1.0f;
279  currentIndex++;
280  (*coordinates)[currentIndex] = -1.0f;
281  currentIndex++;
282  (*coordinates)[currentIndex] = 1.0f;
283  currentIndex++;
284  /*C*/
285  (*coordinates)[currentIndex] = 1.0f;
286  currentIndex++;
287  (*coordinates)[currentIndex] = 1.0f;
288  currentIndex++;
289  (*coordinates)[currentIndex] = -1.0f;
290  currentIndex++;
291  /*G*/
292  (*coordinates)[currentIndex] = 1.0f;
293  currentIndex++;
294  (*coordinates)[currentIndex] = -1.0f;
295  currentIndex++;
296  (*coordinates)[currentIndex] = -1.0f;
297  currentIndex++;
298 
299  /* Left face. */
300  /*F*/
301  (*coordinates)[currentIndex] = -1.0f;
302  currentIndex++;
303  (*coordinates)[currentIndex] = -1.0f;
304  currentIndex++;
305  (*coordinates)[currentIndex] = -1.0f;
306  currentIndex++;
307  /*B*/
308  (*coordinates)[currentIndex] = -1.0f;
309  currentIndex++;
310  (*coordinates)[currentIndex] = 1.0f;
311  currentIndex++;
312  (*coordinates)[currentIndex] = -1.0f;
313  currentIndex++;
314  /*A*/
315  (*coordinates)[currentIndex] = -1.0f;
316  currentIndex++;
317  (*coordinates)[currentIndex] = 1.0f;
318  currentIndex++;
319  (*coordinates)[currentIndex] = 1.0f;
320  currentIndex++;
321 
322  /*F*/
323  (*coordinates)[currentIndex] = -1.0f;
324  currentIndex++;
325  (*coordinates)[currentIndex] = -1.0f;
326  currentIndex++;
327  (*coordinates)[currentIndex] = -1.0f;
328  currentIndex++;
329  /*A*/
330  (*coordinates)[currentIndex] = -1.0f;
331  currentIndex++;
332  (*coordinates)[currentIndex] = 1.0f;
333  currentIndex++;
334  (*coordinates)[currentIndex] = 1.0f;
335  currentIndex++;
336  /*E*/
337  (*coordinates)[currentIndex] = -1.0f;
338  currentIndex++;
339  (*coordinates)[currentIndex] = -1.0f;
340  currentIndex++;
341  (*coordinates)[currentIndex] = 1.0f;
342  currentIndex++;
343 
344  /* Calculate size of a cube. */
345  for (int i = 0; i < numberOfCubeTriangleCoordinates; i++)
346  {
347  (*coordinates)[i] = scalingFactor * (*coordinates)[i];
348  }
349 
350  if (numberOfCoordinates != NULL)
351  {
352  *numberOfCoordinates = numberOfCubeTriangleCoordinates;
353  }
354  }
355 
356  void CubeModel::getNormals(int* numberOfCoordinates, float** normals)
357  {
358  /* Set the same normals for both triangles from each face.
359  * For details: see example for getCubeTriangleRepresentation() function.
360  */
361 
362  if (normals == NULL)
363  {
364  LOGE("Cannot use null pointer while calculating coordinates.");
365 
366  return;
367  }
368 
369  /* 6 faces, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
370  const int numberOfCubeNormalsCoordinates = 6 * 2 * 3 * 3;
371  /* Index of an array we will put new point coordinates at. */
372  int currentIndex = 0;
373 
374  /* Allocate memory for result array. */
375  *normals = (float*) malloc (numberOfCubeNormalsCoordinates * sizeof(float));
376 
377  /* Is allocation successfu?. */
378  if (*normals == NULL)
379  {
380  LOGE("Could not allocate memory for result array.");
381 
382  return;
383  }
384 
385  /* There are 2 triangles for each face. Each triangle consists of 3 vertices. */
386  int numberOfCoordinatesForOneFace = 2 * 3;
387 
388  /* Top face. */
389  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
390  {
391  (*normals)[currentIndex] = 0;
392  currentIndex++;
393  (*normals)[currentIndex] = 1;
394  currentIndex++;
395  (*normals)[currentIndex] = 0;
396  currentIndex++;
397  }
398 
399  /* Bottom face. */
400  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
401  {
402  (*normals)[currentIndex] = 0;
403  currentIndex++;
404  (*normals)[currentIndex] = -1;
405  currentIndex++;
406  (*normals)[currentIndex] = 0;
407  currentIndex++;
408  }
409 
410  /* Back face. */
411  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
412  {
413  (*normals)[currentIndex] = 0;
414  currentIndex++;
415  (*normals)[currentIndex] = 0;
416  currentIndex++;
417  (*normals)[currentIndex] = -1;
418  currentIndex++;
419  }
420 
421  /* Front face. */
422  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
423  {
424  (*normals)[currentIndex] = 0;
425  currentIndex++;
426  (*normals)[currentIndex] = 0;
427  currentIndex++;
428  (*normals)[currentIndex] = 1;
429  currentIndex++;
430  }
431 
432  /* Right face. */
433  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
434  {
435  (*normals)[currentIndex] = 1;
436  currentIndex++;
437  (*normals)[currentIndex] = 0;
438  currentIndex++;
439  (*normals)[currentIndex] = 0;
440  currentIndex++;
441  }
442 
443  /* Left face. */
444  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
445  {
446  (*normals)[currentIndex] = -1;
447  currentIndex++;
448  (*normals)[currentIndex] = 0;
449  currentIndex++;
450  (*normals)[currentIndex] = 0;
451  currentIndex++;
452  }
453 
454  if (numberOfCoordinates != NULL)
455  {
456  *numberOfCoordinates = numberOfCubeNormalsCoordinates;
457 
458  }
459  }
460 }
static void getNormals(int *numberOfCoordinates, float **normals)
Create normals for a cube which was created with getTriangleRepresentation() function.
Definition: CubeModel.cpp:356
float scalingFactor
Definition: Native.cpp:138
int numberOfCubeTriangleCoordinates
Definition: Native.cpp:55
static void getTriangleRepresentation(float scalingFactor, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a cube.
Definition: CubeModel.cpp:29
#define LOGE(...)
Definition: AstcTextures.h:30
GLfloat normals[]
Definition: Native.cpp:283