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 Entry {
53  Entry();
54  Entry(const geom::Vec3& vv, const geom::Vec3& nn, const Color& cc, const geom::Vec2& tt);
55  float t[2];
56  float c[4];
57  float n[3];
58  float v[3];
59  };
60 
63  TriID tri_list[50]; // this is a hack until the iso-contouring is fixed
65  float weight;
66  };
67 
70  VertexID id0,id1,id2;
72  float weight;
73  };
74 
75  typedef std::vector<Entry> EntryList;
76  typedef std::vector<VertexID> IndexList;
77  typedef std::vector<NormalizerVertexEntry> NVEntryList;
78  typedef std::vector<NormalizerTriEntry> NTEntryList;
79 
82 
84  IndexedVertexArray& operator=(const IndexedVertexArray& va);
85 
86  static unsigned int GetFormat();
87 
88  void Cleanup();
89 
90  /*
91  bitmask : 0 = render points only
92  1 = render lines
93  2 = render tris and quads
94  */
95  void SetMode(int m);
96  int GetMode() const;
97 
98  // polygon mode, 2:fill, 1:line, 0:point (different to overall mode)
99  void SetPolyMode(int m);
100 
101  void SetLighting(bool f);
102  void SetTwoSided(bool f);
103  void SetCullFace(bool f);
104  void SetColorMaterial(bool f);
105  void SetLineWidth(float lw);
106  void SetAALines(bool f);
107  void SetPointSize(float ps);
108  void SetLineHalo(float lh);
109 
110  void SetOutlineMode(int m);
111  int GetOutlineMode() const {return outline_mode_;}
112  void SetOutlineWidth(float f);
113  float GetOutlineWidth() const {return outline_width_;}
114  void SetOutlineMaterial(const Material& m);
115  void SetOutlineExpandFactor(float f);
116  float GetOutlineExpandFactor() const {return outline_exp_factor_;}
117  void SetOutlineExpandColor(const Color& c);
118  Color GetOutlineExpandColor() const {return outline_exp_color_;}
119 
120  void SetSolid(bool f) {solid_=f;}
121  bool GetSolid() const {return solid_;}
122  void SetSolidColor(const Color& c) {solid_color_=c;}
123  bool GetSolidcolor() const {return solid_color_;}
124 
125  // vertex, normal, color and texcoord (T2F_C4F_N3F_V3F)
126  VertexID Add(const geom::Vec3& vert, const geom::Vec3& norm, const Color& col, const geom::Vec2& tex=geom::Vec2());
127 
128  unsigned int GetVertexCount() const;
129  void DumpVertices() const;
130 
131  // add line given two vertex ids
132  LineID AddLine(VertexID id0, VertexID id1);
133 
134  // add triangle given three vertex ids
135  TriID AddTri(VertexID id0, VertexID id1, VertexID id2);
136 
137  // as above, but generate a normal based on the given ids
138  TriID AddTriN(VertexID id0, VertexID id1, VertexID id2);
139 
140  // add quad given four vertex ids
141  QuadID AddQuad(VertexID id0, VertexID id1, VertexID id2, VertexID id3);
142 
143  // add a normal sphere
144  void AddSphere(const SpherePrim& prim, unsigned int detail);
145 
146  // add an icosahedral based sphere with the given params to the va
147  void AddIcoSphere(const SpherePrim& prim, unsigned int detail);
148 
149  void AddCylinder(const CylinderPrim& prim, unsigned int detail,bool cap=false);
150 
151  geom::Vec3 GetVert(VertexID id) const;
152  void SetVert(VertexID id, const geom::Vec3& vert);
153 
154  geom::Vec3 GetNormal(VertexID id) const;
155  void SetNormal(VertexID id, const geom::Vec3& norm);
156 
157  Color GetColor(VertexID id) const;
158  void SetColor(VertexID id, const Color& col);
159 
160  geom::Vec2 GetTexCoord(VertexID id) const;
161  void SetTexCoord(VertexID id, const geom::Vec2& tex);
162 
163  void SetOpacity(float o);
164 
165  // OpenGL rendering call
166  void RenderGL();
167 
168  // POVray export
169  void RenderPov(PovState& pov, const std::string& name);
170 
171  void Export(Exporter* ex) const;
172 
173  // only removes the drawing elements
174  void Clear();
175  // removes all elements and resets internal state to default
176  void Reset();
177 
178  // forces re-calculation of some buffered features
179  void FlagRefresh();
180 
181  // for debugging, draw all normals
182  void DrawNormals(bool f);
183 
184  // NOTE: all methods below could be delegated to the outside,
185  // using the GetEntries() and Get*Indices() member functions
186 
187  // experimental, do not use
188  void CalcNormals(float smoothf);
189  // experimental, do not use
190  void CalcFullNormals();
191  // experimental, do not use
192  void SmoothNormals(float smoothf);
193  // experimental, do not use
194  void NPatch();
195  // experimental, do not use
196  void SmoothVertices(float smoothf);
197 
199  void UseTex(bool b) {use_tex_=b;}
201  uint& TexID() {return tex_id_;}
202 
203  const EntryList& GetEntries() const {return entry_list_;}
204  const IndexList& GetQuadIndices() const {return quad_index_list_;}
205  const IndexList& GetTriIndices() const {return tri_index_list_;}
206  const IndexList& GetLineIndices() const {return line_index_list_;}
207 
209  geom::AlignedCuboid GetBoundingBox() const;
210 
211  private:
212  bool initialized_;
213 
214  EntryList entry_list_;
215  IndexList quad_index_list_;
216  IndexList tri_index_list_;
217  IndexList line_index_list_;
218  NTEntryList ntentry_list_;
219 
220  bool dirty_;
221 
222  int mode_;
223  int poly_mode_;
224  bool lighting_;
225  bool cull_face_;
226  bool two_sided_;
227  bool color_mat_;
228  float line_width_;
229  bool aalines_flag_;
230  float point_size_;
231  float line_halo_;
232  float opacity_;
233 
234  int outline_mode_;
235  float outline_width_;
236  Material outline_mat_;
237  bool outline_mat_update_;
238  unsigned int outline_mat_dlist_;
239  float outline_exp_factor_;
240  Color outline_exp_color_;
241  bool solid_;
242  Color solid_color_;
243  bool draw_normals_;
244 
245  bool use_tex_;
246  uint tex_id_;
247 
248  unsigned int buffer_id_[7]; // magic number related to the .cc buffer use
249 
250  void copy(const IndexedVertexArray& va);
251  bool prep_buff();
252  void draw_ltq(bool use_buff);
253  void draw_p(bool use_buff);
254  void draw_aalines();
255  void draw_line_halo(bool use_buff);
256 };
257 
258 }} // ns
259 
260 #endif