g4tools  5.4.0
gl
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_gl
5 #define tools_sg_gl
6 
7 #include "../OpenGL"
8 
9 #include <tools/platform> //for APPLE TargetConditionals.h
10 #include <tools/img>
11 
12 #include <ostream>
13 
14 namespace tools {
15 namespace sg {
16 //namespace gl : no, problem with g4tools.
17 
18 inline void gl_clear_errors() {
19  GLenum glerror = ::glGetError();
20  while(glerror!=GL_NO_ERROR) {
21  glerror = ::glGetError();
22  }
23 }
24 
25 inline bool gl_dump_if_errors(std::ostream& a_out,const std::string& a_head) {
26  bool retval = false;
27  GLenum glerror = ::glGetError();
28  if(glerror!=GL_NO_ERROR) {
29  a_out << a_head
30  << " we have gl errors :"
31  << std::endl;
32  retval = true;
33  }
34  while(glerror!=GL_NO_ERROR) {
35  //#define GL_NO_ERROR 0
36  //#define GL_INVALID_ENUM 0x0500
37  //#define GL_INVALID_VALUE 0x0501
38  //#define GL_INVALID_OPERATION 0x0502
39  //#define GL_STACK_OVERFLOW 0x0503
40  //#define GL_STACK_UNDERFLOW 0x0504
41  //#define GL_OUT_OF_MEMORY 0x0505
42 
43  //#define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
44 
45  a_out << "0x" << std::hex << glerror << std::endl;
46 
47  glerror = ::glGetError();
48  }
49  return retval;
50 }
51 
52 inline void gl_dump_infos(std::ostream& a_out) {
53  std::string gl_version;
54  {const char* _sv = (const char*)::glGetString(GL_VERSION);
55  if(_sv) gl_version = _sv;}
56 
57  std::string gl_vendor;
58  {const char* _sv = (const char*)::glGetString(GL_VENDOR);
59  if(_sv) gl_vendor = _sv;}
60 
61  std::string gl_renderer;
62  {const char* _sv = (const char*)::glGetString(GL_RENDERER);
63  if(_sv) gl_renderer = _sv;}
64 
65  std::string gl_extensions;
66  {const char* _sv = (const char*)::glGetString(GL_EXTENSIONS);
67  if(_sv) gl_extensions = _sv;}
68 
69  GLint gl_texture_size;
70  ::glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_texture_size);
71 
72  //GLint gl_buffer_size;
73  //::glGetIntegerv(GL_BUFFER_SIZE, &gl_buffer_size);
74 
75  a_out << "tools::sg::gl::dump_infos :" << std::endl;
76  a_out << " GL_VERSION " << gl_version << std::endl;
77  a_out << " GL_VENDOR " << gl_vendor << std::endl;
78  a_out << " GL_RENDERER " << gl_renderer << std::endl;
79  a_out << " GL_EXTENSIONS " << gl_extensions << std::endl;
80  a_out << " GL_MAX_TEXTURE_SIZE " << gl_texture_size << std::endl;
81  //a_out << " GL_BUFFER_SIZE " << gl_buffer_size << std::endl;
82 }
83 
84 inline bool gl_tex_img(std::ostream& a_out,const tools::img_byte& a_img) {
85  if(a_img.bpp()==1) {
86  //to pass eiffel-tower.png
87 #if defined(ANDROID) || TARGET_OS_IPHONE
88  tools::img_byte res;
89  if(!a_img.bw2x(3,res)) {
90  a_out << "tools::sg::gl::tex_img : tools::img.bw2x() failed." << std::endl;
91  return false;
92  }
93  ::glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,res.width(),res.height(),0,GL_RGB,GL_UNSIGNED_BYTE,res.buffer());
94 #else
95  ::glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,a_img.width(),a_img.height(),0,GL_LUMINANCE,GL_UNSIGNED_BYTE,a_img.buffer());
96 #endif
97  } else if(a_img.bpp()==3) {
98  ::glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,a_img.width(),a_img.height(),0,GL_RGB,GL_UNSIGNED_BYTE,a_img.buffer());
99  } else if(a_img.bpp()==4) {
100  ::glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,a_img.width(),a_img.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,a_img.buffer());
101  } else {
102  a_out << "tools::sg::gl::tex_img : img.bpp() " << a_img.bpp() << " not treated." << std::endl;
103  return false;
104  }
105  return true;
106 }
107 
108 }}
109 
110 #endif
platform
tools::img< unsigned char >
tools::img::bw2x
bool bw2x(unsigned int a_n, img< T > &a_res) const
Definition: img:654
tools::sg::gl_dump_if_errors
bool gl_dump_if_errors(std::ostream &a_out, const std::string &a_head)
Definition: gl:25
img
tools::img::width
unsigned int width() const
Definition: img:214
tools::sg::gl_clear_errors
void gl_clear_errors()
Definition: gl:18
tools::sg::gl_tex_img
bool gl_tex_img(std::ostream &a_out, const tools::img_byte &a_img)
Definition: gl:84
tools::sg::gl_dump_infos
void gl_dump_infos(std::ostream &a_out)
Definition: gl:52
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::img::bpp
unsigned int bpp() const
Definition: img:217
tools::img::height
unsigned int height() const
Definition: img:215
tools::img::buffer
const T * buffer() const
Definition: img:218