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 "Common.h"
22 #include "CubeModel.h"
23 
24 #include <cstdlib>
25 
26 namespace MaliSDK
27 {
29  void CubeModel::getNormals(float** normalsPtrPtr,
30  int* numberOfCoordinatesPtr)
31  {
32  /* Set the same normals for both triangles from each face.
33  * For details: see example for getCubeTriangleRepresentation() function. */
34  ASSERT(normalsPtrPtr != NULL,
35  "Cannot use null pointer while calculating coordinates");
36 
37  /* Index of an array we will put new point coordinates at. */
38  int currentIndex = 0;
39  /* 6 faces of open cube, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
40  const int numberOfCubeNormalsCoordinates = NUMBER_OF_CUBE_FACES *
44  /* There are 2 triangles for each face. Each triangle consists of 3 vertices. */
45  const int numberOfCoordinatesForOneFace = NUMBER_OF_TRIANGLES_IN_QUAD *
47 
48  /* Allocate memory for result array. */
49  *normalsPtrPtr = (float*) malloc(numberOfCubeNormalsCoordinates * sizeof(float));
50 
51  /* Is allocation successful?. */
52  ASSERT(*normalsPtrPtr != NULL,
53  "Could not allocate memory for result array");
54 
55  /* Top face. */
56  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
57  {
58  (*normalsPtrPtr)[currentIndex++] = 0;
59  (*normalsPtrPtr)[currentIndex++] = 1;
60  (*normalsPtrPtr)[currentIndex++] = 0;
61  }
62 
63  /* Bottom face. */
64  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
65  {
66  (*normalsPtrPtr)[currentIndex++] = 0;
67  (*normalsPtrPtr)[currentIndex++] = -1;
68  (*normalsPtrPtr)[currentIndex++] = 0;
69  }
70 
71  /* Back face. */
72  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
73  {
74  (*normalsPtrPtr)[currentIndex++] = 0;
75  (*normalsPtrPtr)[currentIndex++] = 0;
76  (*normalsPtrPtr)[currentIndex++] = -1;
77  }
78 
79  /* Front face. */
80  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
81  {
82  (*normalsPtrPtr)[currentIndex++] = 0;
83  (*normalsPtrPtr)[currentIndex++] = 0;
84  (*normalsPtrPtr)[currentIndex++] = 1;
85  }
86 
87  /* Right face. */
88  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
89  {
90  (*normalsPtrPtr)[currentIndex++] = 1;
91  (*normalsPtrPtr)[currentIndex++] = 0;
92  (*normalsPtrPtr)[currentIndex++] = 0;
93  }
94 
95  /* Left face. */
96  for (int i = 0; i < numberOfCoordinatesForOneFace; i++)
97  {
98  (*normalsPtrPtr)[currentIndex++] = -1;
99  (*normalsPtrPtr)[currentIndex++] = 0;
100  (*normalsPtrPtr)[currentIndex++] = 0;
101  }
102 
103  if (numberOfCoordinatesPtr != NULL)
104  {
105  *numberOfCoordinatesPtr = numberOfCubeNormalsCoordinates;
106  }
107  }
108 
110  void CubeModel::getTriangleRepresentation(float** coordinatesPtrPtr,
111  int* numberOfCoordinatesPtr,
112  float scalingFactor)
113  {
114  ASSERT(coordinatesPtrPtr != NULL,
115  "Cannot use null pointer while calculating coordinates");
116 
117  /* Index of an array we will put new point coordinates at. */
118  int currentIndex = 0;
119  /* 6 faces of open cube, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
122  NUMBER_OF_TRIANGLE_VERTICES *
124 
125  /* Allocate memory for result array. */
126  *coordinatesPtrPtr = (float*) malloc(numberOfCubeTriangleCoordinates * sizeof(float));
127 
128  /* Is allocation successful?. */
129  ASSERT(*coordinatesPtrPtr != NULL,
130  "Could not allocate memory for result array.")
131 
132  /* Example:
133  * Coordinates for cube points:
134  * A -1.0f, 1.0f, 1.0f
135  * B -1.0f, 1.0f, -1.0f
136  * C 1.0f, 1.0f, -1.0f
137  * D 1.0f, 1.0f, 1.0f
138  * E -1.0f, -1.0f, 1.0f
139  * F -1.0f, -1.0f, -1.0f
140  * G 1.0f, -1.0f, -1.0f
141  * H 1.0f, -1.0f, 1.0f
142  * Create 2 triangles for each face of the cube. Vertices are written in clockwise order.
143  * B ________ C
144  * / | / |
145  * A ......... D |
146  * . | . |
147  * . F|_ _.___ |G
148  * . / . /
149  * E ......... H
150  */
151 
152  const Vec3f pointA = {-1.0f, 1.0f, 1.0f};
153  const Vec3f pointB = {-1.0f, 1.0f, -1.0f};
154  const Vec3f pointC = { 1.0f, 1.0f, -1.0f};
155  const Vec3f pointD = { 1.0f, 1.0f, 1.0f};
156  const Vec3f pointE = {-1.0f, -1.0f, 1.0f};
157  const Vec3f pointF = {-1.0f, -1.0f, -1.0f};
158  const Vec3f pointG = { 1.0f, -1.0f, -1.0f};
159  const Vec3f pointH = { 1.0f, -1.0f, 1.0f};
160 
161  /* Fill the array with coordinates. */
162  /* Top face. */
163  /* A */
164  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
165  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
166  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
167  /* B */
168  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
169  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
170  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
171  /* C */
172  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
173  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
174  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
175 
176  /* A */
177  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
178  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
179  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
180  /* C */
181  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
182  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
183  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
184  /* D */
185  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
186  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
187  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
188 
189  /* Bottom face. */
190  /* F */
191  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;;
192  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;;
193  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;;
194  /* E */
195  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
196  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
197  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
198  /* H */
199  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
200  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
201  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
202 
203  /* F */
204  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
205  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
206  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
207  /* H */
208  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
209  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
210  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
211  /* G */
212  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
213  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
214  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
215 
216  /* Back face. */
217  /* G */
218  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
219  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
220  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
221  /* C */
222  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
223  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
224  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
225  /* B */
226  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
227  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
228  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
229 
230  /* G */
231  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
232  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
233  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
234  /* B */
235  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
236  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
237  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
238  /* F */
239  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
240  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
241  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
242 
243  /* Front face. */
244  /* E */
245  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
246  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
247  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
248  /* A */
249  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
250  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
251  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
252  /* D */
253  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
254  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
255  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
256 
257  /* E */
258  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
259  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
260  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
261  /* D */
262  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
263  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
264  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
265  /* H */
266  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
267  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
268  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
269 
270  /* Right face. */
271  /* H */
272  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
273  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
274  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
275  /* D */
276  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
277  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
278  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
279  /* C */
280  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
281  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
282  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
283 
284  /* H */
285  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
286  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
287  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
288  /* C */
289  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
290  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
291  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
292  /* G */
293  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
294  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
295  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
296 
297  /* Left face. */
298  /* F */
299  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
300  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
301  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
302  /* B */
303  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
304  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
305  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
306  /* A */
307  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
308  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
309  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
310 
311  /* F */
312  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
313  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
314  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
315  /* A */
316  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
317  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
318  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
319  /* E */
320  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
321  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
322  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
323 
324  /* Calculate size of a cube. */
325  if (scalingFactor != 1.0f)
326  {
327  for (int i = 0; i < numberOfCubeTriangleCoordinates; i++)
328  {
329  (*coordinatesPtrPtr)[i] *= scalingFactor;
330  }
331  }
332 
333  if (numberOfCoordinatesPtr != NULL)
334  {
335  *numberOfCoordinatesPtr = numberOfCubeTriangleCoordinates;
336  }
337  }
338 }
#define NUMBER_OF_TRIANGLE_VERTICES
Number of vertices which make up a triangle shape.
Definition: Common.h:41
static void getNormals(int *numberOfCoordinates, float **normals)
Create normals for a cube which was created with getTriangleRepresentation() function.
Definition: CubeModel.cpp:356
A 3D floating point vector.
Definition: VectorTypes.h:83
float scalingFactor
Definition: Native.cpp:138
#define NUMBER_OF_CUBE_FACES
Number of faces which make up a cubic shape.
Definition: Common.h:31
int numberOfCubeTriangleCoordinates
Definition: Native.cpp:55
GLfloat GLfloat f
Definition: gl2ext.h:2707
static void getTriangleRepresentation(float scalingFactor, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a cube.
Definition: CubeModel.cpp:29
#define ASSERT(x, s)
Definition: common.h:45
#define NUMBER_OF_POINT_COORDINATES
Number of coordinates for a point in 3D space.
Definition: Common.h:36
#define NUMBER_OF_TRIANGLES_IN_QUAD
Number of triangles which make up a quad.
Definition: Common.h:46