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  int* numberOfPointsPtr,
66  float scalingFactor)
67  {
68  /* Example:
69  * z D __________ C
70  * . | / |
71  * / \ | / |
72  * | | / |
73  * | |/_________|
74  * | A B
75  * |----------> x
76  */
77  /* Coordinates of a points: A, B, C and D as shown in a schema above. */
78  const Vec3f coordinatesOfPointA = {-1.0f, 0.0f, -1.0f };
79  const Vec3f coordinatesOfPointB = { 1.0f, 0.0f, -1.0f };
80  const Vec3f coordinatesOfPointC = { 1.0f, 0.0f, 1.0f };
81  const Vec3f coordinatesOfPointD = {-1.0f, 0.0f, 1.0f };
82 
83  ASSERT(coordinatesPtrPtr != NULL,
84  "Cannot use null pointer while calculating plane coordinates.")
85 
86  /* 2 triangles, 3 points of triangle, 3 coordinates for each point. */
87  const int numberOfPoints = NUMBER_OF_TRIANGLES_IN_QUAD *
89  const int numberOfSquareCoordinates = numberOfPoints *
90  NUMBER_OF_POINT_COORDINATES;
91 
92  /* Allocate memory for result array. */
93  *coordinatesPtrPtr = (float*) malloc(numberOfSquareCoordinates * sizeof(float));
94 
95  /* Is allocation successful? */
96  ASSERT(*coordinatesPtrPtr != NULL,
97  "Could not allocate memory for plane coordinates result array.")
98 
99  /* Index of an array we will put new point coordinates at. */
100  int currentIndex = 0;
101 
102  /* First triangle. */
103  /* A */
104  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.x;
105  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.y;
106  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.z;
107 
108  /* B */
109  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.x;
110  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.y;
111  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointB.z;
112 
113  /* C */
114  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.x;
115  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.y;
116  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.z;
117 
118  /* Second triangle. */
119  /* A */
120  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.x;
121  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.y;
122  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointA.z;
123  /* C */
124  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.x;
125  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.y;
126  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointC.z;
127  /* D */
128  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.x;
129  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.y;
130  (*coordinatesPtrPtr)[currentIndex++] = coordinatesOfPointD.z;
131 
132  if (scalingFactor != 1.0f)
133  {
134  for (int i = 0; i < numberOfSquareCoordinates; i++)
135  {
136  (*coordinatesPtrPtr)[i] *= scalingFactor;
137  }
138  }
139 
140  if (numberOfCoordinatesPtr != NULL)
141  {
142  *numberOfCoordinatesPtr = numberOfSquareCoordinates;
143  }
144 
145  if (numberOfPointsPtr != NULL)
146  {
147  *numberOfPointsPtr = numberOfPoints;
148  }
149  }
150 }
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
Mesh plane
Definition: app.cpp:40
#define NUMBER_OF_TRIANGLE_VERTICES
Number of vertices which make up a triangle shape.
Definition: Common.h:41
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
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
#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
GLint y
Definition: gl2ext.h:179