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  int* numberOfPointsPtr,
113  float scalingFactor)
114  {
115  ASSERT(coordinatesPtrPtr != NULL,
116  "Cannot use null pointer while calculating coordinates");
117 
118  /* Index of an array we will put new point coordinates at. */
119  int currentIndex = 0;
120  /* 6 faces of open cube, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
121  const int numberOfCubeTrianglePoints = NUMBER_OF_CUBE_FACES *
124  const int numberOfCubeTriangleCoordinates = numberOfCubeTrianglePoints *
126 
127  /* Allocate memory for result array. */
128  *coordinatesPtrPtr = (float*) malloc(numberOfCubeTriangleCoordinates * sizeof(float));
129 
130  /* Is allocation successful?. */
131  ASSERT(*coordinatesPtrPtr != NULL,
132  "Could not allocate memory for result array.")
133 
134  /* Example:
135  * Coordinates for cube points:
136  * A -1.0f, 1.0f, 1.0f
137  * B -1.0f, 1.0f, -1.0f
138  * C 1.0f, 1.0f, -1.0f
139  * D 1.0f, 1.0f, 1.0f
140  * E -1.0f, -1.0f, 1.0f
141  * F -1.0f, -1.0f, -1.0f
142  * G 1.0f, -1.0f, -1.0f
143  * H 1.0f, -1.0f, 1.0f
144  * Create 2 triangles for each face of the cube. Vertices are written in clockwise order.
145  * B ________ C
146  * / | / |
147  * A ......... D |
148  * . | . |
149  * . F|_ _.___ |G
150  * . / . /
151  * E ......... H
152  */
153 
154  const Vec3f pointA = {-1.0f, 1.0f, 1.0f};
155  const Vec3f pointB = {-1.0f, 1.0f, -1.0f};
156  const Vec3f pointC = { 1.0f, 1.0f, -1.0f};
157  const Vec3f pointD = { 1.0f, 1.0f, 1.0f};
158  const Vec3f pointE = {-1.0f, -1.0f, 1.0f};
159  const Vec3f pointF = {-1.0f, -1.0f, -1.0f};
160  const Vec3f pointG = { 1.0f, -1.0f, -1.0f};
161  const Vec3f pointH = { 1.0f, -1.0f, 1.0f};
162 
163  /* Fill the array with coordinates. */
164  /* Top face. */
165  /* A */
166  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
167  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
168  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
169  /* B */
170  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
171  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
172  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
173  /* C */
174  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
175  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
176  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
177 
178  /* A */
179  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
180  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
181  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
182  /* C */
183  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
184  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
185  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
186  /* D */
187  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
188  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
189  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
190 
191  /* Bottom face. */
192  /* F */
193  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;;
194  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;;
195  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;;
196  /* E */
197  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
198  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
199  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
200  /* H */
201  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
202  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
203  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
204 
205  /* F */
206  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
207  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
208  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
209  /* H */
210  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
211  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
212  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
213  /* G */
214  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
215  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
216  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
217 
218  /* Back face. */
219  /* G */
220  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
221  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
222  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
223  /* C */
224  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
225  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
226  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
227  /* B */
228  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
229  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
230  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
231 
232  /* G */
233  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
234  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
235  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
236  /* B */
237  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
238  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
239  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
240  /* F */
241  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
242  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
243  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
244 
245  /* Front face. */
246  /* E */
247  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
248  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
249  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
250  /* A */
251  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
252  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
253  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
254  /* D */
255  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
256  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
257  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
258 
259  /* E */
260  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
261  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
262  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
263  /* D */
264  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
265  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
266  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
267  /* H */
268  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
269  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
270  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
271 
272  /* Right face. */
273  /* H */
274  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
275  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
276  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
277  /* D */
278  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
279  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
280  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
281  /* C */
282  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
283  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
284  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
285 
286  /* H */
287  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
288  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
289  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
290  /* C */
291  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
292  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
293  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
294  /* G */
295  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
296  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
297  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
298 
299  /* Left face. */
300  /* F */
301  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
302  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
303  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
304  /* B */
305  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
306  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
307  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
308  /* A */
309  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
310  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
311  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
312 
313  /* F */
314  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
315  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
316  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
317  /* A */
318  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
319  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
320  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
321  /* E */
322  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
323  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
324  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
325 
326  /* Calculate size of a cube. */
327  if (scalingFactor != 1.0f)
328  {
329  for (int i = 0; i < numberOfCubeTriangleCoordinates; i++)
330  {
331  (*coordinatesPtrPtr)[i] *= scalingFactor;
332  }
333  }
334 
335  if (numberOfCoordinatesPtr != NULL)
336  {
337  *numberOfCoordinatesPtr = numberOfCubeTriangleCoordinates;
338  }
339 
340  if (numberOfPointsPtr != NULL)
341  {
342  *numberOfPointsPtr = numberOfCubeTrianglePoints;
343  }
344  }
345 }
#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
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