OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PlaneModel.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-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 "PlaneModel.h"
22 
23 #include "Mathematics.h"
24 #include "Platform.h"
25 
26 #include <cassert>
27 
28 namespace MaliSDK
29 {
30  void PlaneModel::getTriangleRepresentationUVCoordinates(int* numberOfCoordinates, float** uvCoordinates)
31  {
32  /* Example:
33  * v D __________ C
34  * . | / |
35  * / \ | / |
36  * | | / |
37  * | |/_________|
38  * | A B
39  * |----------> u
40  */
41 
42  if (uvCoordinates == NULL)
43  {
44  LOGE("Cannot use null pointer while calculating coordinates.");
45 
46  return;
47  }
48 
49  /* 2 triangles, 3 points of triangle, 2 coordinates for each point. */
50  const int numberOfUVCoordinates = 2 * 3 * 2;
51 
52  /* Allocate memory for result array. */
53  *uvCoordinates = (float*) malloc (numberOfUVCoordinates * sizeof(float));
54 
55  /* Is allocation successfu?. */
56  if (*uvCoordinates == NULL)
57  {
58  LOGE("Could not allocate memory for result array.");
59  return;
60  }
61 
62  /* Index of an array we will put new point coordinates at. */
63  int currentIndex = 0;
64 
65  /*** First triangle. ***/
66  /* A */
67  /* u */
68  (*uvCoordinates)[currentIndex] = 0.0f;
69  currentIndex++;
70  /* v */
71  (*uvCoordinates)[currentIndex] = 0.0f;
72  currentIndex++;
73  /* B */
74  /* u */
75  (*uvCoordinates)[currentIndex] = 1.0f;
76  currentIndex++;
77  /* v */
78  (*uvCoordinates)[currentIndex] = 0.0f;
79  currentIndex++;
80  /* C */
81  /* u */
82  (*uvCoordinates)[currentIndex] = 1.0f;
83  currentIndex++;
84  /* v */
85  (*uvCoordinates)[currentIndex] = 1.0f;
86  currentIndex++;
87 
88  /*** Second triangle. ***/
89  /* A */
90  /* u */
91  (*uvCoordinates)[currentIndex] = 0.0f;
92  currentIndex++;
93  /* v */
94  (*uvCoordinates)[currentIndex] = 0.0f;
95  currentIndex++;
96  /* C */
97  /* u */
98  (*uvCoordinates)[currentIndex] = 1.0f;
99  currentIndex++;
100  /* v */
101  (*uvCoordinates)[currentIndex] = 1.0f;
102  currentIndex++;
103  /* D */
104  /* u */
105  (*uvCoordinates)[currentIndex] = 0.0f;
106  currentIndex++;
107  /* v */
108  (*uvCoordinates)[currentIndex] = 1.0f;
109  currentIndex++;
110 
111  if (numberOfCoordinates != NULL)
112  {
113  *numberOfCoordinates = numberOfUVCoordinates;
114  }
115  }
116 
117  void PlaneModel::getTriangleRepresentation(int* numberOfCoordinates, float** coordinates)
118  {
119  /* Example:
120  * z D __________ C
121  * . | / |
122  * / \ | / |
123  * | | / |
124  * | |/_________|
125  * | A B
126  * |----------> x
127  */
128 
129  if (coordinates == NULL)
130  {
131  LOGE("Cannot use null pointer while calculating coordinates.");
132 
133  return;
134  }
135 
136  /* 2 triangles, 3 points of triangle, 4 coordinates for each point. */
137  const int numberOfSquareCoordinates = 2 * 3 * 4;
138 
139  /* Allocate memory for result array. */
140  *coordinates = (float*) malloc (numberOfSquareCoordinates * sizeof(float));
141 
142  /* Is allocation successfu?. */
143  if (*coordinates == NULL)
144  {
145  LOGE("Could not allocate memory for result array.");
146  return;
147  }
148 
149  /* Index of an array we will put new point coordinates at. */
150  int currentIndex = 0;
151 
152  /* First triangle. */
153  /* A */
154  /* x */
155  (*coordinates)[currentIndex] = -1.0f;
156  currentIndex++;
157  /* y */
158  (*coordinates)[currentIndex] = 0.0f;
159  currentIndex++;
160  /* z */
161  (*coordinates)[currentIndex] = -1.0f;
162  currentIndex++;
163  /* w */
164  (*coordinates)[currentIndex] = 1.0f;
165  currentIndex++;
166  /* B */
167  /* x */
168  (*coordinates)[currentIndex] = 1.0f;
169  currentIndex++;
170  /* y */
171  (*coordinates)[currentIndex] = 0.0f;
172  currentIndex++;
173  /* z */
174  (*coordinates)[currentIndex] = -1.0f;
175  currentIndex++;
176  /* w */
177  (*coordinates)[currentIndex] = 1.0f;
178  currentIndex++;
179  /* C */
180  /* x */
181  (*coordinates)[currentIndex] = 1.0f;
182  currentIndex++;
183  /* y */
184  (*coordinates)[currentIndex] = 0.0f;
185  currentIndex++;
186  /* z */
187  (*coordinates)[currentIndex] = 1.0f;
188  currentIndex++;
189  /* w */
190  (*coordinates)[currentIndex] = 1.0f;
191  currentIndex++;
192 
193  /* Second triangle. */
194  /* A */
195  /* x */
196  (*coordinates)[currentIndex] = -1.0f;
197  currentIndex++;
198  /* y */
199  (*coordinates)[currentIndex] = 0.0f;
200  currentIndex++;
201  /* z */
202  (*coordinates)[currentIndex] = -1.0f;
203  currentIndex++;
204  /* w */
205  (*coordinates)[currentIndex] = 1.0f;
206  currentIndex++;
207  /* C */
208  /* x */
209  (*coordinates)[currentIndex] = 1.0f;
210  currentIndex++;
211  /* y */
212  (*coordinates)[currentIndex] = 0.0f;
213  currentIndex++;
214  /* z */
215  (*coordinates)[currentIndex] = 1.0f;
216  currentIndex++;
217  /* w */
218  (*coordinates)[currentIndex] = 1.0f;
219  currentIndex++;
220  /* D */
221  /* x */
222  (*coordinates)[currentIndex] = -1.0f;
223  currentIndex++;
224  /* y */
225  (*coordinates)[currentIndex] = 0.0f;
226  currentIndex++;
227  /* z */
228  (*coordinates)[currentIndex] = 1.0f;
229  currentIndex++;
230  /* w */
231  (*coordinates)[currentIndex] = 1.0f;
232  currentIndex++;
233 
234  if (numberOfCoordinates != NULL)
235  {
236  *numberOfCoordinates = numberOfSquareCoordinates;
237  }
238  }
239 
240  void PlaneModel::getNormals(int* numberOfCoordinates, float** normals)
241  {
242  if (normals == NULL)
243  {
244  LOGE("Cannot use null pointer while calculating coordinates.");
245 
246  return;
247  }
248 
249  /* 2 triangles, 3 points of triangle, 3 coordinates for each point. */
250  const int numberOfNormalsCoordinates = 2 * 3 * 3;
251  /* Index of an array we will put new point coordinates at. */
252  int currentIndex = 0;
253 
254  /* Allocate memory for result array. */
255  *normals = (float*) malloc (numberOfNormalsCoordinates * sizeof(float));
256 
257  /* Is allocation successfu?. */
258  if (*normals == NULL)
259  {
260  LOGE("Could not allocate memory for result array.");
261 
262  return;
263  }
264 
265  for (int i = 0; i < numberOfNormalsCoordinates; i+=3)
266  {
267  (*normals)[currentIndex] = 0.0f;
268  currentIndex++;
269  (*normals)[currentIndex] = 1.0f;
270  currentIndex++;
271  (*normals)[currentIndex] = 0.0f;
272  currentIndex++;
273  }
274 
275  if (numberOfCoordinates != NULL)
276  {
277  *numberOfCoordinates = numberOfNormalsCoordinates;
278  }
279  }
280 
281  void PlaneModel::transform(Matrix transform, int numberOfCoordinates, float** squareCoordinates)
282  {
283  /* Loop through all the coordinates and transform them using the rotation matrix. */
284  for (int allCoordinates = 0; allCoordinates < numberOfCoordinates; allCoordinates +=4)
285  {
286  Vec4f currentVertex = {(*squareCoordinates)[allCoordinates], (*squareCoordinates)[allCoordinates + 1], (*squareCoordinates)[allCoordinates + 2], (*squareCoordinates)[allCoordinates + 3]};
287 
288  Vec4f rotatedVertex = Matrix::vertexTransform(&currentVertex, &transform);
289 
290  (*squareCoordinates)[allCoordinates] = rotatedVertex.x;
291  (*squareCoordinates)[allCoordinates + 1] = rotatedVertex.y;
292  (*squareCoordinates)[allCoordinates + 2] = rotatedVertex.z;
293  (*squareCoordinates)[allCoordinates + 3] = rotatedVertex.w;
294  }
295  }
296 }
static void getTriangleRepresentation(int *numberOfCoordinates, float **coordinates)
Get coordinates of points which make up a plane. The plane is located in XZ space.
Definition: PlaneModel.cpp:117
Functions for manipulating matrices.
Definition: Matrix.h:31
static void transform(Matrix transform, int numberOfCoordinates, float **coordinates)
Transform a plane by a matrix.
Definition: PlaneModel.cpp:281
static void getTriangleRepresentationUVCoordinates(int *numberOfCoordinates, float **uvCoordinates)
Get U/V 2D texture coordinates that can be mapped onto a plane generated from this class...
Definition: PlaneModel.cpp:30
static Vec4f vertexTransform(Vec4f *vector, Matrix *matrix)
Transform a 4D vertex by a matrix.
Definition: Matrix.cpp:559
static void getNormals(int *numberOfCoordinates, float **normals)
Get normals for plane placed in XZ space.
Definition: PlaneModel.cpp:240
#define LOGE(...)
Definition: AstcTextures.h:30
A 4D floating point vector.
Definition: VectorTypes.h:127
GLfloat normals[]
Definition: Native.cpp:283