2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "geometry-api.h"
23 #include <dali/public-api/object/type-registry.h>
27 #include <rendering/geometry-wrapper.h>
28 #include <object/property-buffer-api.h>
39 * Geometry is handle to an object that can be used to define a geometric elements.
45 Geometry GeometryApi::GetGeometry( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
47 v8::HandleScope handleScope( isolate );
49 v8::Local<v8::Object> object = args.This();
50 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField(0) );
51 void* ptr = field->Value();
53 GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(ptr);
54 return wrapper->GetGeometry();
57 Geometry GeometryApi::GetGeometryFromParams( int paramIndex,
60 const v8::FunctionCallbackInfo< v8::Value >& args )
64 v8::HandleScope handleScope( isolate );
65 BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::GEOMETRY, isolate, args );
69 GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(wrappedObject);
70 return wrapper->GetGeometry();
79 * Create a new geometry object.
84 * @return {Object} Geometry
86 Geometry GeometryApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
88 return Geometry::New();
92 * Add a PropertyBuffer to be used as source of geometry vertices
94 * @method addVertexBuffer
96 * @param {Object} vertexBuffer PropertyBuffer to be used as source of geometry vertices
97 * @return {interger} Index of the newly added buffer, can be used with RemoveVertexBuffer
98 * to remove this buffer if no longer required
101 * var vertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2 };
102 * var vertexData = [ 0, 1,
108 * var vertexDataArray = new Float32Array(vertexData.length);
109 * vertexDataArray.set(vertexData, 0);
110 * var vertices = new dali.PropertyBuffer(vertexFormat, 5);
111 * vertices.setData(vertexDataArray);
113 * var geometry = new dali.Geometry();
114 * geometry.addVertexBuffer( vertices );
117 void GeometryApi::AddVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
119 v8::Isolate* isolate = args.GetIsolate();
120 v8::HandleScope handleScope( isolate );
122 Geometry geometry = GetGeometry( isolate, args );
125 PropertyBuffer vertexBuffer = PropertyBufferApi::GetPropertyBufferFromParams( 0, found, isolate, args );
128 DALI_SCRIPT_EXCEPTION( isolate, "invalid property buffer parameter" );
132 args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.AddVertexBuffer(vertexBuffer) ) );
137 * Retrieve the number of vertex buffers that have been added to this geometry
139 * @method getNumberOfVertexBuffers
141 * @return {integer} Number of vertex buffers that have been added to this geometry
143 void GeometryApi::GetNumberOfVertexBuffers( const v8::FunctionCallbackInfo<v8::Value>& args )
145 v8::Isolate* isolate = args.GetIsolate();
146 v8::HandleScope handleScope( isolate );
148 Geometry geometry = GetGeometry( isolate, args );
150 args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetNumberOfVertexBuffers() ) );
154 * Remove a vertex buffer
156 * @method removeVertexBuffer
158 * @param {integer} index Index to the vertex buffer to remove,
159 * which must be between 0 and getNumberOfVertexBuffers()
161 void GeometryApi::RemoveVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args )
163 v8::Isolate* isolate = args.GetIsolate();
164 v8::HandleScope handleScope( isolate );
166 Geometry geometry = GetGeometry( isolate, args );
169 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
172 DALI_SCRIPT_EXCEPTION( isolate, "missing index from param 0" );
176 geometry.RemoveVertexBuffer(static_cast<std::size_t>(index));
181 * Set a PropertyBuffer to be used as a source of indices for the geometry
183 * This buffer is required to have exactly one component and it must be of the
184 * type dali.PROPERTY_INTEGER
186 * By setting this buffer it will cause the geometry to be rendered using indices.
187 * To unset call SetIndexBuffer with an empty handle.
189 * @method setIndexBuffer
191 * @param {Uint32Array} data The data that will be copied to the buffer
194 * var indexData = [0, 1, 1, 2, 2, 3, 3, 4, 4, 0];
195 * var indexDataArray = new Uint32Array(indexData.length);
196 * indexDataArray.set(indexData, 0);
198 * geometry.SetIndexBuffer( indexDataArray, indexDataArray.length );
201 void GeometryApi::SetIndexBuffer( const v8::FunctionCallbackInfo<v8::Value>& args )
203 v8::Isolate* isolate = args.GetIsolate();
204 v8::HandleScope handleScope( isolate );
206 Geometry geometry = GetGeometry( isolate, args );
209 void* data = V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args);
213 DALI_SCRIPT_EXCEPTION( isolate, "invalid data parameter" );
217 unsigned int size = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0);
220 DALI_SCRIPT_EXCEPTION( isolate, "missing buffer size from param 1" );
224 Dali::Vector<unsigned short> indices;
225 indices.Resize( size );
226 unsigned int* indexData = static_cast<unsigned int*>(data);
227 for( size_t i(0); i<size; ++i )
229 indices[i] = indexData[i];
231 geometry.SetIndexBuffer( &indices[0], size );
237 * Set the type of primitives this geometry contains
241 * @param {integer} geometryType Type of primitives this geometry contains
243 * // geometry type is one of the following
244 * dali.GEOMETRY_POINTS
245 * dali.GEOMETRY_LINES
246 * dali.GEOMETRY_LINE_LOOP
247 * dali.GEOMETRY_LINE_STRIP
248 * dali.GEOMETRY_TRIANGLES
249 * dali.GEOMETRY_TRIANGLE_FAN
250 * dali.GEOMETRY_TRIANGLE_STRIP
252 * geometry.SetType( dali.GEOMETRY_LINES );
254 void GeometryApi::SetType( const v8::FunctionCallbackInfo< v8::Value >& args )
256 v8::Isolate* isolate = args.GetIsolate();
257 v8::HandleScope handleScope( isolate );
259 Geometry geometry = GetGeometry( isolate, args );
262 int geometryType = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
265 DALI_SCRIPT_EXCEPTION( isolate, "missing geometryType from param 0" );
269 geometry.SetType(static_cast<Geometry::Type>(geometryType));
274 * Get the type of primitives this geometry contains
278 * @return {integer} Type of primitives this geometry contains
280 * // returns one of the following
281 * dali.GEOMETRY_POINTS
282 * dali.GEOMETRY_LINES
283 * dali.GEOMETRY_LINE_LOOP
284 * dali.GEOMETRY_LINE_STRIP
285 * dali.GEOMETRY_TRIANGLES
286 * dali.GEOMETRY_TRIANGLE_FAN
287 * dali.GEOMETRY_TRIANGLE_STRIP
289 void GeometryApi::GetType( const v8::FunctionCallbackInfo< v8::Value >& args )
291 v8::Isolate* isolate = args.GetIsolate();
292 v8::HandleScope handleScope( isolate );
294 Geometry geometry = GetGeometry( isolate, args );
296 args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetType() ) );
299 } // namespace V8Plugin