Pi3D


216 posts   Page 2 of 9   1, 2, 3, 4, 5 ... 9
by paddyg » Mon Jun 25, 2012 6:29 pm
The way to make it all load fast seems to be to take out the python wrapper eglchars() completely. I haven't tested this exhaustively but it seems to work on a couple of things I've just tried so it should form the basis of 'a better method'. I use the ctypes.c_char_p() so only have to send a pointer, also don't need to convert it back with a byref. i.e.

image = ctype.c_char_p(im.convert(RGBs).tostring("raw",RGBs))
and later...
opengles.glTexImage2D(GL_TEXTURE_2D,0,RGBv,ix,iy,0,RGBv,GL_UNSIGNED_BYTE, image)

image loading goes from several seconds to fractions of a millisecond :)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d
User avatar
Posts: 850
Joined: Sat Jan 28, 2012 11:57 am
Location: Bingley, Yorkshire
by tipam » Mon Jun 25, 2012 9:11 pm
Hmm - I wonder if there's a way of passing pointers rather than copying strings?
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Mon Jun 25, 2012 11:21 pm
paddyg wrote:
paddyg wrote:.tostring("raw",RGBs)
actually it's the eglchars() method that takes all the time. I tried saving the images as 'raw' strings but it seemed to take the same time and this is why.


If at first you don't succeed try, try again (and don't give up!) ... I finally found a way that *instantly* loads the textures ...

im = Image.open(filestring)
ix = im.size[0]
iy = im.size[1]
image = im.convert("RGB").tostring("raw","RGB")
tex = eglint()
opengles.glGenTextures(1,ctypes.byref(tex))
opengles.glBindTexture(GL_TEXTURE_2D,tex)
opengles.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ix,iy,0, GL_RGB,GL_UNSIGNED_BYTE, ctypes.string_at(image, len(image)))

The ctypes.string_at function bypassed all the string conversions and basically passes a pointer to the image string.
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Mon Jun 25, 2012 11:23 pm
paddyg wrote:The way to make it all load fast seems to be to take out the python wrapper eglchars() completely. I haven't tested this exhaustively but it seems to work on a couple of things I've just tried so it should form the basis of 'a better method'. I use the ctypes.c_char_p() so only have to send a pointer, also don't need to convert it back with a byref. i.e.

image = ctype.c_char_p(im.convert(RGBs).tostring("raw",RGBs))
and later...
opengles.glTexImage2D(GL_TEXTURE_2D,0,RGBv,ix,iy,0,RGBv,GL_UNSIGNED_BYTE, image)

image loading goes from several seconds to fractions of a millisecond :)


... Ugh - you beat me to it!! ... thanks Paddy - I like yours better (my solution in last post!)
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by Huulivoide » Wed Jun 27, 2012 8:51 pm
Also on the CPU% load "problem"
On fully up-to-date Arch linux distro, all of the examples
cause CPU usage to go up to 100%
Posts: 26
Joined: Fri Aug 19, 2011 8:24 pm
by tipam » Thu Jun 28, 2012 9:45 am
Another update (inbetween work!) ...

Added environment cube ...
Image

Also working on bitmap fonts ... I've written a util that will convert fonts from your PC/Mac into a single image and export the Python code to read it ...

Image

Much of my time is taken up thinking of how to standardise pi3d functions and classes so that it's easy to understand and flexible for future devs. Not long now for next release!!
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by paddyg » Thu Jun 28, 2012 6:38 pm
Tim, looks good. Here are some pics of the latest iteration of loadModel. Here's a link to the files (large because of the 128 water pictures, sorry!) below pics is a copy of the readme in case you're interested but can't be bothered to load all the pics.
Image
Image
Image
files: http://www.eldwick.org.uk/files/loadModel_mods.zip
(as with previous zips this needs the various other support files from https://github.com/tipam/pi3d/tree/)

readme.txt:
Some notes on the loadModel() class in pi3d

__init__ takes just the file path/name:
If the name is not “” it generates a dict object (vGroup) where each key is a 'group' name in the egg file (separate object in blender). Parsing still takes a while, I might try and do conversion of the {} structure in one go.
Each group can have only one material or texture (which will be taken from the last polygon as the group is parsed)
Material used to add colour to the commented out code (from Tims cube) but these colours disappear when lighting used. Ideally the material properties would get set with glMaterialfv() but can't get that to work.
If the texture is set using load_textureAlpha it seems to scramble the transparency and the depth calculation, not just for loadModel() but for others such as plane.

clone():
method returns a new object in which vGroup points to the vGroup of self. i.e. positions and rotations are available and have their own instance but the 3d object data is just a pointer to the original

reparentTo(loadModel obj):
method adds obj to a list of loadModels in self (and sets a parent pointer in obj to point to self). The positions and rotations of obj are then relative to self and when self.draw() runs it recursively draws all its children after working out where they should be

draw():
method is pretty much as before but 1. sets glShadeModel(GL_SMOOTH) 2. loops through each vGroup and draws that with the relevant texture or material 3. draws all the children

there are two helper methods (outside loadModel):
extBracket() does the parsing out of the <tab> key { values } and could well be re-written to try to make it faster by avoiding re-parsing bits of the string.

rotVector():
uses the opengles methods to speed up the process (used for calculting the location of child objects)


Other comments.

See the load_textureAlpha() comment above.
I use ctypes.c_float arrays for vertices, normals, tex_coords and ctypes.c_short for the triangles
I have altered quite a bit of the light stuff from the original pi3d. I'm not sure which bits are necessary and which are random changes while trying to get something working that just don't obviously break anything!
I add the w dimension as a parameter in the __init__ as this is needed to define directional lights (0) and spotlights (-1) and xyz actually represents xyzw.
I made self.no = eglint(GL_LIGHT0 + no) as I think this is what it's supposed to be, certainly the on() method shouldn't just enable LIGHT0
I set lighton to False to start with and change it as relevant in the on() and off() methods
I tried but failed to get material properties such as shininess to work either in the light class or in the loadModel class (see above)
Loading image files now uses ctypes c_char_p to it speed up. Also tried various ways of using arrays for the vertices, normals etc and passing pointers rather than using the python wrapper but didn't manage.
I made an eggtexturecard in python rather than trying to get the functionality from parsing an egg. Part of the reason for this is that the timing functions for changing frames are not built into the pi3d module as yet (and possibly don't really need to be)
As per Tim's comment; it would be a good idea to split loadModel into core functionality for object in different formats (obj etc) and functionality specific to parsing panda3d eggs.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d
User avatar
Posts: 850
Joined: Sat Jan 28, 2012 11:57 am
Location: Bingley, Yorkshire
by paddyg » Fri Jun 29, 2012 11:37 am
paddyg wrote:I tried but failed to get material properties
also didn't get much joy from the camera class. Not really sure how to use it but I imagined the scene would change as the camera moved or turned as if viewed from the camera.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d
User avatar
Posts: 850
Joined: Sat Jan 28, 2012 11:57 am
Location: Bingley, Yorkshire
by tipam » Fri Jun 29, 2012 12:57 pm
paddyg wrote:Tim, looks good. Here are some pics of the latest iteration of loadModel. Here's a link to the files (large because of the 128 water pictures, sorry!) below pics is a copy of the readme in case you're interested but can't be bothered to load all the pics.
Image
Image
Image
files: http://www.eldwick.org.uk/files/loadModel_mods.zip
(as with previous zips this needs the various other support files from https://github.com/tipam/pi3d/tree/)

readme.txt:
Some notes on the loadModel() class in pi3d

__init__ takes just the file path/name:
If the name is not “” it generates a dict object (vGroup) where each key is a 'group' name in the egg file (separate object in blender). Parsing still takes a while, I might try and do conversion of the {} structure in one go.
Each group can have only one material or texture (which will be taken from the last polygon as the group is parsed)
Material used to add colour to the commented out code (from Tims cube) but these colours disappear when lighting used. Ideally the material properties would get set with glMaterialfv() but can't get that to work.
If the texture is set using load_textureAlpha it seems to scramble the transparency and the depth calculation, not just for loadModel() but for others such as plane.

clone():
method returns a new object in which vGroup points to the vGroup of self. i.e. positions and rotations are available and have their own instance but the 3d object data is just a pointer to the original

reparentTo(loadModel obj):
method adds obj to a list of loadModels in self (and sets a parent pointer in obj to point to self). The positions and rotations of obj are then relative to self and when self.draw() runs it recursively draws all its children after working out where they should be

draw():
method is pretty much as before but 1. sets glShadeModel(GL_SMOOTH) 2. loops through each vGroup and draws that with the relevant texture or material 3. draws all the children

there are two helper methods (outside loadModel):
extBracket() does the parsing out of the <tab> key { values } and could well be re-written to try to make it faster by avoiding re-parsing bits of the string.

rotVector():
uses the opengles methods to speed up the process (used for calculting the location of child objects)


Other comments.

See the load_textureAlpha() comment above.
I use ctypes.c_float arrays for vertices, normals, tex_coords and ctypes.c_short for the triangles
I have altered quite a bit of the light stuff from the original pi3d. I'm not sure which bits are necessary and which are random changes while trying to get something working that just don't obviously break anything!
I add the w dimension as a parameter in the __init__ as this is needed to define directional lights (0) and spotlights (-1) and xyz actually represents xyzw.
I made self.no = eglint(GL_LIGHT0 + no) as I think this is what it's supposed to be, certainly the on() method shouldn't just enable LIGHT0
I set lighton to False to start with and change it as relevant in the on() and off() methods
I tried but failed to get material properties such as shininess to work either in the light class or in the loadModel class (see above)
Loading image files now uses ctypes c_char_p to it speed up. Also tried various ways of using arrays for the vertices, normals etc and passing pointers rather than using the python wrapper but didn't manage.
I made an eggtexturecard in python rather than trying to get the functionality from parsing an egg. Part of the reason for this is that the timing functions for changing frames are not built into the pi3d module as yet (and possibly don't really need to be)
As per Tim's comment; it would be a good idea to split loadModel into core functionality for object in different formats (obj etc) and functionality specific to parsing panda3d eggs.


@ Paddy ... Excellent work! I'll get your code into the next release of Pi3D ... I've also got code which I can translate for VRML 2.0, 3DS and X files (currently written in RealBasic). Thanks also for getting the lights working - I must admit I only put lights and cameras for testing and hadn't really got them working. I'm still working on camera's which are still in the melting pot! Also, you might find this code snippet useful for taking screen shots from OpenGL ...

size = height * width * 3
img = (ctypes.c_char * size)()
opengles.glReadPixels(0,0,width,height, GL_RGB, GL_UNSIGNED_BYTE, ctypes.byref(img))
im = Image.frombuffer(“RGB”,(width, height), img, “raw”, “RGB”, 0, 1)
im = im.transpose(Image.FLIP_TOP_BOTTOM)
im.save(“screenshot.jpg”)

(width and height are your OpenGL window dimensions and it uses PIL to save).

Paddy, could you give me your email details on this address timskillman@gmail.com ? - it would be good to get you updates and converse a little more than through this forum. Tks - Tim.
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Tue Jul 03, 2012 12:38 am
Another update ...

Pi3D now has Paddy's Panda3D file loader and several more things have been added including some gaming ability. I'm working on a small shoot-em-up and made a few other demo's like 'ForestWalk' which runs in full screen at a good frame rate ...

Image
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by Huulivoide » Wed Jul 04, 2012 12:06 am
When will be the source code be up? Pictures are nice, but source code one can play with would be nicer ^_^
Posts: 26
Joined: Fri Aug 19, 2011 8:24 pm
by tipam » Wed Jul 04, 2012 7:52 am
Huulivoide wrote:When will be the source code be up? Pictures are nice, but source code one can play with would be nicer ^_^


Neeearly there! Just tieing up last loose ends for this version so there aren't huge changes with the interfaces next time - there will be a lot more stuff to play with! Friday I should get it out!!
Tim
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Fri Jul 06, 2012 1:14 am
Pi3D RELEASE 2 NOW UPLOADED!!

Go to http://www.github.com/tipam/pi3d for the latest release of Pi3D plus several demos that use the new features!!
Here's a very brief outline of what's been added ...

- Lots of built-in shapes: Sphere (hemi), Cone, Cylinder, Extrude, Tube, Lathe ...
- Paddy Gaunt's EGG file loader which is brilliant!
- New merge shape that merges together built-in shapes into a single vertex buffer (fast rendering) ... also includes a cluster method (to scatter trees for example) and a radialCopy feature
- Bitmap coloured fonts that work in 2D and 3D with lots of font's to play with
- Landscape generated from height map - including smooth terrain following and accurate shape placement.
- Environment cube mapping, helpers, screenshot feature etc....

Please note that there are unfinished feartures and bugs - this second release is for your experimentation and fun!

Tim Skillman

pics ...
Image
Image
Image
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Fri Jul 06, 2012 5:56 pm
If you haven't downloaded the latest update yet - GOOD! ... I've just updated again with fixes on lighting, normals, matrices and model loading. There is now a Shapes.py example to give you an idea of the shapes available. Tim..
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by Huulivoide » Mon Jul 09, 2012 12:33 pm
Here's litle something for those who wan't to put some moons to orbit planets or a whole group of RPi fans running around Eben holding a RPi ^_^

I'm working on a function that wil allow one to rotate object around a point in the 3d space, while maintaining a constant distance to it. Here is a video demonstrating it.

http://www.youtube.com/watch?v=b2M-J3LpdCg

Get the code from my 'spin' branch at https://github.com/Huulivoide/pi3d/tree/spin
Posts: 26
Joined: Fri Aug 19, 2011 8:24 pm
by tipam » Mon Jul 09, 2012 9:11 pm
Hi Jesse ... I must admit I haven't uploaded any heirarchical examples, but you can do this with the pi3d.matrix function. I'll upload an Earth2.py which should demonstrate how this is done with the moon orbiting at an angle with satellites orbiting the moon (see below). I'll also make an amend to pi3d.py as I didn't put all the def's in. Shader's should be coming soon!!
Tim

Image
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by tipam » Sat Jul 21, 2012 5:59 am
Not too sure if people are still watching this, but Pi3D has been updated with improved Texture handling (deletes textures on exit and automatically detects alpha). Also works for latest Raspbian Wheezy as there were a couple of bugs. Get the latest update from http://www.github.com/tipam/pi3d.
Tim.
Posts: 44
Joined: Fri Dec 30, 2011 1:32 pm
by portets » Sat Jul 21, 2012 8:35 am
Nice, thanks for the work. :) Been messing around with it a little. Excited for pi3d2! :D
Posts: 185
Joined: Sat Oct 29, 2011 6:24 am
by Gibble » Sat Jul 21, 2012 9:30 am
Yep, indeed, thanks for all the work. I'm keeping an eye out for 2.0 as well, I'd been fiddling with 2.0 a bit, as it feels like its the way to go for learning, so will definitely be playing with this is as well :)
Posts: 56
Joined: Wed May 09, 2012 9:52 am
by Jim Manley » Sat Jul 21, 2012 1:47 pm
Great work, guys!

I've been watching your progress and I was wondering if anyone has found any gotchas using Pi3D on the just-released Wheezy Raspbian hard-float version.

We are having our first Raspberry Jam in Silicon Valley at the Computer History Museum from 9 PM Saturday to 1 AM Sunday GMT and I want to show off as much of the Pi's 3-D capabilities as possible. The museum is in what was originally Silicon Graphics' international marketing building, so, we will literally be in The House That GL Built! :D

Thanks and keep up the fantastic effort!
The best things in life aren't things ... but, a Pi comes pretty darned close! :D
"Education is not the filling of a pail, but the lighting of a fire." -- W.B. Yeats
In theory, theory & practice are the same - in practice, they aren't!!!
User avatar
Posts: 1356
Joined: Thu Feb 23, 2012 8:41 pm
Location: SillyCon Valley, California, USA
by SN » Sat Jul 21, 2012 3:49 pm
so how far has anybody pushed the poly count on this?
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?
User avatar
Posts: 1008
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
by juantar » Thu Jul 26, 2012 5:31 pm
This might be a stupid question, but is there any way of practicing and learning to use Pi3D in a Windows machine? The reason I ask is that I spent most of my time in a Windows machine, so I would like to be able to write the python scripts in the Windows Desktop and then copying the python files to my raspberry pi when I get home.
Posts: 2
Joined: Wed Jul 18, 2012 1:17 pm
by JamesR » Sun Aug 05, 2012 11:23 pm
Should this run under windows with python installed or am I being silly?
If egg files are recommended which software is recommended for the modelling and which exporter (versions) Do I need to do UV maps etc...

Cheers
Posts: 78
Joined: Sun Aug 05, 2012 11:21 pm
by Gibble » Thu Aug 09, 2012 7:50 pm
juantar wrote:This might be a stupid question, but is there any way of practicing and learning to use Pi3D in a Windows machine? The reason I ask is that I spent most of my time in a Windows machine, so I would like to be able to write the python scripts in the Windows Desktop and then copying the python files to my raspberry pi when I get home.


I don't think this will be possible, if you mean eliminating the Pi entirely. You can remote ssh into the Pi from Windows, but you will still need the Pi to display and process the 3d.

Something like PyGame may be possible if you just want to some 2d game stuff in Python on Windows and the Pi and transfer between.
Posts: 56
Joined: Wed May 09, 2012 9:52 am
by Gibble » Thu Aug 09, 2012 7:54 pm
JamesR wrote:Should this run under windows with python installed or am I being silly?
If egg files are recommended which software is recommended for the modelling and which exporter (versions) Do I need to do UV maps etc...

Cheers


This won't run under Windows. I think egg files are create in Panda3D. There are some converters out there I think that could possibly convert from one format to another.

Blender is another modelling program that you could use to export your files into pi3d without too much hassle as an obj format.
Posts: 56
Joined: Wed May 09, 2012 9:52 am