zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Vertex_Buffer.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  * zenilib 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
30 #ifndef ZENI_VERTEX_BUFFER_H
31 #define ZENI_VERTEX_BUFFER_H
32 
33 #include <Zeni/Triangle.h>
34 #include <Zeni/Quadrilateral.h>
35 #include <Zeni/Vertex2f.h>
36 #include <Zeni/Vertex3f.h>
37 
38 #include <vector>
39 #include <set>
40 #include <memory>
41 
42 #ifndef DISABLE_GL
43 #if defined(REQUIRE_GL_ES)
44 #include <GLES/gl.h>
45 #else
46 #include <GL/glew.h>
47 #endif
48 #endif
49 
51 
52 namespace Zeni {
53 
54  class ZENI_GRAPHICS_DLL Render_Wrapper;
55  class ZENI_GRAPHICS_DLL Vertex_Buffer;
56 
57  class ZENI_GRAPHICS_DLL Vertex_Buffer_Microrenderer {
58  public:
60 
61  virtual void operator()() const = 0;
62  };
63 
64  class ZENI_GRAPHICS_DLL Vertex_Buffer_Macrorenderer {
65  public:
67 
68  virtual void operator()(const Vertex_Buffer_Microrenderer &microrenderer) const;
69  };
70 
71  class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer {
73  Vertex_Buffer_Renderer & operator=(const Vertex_Buffer_Renderer &);
74 
75  public:
76  Vertex_Buffer_Renderer(Vertex_Buffer &vertex_buffer);
78 
79  virtual void render() = 0;
80 
81  protected:
83  };
84 
85  class ZENI_GRAPHICS_DLL Vertex_Buffer {
87  Vertex_Buffer & operator=(const Vertex_Buffer &);
88 
89  friend class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_GL_Fixed;
90  friend class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_GL_Shader;
91  friend class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_DX9;
92 
93  public:
94  struct ZENI_GRAPHICS_DLL Vertex_Buffer_Range {
95  Vertex_Buffer_Range(Material * const &m, const size_t &s, const size_t &ne);
96 
97 #ifdef _WINDOWS
98 #pragma warning( push )
99 #pragma warning( disable : 4251 )
100 #endif
101  std::auto_ptr<Material> material;
102 #ifdef _WINDOWS
103 #pragma warning( pop )
104 #endif
105  size_t start;
106  size_t num_elements;
107  };
108 
109  Vertex_Buffer();
110  ~Vertex_Buffer();
111 
112  inline void do_normal_alignment(const bool align_normals_ = true); // Set whether Vertex_Buffer should try to fix broken normals in the prerender phase;
113  inline bool will_do_normal_alignment() const; // Find out whether Vertex_Buffer is set to try to fix broken normals in the prerender phase;
114 
115  void give_Triangle(Triangle<Vertex2f_Color> * const &triangle);
116  void fax_Triangle(const Triangle<Vertex2f_Color> * const &triangle);
117  void give_Quadrilateral(Quadrilateral<Vertex2f_Color> * const &quadrilateral);
118  void fax_Quadrilateral(const Quadrilateral<Vertex2f_Color> * const &quadrilateral);
119 
120  void give_Triangle(Triangle<Vertex2f_Texture> * const &triangle);
121  void fax_Triangle(const Triangle<Vertex2f_Texture> * const &triangle);
122  void give_Quadrilateral(Quadrilateral<Vertex2f_Texture> * const &quadrilateral);
123  void fax_Quadrilateral(const Quadrilateral<Vertex2f_Texture> * const &quadrilateral);
124 
125  void give_Triangle(Triangle<Vertex3f_Color> * const &triangle);
126  void fax_Triangle(const Triangle<Vertex3f_Color> * const &triangle);
127  void give_Quadrilateral(Quadrilateral<Vertex3f_Color> * const &quadrilateral);
128  void fax_Quadrilateral(const Quadrilateral<Vertex3f_Color> * const &quadrilateral);
129 
130  void give_Triangle(Triangle<Vertex3f_Texture> * const &triangle);
131  void fax_Triangle(const Triangle<Vertex3f_Texture> * const &triangle);
132  void give_Quadrilateral(Quadrilateral<Vertex3f_Texture> * const &quadrilateral);
133  void fax_Quadrilateral(const Quadrilateral<Vertex3f_Texture> * const &quadrilateral);
134 
135  void debug_render();
136  void give_Macrorenderer(Vertex_Buffer_Macrorenderer * const &macrorenderer);
137 
138  void render();
139  void lose();
140 
141  private:
142  void prerender();
143 
144  inline size_t num_vertices_cm() const;
145  inline size_t num_vertices_t() const;
146 
147  inline void unprerender();
148 
149  // Sort buffers by Material
150  void sort_triangles();
151 
152  // Generate lists of vertex ranges to be rendered more efficiently
153  void set_descriptors();
154 
155  // Align normals of similar vertices
156  void align_similar_normals();
157 
158 #ifdef _WINDOWS
159 #pragma warning( push )
160 #pragma warning( disable : 4251 )
161 #endif
162  std::vector<Triangle<Vertex3f_Color> *> m_triangles_cm;
163  std::vector<Triangle<Vertex3f_Texture> *> m_triangles_t;
164 
165  std::vector<Vertex_Buffer_Range *> m_descriptors_cm;
166  std::vector<Vertex_Buffer_Range *> m_descriptors_t;
167 #ifdef _WINDOWS
168 #pragma warning( pop )
169 #endif
170 
171  bool m_align_normals;
172 
173  Vertex_Buffer_Renderer * m_renderer;
174  bool m_prerendered;
175 
176  Vertex_Buffer_Macrorenderer * m_macrorenderer;
177 
178  public:
179  static void lose_all();
180 
181  private:
182  private:
183 #ifdef _WINDOWS
184 #pragma warning( push )
185 #pragma warning( disable : 4251 )
186 #endif
187  static class Uninit : public Event::Handler {
188  void operator()() {
190  }
191 
192  Uninit * duplicate() const {
193  return new Uninit;
194  }
195 
196  public:
197  Uninit() {}
198 
199  private:
200  // Undefined
201  Uninit(const Uninit &);
202  Uninit operator=(const Uninit &);
203  } g_uninit;
204 #ifdef _WINDOWS
205 #pragma warning( pop )
206 #endif
207 
208  static std::set<Vertex_Buffer *> & get_vbos();
209  };
210 
211 #ifndef DISABLE_GL
212 
213  class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_GL_Fixed : public Vertex_Buffer_Renderer {
215  Vertex_Buffer_Renderer_GL_Fixed operator=(const Vertex_Buffer_Renderer_GL_Fixed &);
216 
217  public:
218  Vertex_Buffer_Renderer_GL_Fixed(Vertex_Buffer &vertex_buffer);
219  virtual ~Vertex_Buffer_Renderer_GL_Fixed();
220 
221  virtual void render();
222 
223  private:
224  inline size_t vertex_size() const;
225  inline size_t normal_size() const;
226  inline size_t color_size() const;
227  inline size_t texel_size() const;
228  inline bool buffers_supported(Video_GL_Fixed &vgl) const;
229 
230  union ZENI_GRAPHICS_DLL VBO_GL {
231  GLuint vbo;
232  unsigned char * alt;
233  } m_vbuf[6];
234  };
235 
236  class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_GL_Shader : public Vertex_Buffer_Renderer {
238  Vertex_Buffer_Renderer_GL_Shader operator=(const Vertex_Buffer_Renderer_GL_Shader &);
239 
240  public:
241  Vertex_Buffer_Renderer_GL_Shader(Vertex_Buffer &vertex_buffer);
242  virtual ~Vertex_Buffer_Renderer_GL_Shader();
243 
244  virtual void render();
245 
246  private:
247  inline size_t vertex_size() const;
248  inline size_t normal_size() const;
249  inline size_t color_size() const;
250  inline size_t texel_size() const;
251 
252  union ZENI_GRAPHICS_DLL VBO_GL {
253  GLuint vbo;
254  } m_vbuf[6];
255  };
256 
257 #endif
258 #ifndef DISABLE_DX9
259 
260  class ZENI_GRAPHICS_DLL Vertex_Buffer_Renderer_DX9 : public Vertex_Buffer_Renderer {
262  Vertex_Buffer_Renderer_DX9 operator=(const Vertex_Buffer_Renderer_DX9 &);
263 
264  public:
265  Vertex_Buffer_Renderer_DX9(Vertex_Buffer &vertex_buffer);
266  virtual ~Vertex_Buffer_Renderer_DX9();
267 
268  virtual void render();
269 
270  private:
271  inline size_t vertex_c_size() const;
272  inline size_t vertex_t_size() const;
273 
274  public:
275  struct ZENI_GRAPHICS_DLL VBO_DX9 {
276  bool is_vbo;
277 
278  union ZENI_GRAPHICS_DLL VBO_DX9_impl {
280  char * alt;
281  } data;
282  } m_buf_c, m_buf_t;
283  };
284 
285 #endif
286 
287  struct ZENI_GRAPHICS_DLL VBuf_Init_Failure : public Error {
288  VBuf_Init_Failure() : Error("Zeni Vertex Buffer Failed to Initialize Correctly") {}
289  };
290 
291  struct ZENI_GRAPHICS_DLL VBuf_Render_Failure : public Error {
292  VBuf_Render_Failure() : Error("Zeni Vertex Buffer Failed to Render") {}
293  };
294 
295 }
296 
297 #endif
GLdouble s
Definition: glew.h:1376
static void render(const Vertex_Buffer_Macrorenderer &macrorenderer, std::vector< Vertex_Buffer::Vertex_Buffer_Range * > &descriptors)
An Abstraction of a Material.
Definition: Material.h:56
An Abstraction of a Quadrilateral.
Definition: Quadrilateral.h:37
The OpenGL Rendering System.
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl2ext.h:848
void align_similar_normals(const VERTEX v0, Triangle< VERTEX > &t1, const int &which)
static void lose_all()
A Vertex_Buffer that accepts Triangle and Quadrilaterals.
Definition: Vertex_Buffer.h:85
std::auto_ptr< Material > material
unsigned int GLuint
Definition: gl2.h:32
interface IDirect3DVertexBuffer9 IDirect3DVertexBuffer9
Definition: d3d9.h:267
An Abstraction of a Triangle.
Definition: Triangle.h:36
The Error Class.
Definition: Error.h:52
const GLdouble * m
Definition: glew.h:8385