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  /* [Get cube triangle coordinates] */
29  /* Please see header for the specification. */
30  void CubeModel::getTriangleRepresentation(float** coordinatesPtrPtr,
31  int* numberOfCoordinatesPtr,
32  int* numberOfPointsPtr,
33  float scalingFactor)
34  {
35  ASSERT(coordinatesPtrPtr != NULL,
36  "Cannot use null pointer while calculating coordinates");
37 
38  /* Index of an array we will put new point coordinates at. */
39  int currentIndex = 0;
40  /* 6 faces of cube, 2 triangles for each face, 3 points of triangle, 3 coordinates for each point. */
45 
46  /* Allocate memory for result array. */
47  *coordinatesPtrPtr = (float*) malloc(numberOfCubeTriangleCoordinates * sizeof(float));
48 
49  /* Is allocation successful?. */
50  ASSERT(*coordinatesPtrPtr != NULL,
51  "Could not allocate memory for result array.")
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  const Vec3f pointA = {-1.0f, 1.0f, 1.0f};
74  const Vec3f pointB = {-1.0f, 1.0f, -1.0f};
75  const Vec3f pointC = { 1.0f, 1.0f, -1.0f};
76  const Vec3f pointD = { 1.0f, 1.0f, 1.0f};
77  const Vec3f pointE = {-1.0f, -1.0f, 1.0f};
78  const Vec3f pointF = {-1.0f, -1.0f, -1.0f};
79  const Vec3f pointG = { 1.0f, -1.0f, -1.0f};
80  const Vec3f pointH = { 1.0f, -1.0f, 1.0f};
81 
82  /* Fill the array with coordinates. */
83  /* Top face. */
84  /* A */
85  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
86  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
87  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
88  /* B */
89  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
90  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
91  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
92  /* C */
93  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
94  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
95  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
96 
97  /* A */
98  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
99  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
100  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
101  /* C */
102  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
103  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
104  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
105  /* D */
106  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
107  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
108  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
109 
110  /* Bottom face. */
111  /* F */
112  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;;
113  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;;
114  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;;
115  /* E */
116  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
117  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
118  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
119  /* H */
120  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
121  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
122  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
123 
124  /* F */
125  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
126  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
127  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
128  /* H */
129  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
130  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
131  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
132  /* G */
133  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
134  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
135  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
136 
137  /* Back face. */
138  /* G */
139  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
140  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
141  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
142  /* C */
143  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
144  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
145  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
146  /* B */
147  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
148  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
149  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
150 
151  /* G */
152  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
153  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
154  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
155  /* B */
156  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
157  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
158  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
159  /* F */
160  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
161  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
162  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
163 
164  /* Front face. */
165  /* E */
166  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
167  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
168  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
169  /* A */
170  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
171  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
172  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
173  /* D */
174  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
175  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
176  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
177 
178  /* E */
179  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
180  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
181  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
182  /* D */
183  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
184  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
185  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
186  /* H */
187  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
188  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
189  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
190 
191  /* Right face. */
192  /* H */
193  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
194  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
195  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
196  /* D */
197  (*coordinatesPtrPtr)[currentIndex++] = pointD.x;
198  (*coordinatesPtrPtr)[currentIndex++] = pointD.y;
199  (*coordinatesPtrPtr)[currentIndex++] = pointD.z;
200  /* C */
201  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
202  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
203  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
204 
205  /* H */
206  (*coordinatesPtrPtr)[currentIndex++] = pointH.x;
207  (*coordinatesPtrPtr)[currentIndex++] = pointH.y;
208  (*coordinatesPtrPtr)[currentIndex++] = pointH.z;
209  /* C */
210  (*coordinatesPtrPtr)[currentIndex++] = pointC.x;
211  (*coordinatesPtrPtr)[currentIndex++] = pointC.y;
212  (*coordinatesPtrPtr)[currentIndex++] = pointC.z;
213  /* G */
214  (*coordinatesPtrPtr)[currentIndex++] = pointG.x;
215  (*coordinatesPtrPtr)[currentIndex++] = pointG.y;
216  (*coordinatesPtrPtr)[currentIndex++] = pointG.z;
217 
218  /* Left face. */
219  /* F */
220  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
221  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
222  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
223  /* B */
224  (*coordinatesPtrPtr)[currentIndex++] = pointB.x;
225  (*coordinatesPtrPtr)[currentIndex++] = pointB.y;
226  (*coordinatesPtrPtr)[currentIndex++] = pointB.z;
227  /* A */
228  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
229  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
230  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
231 
232  /* F */
233  (*coordinatesPtrPtr)[currentIndex++] = pointF.x;
234  (*coordinatesPtrPtr)[currentIndex++] = pointF.y;
235  (*coordinatesPtrPtr)[currentIndex++] = pointF.z;
236  /* A */
237  (*coordinatesPtrPtr)[currentIndex++] = pointA.x;
238  (*coordinatesPtrPtr)[currentIndex++] = pointA.y;
239  (*coordinatesPtrPtr)[currentIndex++] = pointA.z;
240  /* E */
241  (*coordinatesPtrPtr)[currentIndex++] = pointE.x;
242  (*coordinatesPtrPtr)[currentIndex++] = pointE.y;
243  (*coordinatesPtrPtr)[currentIndex++] = pointE.z;
244 
245  /* Calculate size of a cube. */
246  if (scalingFactor != 1.0f)
247  {
248  for (int i = 0; i < numberOfCubeTriangleCoordinates; i++)
249  {
250  (*coordinatesPtrPtr)[i] *= scalingFactor;
251  }
252  }
253 
254  if (numberOfCoordinatesPtr != NULL)
255  {
256  *numberOfCoordinatesPtr = numberOfCubeTriangleCoordinates;
257  }
258 
259  if (numberOfPointsPtr != NULL)
260  {
261  *numberOfPointsPtr = numberOfCubeTriangleCoordinates / NUMBER_OF_POINT_COORDINATES;
262  }
263  }
264  /* [Get cube triangle coordinates] */
265 }
#define NUMBER_OF_TRIANGLE_VERTICES
Number of vertices which make up a triangle shape.
Definition: Common.h:41
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