3D library

Are you developing tools to extend the LiveCode environment? This is the place to talk about the nuts and bolts of extending our nuts and bolts. If you want to use a LiveCode or third party Environment extension, visit the Using Evironment Extensions forum.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Thu May 29, 2014 12:04 pm

May I post a couple links that could be of interest
http://www.grasshopper3d.com/forum/topi ... g-multiple
I like the idea of storing colors into one bitmap

http://www.grasshopper3d.com/group/slingshot
uses mySQL

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Thu May 29, 2014 1:20 pm

FredBeck wrote:Nice, I wasn't expecting so much! Thank you so much for sharing! I'll take a look soon and post my questions.
Can you put an example of
#"world" contains a list of object to render:
# it has
#[1]["model"] (contains faces and points of the object)
#[1]["properties"] (contains translation, rotation and other transformation of the object, very useful in animation)
#[1]["color"] (the color to render the object)
Ok, I'll try:
world is an array containing quite all informations of all objects present in the world to render.
For example:
  • world[1] contains a red cube
  • world[2] contains a blue pyramid
  • world[3] contains a green apple
Inside each element there are other data, for example:
  • world[1]["model"] contains all geometry data (vertex and faces)
  • world[1]["properties"] contains all transformation data, like translations and rotations from the initial position of the vertex. It's just a 4x4 matrix.
  • world[1]["color"] contain the color, like red or 255,0,0
Now the best trick: inside the model there are the vertex position and each face description which contains a list of the vertex delimiting the face itself. A triangle has just 3 vertex, a rectangle has 4 vertexes, and so on. This way you don't need to simplify a face in triangles 8) !
Fore example a cube has (it isn't real code, but it show how it works):

Code: Select all

# vertices
        [ 0 0 0 ] #point 1
        [ 1 0 0 ] #point 2
        [ 1 1 0 ] #point 3 and so on...
        [ 0 1 0 ]
        [ 0 0 1 ] #point 5
        [ 1 0 1 ] #point 6
        [ 1 1 1 ] #point 7 and so on...
        [ 0 1 1 ] 
#faces
    # faces - anticlockwise winding
    #just six face square, instead of 12 triangles
        [ 4 3 2 1]  # face made of  points 4, 3, 2 and 1
        [ 5 6 7 8 ]
        [ 1 5 8 4 ] # face made of point 1, 5, 8 and 4
        [ 1 2 6 5]
        [ 2 3 7 6 ]
        [ 8 7 3 4  ]        
It's hard to work with matrix and arrays in Livecode (see function r3d_inverse_m4), but I quite finished. :mrgreen:
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Thu May 29, 2014 4:15 pm

It works :D :
Image

Now I'd like to speed up drawing, at the present the code is:

Code: Select all

set the rect of the templateimage to 0,0,400,360
if exists(image r3d) then
delete image r3d
end if
create image"r3d"
set the brush to 1
choose the polygon tool
set the brushcolor to 170,0,0
set the pencolor to 170,0,0
drag from 174,192 to 200,210
drag from 200,210 to 200,180
drag from 200,180 to 172,163
drag from 172,163 to 174,192
choose the browser tool
choose the polygon tool
set the brushcolor to 184,0,0
set the pencolor to 184,0,0
drag from 199,149 to 172,163
drag from 172,163 to 200,180
drag from 200,180 to 227,163
drag from 227,163 to 199,149
choose the browser tool
choose the polygon tool
set the brushcolor to 163,0,0
set the pencolor to 163,0,0
drag from 227,163 to 200,180
drag from 200,180 to 200,210
drag from 200,210 to 224,192
drag from 224,192 to 227,163
choose the browser tool
Any suggestions?
Is it correct to switch continuosly between browser tool and polygon tool to draw many polygons?
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Thu May 29, 2014 7:22 pm

Cool!
I'd like to keep up with you, but I'm having small health issues :(
like malte says, better go with graphic controls. Here is the thing with polyline I told you about
https://dl.dropboxusercontent.com/u/683 ... ylines.zip
it's much easier this way and probably faster.

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Thu May 29, 2014 8:22 pm

Hey I noticed you wanted off file format. Never heard of it... Does it carry color?
I think ply may be better and more spread. I'll look into it.

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Fri May 30, 2014 12:00 pm

Here's a PLY parser, along with two test .ply files Did I get the world array right?
I didn't try your stuff yet.
https://dl.dropboxusercontent.com/u/683 ... parser.zip

Code: Select all


function parsePLY pFile
   
   set the itemDelimiter to space
   
   ##  separate header from model data
   put lineOffset("end_header", pFile) into tEndHeaderLine
   put line 1 to tEndHeaderLine of pFile into tHeader
   put line tEndHeaderLine + 1 to -1 of pFile into tData
   
   ##  get number of vertices and faces
   put lineOffset("element vertex",  tHeader) into tVertexNumLine
   put item -1 of line tVertexNumLine of pFile into tVertexNumber
   put lineOffset("element face", tHeader) into tFaceNumberLine
   put item -1 of line tFaceNumberLine of pFile into tFaceNumber
   
   ##  separate vertex anf faces data
   
   put line 1 to tVertexNumber of tData into tRawVertices
   put line (tVertexNumber + 1) to (tVertexNumber + tFaceNumber) of tData into tRawFaces
   
   
   ##  register points and vertex colors
   repeat with p = 1 to tVertexNumber
      put line p of tRawVertices into tRawVertex
      repeat with q = 1 to 3
         put word q of tRawVertex into tVertexArr [p] [q]
      end repeat
      put word 4 of tRawVertex into tVertexColorArr [p]
   end repeat
   
   ##  register faces 
   repeat with p = 1 to tFaceNumber
      put line p of tRawFaces into tRawFace
      put word 1 of tRawFace into tVcount
      
      
      repeat with q = 2 to tVcount + 1
         put word q of tRawFace into tIndex
         put tIndex into tFaceArr [p] [q]  
         
         ##  register face color
         put tVertexColorArr [tIndex] into tNewColor
         put  tColorArr [p] into tColor
         get AverageColor(tNewColor, tColor)
         put tColor into tColorArr [p]
      end repeat
   end repeat
   
   put tVertexArr into tWorld ["model"] ["points"]
   put tFaceArr into tWorld ["model"]  ["faces"]
   put tColorArr into tWorld ["color"]
   
   return tWorld
end parsePLY


function AverageColor pNewColor @pColor
   set the itemDelimiter to comma
   if pColor is not empty then
      repeat with x = 1 to 3
         put ((item x of pNewColor + item x of pColor) / 2) into item x of pColor
      end repeat
   else
      put pNewColor into pColor
   end if
   return pColor
end AverageColor

function displayArrayData pArray, pIndent
   # create the variable that loops through the keys in the array
   local tKey
   if pArray is an array then
      # print information to indicate that we are entering a new nested level of the array
      get "Array" & return
      # print full stops that allow the reader to track the depth of an element
      put "." after pIndent
      # create the indentation
      put tab after pIndent
      repeat for each key tKey in pArray
         # call displayArrayData with a nested array
         put format("%s[%s] => %s\n", pIndent, tKey, displayArrayData (pArray[tKey], pIndent)) after it
      end repeat
      delete the last char of it
      return it
   else
      return pArray
   end if
end displayArrayData

EDIT : oops sorry wrong file I updated the link and code
EDIT 2 : the color thing is a mess I'm fixing it

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Fri May 30, 2014 2:20 pm

FIXED - colors are good now!

Code: Select all

function parsePLY pFile
   
   set the itemDelimiter to space
   
   ##  separate header from model data
   put lineOffset("end_header", pFile) into tEndHeaderLine
   put line 1 to tEndHeaderLine of pFile into tHeader
   put line tEndHeaderLine + 1 to -1 of pFile into tData
   
   ##  get number of vertices and faces
   put lineOffset("element vertex",  tHeader) into tVertexNumLine
   put item -1 of line tVertexNumLine of pFile into tVertexNumber
   put lineOffset("element face", tHeader) into tFaceNumberLine
   put item -1 of line tFaceNumberLine of pFile into tFaceNumber
   
   ##  separate vertex anf face data
   put line 1 to tVertexNumber of tData into tRawVertices
   put line (tVertexNumber + 1) to (tVertexNumber + tFaceNumber) of tData into tRawFaces
   
   
   ##  register points and vertex colors
   repeat with p = 1 to tVertexNumber
      put line p of tRawVertices into tRawVertex
      repeat with q = 1 to 3
         put word q of tRawVertex into tVertexArr [p] [q]
      end repeat
      repeat with r = 1 to 3
         put word (r + 3) of tRawVertex into tVertexColorArr [p] [r]
      end repeat
   end repeat
   
   
   ##  register faces 
   repeat with p = 1 to tFaceNumber
      put line p of tRawFaces into tRawFace
      put word 1 of tRawFace into tVcount
      repeat with q = 2 to tVcount + 1
         put word q of tRawFace into tIndex
         put tIndex into tFaceArr [p] [q - 1]  
      end repeat
   end repeat
   
   
   ##  register face colors
   repeat for each key tFaceKey in tFaceArr
      put tFaceArr [tFaceKey] into tArr
      repeat for each key tVertexKey in tArr 
         put tArr [tVertexKey] into tIndex
         put tVertexColorArr [tIndex] into tNewColor
         get AverageColor(tNewColor, tColor)
      end repeat
      combine tColor using comma
      put tColor into tColorArr [tFaceKey]
   end repeat
   
   
   put tVertexArr into tWorld ["model"] ["points"]
   put tFaceArr into tWorld ["model"]  ["faces"]
   put tColorArr into tWorld ["color"]
   
   return tWorld
end parsePLY


function AverageColor pNewColor @pColor
   ## blends the nth vertex color with the face color
   repeat with x = 1 to 3
      if pColor [x] is not empty then
         put round((pNewColor [x] + x * pColor [x]) / (x + 1)) into pColor [x]
      else 
         put pNewColor [x]  into pColor [x]
      end if
   end repeat
   return pColor
end AverageColor


## from Livecode Lessons
function displayArrayData pArray, pIndent
   # create the variable that loops through the keys in the array
   local tKey
   if pArray is an array then
      # print information to indicate that we are entering a new nested level of the array
      get "Array" & return
      # print full stops that allow the reader to track the depth of an element
      put "." after pIndent
      # create the indentation
      put tab after pIndent
      repeat for each key tKey in pArray
         # call displayArrayData with a nested array
         put format("%s[%s] => %s\n", pIndent, tKey, displayArrayData (pArray[tKey], pIndent)) after it
      end repeat
      delete the last char of it
      return it
   else
      return pArray
   end if
end displayArrayData

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: 3D library

Post by ChessGredon » Fri May 30, 2014 8:47 pm

That is really fantastic topic guys thanks for posting your work.

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Sat May 31, 2014 1:08 pm

Hi max,
Here's the last version of the ply parser.
PLYparser 0.2.zip
my stack with 2 additional test .ply
(182.02 KiB) Downloaded 117 times
Now it will handle different format of ply, provided it is ascii encoded.
all information but points, faces and color will be ignored. if color is absent, then it it set to white.
I couldn't make your code work. all I have is a list of "-31.813371;400,300:399,303:399,303:399,303" in the "tutto" field... Maybe I didn't set up the world array in the right way.
Could you make any progress regarding the graphics way?
Looking forward to hear from you.
F.

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Mon Jun 02, 2014 10:20 pm

FredBeck wrote:Hi max,
Here's the last version of the ply parser.
PLYparser 0.2.zip
Now it will handle different format of ply, provided it is ascii encoded.
all information but points, faces and color will be ignored. if color is absent, then it it set to white.
I couldn't make your code work. all I have is a list of "-31.813371;400,300:399,303:399,303:399,303" in the "tutto" field... Maybe I didn't set up the world array in the right way.
Could you make any progress regarding the graphics way?
Looking forward to hear from you.
F.
If you look at https://github.com/angerangel/LCR3D/blob/master/r3d.txt now there is the r3d_load_OFF function, it loads and convert file and add the scale factor to the model, this way is centered on the screen. :mrgreen:
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Mon Jun 02, 2014 10:57 pm

Hi all,
this video shows how the library build slowly a 3D solid: https://www.youtube.com/watch?v=Iry7hOWVLCs
I'll post a complete easy to use stack soon this month, at the present I have a lot of work for the next 2 weeks. Keep your eyes open on https://github.com/angerangel/LCR3D :D
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: 3D library

Post by Simon » Tue Jun 03, 2014 3:11 am

Fantastic stuff MaxV!

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FredBeck
Posts: 76
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: 3D library

Post by FredBeck » Tue Jun 03, 2014 11:13 am

Awesome! Now I'll be able to fix my ply stuff and test a couple ideas I have regarding the drawing process. I don't really understand the forking and pulling on github but this is going to be fun! Thanks again for sharing :wink:

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Wed Jun 18, 2014 1:52 pm

Status update: the first step is done, all OFF models are correctly imported and rendered.
You can use https://github.com/angerangel/LCR3D/raw ... D.livecode stack ready to use to load and render OFF models.
There are 2 buttons in the stack to load OFF model: one renders drawing it, the other one render using vector graphic.
If you create the standalone version of the stack, it's quicker and vector graphic version has no holes (that in Livecode IDE are generated by IDE to permit editing by mouse).
OFF models are in the current folder: https://github.com/angerangel/LCR3D/tre ... /3D-models

All the library is written also in plain text on https://github.com/angerangel/LCR3D/blob/master/r3d.txt , please use this file to propose patch and changes. This way control version of the library is very easy. :mrgreen:

You are welcome to test it on your machine, to suggest any idea to improve it. :D I'd like to speed up rendering process. :wink: It's a livecode native library, so it should work on any device and on any operating system.

You are also welcome to fork the project and play with it on https://github.com/angerangel/LCR3D :wink:

Next step is a stack where you can play with objects and camera using scrollers: rotation, translation, deformations and so on... :mrgreen:

This is a screenshot of the teapot test:
Image
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1540
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: 3D library

Post by MaxV » Fri Jun 27, 2014 3:45 pm

Update: added guide about library usage here: https://github.com/angerangel/LCR3D/wiki
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply

Return to “Extending LiveCode”