1 //
2 //  Copyright (C) 2009  Nick Gasson
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 
18 #ifndef INC_OPENGL_HELPER_HPP
19 #define INC_OPENGL_HELPER_HPP
20 
21 #include "Platform.hpp"
22 #include "IWindow.hpp"
23 #include "IGraphics.hpp"
24 #include "IPickBuffer.hpp"
25 #include "Colour.hpp"
26 
27 #include <GL/gl.h>
28 
29 // Helper functions used by the different IWindow implementations
30 void initGL();
31 void drawGLScene(IWindowPtr a_window, IGraphicsPtr a_context, IScreenPtr a_screen);
32 void resizeGLScene(IWindowPtr a_window);
33 void printGLVersion();
34 void checkGLError();
35 
36 // Wrappers for OpenGL picking features
37 void begin_pick(IWindowPtr a_window, unsigned* a_buffer, int x, int y);
38 unsigned end_pick(unsigned* a_buffer);
39 
40 // Helper functions for using our Vector and Colour objects
41 // as OpenGL types
42 namespace gl {
43 
colour(const Colour & c)44    inline void colour(const Colour& c)
45    {
46       glColor4f(c.r, c.g, c.b, c.a);
47    }
48 
49    template <class T>
50    inline void translate(const Vector<T>& v);
51 
52    template <>
translate(const Vector<float> & v)53    inline void translate(const Vector<float>& v)
54    {
55       glTranslatef(v.x, v.y, v.z);
56    }
57 
58    template <class T>
59    inline void vertex(const Vector<T>& v);
60 
61    template <>
vertex(const Vector<float> & v)62    inline void vertex(const Vector<float>& v)
63    {
64       glVertex3f(v.x, v.y, v.z);
65    }
66 
67    template <class T>
68    inline void normal(const Vector<T>& v);
69 
70    template <>
normal(const Vector<float> & v)71    inline void normal(const Vector<float>& v)
72    {
73       glNormal3f(v.x, v.y, v.z);
74    }
75 
76    template <class T>
point(const Vector<T> & v)77    inline void point(const Vector<T>& v)
78    {
79       glBegin(GL_POINTS);
80       vertex(v);
81       glEnd();
82    }
83 
84    template <class T>
quad(const Vector<T> & a,const Vector<T> & b,const Vector<T> & c,const Vector<T> & d)85    inline void quad(const Vector<T>& a, const Vector<T>& b,
86                     const Vector<T>& c, const Vector<T>& d)
87    {
88       glBegin(GL_QUADS);
89       vertex(a);
90       vertex(b);
91       vertex(c);
92       vertex(d);
93       glEnd();
94    }
95 
96    template <class T>
triangle(const Vector<T> & a,const Vector<T> & b,const Vector<T> & c)97    inline void triangle(const Vector<T>& a, const Vector<T>& b,
98                         const Vector<T>& c)
99    {
100       glBegin(GL_TRIANGLES);
101       vertex(a);
102       vertex(b);
103       vertex(c);
104       glEnd();
105    }
106 }
107 
108 #endif
109