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 "Common.h"
22 #include "PlaneModel.h"
23 #include "Mathematics.h"
24 
25 namespace MaliSDK
26 {
28  void PlaneModel::getNormals(float** normalsPtrPtr,
29  int* numberOfCoordinatesPtr)
30  {
31  ASSERT(normalsPtrPtr != NULL,
32  "Cannot use null pointer while calculating coordinates.");
33 
34  /* 2 triangles, 3 points of triangle, 3 coordinates for each point. */
35  const int numberOfNormalsCoordinates = NUMBER_OF_TRIANGLES_IN_QUAD *
38 
39  /* Allocate memory for result array. */
40  *normalsPtrPtr = (float*) malloc(numberOfNormalsCoordinates * sizeof(float));
41 
42  /* Is allocation successful? */
43  ASSERT(*normalsPtrPtr != NULL,
44  "Could not allocate memory for result array.");
45 
46  /* Index of an array we will put new point coordinates at. */
47  int currentIndex = 0;
48 
49  for (int i = 0; i < numberOfNormalsCoordinates; i += NUMBER_OF_TRIANGLE_VERTICES)
50  {
51  (*normalsPtrPtr)[currentIndex++] = 0.0f;
52  (*normalsPtrPtr)[currentIndex++] = 1.0f;
53  (*normalsPtrPtr)[currentIndex++] = 0.0f;
54  }
55 
56  if (numberOfCoordinatesPtr != NULL)
57  {
58  *numberOfCoordinatesPtr = numberOfNormalsCoordinates;
59  }
60  }
61 
63  void PlaneModel::getTriangleRepresentation(float** coordinatesPtrPtr,
64  int* numberOfCoordinatesPtr,
65  float scalingFactor)
66  {
67  /* Example:
68  * z D __________ C
69  * . | / |
70  * / \ | / |
71  * | | / |
72  * | |/_________|
73  * | A B
74  * |----------> x
75  */
76  /* Coordinates of a points: A, B, C and D as shown in a schema above. */
77  const Vec3f coordinatesOfPointA = {-1.0f, 0.0f, -1.0f };
78  const Vec3f coordinatesOfPointB = { 1.0f, 0.0f, -1.0f };
79  const Vec3f coordinatesOfPointC = { 1.0f, 0.0f, 1.0f };
80  const Vec3f coordinatesOfPointD = {-1.0f, 0.0f, 1.0f };
81 
82  ASSERT(coordinatesPtrPtr != NULL,
83  "Cannot use null pointer while calculating plane coordinates.")
84 
85  /* 2 triangles, 3 points of triangle, 3 coordinates for each point. */
86  const int numberOfSquareCoordinates = NUMBER_OF_TRIANGLES_IN_QUAD *
89 
90  /* Allocate memory for result array. */
91  *coordinatesPtrPtr = (float*) malloc(numberOfSquareCoordinates * sizeof(float));
92 
93  /* Is allocation successful? */
94  ASSERT(*coordinatesPtrPtr != NULL,
95  "Could not allocate memory for plane coordinates result array.")
96 
97  /* Index of an array we will put new point coordinates at. */
98  int currentIndex = 0;
99 
100  /* First triangle. */
101  /* A */
102  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.x;
103  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.y;
104  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.z;
105 
106  /* B */
107  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.x;
108  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.y;
109  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.z;
110 
111  /* C */
112  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.x;
113  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.y;
114  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.z;
115 
116  /* Second triangle. */
117  /* A */
118  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.x;
119  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.y;
120  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.z;
121  /* C */
122  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.x;
123  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.y;
124  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.z;
125  /* D */
126  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.x;
127  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.y;
128  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.z;
129 
130  if (scalingFactor != 1.0f)
131  {
132  for (int i = 0; i < numberOfSquareCoordinates; i++)
133  {
134  (*coordinatesPtrPtr)[i] *= scalingFactor;
135  }
136  }
137 
138  if (numberOfCoordinatesPtr != NULL)
139  {
140  *numberOfCoordinatesPtr = numberOfSquareCoordinates;
141  }
142  }
143 }
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
#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
GLfloat GLfloat f
Definition: gl2ext.h:2707
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
#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