Try PBworks for yourself
View
 

Third Layar Tutorial - layer with 2d and 3d objects

Page history last edited by xuan wang 3 months, 4 weeks ago

 

Please NOTE that this documentation will not be updated anymore. Please see our new Developer Documentation Site for the latest updates!

In this third tutorial, we will explain how to include 2d and 3d objects in a layer.

 

In general, there are two types of layer, the characteristics of each type can be summarized as below in the table. For more information, please check Edit a layer page.

 

layer type
POI representative
POI type
POI dimension
JSON Response
Generic (2D)
custom POI widget (CIW) sets that can be defined under "Look and feel" tab of layer editing section on the publishing site.
A type number is given when a set of CIW is defined. This number should be filled in the "type" field of a POI.

dimension:1 (default value). 

 

Object and Transform are not needed in the JSON response.

3D and 2D objects in 3D space
POIs are represented by 2d images or 3d objects. 
Custom CIWs can also be defined here. These icons will be used when 2d or 3d objects are not loaded. 

2d Image: 2

3d object: 3

 

Object and Transform are mandatory in the JSON response.

 

Some developers have asked about the differences between using custom CIWs and 2d images in 3D space, the table below provides some comparisons:

 

POI representative
Definition
 Usage
Custom CIWs
Each CIW set contains four icons which are used to represent POIs in focus, inner circle,  middle circle and outer circle status, respectively. Each icon has a fixed size.
When POIs can be divided into different groups and each group needs the same icon set. The icon set is not directly related to the POI content.
2D image
More manipulations can be done, such as image size, rotation angle, reduced image, icon, etc.  
When POI representative is used to reflect the POI content, such as billboard, photograph, etc. 

 

Before diving into the code, please read create 3d objects carefully to learn how to create a good 3d object.

 

The changes made to implement 2d and 3d objects are:

 

  • MySQL database - Add two tables to the database, namely "OBJECT_Table" and "TRANSFORM_Table".
  • PHP code - Add two functions  "Getobject" and "Gettransform" to retrieve object and transform applied to the POI. 

 

These changes are made based on the sample code from the Second Tutorial . It is a good idea to review the previous tutorials before moving on.

 

1. Create two separate tables for Object and Transform

 

Two tables are created to store Object and Transform dictionary defined in GetPOIs-JSON Response , namely "OBJECT_Table" and "TRANSFORM_Table". Each table has its own id field as primary key and a POI id field to store the corresponding POI Id in POI_Table (POI_Table is created in the first tutorial to store POIs info).

 

The MySQL statement for creating these two tables are:


--
-- Table structure for table `OBJECT_Table`
--

CREATE TABLE IF NOT EXISTS `OBJECT_Table` (
  `ID` int(10) NOT NULL auto_increment,
  `poiID` varchar(255) NOT NULL,
  `baseURL` varchar(255) NOT NULL,
  `full` varchar(255) NOT NULL,
  `reduced` varchar(255) default NULL,
  `icon` varchar(255) default NULL,
  `size` float(15,5) NOT NULL,
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

 


 


--
-- Table structure for table `TRANSFORM_Table`
--

CREATE TABLE IF NOT EXISTS `TRANSFORM_Table` (
  `ID` int(10) NOT NULL auto_increment,
  `poiID` varchar(255) NOT NULL,
  `rel` tinyint(1) default '0',
  `angle` decimal(5,2) default '0.00',
  `scale` decimal(12,2) NOT NULL default '1.00',
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; 

 


 

An example of how each table looks like is shown as below:

 

  • An example of one object description in "OBJECT_Table" .

 

 

  •  An example of one transformation in "TRANSFORM_Table".

 

 

 

 

2. Create function Getobject to retrieve object assigned to a POI

 

  • Use a SQL statement to retrieve object that has the same poi ID as the POI in POI_Table.
  • Assign retrieved object information to $poi["object"] array.

 


// Put fetched object related parameters for each POI into an associative array. The returned values are assigned to $poi[object].
//
// Arguments:
//   poi ; The POI handler.
//   $db ; The database connection handler.
//
// Returns:
//   array ; An array of received object related parameters for this POI.
//
function Getobject( $poi, $db ) {
 
  // A new table called "OBJECT_Table" is created to store object related parameters, namely "baseURL", "full", "reduced", "icon" and "size".
  // "poiID" which shows the POI id that this object belongs to.
  // The SQL statement returns object which has the same poiID as the id of $poi ($poi['id']).
  $sql_object = $db->prepare( " SELECT baseURL, full, reduced, icon, size
                                                  FROM OBJECT_Table
                                                  WHERE poiID = :id
                                                  LIMIT 0,1 " );
                                 
  // Binds the named parameter markers ":id" to the specified parameter values "$poi['id']".                               
  $sql_object->bindParam( ':id', $poi['id'], PDO::PARAM_INT );
    
  // Use PDO::execute() to execute the prepared statement $sql_object.
  $sql_object->execute();
    
  // Fetch the poi object.
  $object = $sql_object->fetchAll( PDO::FETCH_ASSOC );
 
  /* Process the $object result */
 
  // if $object array is empty, return NULL.
  if ( empty( $object ) ) {
 
      $poi["object"] = null;
      
  }//if
  else {
      // Since each POI only has one object. Logically, only one object should be returned. Assign the first object in the array to $poi["object"]
    $poi["object"] = $object[0];
    
    // Change "size" type to float.
    $poi["object"]["size"] = ChangetoFloat( $poi["object"]["size"] );
    
   }//else
   
   return $poi["object"];

}//Getobject


 

3. Create function Gettransform to retrieve transform applied to the POI

 

  • Use a SQL statement to retrieve transform that has the same poi ID as the POI in POI_Table.
  • Assign retrieved transform information to $poi["transform"] array.

 

// Put fetched transform related parameters for each POI into an associative array. The returned values are assigned to $poi[transform].
//
// Arguments:
//   poi ; The POI handler.
//   $db ; The database connection handler.
//
// Returns:
//   array ; An array of received transform related parameters for this POI.
//
function Gettransform( $poi, $db ) {
 
  // A new table called "TRANSFORM_Table" is created to store transform related parameters, namely "rel", "angle" and "scale"
  // "poiID" which shows the POI id that this transform belongs to.
  // The SQL statement returns transform which has the same poiID as the id of $poi ($poi['id']).
  $sql_transform = $db->prepare( " SELECT rel, angle, scale
                                                        FROM TRANSFORM_Table
                                                        WHERE poiID = :id
                                                        LIMIT 0,1 " );
                                 
  // Binds the named parameter markers ":id" to the specified parameter values "$poi['id']".                               
  $sql_transform->bindParam( ':id', $poi['id'], PDO::PARAM_INT );
    
  // Use PDO::execute() to execute the prepared statement $sql_transform.
  $sql_transform->execute();
    
  // Fetch the poi transform.
  $transform = $sql_transform->fetchAll( PDO::FETCH_ASSOC );
 
  /* Process the $transform result */
 
  // if $transform array is empty, return NULL.
  if ( empty( $transform ) ) {
 
      $poi["transform"] = null;
      
  }//if
  else {
      // Since each POI only has one transform. Logically, only one transform should be returned. Assign the first transform in the array to $poi["transform"]
    $poi["transform"] = $transform[0];
    
    // Change the value of "rel" into boolean value,if the value is NULL, return NULL.
    $poi["transform"]["rel"] = ChangetoBool( $poi["transform"]["rel"] );
    
    // Change the values of "angle" and "scale" to demical.
    $poi["transform"]["angle"] = ChangetoFloat( $poi["transform"]["angle"] );
    $poi["transform"]["scale"] = ChangetoFloat( $poi["transform"]["scale"] );
  }//else
 
   return $poi["transform"];

}//Gettransform


 

4. In function Gethotspots, Added the following lines to retrieve relevant object and transform for each POI.

 


// If POI "dimension" =2 or 3, use function Getobject() to return an object associated with the current POI.
          if ($poi["dimension"] == '2' || $poi["dimension"] == '3')
              $poi["object"] = Getobject ( $poi, $db);
      
// If POI "dimension" =2 or 3, use function Gettransform() to return a transform dictionary associated with the current POI.
          if ($poi["dimension"] == '2' || $poi["dimension"] == '3')
              $poi["transform"] = Gettransform ( $poi, $db);


 

Sample code can be found HERE . With this tutorial, you can already create a layer with actions and 3d objects enabled. For more information on how 2d and 3d objects are rendered on Layar client, please read here .  Enjoy it!

 

 

 

 

Comments (2)

Menno said

at 1:13 pm on Jul 12, 2011

Thanks for the effort of the tutorial, but I believe it's not complete. I can't seem to get it working either.
I miss a couple of things:

1. What modifications should I do to the Layer in the Layar Publishing section in order to enable 2d and 3d objects?
I've set the Layer type to "2D and 3D objects in 3D space".

2. The example data for the object table is non existent. (http://custom.layar.nl/layarimage_full.jpg for example) so I replaced that for content on my own server where I used the exact same image names, but just changed the hostname and obviously uploaded 3 Jpeg images (128x128px, 64x64px and 16x16px), the largest being 7Kb.
Later I tried again with png images

3. In the database I've changed the dimension of my test POI to 2 and 3. (found that in the "GetPOIs-JSON Response" documentation, it's not mentioned in this article)
When testing with dimension = 2 I get no error but also see no changes. Just the normal POI indicator "lens".
I'm testing the layer on my Android device, and I never receive any calls to those images on my server. I do ibviously see the calls to the hotspot lookup script.

When testing with dimension = 3 I get an error that "The 3D models could not be loaded. Please try to refresh." However, still no calls to my server.

Testing the layer works fine. No errors.

Any thoughts?

Menno said

at 2:36 pm on Jul 12, 2011

Aaargh! Issue found: Lacking trailing slash behind the hostname of my server.

You don't have permission to comment on this page.