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