Visualization Library 2.0.0-b3

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ioSTL.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include "ioSTL.hpp"
33 #include <vlCore/Log.hpp>
34 #include <vlCore/Say.hpp>
36 #include <vlCore/FileSystem.hpp>
37 #include <vlCore/TextStream.hpp>
38 #include <vlCore/VirtualFile.hpp>
39 #include <vlGraphics/Effect.hpp>
40 #include <vlGraphics/Actor.hpp>
42 #include <stdio.h>
43 
44 using namespace vl;
45 //-----------------------------------------------------------------------------
47 {
49 
50  if (file)
51  return loadSTL( file.get() );
52  else
53  {
54  Log::error( Say("Could not locate '%s'.\n") << path );
55  return NULL;
56  }
57 }
58 //-----------------------------------------------------------------------------
60 {
61  STLLoader stl;
62  ref<ResourceDatabase> res_db = stl.loadSTL(file);
63  return res_db;
64 }
65 //-----------------------------------------------------------------------------
67 {
68  // skip header
69  char header[80];
70  file->read( header,80 );
71  unsigned int tri_count = file->readUInt32();
72 
73  ref<ArrayFloat3> verts = new ArrayFloat3;
74  ref<ArrayFloat3> normals = new ArrayFloat3;
75  verts->resize(tri_count*3);
76  normals->resize(tri_count*3);
77  ref<DrawArrays> de = new DrawArrays(PT_TRIANGLES,0,tri_count*3);
78  ref<Geometry> geom = new Geometry;
79  geom->drawCalls().push_back(de.get());
80  geom->setVertexArray(verts.get());
81  geom->setNormalArray(normals.get());
82 
83  struct STriangle {
84  fvec3 n;
85  fvec3 v0;
86  fvec3 v1;
87  fvec3 v2;
88  vl::u16 abc; // attribute byte count
89  } tri;
90 
91  std::vector<u8> tris;
92  tris.resize( tri_count * 50 );
93  file->read( &tris[0], tri_count * 50 );
94 
95  // read triangles
96  for(unsigned int i=0; i<tri_count; ++i)
97  {
98  STriangle& tri = *((STriangle*)&tris[ i * 50 ]);
99 
100  normals->at(i*3+0) = tri.n;
101  normals->at(i*3+1) = tri.n;
102  normals->at(i*3+2) = tri.n;
103  verts->at(i*3+0) = tri.v0;
104  verts->at(i*3+1) = tri.v1;
105  verts->at(i*3+2) = tri.v2;
106  }
107 
109  ref<Effect> effect = new Effect;
110  res_db->resources().push_back( geom );
111  res_db->resources().push_back( new Actor(geom.get(), effect.get(), NULL ) );
112  res_db->resources().push_back( effect.get() );
113  return res_db;
114 }
115 //-----------------------------------------------------------------------------
117 {
118  TextStream line_reader;
119  line_reader.setInputFile(file);
120  std::string str;
121 
122  // skip header
123  line_reader.readLine(str);
124 
125  std::vector<fvec3> verts;
126  std::vector<fvec3> norms;
127 
128  fvec3 v[3], n;
129  char parola1[32];
130  char parola2[32];
131  while( line_reader.readLine(str) )
132  {
133  sscanf(str.c_str(), "%s %s %f %f %f", parola1, parola2, &n.x(), &n.y(), &n.z());
134  // skip outer loop
135  line_reader.readLine(str);
136  line_reader.readLine(str); sscanf(str.c_str(), "%s %f %f %f", parola1, &v[0].x(), &v[0].y(), &v[0].z());
137  line_reader.readLine(str); sscanf(str.c_str(), "%s %f %f %f", parola1, &v[1].x(), &v[1].y(), &v[1].z());
138  line_reader.readLine(str); sscanf(str.c_str(), "%s %f %f %f", parola1, &v[2].x(), &v[2].y(), &v[2].z());
139  // skip endloop
140  line_reader.readLine(str);
141  // skip endfacet
142  line_reader.readLine(str);
143  // emit triangle
144  norms.push_back(n);
145  norms.push_back(n);
146  norms.push_back(n);
147  verts.push_back(v[0]);
148  verts.push_back(v[1]);
149  verts.push_back(v[2]);
150  }
151 
152  ref<ArrayFloat3> vertices = new ArrayFloat3;
153  ref<ArrayFloat3> normals = new ArrayFloat3;
154  vertices->resize(verts.size());
155  normals->resize(verts.size());
156  memcpy(normals ->ptr(), &norms[0], sizeof(norms[0])*norms.size());
157  memcpy(vertices->ptr(), &verts[0], sizeof(verts[0])*verts.size());
158  ref<DrawArrays> de = new DrawArrays(PT_TRIANGLES,0,verts.size());
159  ref<Geometry> geom = new Geometry;
160  geom->drawCalls().push_back(de.get());
161  geom->setVertexArray(vertices.get());
162  geom->setNormalArray(normals.get());
163 
165  ref<Effect> effect = new Effect;
166  res_db->resources().push_back( geom );
167  res_db->resources().push_back( new Actor(geom.get(), effect.get(), NULL ) );
168  res_db->resources().push_back( effect.get() );
169  return res_db;
170 }
171 //-----------------------------------------------------------------------------
173 {
174  file->open(OM_ReadOnly);
175 
176  char header[] = {1,2,3,4,5,0};
177 
178  file->read(header,5);
179  ref<ResourceDatabase> res_db;
180  if ( strcmp( (char*)header, "solid" ) == 0 )
181  {
182  // ascii
183  file->seekSet(0);
184  res_db = loadAscii( file );
185  }
186  else
187  {
188  // binary
189  file->seekSet(0);
190  res_db = loadBinary( file );
191  }
192 
193  file->close();
194  return res_db;
195 }
196 //-----------------------------------------------------------------------------
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadSTL(VirtualFile *file)
Definition: ioSTL.cpp:59
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
Definition: init_core.cpp:153
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
bool readLine(std::string &utf8)
Definition: TextStream.hpp:67
A simple String formatting class.
Definition: Say.hpp:124
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const T_Scalar & z() const
Definition: Vector3.hpp:91
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:164
void setVertexArray(ArrayAbstract *data)
Conventional vertex array.
Definition: Geometry.cpp:155
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
Definition: FileSystem.cpp:61
void resize(size_t dim)
Definition: Array.hpp:233
void setNormalArray(ArrayAbstract *data)
Conventional normal array.
Definition: Geometry.cpp:164
ref< ResourceDatabase > loadBinary(VirtualFile *file)
Definition: ioSTL.cpp:66
ref< ResourceDatabase > loadAscii(VirtualFile *file)
Definition: ioSTL.cpp:116
void setInputFile(VirtualFile *file)
virtual void close()=0
Closes the file.
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
Visualization Library main namespace.
const unsigned char * ptr() const
Returns the pointer to the first element of the local buffer. Equivalent to bufferObject()->ptr() ...
Definition: Array.hpp:103
The TextStream class can be used to conveniently read or parse utf8-encoded text files.
Definition: TextStream.hpp:46
const std::vector< ref< Object > > & resources() const
const T_Scalar & y() const
Definition: Vector3.hpp:90
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
Loads an STL file.
Definition: ioSTL.hpp:91
#define NULL
Definition: OpenGLDefs.hpp:81
bool seekSet(long long offset)
Changes the current read/write position of a file.
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
T_VectorType & at(size_t i)
Definition: Array.hpp:255
const T_Scalar & x() const
Definition: Vector3.hpp:89
unsigned int readUInt32(bool little_endian_data=true)
Reads single entry.
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
An array of vl::fvec3.
Definition: Array.hpp:414
ref< ResourceDatabase > loadSTL(VirtualFile *file)
Loads a STL file.
Definition: ioSTL.cpp:172
Wraps the OpenGL function glDrawArrays().
Definition: DrawArrays.hpp:57
unsigned short u16
16 bits unsigned integer
Definition: std_types.hpp:47
The ResourceDatabase class contains and manipulates a set of resources.
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:102