0

I've found how to export a 3D OpenGL scene to ppm. But is it possible to export to the STL format ? This is the format required for 3D printing.

There's a package for STL parsing but I don't know whether it is possible to use it with OpenGL.

Someone proposes to close this question for unclearness. I just want to know whether it is possible to export a 3D Haskell OpenGL graphic to a file appropriate for 3D printing (not necessarily STL, but as far as I know this is the only way - I may be wrong.) Sorry if it's still unclear, it may be due to my wording and my English. I do the best I can.

Just for info, here is an example with the R package rgl: enter image description here

And here is how the STL file looks like in ASCII representation (it takes 3.7 MB):

solid  testSTL.stl  produced by RGL
facet normal  -0.9965197 -0.05894169 -0.05894333 
outer loop
vertex  0.8633249 0 0 
vertex  0.8656725 0 -0.03968928 
vertex  0.8642709 0.0237557 -0.03974825 
endloop
endfacet
facet normal  -0.9965196 -0.05894321 -0.05894424 
outer loop
vertex  0.8633249 0 0 
vertex  0.8642709 0.0237557 -0.03974825 
vertex  0.8619304 0.0235772 0 
endloop
endfacet
facet normal  -0.9827041 -0.05841724 -0.1757272 
outer loop
vertex  0.8656725 0 -0.03968928 
vertex  0.8726366 0 -0.07863396 
vertex  0.8712143 0.02428522 -0.0787534 
endloop
endfacet
...

So that does not sound impossible to have a STL converter for Haskell...

EDIT

There's a possible way but I didn't manage on Windows (that's frustrating)

  • convert the OpenGL graphic to ppm using the gl-capture library; convert the ppm to svg using ImageMagick (or perhaps don't necessarily convert - the ppm can be opened in InkScape, see next steps)

  • install OpenSCAD and InkScape (there exist portable versions)

  • install this InkScape extension and follow the instructions

EDIT

Another possible way here. Similarly, I've failed.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 4
    There's no such thing as an "OpenGL scene". OpenGL is a rendering API, not a scene storage format. So it all depends on where your actual scene is coming from, and how it's stored. – Thomas Mar 20 '18 at 08:50
  • @Thomas Sorry for the wording, I simply mean a graphic made with Haskell OpenGL. – Stéphane Laurent Mar 20 '18 at 08:51
  • 2
    So you have made a 2D image and want to create a 3D description of it to send to a 3D printer? That's going to be impossible. I'm not just nitpicking here; OpenGL has nothing to do with all this. OpenGL is just for turning a series of draw commands into a 2D image. What you need is something to take the geometry as it is _before_ you feed it to OpenGL, and export that as STL instead. – Thomas Mar 20 '18 at 08:54
  • @Thomas It's possible with the R package `rgl`, which is also a wrapper of OpenGL. I will take a look at the code to see how it works. Looks like everything must be "converted" to triangles. – Stéphane Laurent Mar 20 '18 at 08:56
  • Looks like RGL is not just a rendering library; it also has representations of 3D geometry and ways to manipulate it. – Thomas Mar 20 '18 at 08:59
  • Yes, rgl is a "high-level" library. Very easier to use than Haskell OpenGL (at least for a R user). But less options I think. – Stéphane Laurent Mar 20 '18 at 09:00
  • if you can process your geometry than you can save STL on your own (it is just a simple text file or binary if you want). the problem is meeting the STL mesh properties. So you need to convert your scene to triangles that is easy but the connectivity is that each triangle shares each side completely with neighboring triangle so no subdivisions are present. Not all 3D meshes are done this way and may need re-triangulation. So can you access/iterate through every primitive (triangle/polygon) face of your geometry mesh? Is your mesh complete volume surface without holes? Do you meet conectivity? – Spektre Mar 20 '18 at 11:56
  • @Spektre Thanks for your comment. I have no idea about these technical details, unfortunately. Indeed, re-triangulation is needed. – Stéphane Laurent Mar 20 '18 at 11:58
  • @StéphaneLaurent than look for things like [Triangulation de Delaunay](https://fr.wikipedia.org/wiki/Triangulation_de_Delaunay). There must be tons of **STL** and geometry libs out there as **STL** is used commonly but I am afraid they are more for **C/C++/C#/Python** instead of **Haskell**. I prefer to use my own code for such things... saving **STL** is just few lines of code the real problem is extract data from your current geometry which we have no info about ... – Spektre Mar 20 '18 at 12:02
  • @StéphaneLaurent btw the STL restrictions are there for a reason so slicers do not need to use expensive computing to achieve slices and head positioning info as they often run in RT on lowend MCUs with almost no memory to speak of in comparison to PC... – Spektre Mar 20 '18 at 12:11
  • @Spektre By the way and FYI I've done a Haskell library for Delaunay triangulations, based on the qhull C library: https://github.com/stla/qhull. It works quite nice. – Stéphane Laurent Mar 20 '18 at 12:13
  • @Spektre There's a Voronoi tesselation in InkScape, so maybe there's a Delaunay triangulation as well. – Stéphane Laurent Mar 20 '18 at 12:14
  • @StéphaneLaurent IIRC InkScape is for 2D SVG you need 3D surface instead. The best you can do is use current mesh topology detect conflicts with STL and re-triangulate just those parts by subdividing triangles to match the edge connection. Most faces are low vertex polygons with defined winding rule so you can even use ear clipping ... confronting to local plane normal – Spektre Mar 20 '18 at 12:15
  • Ok @Spektre. I start to be convinced this is not possible like that. I gonna abandon. Thanks for the conversation and your advice. – Stéphane Laurent Mar 20 '18 at 12:17
  • @StéphaneLaurent it is possible but you would need to code it ... – Spektre Mar 20 '18 at 12:17
  • @Spektre Sure, everything is possible if we code :) Anyway I'm an advanced R user and I can use the `rgl` library to achieve my goal. – Stéphane Laurent Mar 20 '18 at 12:20
  • @Spektre I've coded it today for my Haskell library `qhull` for the convex hulls. It works nice. Would you know a way to convert from STL ascii to STL binary ? – Stéphane Laurent Mar 21 '18 at 12:37
  • they are almost the same you just save the float binary representation instead of text. IIRC The only difference is in that the header is constant size and you need to include triangle count, also no comments are allowed. see [Wiki: STL (file format)](https://en.wikipedia.org/wiki/STL_(file_format)) – Spektre Mar 21 '18 at 16:10
  • @Spektre Thanks. Yes I saw that but I don't master the wording (I don't know what is an endian byte integer, etc). – Stéphane Laurent Mar 21 '18 at 16:47
  • @StéphaneLaurent 32bit IEEE `float` (real32) has 4 bytes the 32 bit integer (uint32) too ... the little/big endianess is just in which order the 4 bytes are stored (if LSB or MSB firs)t. Some CPU's use one some the other and some are configurable. You would need binary file access in order to do this... then you simply convert your float variable address to BYTE pointer and save 4 bytes from that address if endianness match if not you would need to reverse the order first (or save byte by byte) ... even better is to have struct matching triangle and save/load that at once ... – Spektre Mar 21 '18 at 18:35
  • IIRC the attributes are a bit tricky and can be used to add more info like colors texturing etc, I remember i reverse it from binarry STL files until the mesh loads properly but would need to dig into source code to confirm ... – Spektre Mar 21 '18 at 18:41
  • Thanks @Spektre. But this is still too unclear to me for the moment. Anyway I could open my ASCII STL file in a software and set some attributes like color. Though I'm lost in these softwares lol (like Meshlab). – Stéphane Laurent Mar 22 '18 at 08:33
  • @StéphaneLaurent if it helps here [Generating outside supporters into mesh for 3D printing](https://stackoverflow.com/a/49555506/2521214) is mine STL loader in C++ (it loads both ASCII and binary STL's the save is just reverse of it (writing instead of reading ...) – Spektre Mar 29 '18 at 14:32

1 Answers1

0

The STL package you link can save STL files in the text format using textSTL, or in the binary format using encode . The latter uses the Serialize instance, so is easy to miss in the STL docs.

The library does not handle any attributes (color and so forth). PRs welcome!

bergey
  • 3,041
  • 11
  • 17
  • Yes, but to export an OpenGL graphic to STL we need all triangles and their normals of the graphic. As I understood from the comments, this is not possible with the final rendering of the graphic. – Stéphane Laurent Mar 22 '18 at 21:55
  • Yes, if by "OpenGL graphic" you mean a raster image, that's not really feasible. – bergey Mar 23 '18 at 01:34
  • I don't know the wording in this domain and my English is not terrible. That's why I have difficulties to explain. The Haskell OpenGL library only opens a window in which it displays the graphic. This graphic is interactive: you can rotate it, etc. So I find strange that there's nothing to export it in a 3D format at this stage. There only exists a library to export an image to `ppm` (a "raster image" as you say), to take a screenshot in other words. – Stéphane Laurent Mar 23 '18 at 06:29