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 {Object} indexBuffer PropertyBuffer to be used as a source of indices
194 void GeometryApi::SetIndexBuffer( const v8::FunctionCallbackInfo<v8::Value>& args )
196 v8::Isolate* isolate = args.GetIsolate();
197 v8::HandleScope handleScope( isolate );
199 Geometry geometry = GetGeometry( isolate, args );
202 PropertyBuffer indexBuffer = PropertyBufferApi::GetPropertyBufferFromParams( 0, found, isolate, args );
205 DALI_SCRIPT_EXCEPTION( isolate, "invalid property buffer parameter" );
209 geometry.SetIndexBuffer(indexBuffer);
214 * Set the type of primitives this geometry contains
216 * @method setGeometryType
218 * @param {integer} geometryType Type of primitives this geometry contains
220 * // geometry type is one of the following
221 * dali.GEOMETRY_POINTS
222 * dali.GEOMETRY_LINES
223 * dali.GEOMETRY_LINE_LOOP
224 * dali.GEOMETRY_LINE_STRIP
225 * dali.GEOMETRY_TRIANGLES
226 * dali.GEOMETRY_TRIANGLE_FAN
227 * dali.GEOMETRY_TRIANGLE_STRIP
229 * geometry.SetGeometryType( dali.GEOMETRY_LINES );
231 void GeometryApi::SetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
233 v8::Isolate* isolate = args.GetIsolate();
234 v8::HandleScope handleScope( isolate );
236 Geometry geometry = GetGeometry( isolate, args );
239 int geometryType = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */);
242 DALI_SCRIPT_EXCEPTION( isolate, "missing geometryType from param 0" );
246 geometry.SetGeometryType(static_cast<Geometry::GeometryType>(geometryType));
251 * Get the type of primitives this geometry contains
253 * @method getGeometryType
255 * @return {integer} Type of primitives this geometry contains
257 * // returns one of the following
258 * dali.GEOMETRY_POINTS
259 * dali.GEOMETRY_LINES
260 * dali.GEOMETRY_LINE_LOOP
261 * dali.GEOMETRY_LINE_STRIP
262 * dali.GEOMETRY_TRIANGLES
263 * dali.GEOMETRY_TRIANGLE_FAN
264 * dali.GEOMETRY_TRIANGLE_STRIP
266 void GeometryApi::GetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args )
268 v8::Isolate* isolate = args.GetIsolate();
269 v8::HandleScope handleScope( isolate );
271 Geometry geometry = GetGeometry( isolate, args );
273 args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetGeometryType() ) );
276 } // namespace V8Plugin