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  // vertex, normal, color and texcoord (T2F_C4F_N3F_V3F)
121  VertexID Add(const geom::Vec3& vert, const geom::Vec3& norm, const Color& col, const geom::Vec2& tex=geom::Vec2());
122 
123  unsigned int GetVertexCount() const;
124  void DumpVertices() const;
125 
126  // add line given two vertex ids
127  LineID AddLine(VertexID id0, VertexID id1);
128 
129  // add triangle given three vertex ids
130  TriID AddTri(VertexID id0, VertexID id1, VertexID id2);
131 
132  // as above, but generate a normal based on the given ids
133  TriID AddTriN(VertexID id0, VertexID id1, VertexID id2);
134 
135  // add quad given four vertex ids
136  QuadID AddQuad(VertexID id0, VertexID id1, VertexID id2, VertexID id3);
137 
138  // add a normal sphere
139  void AddSphere(const SpherePrim& prim, unsigned int detail);
140 
141  // add an icosahedral based sphere with the given params to the va
142  void AddIcoSphere(const SpherePrim& prim, unsigned int detail);
143 
144  void AddCylinder(const CylinderPrim& prim, unsigned int detail,bool cap=false);
145 
146  geom::Vec3 GetVert(VertexID id) const;
147  void SetVert(VertexID id, const geom::Vec3& vert);
148 
149  geom::Vec3 GetNormal(VertexID id) const;
150  void SetNormal(VertexID id, const geom::Vec3& norm);
151 
152  Color GetColor(VertexID id) const;
153  void SetColor(VertexID id, const Color& col);
154 
155  geom::Vec2 GetTexCoord(VertexID id) const;
156  void SetTexCoord(VertexID id, const geom::Vec2& tex);
157 
158  void SetOpacity(float o);
159 
160  // OpenGL rendering call
161  void RenderGL();
162 
163  // POVray export
164  void RenderPov(PovState& pov, const std::string& name);
165 
166  void Export(Exporter* ex) const;
167 
168  // only removes the drawing elements
169  void Clear();
170  // removes all elements and resets internal state to default
171  void Reset();
172 
173  // forces re-calculation of some buffered features
174  void FlagRefresh();
175 
176  // for debugging, draw all normals
177  void DrawNormals(bool f);
178 
179  // NOTE: all methods below could be delegated to the outside,
180  // using the GetEntries() and Get*Indices() member functions
181 
182  // experimental, do not use
183  void CalcNormals(float smoothf);
184  // experimental, do not use
185  void CalcFullNormals();
186  // experimental, do not use
187  void SmoothNormals(float smoothf);
188  // experimental, do not use
189  void NPatch();
190  // experimental, do not use
191  void SmoothVertices(float smoothf);
192 
194  void UseTex(bool b) {use_tex_=b;}
196  uint& TexID() {return tex_id_;}
197 
198  const EntryList& GetEntries() const {return entry_list_;}
199  const IndexList& GetQuadIndices() const {return quad_index_list_;}
200  const IndexList& GetTriIndices() const {return tri_index_list_;}
201  const IndexList& GetLineIndices() const {return line_index_list_;}
202 
204  geom::AlignedCuboid GetBoundingBox() const;
205 
206  private:
207  bool initialized_;
208 
209  EntryList entry_list_;
210  IndexList quad_index_list_;
211  IndexList tri_index_list_;
212  IndexList line_index_list_;
213  NTEntryList ntentry_list_;
214 
215  bool dirty_;
216 
217  int mode_;
218  int poly_mode_;
219  bool lighting_;
220  bool cull_face_;
221  bool two_sided_;
222  bool color_mat_;
223  float line_width_;
224  bool aalines_flag_;
225  float point_size_;
226  float line_halo_;
227  float opacity_;
228 
229  int outline_mode_;
230  float outline_width_;
231  Material outline_mat_;
232  bool outline_mat_update_;
233  unsigned int outline_mat_dlist_;
234  float outline_exp_factor_;
235  Color outline_exp_color_;
236 
237  bool draw_normals_;
238 
239  bool use_tex_;
240  uint tex_id_;
241 
242  unsigned int buffer_id_[7]; // magic number related to the .cc buffer use
243 
244  void copy(const IndexedVertexArray& va);
245  bool prep_buff();
246  void draw_ltq(bool use_buff);
247  void draw_p(bool use_buff);
248  void draw_aalines();
249  void draw_line_halo(bool use_buff);
250 };
251 
252 }} // ns
253 
254 #endif