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) 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 "PlaneModel.h"
22 #include "Common.h"
23 
24 #include <cassert>
25 
26 namespace MaliSDK
27 {
28  void PlaneModel::getTriangleRepresentationUVCoordinates(int* numberOfCoordinates, float** uvCoordinates)
29  {
30  /* Example:
31  * v D __________ C
32  * . | / |
33  * / \ | / |
34  * | | / |
35  * | |/_________|
36  * | A B
37  * |----------> u
38  */
39  ASSERT(uvCoordinates != NULL, "Cannot use null pointer while calculating coordinates.");
40 
41  /* 2 triangles, 3 points of triangle, 2 coordinates for each point. */
42  const int numberOfUVCoordinates = numberOfSquareTriangles *
45 
46  /* Allocate memory for result array. */
47  *uvCoordinates = (float*) malloc (numberOfUVCoordinates * sizeof(float));
48 
49  /* Is allocation successful? */
50  ASSERT(*uvCoordinates != NULL, "Could not allocate memory for result array.");
51 
52  /* Index of an array we will put new point coordinates at. */
53  int currentIndex = 0;
54 
55  /*** First triangle. ***/
56  /* A */
57  /* u */
58  (*uvCoordinates)[currentIndex++] = 0.0f;
59  /* v */
60  (*uvCoordinates)[currentIndex++] = 0.0f;
61  /* B */
62  /* u */
63  (*uvCoordinates)[currentIndex++] = 1.0f;
64  /* v */
65  (*uvCoordinates)[currentIndex++] = 0.0f;
66  /* C */
67  /* u */
68  (*uvCoordinates)[currentIndex++] = 1.0f;
69  /* v */
70  (*uvCoordinates)[currentIndex++] = 1.0f;
71 
72  /*** Second triangle. ***/
73  /* A */
74  /* u */
75  (*uvCoordinates)[currentIndex++] = 0.0f;
76  /* v */
77  (*uvCoordinates)[currentIndex++] = 0.0f;
78  /* C */
79  /* u */
80  (*uvCoordinates)[currentIndex++] = 1.0f;
81  /* v */
82  (*uvCoordinates)[currentIndex++] = 1.0f;
83  /* D */
84  /* u */
85  (*uvCoordinates)[currentIndex++] = 0.0f;
86  /* v */
87  (*uvCoordinates)[currentIndex++] = 1.0f;
88 
89  if (numberOfCoordinates != NULL)
90  {
91  *numberOfCoordinates = numberOfUVCoordinates;
92  }
93  }
94 
95  /* [Generate triangular representation of a plane] */
96  void PlaneModel::getTriangleRepresentation(int* numberOfPoints, int* numberOfCoordinates, float** coordinates)
97  {
98  /* Example:
99  * z D __________ C
100  * . | / |
101  * / \ | / |
102  * | | / |
103  * | |/_________|
104  * | A B
105  * |----------> x
106  */
107  ASSERT(coordinates != NULL, "Cannot use null pointer while calculating coordinates.");
108 
109  /* Define point coordinates. */
110  const Vec4f pointA = {-1.0f, 0.0f, -1.0f, 1.0f};
111  const Vec4f pointB = { 1.0f, 0.0f, -1.0f, 1.0f};
112  const Vec4f pointC = { 1.0f, 0.0f, 1.0f, 1.0f};
113  const Vec4f pointD = {-1.0f, 0.0f, 1.0f, 1.0f};
114 
115  /* 2 triangles, 3 points of triangle, 4 coordinates for each point. */
116  const int numberOfSquarePoints = numberOfSquareTriangles *
118  const int numberOfSquareCoordinates = numberOfSquarePoints *
120 
121  /* Allocate memory for result array. */
122  *coordinates = (float*) malloc (numberOfSquareCoordinates * sizeof(float));
123 
124  /* Is allocation successful? */
125  ASSERT(*coordinates != NULL, "Could not allocate memory for result array.");
126 
127  /* Index of an array we will put new point coordinates at. */
128  int currentIndex = 0;
129 
130  /* First triangle. */
131  /* A */
132  (*coordinates)[currentIndex++] = pointA.x;
133  (*coordinates)[currentIndex++] = pointA.y;
134  (*coordinates)[currentIndex++] = pointA.z;
135  (*coordinates)[currentIndex++] = pointA.w;
136  /* B */
137  (*coordinates)[currentIndex++] = pointB.x;
138  (*coordinates)[currentIndex++] = pointB.y;
139  (*coordinates)[currentIndex++] = pointB.z;
140  (*coordinates)[currentIndex++] = pointB.w;
141  /* C */
142  (*coordinates)[currentIndex++] = pointC.x;
143  (*coordinates)[currentIndex++] = pointC.y;
144  (*coordinates)[currentIndex++] = pointC.z;
145  (*coordinates)[currentIndex++] = pointC.w;
146 
147  /* Second triangle. */
148  /* A */
149  (*coordinates)[currentIndex++] = pointA.x;
150  (*coordinates)[currentIndex++] = pointA.y;
151  (*coordinates)[currentIndex++] = pointA.z;
152  (*coordinates)[currentIndex++] = pointA.w;
153  /* C */
154  (*coordinates)[currentIndex++] = pointC.x;
155  (*coordinates)[currentIndex++] = pointC.y;
156  (*coordinates)[currentIndex++] = pointC.z;
157  (*coordinates)[currentIndex++] = pointC.w;
158  /* D */
159  (*coordinates)[currentIndex++] = pointD.x;
160  (*coordinates)[currentIndex++] = pointD.y;
161  (*coordinates)[currentIndex++] = pointD.z;
162  (*coordinates)[currentIndex++] = pointD.w;
163 
164  if (numberOfPoints != NULL)
165  {
166  *numberOfPoints = numberOfSquarePoints;
167  }
168 
169  if (numberOfCoordinates != NULL)
170  {
171  *numberOfCoordinates = numberOfSquareCoordinates;
172  }
173  }
174  /* [Generate triangular representation of a plane] */
175 
176  /* [Get plane normals] */
177  void PlaneModel::getNormals(int* numberOfCoordinates, float** normals)
178  {
179  ASSERT(normals != NULL, "Cannot use null pointer while calculating coordinates.");
180 
181  /* 2 triangles, 3 points of triangle, 3 coordinates for each point. */
182  const int numberOfNormalsCoordinates = numberOfSquareTriangles *
185 
186  /* Index of an array we will put new point coordinates at. */
187  int currentIndex = 0;
188 
189  /* Allocate memory for result array. */
190  *normals = (float*) malloc (numberOfNormalsCoordinates * sizeof(float));
191 
192  /* Is allocation successful? */
193  ASSERT(*normals != NULL, "Could not allocate memory for result array.");
194 
195  for (int i = 0; i < numberOfNormalsCoordinates; i += numberOfPointCoordinates)
196  {
197  (*normals)[currentIndex++] = 0.0f;
198  (*normals)[currentIndex++] = 1.0f;
199  (*normals)[currentIndex++] = 0.0f;
200  (*normals)[currentIndex++] = 1.0f;
201  }
202 
203  if (numberOfCoordinates != NULL)
204  {
205  *numberOfCoordinates = numberOfNormalsCoordinates;
206  }
207  }
208  /* [Get plane normals] */
209 
210  void PlaneModel::transform(Matrix transform, int numberOfCoordinates, float** squareCoordinates)
211  {
212  /* Loop through all the coordinates and transform them using the rotation matrix. */
213  for (int allCoordinates = 0; allCoordinates < numberOfCoordinates; allCoordinates += numberOfPointCoordinates)
214  {
215  Vec4f currentVertex = {(*squareCoordinates)[allCoordinates],
216  (*squareCoordinates)[allCoordinates + 1],
217  (*squareCoordinates)[allCoordinates + 2],
218  (*squareCoordinates)[allCoordinates + 3]};
219 
220  Vec4f rotatedVertex = Matrix::vertexTransform(&currentVertex, &transform);
221 
222  (*squareCoordinates)[allCoordinates] = rotatedVertex.x;
223  (*squareCoordinates)[allCoordinates + 1] = rotatedVertex.y;
224  (*squareCoordinates)[allCoordinates + 2] = rotatedVertex.z;
225  (*squareCoordinates)[allCoordinates + 3] = rotatedVertex.w;
226  }
227  }
228 }
static const int numberOfSquareTriangles
Number of triangles which make up a square.
Definition: PlaneModel.h:38
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
static const int numberOfPointCoordinates
Number of coordinates for a point in 3D space.
Definition: PlaneModel.h:36
static const int numberOfPointUvCoordinates
Number of coordinates for point UVs.
Definition: PlaneModel.h:42
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 const int numberOfTrianglePoints
number of points that make up a shape of a triangle.
Definition: PlaneModel.h:40
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 ASSERT(x, s)
Definition: common.h:45
A 4D floating point vector.
Definition: VectorTypes.h:127
GLfloat normals[]
Definition: Native.cpp:283