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 int size = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0);
220 DALI_SCRIPT_EXCEPTION( isolate, "missing buffer size from param 1" );
224 geometry.SetIndexBuffer( static_cast<const unsigned short*>(data), size );
230 * Set the type of primitives this geometry contains
232 * @method setGeometryType
234 * @param {integer} geometryType Type of primitives this geometry contains
236 * // geometry type is one of the following
237 * dali.GEOMETRY_POINTS
238 * dali.GEOMETRY_LINES
239 * dali.GEOMETRY_LINE_LOOP
240 * dali.GEOMETRY_LINE_STRIP
241 * dali.GEOMETRY_TRIANGLES
242 * dali.GEOMETRY_TRIANGLE_FAN
243 * dali.GEOMETRY_TRIANGLE_STRIP
245 * geometry.SetGeometryType( dali.GEOMETRY_LINES );
247 void GeometryApi::SetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
249 v8::Isolate* isolate = args.GetIsolate();
250 v8::HandleScope handleScope( isolate );
252 Geometry geometry = GetGeometry( isolate, args );
255 int geometryType = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
258 DALI_SCRIPT_EXCEPTION( isolate, "missing geometryType from param 0" );
262 geometry.SetGeometryType(static_cast<Geometry::GeometryType>(geometryType));
267 * Get the type of primitives this geometry contains
269 * @method getGeometryType
271 * @return {integer} Type of primitives this geometry contains
273 * // returns one of the following
274 * dali.GEOMETRY_POINTS
275 * dali.GEOMETRY_LINES
276 * dali.GEOMETRY_LINE_LOOP
277 * dali.GEOMETRY_LINE_STRIP
278 * dali.GEOMETRY_TRIANGLES
279 * dali.GEOMETRY_TRIANGLE_FAN
280 * dali.GEOMETRY_TRIANGLE_STRIP
282 void GeometryApi::GetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
284 v8::Isolate* isolate = args.GetIsolate();
285 v8::HandleScope handleScope( isolate );
287 Geometry geometry = GetGeometry( isolate, args );
289 args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetGeometryType() ) );
292 } // namespace V8Plugin