OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
vertex_array.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef OST_VERTEX_ARRAY_HH
20 #define OST_VERTEX_ARRAY_HH
21 
22 /*
23  encapsulation of vertex-array based OpenGL rendering
24 
25  Author: Ansgar Philippsen
26 */
27 
28 #include <vector>
29 #include <cassert>
30 #include <iostream>
31 #include <cmath>
32 
33 #include <ost/log.hh>
34 #include <ost/geom/geom.hh>
36 
37 
38 #include "color.hh"
39 #include "material.hh"
40 #include "gfx_prim.hh"
41 #include "povray_fw.hh"
42 #include "exporter_fw.hh"
43 
44 namespace ost { namespace gfx {
45 
46 typedef unsigned int VertexID;
47 typedef unsigned int LineID;
48 typedef unsigned int TriID;
49 typedef unsigned int QuadID;
51  public:
52  struct DLLEXPORT Entry {
54  {
55  v[0]=0.0; v[1]=0.0; v[2]=0.0;
56  n[0]=0.0; n[1]=0.0; n[2]=1.0;
57  c[0]=0.0; c[1]=0.0; c[2]=0.0; c[3]=0.0;
58  t[0]=0.0; t[1]=0.0;
59  }
60 
61  Entry(const geom::Vec3& vv, const geom::Vec3& nn, const Color& cc,
62  const geom::Vec2& tt)
63  {
64  v[0]=vv[0]; v[1]=vv[1]; v[2]=vv[2];
65  n[0]=nn[0]; n[1]=nn[1]; n[2]=nn[2];
66  c[0]=cc[0]; c[1]=cc[1]; c[2]=cc[2]; c[3]=cc[3];
67  t[0]=tt[0]; t[1]=tt[1];
68  }
69  float t[2];
70  float c[4];
71  float n[3];
72  float v[3];
73  };
74 
77  TriID tri_list[50]; // this is a hack until the iso-contouring is fixed
79  float weight;
80  };
81 
84  VertexID id0,id1,id2;
86  float weight;
87  };
88 
89  typedef std::vector<Entry> EntryList;
90  typedef std::vector<VertexID> IndexList;
91  typedef std::vector<NormalizerVertexEntry> NVEntryList;
92  typedef std::vector<NormalizerTriEntry> NTEntryList;
93 
96 
98  IndexedVertexArray& operator=(const IndexedVertexArray& va);
99 
100  static unsigned int GetFormat();
101 
102  void Cleanup();
103 
104  /*
105  bitmask : 0 = render points only
106  1 = render lines
107  2 = render tris and quads
108  */
109  void SetMode(int m);
110  int GetMode() const;
111 
112  // polygon mode, 2:fill, 1:line, 0:point (different to overall mode)
113  void SetPolyMode(int m);
114 
115  void SetLighting(bool f);
116  void SetTwoSided(bool f);
117  void SetCullFace(bool f);
118  void SetColorMaterial(bool f);
119  void SetLineWidth(float lw);
120  void SetAALines(bool f);
121  void SetPointSize(float ps);
122  void SetLineHalo(float lh);
123 
124  void SetOutlineMode(int m);
125  int GetOutlineMode() const {return outline_mode_;}
126  void SetOutlineWidth(float f);
127  float GetOutlineWidth() const {return outline_width_;}
128  void SetOutlineMaterial(const Material& m);
129  void SetOutlineExpandFactor(float f);
130  float GetOutlineExpandFactor() const {return outline_exp_factor_;}
131  void SetOutlineExpandColor(const Color& c);
132  Color GetOutlineExpandColor() const {return outline_exp_color_;}
133 
134  void SetSolid(bool f) {solid_=f;}
135  bool GetSolid() const {return solid_;}
136  void SetSolidColor(const Color& c) {solid_color_=c;}
137  Color GetSolidcolor() const {return solid_color_;}
138  void SetClipOffset(float f) {clip_offset_=f;}
139  float GetClipOffset() const {return clip_offset_;}
140 
141  // vertex, normal, color and texcoord (T2F_C4F_N3F_V3F)
142  VertexID Add(const geom::Vec3& vert, const geom::Vec3& norm,
143  const Color& col, const geom::Vec2& texc=geom::Vec2()) {
144  dirty_=true;
145  entry_list_.push_back(Entry(vert,norm,col,texc));
146  entry_list_.back().c[3] = opacity_;
147  return entry_list_.size()-1;
148  }
149 
150  unsigned int GetVertexCount() const;
151  void DumpVertices() const;
152 
153  // add line given two vertex ids
154  LineID AddLine(VertexID id0, VertexID id1);
155 
156  // add triangle given three vertex ids
157  TriID AddTri(VertexID id0, VertexID id1, VertexID id2);
158 
159  // as above, but generate a normal based on the given ids
160  TriID AddTriN(VertexID id0, VertexID id1, VertexID id2);
161 
162  // add quad given four vertex ids
163  QuadID AddQuad(VertexID id0, VertexID id1, VertexID id2, VertexID id3);
164 
165  // add a normal sphere
166  void AddSphere(const SpherePrim& prim, unsigned int detail);
167 
168  // add an icosahedral based sphere with the given params to the va
169  void AddIcoSphere(const SpherePrim& prim, unsigned int detail);
170 
171  void AddCylinder(const CylinderPrim& prim, unsigned int detail,bool cap=false);
172 
173  void SetOpacity(float o);
174 
175  // OpenGL rendering call
176  void RenderGL();
177 
178  // POVray export
179  void RenderPov(PovState& pov, const std::string& name);
180 
181  void Export(Exporter* ex) const;
182 
183  // only removes the drawing elements
184  void Clear();
185  // removes all elements and resets internal state to default
186  void Reset();
187 
188  // forces re-calculation of some buffered features
189  void FlagRefresh();
190 
191  // for debugging, draw all normals
192  void DrawNormals(bool f);
193 
194  // NOTE: all methods below could be delegated to the outside,
195  // using the GetEntries() and Get*Indices() member functions
196 
197  // experimental, do not use
198  void CalcNormals(float smoothf);
199  // experimental, do not use
200  void CalcFullNormals();
201  // experimental, do not use
202  void SmoothNormals(float smoothf);
203  // experimental, do not use
204  void NPatch();
205  // experimental, do not use
206  void SmoothVertices(float smoothf);
207 
209  void UseTex(bool b) {use_tex_=b;}
211  uint& TexID() {return tex_id_;}
212 
213  const EntryList& GetEntries() const {return entry_list_;}
214  const IndexList& GetQuadIndices() const {return quad_index_list_;}
215  const IndexList& GetTriIndices() const {return tri_index_list_;}
216  const IndexList& GetLineIndices() const {return line_index_list_;}
217 
219  geom::AlignedCuboid GetBoundingBox() const;
220 
222  {
223  geom::Vec3 nrvo;
224  if(id>=entry_list_.size()) return nrvo;
225  nrvo = geom::Vec3(entry_list_[id].v);
226  return nrvo;
227  }
228 
229  void SetVert(VertexID id, const geom::Vec3& v)
230  {
231  if(id>=entry_list_.size()) return;
232  entry_list_[id].v[0]=v[0];
233  entry_list_[id].v[1]=v[1];
234  entry_list_[id].v[2]=v[2];
235  }
236 
238  {
239  geom::Vec3 nrvo;
240  if(id>=entry_list_.size()) return nrvo;
241  nrvo = geom::Vec3(entry_list_[id].n);
242  return nrvo;
243  }
244 
245  void SetNormal(VertexID id, const geom::Vec3& n)
246  {
247  if(id>=entry_list_.size()) return;
248  entry_list_[id].n[0]=n[0];
249  entry_list_[id].n[1]=n[1];
250  entry_list_[id].n[2]=n[2];
251  }
252 
254  {
255  Color nrvo;
256  if(id>=entry_list_.size()) return nrvo;
257  nrvo = Color(entry_list_[id].c[0],
258  entry_list_[id].c[1],
259  entry_list_[id].c[2],
260  entry_list_[id].c[3]);
261  return nrvo;
262  }
263 
264  void SetColor(VertexID id, const Color& c)
265  {
266  if(id>=entry_list_.size()) return;
267  entry_list_[id].c[0]=c[0];
268  entry_list_[id].c[1]=c[1];
269  entry_list_[id].c[2]=c[2];
270  entry_list_[id].c[3]=opacity_;
271  }
272 
274  {
275  geom::Vec2 nrvo;
276  if(id>=entry_list_.size()) return nrvo;
277  nrvo = geom::Vec2(entry_list_[id].t);
278  return nrvo;
279  }
280 
281  void SetTexCoord(VertexID id, const geom::Vec2& t)
282  {
283  if(id>=entry_list_.size()) return;
284  entry_list_[id].t[0]=t[0];
285  entry_list_[id].t[1]=t[1];
286  }
287  private:
288  bool initialized_;
289 
290  EntryList entry_list_;
291  IndexList quad_index_list_;
292  IndexList tri_index_list_;
293  IndexList line_index_list_;
294  NTEntryList ntentry_list_;
295 
296  bool dirty_;
297 
298  int mode_;
299  int poly_mode_;
300  bool lighting_;
301  bool cull_face_;
302  bool two_sided_;
303  bool color_mat_;
304  float line_width_;
305  bool aalines_flag_;
306  float point_size_;
307  float line_halo_;
308  float opacity_;
309 
310  int outline_mode_;
311  float outline_width_;
312  Material outline_mat_;
313  bool outline_mat_update_;
314  unsigned int outline_mat_dlist_;
315  float outline_exp_factor_;
316  Color outline_exp_color_;
317  bool solid_;
318  Color solid_color_;
319  float clip_offset_;
320  bool draw_normals_;
321 
322  bool use_tex_;
323  uint tex_id_;
324 
325  unsigned int buffer_id_[7]; // magic number related to the .cc buffer use
326 
327  void copy(const IndexedVertexArray& va);
328  bool prep_buff();
329  void draw_ltq(bool use_buff);
330  void draw_p(bool use_buff);
331  void draw_aalines();
332  void draw_line_halo(bool use_buff);
333  void set_clip_offset(float);
334 };
335 
336 }} // ns
337 
338 #endif
void SetColor(VertexID id, const Color &c)
unsigned int QuadID
Definition: vertex_array.hh:49
const EntryList & GetEntries() const
std::vector< NormalizerVertexEntry > NVEntryList
Definition: vertex_array.hh:91
std::vector< NormalizerTriEntry > NTEntryList
Definition: vertex_array.hh:92
geom::Vec3 pos
Definition: vertex_array.hh:76
axis-aligned cuboid
unsigned int LineID
Definition: vertex_array.hh:47
VertexID Add(const geom::Vec3 &vert, const geom::Vec3 &norm, const Color &col, const geom::Vec2 &texc=geom::Vec2())
Color GetColor(VertexID id) const
Definition: vertex_array.hh:52
geom::Vec3 GetVert(VertexID id) const
void SetVert(VertexID id, const geom::Vec3 &v)
uint tri_count
Definition: vertex_array.hh:78
const IndexList & GetLineIndices() const
#define DLLEXPORT_OST_GFX
const IndexList & GetTriIndices() const
Definition: vertex_array.hh:75
void SetSolidColor(const Color &c)
float weight
Definition: vertex_array.hh:79
void UseTex(bool b)
experimental
geom::Vec3 norm
Definition: vertex_array.hh:85
geom::Vec3 GetNormal(VertexID id) const
Three dimensional vector class, using Real precision.
Definition: vec3.hh:42
std::vector< VertexID > IndexList
Definition: vertex_array.hh:90
TriID tid
Definition: vertex_array.hh:83
unsigned int TriID
Definition: vertex_array.hh:48
ImageStateConstModIPAlgorithm< ClearFnc > Clear
Definition: clear.hh:47
Entry()
Definition: vertex_array.hh:53
std::vector< Entry > EntryList
Definition: vertex_array.hh:89
unsigned int VertexID
Definition: vertex_array.hh:46
Definition: vertex_array.hh:82
Entry(const geom::Vec3 &vv, const geom::Vec3 &nn, const Color &cc, const geom::Vec2 &tt)
Definition: vertex_array.hh:61
uint & TexID()
experimental
float GetOutlineExpandFactor() const
geom::Vec2 GetTexCoord(VertexID id) const
VertexID id2
Definition: vertex_array.hh:84
const IndexList & GetQuadIndices() const
float weight
Definition: vertex_array.hh:86
void SetTexCoord(VertexID id, const geom::Vec2 &t)
unsigned int uint
Definition: base.hh:29
void SetNormal(VertexID id, const geom::Vec3 &n)
Color GetOutlineExpandColor() const