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) 2014-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 namespace MaliSDK
24 {
25  void CubeModel::getTriangleRepresentation(float scalingFactor, int* numberOfCoordinates, float** coordinates)
26  {
27  if (coordinates == NULL)
28  {
29  LOGE("Cannot use null pointer while calculating coordinates.");
30  return;
31  }
32 
33  /* 6 faces, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
34  const int numberOfCubeTriangleCoordinates = 6 * 2 * 3 * 3;
35  /* Index of an array we will put new point coordinates at. */
36  int currentIndex = 0;
37 
38  /* Allocate memory for result array. */
39  *coordinates = (float*) malloc (numberOfCubeTriangleCoordinates * sizeof(float));
40 
41  /* Is allocation successful?. */
42  if (*coordinates == NULL)
43  {
44  LOGE("Could not allocate memory for result array.");
45 
46  return;
47  }
48 
49  /* Example:
50  * Coordinates for cube points:
51  * A -1.0f, 1.0f, 1.0f
52  * B -1.0f, 1.0f, -1.0f
53  * C 1.0f, 1.0f, -1.0f
54  * D 1.0f, 1.0f, 1.0f
55  * E -1.0f, -1.0f, 1.0f
56  * F -1.0f, -1.0f, -1.0f
57  * G 1.0f, -1.0f, -1.0f
58  * H 1.0f, -1.0f, 1.0f
59  * Create 2 triangles for each face of the cube. Vertices are written in clockwise order.
60  * B ________ C
61  * / | / |
62  * A ......... D |
63  * . | . |
64  * . F|_ _.___ |G
65  * . / . /
66  * E ......... H
67  */
68 
69  /* Fill the array with coordinates. */
70  /* Top face. */
71  /*A*/
72  (*coordinates)[currentIndex] = -1.0f;
73  currentIndex++;
74  (*coordinates)[currentIndex] = 1.0f;
75  currentIndex++;
76  (*coordinates)[currentIndex] = 1.0f;
77  currentIndex++;
78  /*B*/
79  (*coordinates)[currentIndex] = -1.0f;
80  currentIndex++;
81  (*coordinates)[currentIndex] = 1.0f;
82  currentIndex++;
83  (*coordinates)[currentIndex] = -1.0f;
84  currentIndex++;
85  /*C*/
86  (*coordinates)[currentIndex] = 1.0f;
87  currentIndex++;
88  (*coordinates)[currentIndex] = 1.0f;
89  currentIndex++;
90  (*coordinates)[currentIndex] = -1.0f;
91  currentIndex++;
92 
93  /*A*/
94  (*coordinates)[currentIndex] = -1.0f;
95  currentIndex++;
96  (*coordinates)[currentIndex] = 1.0f;
97  currentIndex++;
98  (*coordinates)[currentIndex] = 1.0f;
99  currentIndex++;
100  /*C*/
101  (*coordinates)[currentIndex] = 1.0f;
102  currentIndex++;
103  (*coordinates)[currentIndex] = 1.0f;
104  currentIndex++;
105  (*coordinates)[currentIndex] = -1.0f;
106  currentIndex++;
107  /*D*/
108  (*coordinates)[currentIndex] = 1.0f;
109  currentIndex++;
110  (*coordinates)[currentIndex] = 1.0f;
111  currentIndex++;
112  (*coordinates)[currentIndex] = 1.0f;
113  currentIndex++;
114 
115  /* Bottom face. */
116  /*E*/
117  (*coordinates)[currentIndex] = -1.0f;
118  currentIndex++;
119  (*coordinates)[currentIndex] = -1.0f;
120  currentIndex++;
121  (*coordinates)[currentIndex] = 1.0f;
122  currentIndex++;
123  /*F*/
124  (*coordinates)[currentIndex] = -1.0f;
125  currentIndex++;
126  (*coordinates)[currentIndex] = -1.0f;
127  currentIndex++;
128  (*coordinates)[currentIndex] = -1.0f;
129  currentIndex++;
130  /*G*/
131  (*coordinates)[currentIndex] = 1.0f;
132  currentIndex++;
133  (*coordinates)[currentIndex] = -1.0f;
134  currentIndex++;
135  (*coordinates)[currentIndex] = -1.0f;
136  currentIndex++;
137 
138  /*E*/
139  (*coordinates)[currentIndex] = -1.0f;
140  currentIndex++;
141  (*coordinates)[currentIndex] = -1.0f;
142  currentIndex++;
143  (*coordinates)[currentIndex] = 1.0f;
144  currentIndex++;
145  /*G*/
146  (*coordinates)[currentIndex] = 1.0f;
147  currentIndex++;
148  (*coordinates)[currentIndex] = -1.0f;
149  currentIndex++;
150  (*coordinates)[currentIndex] = -1.0f;
151  currentIndex++;
152  /*H*/
153  (*coordinates)[currentIndex] = 1.0f;
154  currentIndex++;
155  (*coordinates)[currentIndex] = -1.0f;
156  currentIndex++;
157  (*coordinates)[currentIndex] = 1.0f;
158  currentIndex++;
159 
160  /* Back face. */
161  /*G*/
162  (*coordinates)[currentIndex] = 1.0f;
163  currentIndex++;
164  (*coordinates)[currentIndex] = -1.0f;
165  currentIndex++;
166  (*coordinates)[currentIndex] = -1.0f;
167  currentIndex++;
168  /*C*/
169  (*coordinates)[currentIndex] = 1.0f;
170  currentIndex++;
171  (*coordinates)[currentIndex] = 1.0f;
172  currentIndex++;
173  (*coordinates)[currentIndex] = -1.0f;
174  currentIndex++;
175  /*B*/
176  (*coordinates)[currentIndex] = -1.0f;
177  currentIndex++;
178  (*coordinates)[currentIndex] = 1.0f;
179  currentIndex++;
180  (*coordinates)[currentIndex] = -1.0f;
181  currentIndex++;
182 
183  /*G*/
184  (*coordinates)[currentIndex] = 1.0f;
185  currentIndex++;
186  (*coordinates)[currentIndex] = -1.0f;
187  currentIndex++;
188  (*coordinates)[currentIndex] = -1.0f;
189  currentIndex++;
190  /*B*/
191  (*coordinates)[currentIndex] = -1.0f;
192  currentIndex++;
193  (*coordinates)[currentIndex] = 1.0f;
194  currentIndex++;
195  (*coordinates)[currentIndex] = -1.0f;
196  currentIndex++;
197  /*F*/
198  (*coordinates)[currentIndex] = -1.0f;
199  currentIndex++;
200  (*coordinates)[currentIndex] = -1.0f;
201  currentIndex++;
202  (*coordinates)[currentIndex] = -1.0f;
203  currentIndex++;
204 
205  /* Front face. */
206  /*E*/
207  (*coordinates)[currentIndex] = -1.0f;
208  currentIndex++;
209  (*coordinates)[currentIndex] = -1.0f;
210  currentIndex++;
211  (*coordinates)[currentIndex] = 1.0f;
212  currentIndex++;
213  /*A*/
214  (*coordinates)[currentIndex] = -1.0f;
215  currentIndex++;
216  (*coordinates)[currentIndex] = 1.0f;
217  currentIndex++;
218  (*coordinates)[currentIndex] = 1.0f;
219  currentIndex++;
220  /*D*/
221  (*coordinates)[currentIndex] = 1.0f;
222  currentIndex++;
223  (*coordinates)[currentIndex] = 1.0f;
224  currentIndex++;
225  (*coordinates)[currentIndex] = 1.0f;
226  currentIndex++;
227 
228  /*E*/
229  (*coordinates)[currentIndex] = -1.0f;
230  currentIndex++;
231  (*coordinates)[currentIndex] = -1.0f;
232  currentIndex++;
233  (*coordinates)[currentIndex] = 1.0f;
234  currentIndex++;
235  /*D*/
236  (*coordinates)[currentIndex] = 1.0f;
237  currentIndex++;
238  (*coordinates)[currentIndex] = 1.0f;
239  currentIndex++;
240  (*coordinates)[currentIndex] = 1.0f;
241  currentIndex++;
242  /*H*/
243  (*coordinates)[currentIndex] = 1.0f;
244  currentIndex++;
245  (*coordinates)[currentIndex] = -1.0f;
246  currentIndex++;
247  (*coordinates)[currentIndex] = 1.0f;
248  currentIndex++;
249 
250  /* Right face. */
251  /*H*/
252  (*coordinates)[currentIndex] = 1.0f;
253  currentIndex++;
254  (*coordinates)[currentIndex] = -1.0f;
255  currentIndex++;
256  (*coordinates)[currentIndex] = 1.0f;
257  currentIndex++;
258  /*D*/
259  (*coordinates)[currentIndex] = 1.0f;
260  currentIndex++;
261  (*coordinates)[currentIndex] = 1.0f;
262  currentIndex++;
263  (*coordinates)[currentIndex] = 1.0f;
264  currentIndex++;
265  /*C*/
266  (*coordinates)[currentIndex] = 1.0f;
267  currentIndex++;
268  (*coordinates)[currentIndex] = 1.0f;
269  currentIndex++;
270  (*coordinates)[currentIndex] = -1.0f;
271  currentIndex++;
272 
273  /*H*/
274  (*coordinates)[currentIndex] = 1.0f;
275  currentIndex++;
276  (*coordinates)[currentIndex] = -1.0f;
277  currentIndex++;
278  (*coordinates)[currentIndex] = 1.0f;
279  currentIndex++;
280  /*C*/
281  (*coordinates)[currentIndex] = 1.0f;
282  currentIndex++;
283  (*coordinates)[currentIndex] = 1.0f;
284  currentIndex++;
285  (*coordinates)[currentIndex] = -1.0f;
286  currentIndex++;
287  /*G*/
288  (*coordinates)[currentIndex] = 1.0f;
289  currentIndex++;
290  (*coordinates)[currentIndex] = -1.0f;
291  currentIndex++;
292  (*coordinates)[currentIndex] = -1.0f;
293  currentIndex++;
294 
295  /* Left face. */
296  /*F*/
297  (*coordinates)[currentIndex] = -1.0f;
298  currentIndex++;
299  (*coordinates)[currentIndex] = -1.0f;
300  currentIndex++;
301  (*coordinates)[currentIndex] = -1.0f;
302  currentIndex++;
303  /*B*/
304  (*coordinates)[currentIndex] = -1.0f;
305  currentIndex++;
306  (*coordinates)[currentIndex] = 1.0f;
307  currentIndex++;
308  (*coordinates)[currentIndex] = -1.0f;
309  currentIndex++;
310  /*A*/
311  (*coordinates)[currentIndex] = -1.0f;
312  currentIndex++;
313  (*coordinates)[currentIndex] = 1.0f;
314  currentIndex++;
315  (*coordinates)[currentIndex] = 1.0f;
316  currentIndex++;
317 
318  /*F*/
319  (*coordinates)[currentIndex] = -1.0f;
320  currentIndex++;
321  (*coordinates)[currentIndex] = -1.0f;
322  currentIndex++;
323  (*coordinates)[currentIndex] = -1.0f;
324  currentIndex++;
325  /*A*/
326  (*coordinates)[currentIndex] = -1.0f;
327  currentIndex++;
328  (*coordinates)[currentIndex] = 1.0f;
329  currentIndex++;
330  (*coordinates)[currentIndex] = 1.0f;
331  currentIndex++;
332  /*E*/
333  (*coordinates)[currentIndex] = -1.0f;
334  currentIndex++;
335  (*coordinates)[currentIndex] = -1.0f;
336  currentIndex++;
337  (*coordinates)[currentIndex] = 1.0f;
338  currentIndex++;
339 
340  /* Calculate size of a cube. */
341  for (int i = 0; i < numberOfCubeTriangleCoordinates; i++)
342  {
343  (*coordinates)[i] = scalingFactor * (*coordinates)[i];
344  }
345 
346  if (numberOfCoordinates != NULL)
347  {
348  *numberOfCoordinates = numberOfCubeTriangleCoordinates;
349  }
350  }
351 
352  void CubeModel::getNormals(int* numberOfCoordinates, float** normals)
353  {
354  /* Set the same normals for both triangles from each face.
355  * For details: see example for getCubeTriangleRepresentation() function.
356  */
357 
358  if (normals == NULL)
359  {
360  LOGE("Cannot use null pointer while calculating coordinates.");
361 
362  return;
363  }
364 
365  /* 6 faces, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
366  const int numberOfCubeNormalsCoordinates = 6 * 2 * 3 * 3;
367  /* Index of an array we will put new point coordinates at. */
368  int currentIndex = 0;
369 
370  /* Allocate memory for result array. */
371  *normals = (float*) malloc (numberOfCubeNormalsCoordinates * sizeof(float));
372 
373  /* Is allocation successfu?. */
374  if (*normals == NULL)
375  {
376  LOGE("Could not allocate memory for result array.");
377 
378  return;
379  }
380 
381  /* There are 2 triangles for each face. Each triangle consists of 3 vertices. */
382  int numberOfCoordinatesForOneFace = 2 * 3;
383 
384  /* Top face. */
385  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
386  {
387  (*normals)[currentIndex] = 0;
388  currentIndex++;
389  (*normals)[currentIndex] = 1;
390  currentIndex++;
391  (*normals)[currentIndex] = 0;
392  currentIndex++;
393  }
394 
395  /* Bottom face. */
396  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
397  {
398  (*normals)[currentIndex] = 0;
399  currentIndex++;
400  (*normals)[currentIndex] = -1;
401  currentIndex++;
402  (*normals)[currentIndex] = 0;
403  currentIndex++;
404  }
405 
406  /* Back face. */
407  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
408  {
409  (*normals)[currentIndex] = 0;
410  currentIndex++;
411  (*normals)[currentIndex] = 0;
412  currentIndex++;
413  (*normals)[currentIndex] = -1;
414  currentIndex++;
415  }
416 
417  /* Front face. */
418  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
419  {
420  (*normals)[currentIndex] = 0;
421  currentIndex++;
422  (*normals)[currentIndex] = 0;
423  currentIndex++;
424  (*normals)[currentIndex] = 1;
425  currentIndex++;
426  }
427 
428  /* Right face. */
429  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
430  {
431  (*normals)[currentIndex] = 1;
432  currentIndex++;
433  (*normals)[currentIndex] = 0;
434  currentIndex++;
435  (*normals)[currentIndex] = 0;
436  currentIndex++;
437  }
438 
439  /* Left face. */
440  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
441  {
442  (*normals)[currentIndex] = -1;
443  currentIndex++;
444  (*normals)[currentIndex] = 0;
445  currentIndex++;
446  (*normals)[currentIndex] = 0;
447  currentIndex++;
448  }
449 
450  if (numberOfCoordinates != NULL)
451  {
452  *numberOfCoordinates = numberOfCubeNormalsCoordinates;
453 
454  }
455  }
456 }
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