NGL  6.5
The NCCA Graphics Library
NCCABinMesh.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <cstring>
18 #include <iostream>
19 #include "NCCABinMesh.h"
20 #include <memory>
21 //----------------------------------------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------------------------------------
25 
26 namespace ngl
27 {
28 
29 //----------------------------------------------------------------------------------------------------------------------
30 bool NCCABinMesh::load(const std::string &_fname,bool _calcBB) noexcept
31 {
32 
33  // open a file stream for ip in binary mode
34  std::fstream file;
35  file.open(_fname.c_str(),std::ios::in | std::ios::binary);
36  // see if it worked
37  if (!file.is_open())
38  {
39  std::cerr<<"problems Opening File "<<_fname<<std::endl;
40  return false;
41  }
42  // lets read in the header and see if the file is valid
43  char header[9];
44  file.read(header,8*sizeof(char));
45  header[8]=0; // for strcmp we need \n
46  // basically I used the magick string ngl::bin (I presume unique in files!) and
47  // we test against it.
48  if(strcmp(header,"ngl::bin"))
49  {
50  // best close the file and exit
51  file.close();
52  std::cout<<"this is not an ngl::bin file "<<std::endl;
53  return false;
54  }
55 
57  file.read(reinterpret_cast <char *> (&m_nVerts),sizeof(unsigned long int));
59  file.read(reinterpret_cast <char *>(&m_nNorm),sizeof(unsigned long int));
61  file.read(reinterpret_cast <char *>(&m_nTex),sizeof(unsigned long int));
63  file.read(reinterpret_cast <char *>(&m_nFaces),sizeof(unsigned long int));
64  // now the x,y,z for the center could re-calc but why bother
65  file.read(reinterpret_cast <char *>(&m_center.m_x),sizeof(Real));
66  file.read(reinterpret_cast <char *>(&m_center.m_y),sizeof(Real));
67  file.read(reinterpret_cast <char *>(&m_center.m_z),sizeof(Real));
68  // now if texture is active
69  file.read(reinterpret_cast <char *>(&m_texture),sizeof(bool));
70  // the dimensions of the Bounding box
71  file.read(reinterpret_cast <char *>(&m_maxX),sizeof(Real));
72  file.read(reinterpret_cast <char *>(&m_maxY),sizeof(Real));
73  file.read(reinterpret_cast <char *>(&m_maxZ),sizeof(Real));
74  file.read(reinterpret_cast <char *>(&m_minX),sizeof(Real));
75  file.read(reinterpret_cast <char *>(&m_minY),sizeof(Real));
76  file.read(reinterpret_cast <char *>(&m_minZ),sizeof(Real));
77  // now how our data is packed for the VBO
78  file.read(reinterpret_cast <char *>(& m_dataPackType),sizeof(GLuint));
79  file.read(reinterpret_cast <char *>(& m_indexSize),sizeof(unsigned int));
80  file.read(reinterpret_cast <char *>(& m_bufferPackSize),sizeof(unsigned int));
82  unsigned int size;
83  file.read(reinterpret_cast <char *>(&size),sizeof(unsigned int));
84  // allocate some memory to read into
85  std::unique_ptr <Real>vboMem( new Real[size]);
86  // then read into this buffer
87  file.read(reinterpret_cast<char *>(vboMem.get()),size);
88  // now we need the index arrays so first find how big
89  file.read(reinterpret_cast <char *>(&size),sizeof(unsigned int));
90  // now re-size our std::vector so add the data
91  m_outIndices.resize(size);
92  // then loop and copy the values in from the file.
93  for( unsigned int i=0; i<size; ++i)
94  {
95  file.read(reinterpret_cast <char *>(&m_outIndices[i]),sizeof(unsigned int));
96  }
97 
98  // now were done with the file lets close it
99 
100  file.close();
101  // and now allocate this as a vbo
102  glGenBuffers(1, &m_vboBuffers);
103  glBindBuffer(GL_ARRAY_BUFFER, m_vboBuffers);
104 
105  // resize buffer
106  glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr> (m_indexSize*m_bufferPackSize*sizeof(GLfloat)), vboMem.get(), GL_DYNAMIC_DRAW);
107  // create the BBox for the obj
108  if(_calcBB)
109  {
110  m_ext.reset(new BBox(m_minX,m_maxX,m_minY,m_maxY,m_minZ,m_maxZ) );
111  }
112  m_vbo=true;
113  return true;
114 }
115 
116 //----------------------------------------------------------------------------------------------------------------------
118 {
119  m_vbo=false;
120  m_ext=0;
121  m_texture = false;
122  load(_fname);
123  }
124 
125 //----------------------------------------------------------------------------------------------------------------------
126 NCCABinMesh::NCCABinMesh(const std::string& _fname,const std::string& _texName ) noexcept:AbstractMesh()
127 {
128  m_vbo=false;
129  m_ext=0;
130  load(_fname);
131  // load texture
132  loadTexture(_texName);
133  m_texture = true;
134 
135 }
136 
137 //----------------------------------------------------------------------------------------------------------------------
138 void NCCABinMesh::save( const std::string& _fname) noexcept
139 {
141 }
142 
143 
144 
145 } //end ngl namespace
146 
147 
unsigned int GLuint
Definition: glew.h:280
inherited from AbstractMesh export and load binary data in our own format
NCCABinMesh() noexcept
default constructor
Definition: NCCABinMesh.h:54
void save(const std::string &_fname) noexcept
method to save the obj
implementation files for RibExport class
Definition: AABB.cpp:22
GLuint in
Definition: glew.h:11550
float GLfloat
Definition: glew.h:289
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
an abstract base mesh used to build specific meshes such as Obj
Definition: AbstractMesh.h:105
const GLuint GLenum const void * binary
Definition: glew.h:3514
#define glGenBuffers
Definition: glew.h:1709
void saveNCCABinaryMesh(const std::string &_fname) noexcept
save the mesh as NCCA Binary VBO format basically this format is the processed binary vbo mesh data a...
#define GL_DYNAMIC_DRAW
Definition: glew.h:1674
bool load(const std::string &_fname, bool _calcBB=true) noexcept
Method to load the file in.
Definition: NCCABinMesh.cpp:30
GLsizeiptr size
Definition: glew.h:1684
Simple Bounding box class used in various parts of ngl and other example programs.
Definition: BBox.h:40
#define glBindBuffer
Definition: glew.h:1703
#define GL_ARRAY_BUFFER
Definition: glew.h:1648
#define glBufferData
Definition: glew.h:1704
GLsizei const GLchar *const * string
Definition: glew.h:1847