KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_sprite.h
Go to the documentation of this file.
1 /* $Id: gr_sprite.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_sprite_H
21 #define KJB_sprite_H
22 
23 
24 #include <gr_cpp/gr_opengl_texture.h> /* comes before other opengl includes */
25 #include <i_cpp/i_image.h>
26 #include <gr_cpp/gr_renderable.h>
28 #include <map>
29 
30 namespace kjb
31 {
32 
33 namespace opengl
34 {
35 
47 #ifdef KJB_HAVE_OPENGL
48  class Sprite : public Renderable
49  {
50  enum Origin_type {TOP_LEFT, CENTER};
51  public:
52  Sprite (float z_pos = 1);
53  Sprite (const Image& img, float z_pos = 1);
54  Sprite (const Matrix& gray_img, float z_pos = 1);
55  Sprite (const Texture& img, float z_pos = 1);
56 // Sprite (const char* fname, float z_pos = 1);
57  Sprite ( const Sprite& other );
58  ~Sprite ();
59 
60  void swap(Sprite& src)
61  {
62  std::swap(m_texture, src.m_texture);
63  std::swap(m_u, src.m_u);
64  std::swap(m_v, src.m_v);
65  std::swap(m_base_width, src.m_base_width);
66  std::swap(m_base_height, src.m_base_height);
67  std::swap(m_width, src.m_width);
68  std::swap(m_height, src.m_height);
69  std::swap(m_depth_test, src.m_depth_test);
70  std::swap(m_min_filter, src.m_min_filter);
71  std::swap(m_mag_filter, src.m_mag_filter);
72  std::swap(m_z_pos, src.m_z_pos);
73  }
74 
75  void enable_depth_test(bool enable);
76  void set_filters(GLenum min_filter, GLenum mag_filter);
77 
78  void set_position(int u, int v) {
79  set_u(u);
80  set_v(v);
81  }
82 
83  void set_u(int u) { m_u = u; }
84  void set_v(int v) { m_v = v; }
85 
89  void set_crop_relative(float left, float right, float bottom, float top)
90  {
91  m_crop_left = left;
92  m_crop_right = right;
93  m_crop_top = top;
94  m_crop_bottom = bottom;
95  }
96 
100  void set_crop_absolute(float left, float right, float bottom, float top)
101  {
102  m_crop_left = left / m_base_width;
103  m_crop_right = right / m_base_width;
104  m_crop_bottom = bottom / m_base_height;
105  m_crop_top = top / m_base_height;
106  }
107 
109  void set_scale(float u_scale, float v_scale)
110  {
111  m_width = (int) (m_base_width * u_scale);
112  m_height = (int)(m_base_height * v_scale);
113  }
114 
116  void set_size(int width, int height)
117  {
118  m_width = width;
119  m_height = height;
120  }
121 
123  void set_rect(int u, int v, int width, int height) {
124  set_position(u,v);
125  set_size(width, height);
126  }
127 
129  int get_base_width() const { return m_base_width; }
130  int get_base_height() const {return m_base_height; }
131 
133  int get_width() const { return m_width; }
134  int get_height() const { return m_height; }
135 
137  int get_u_scale() const { return m_width / (float) m_base_width; }
138  int get_v_scale() const { return m_height / (float) m_base_height; }
139 
140  int get_u() const { return m_u;}
141  int get_v() const { return m_v;}
142  Vector get_position() const {
143  Vector pos(3);
144  pos[0] = m_u; pos[1] = m_v; pos[2] = 0;
145  return pos;
146  }
147 
152  void center_origin()
153  {
154  m_origin = CENTER;
155  }
156 
161 /* virtual const Vector& get_world_vertex(int i) const;
162  */
163 
164  virtual void render() const;
165  virtual void render_quad() const;
166 
167  Sprite& operator=(const Sprite &src);
168 
169  void set(const Image& img);
170  void set(const Texture& tex);
171  void set(const Matrix& img);
172 
173  const Texture& get_texture() const
174  {
175  return m_texture;
176  }
177 
178  protected:
179  void m_init_texture();
180  protected:
181  Texture m_texture;
182  static std::map<int, int> m_ref_count;
183 
184  int m_u, m_v;
185 
186  int m_base_width;
187  int m_base_height;
188 
189  int m_width, m_height;
190 
191  bool m_depth_test;
192  GLenum m_min_filter;
193  GLenum m_mag_filter;
194 
195  float m_z_pos;
196 
197  Origin_type m_origin;
198 
199  float m_crop_left;
200  float m_crop_right;
201  float m_crop_bottom;
202  float m_crop_top;
203 
204  }; // class Sprite
205 
206  typedef boost::shared_ptr<Sprite> Sprite_ptr;
207 
208 // class Packed_sprite : public Sprite
209 // {
210 // typedef Sprite Base;
211 // typedef Packed_sprite Self;
212 // public:
213 // Packed_sprite (float z_pos = 1) :
214 // Sprite(z_pos)
215 // {}
216 //
217 // Packed_sprite (const Matrix& mat, float z_pos = 1) :
218 // Sprite(z_pos)
219 // {
220 // m_set(mat);
221 // }
222 //
223 // Packed_sprite ( const Packed_sprite& other ) :
224 // Sprite(other)
225 // {}
226 //
227 // ~Packed_sprite()
228 // {}
229 //
230 // Packed_sprite& operator=(const Sprite &src)
231 // {
232 // Base::operator=(src);
233 // return *this;
234 // }
235 //
236 //
237 // private:
238 // void m_set(const Matrix& mat);
239 // }; // class Sprite
240 
241 #endif /* KJB_HAVE_OPENGL */
242 
243 } // namespace opengl
244 
245 
246 #ifdef KJB_HAVE_OPENGL
247 
250 typedef opengl::Sprite Gl_sprite;
251 #endif
252 
253 } // namespace kjb
254 #endif /* ----- #ifndef KJB_sprite_H ----- */
int GLenum
Definition: gr_opengl_headers.h:65
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
height
Definition: APPgetLargeConnectedEdges.m:33
Abstract class to render this object with GL.
void render(const Cuboid &c)
Definition: psi_weighted_box.cpp:56
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Code for a wrapper class around the C struct KJB_Image.