Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfRenderingContext.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfRenderingContext.hh"
26 #include <OpenGLFramework/Base/glfException.hh>
27 #include <Utils/GlewInitWrapper.hh>
28 
29 using namespace BIAS;
30 using namespace std;
31 
33 {
34  width = 800;
35  height = 600;
36  doubleBuffer = false;
37  redSize = 8;
38  greenSize = 8;
39  blueSize = 8;
40  alphaSize = 0;
41  depthSize = 24;
42  stencilSize = 0;
43 }
44 
46 {
47  out << "Width: " << width << endl;
48  out << "Height: " << height << endl;
49  out << "DoubleBuffer: " << doubleBuffer << endl;
50  out << "RedSize: " << redSize << endl;
51  out << "GreenSize: " << greenSize << endl;
52  out << "BlueSize: " << blueSize << endl;
53  out << "AlphaSize: " << alphaSize << endl;
54  out << "DepthSize: " << depthSize << endl;
55  out << "StencilSize: " << stencilSize << endl;
56 }
57 
59  int x, int y, int width, int height)
60 {
61  // activate this rendering context
62  MakeCurrent();
63 
64  // choose format for glReadPixels and numChannels for image.
65  GLenum format;
66  int numChannels;
67  switch (source) {
68  case GRAB_COLOR:
69  format = GL_RGB;
70  numChannels = 3;
71  break;
72  case GRAB_DEPTH:
73  format = GL_DEPTH_COMPONENT;
74  numChannels = 1;
75  break;
76  case GRAB_STENCIL:
77  format = GL_STENCIL_INDEX;
78  numChannels = 1;
79  break;
80  default:
81  GLF_THROW_EXCEPTION("Invalid grab source");
82  break;
83  };
84 
85  // get configuration of context
87  GetConfig(config);
88 
89  // fix offset and size
90  if (width == 0) {
91  x = 0;
92  width = config.width;
93  }
94  if (height == 0) {
95  y = 0;
96  height = config.height;
97  }
98 
99  // verify that offset and size are sane
100  if (x < 0 || y < 0 ||
101  x + width > config.width ||
102  y + height > config.height) {
103  GLF_THROW_EXCEPTION("Grab rectangle is outside valid range");
104  }
105 
106  // create image
107  if (!image.IsEmpty()) {
108  image.Release();
109  }
110  image.Init(width, height, numChannels);
111 
112  // copy data to image
113  glReadPixels(x, y, width, height, format,
114  GL_UNSIGNED_BYTE, image.GetImageData());
115 }
116 
118 {
119 #if defined (BIAS_HAVE_GLEW)
121 #endif
122 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
void Dump(std::ostream &out=std::cout)
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
static GlewInitWrapper * GetInstance()
void InitGlew()
Initializes GLEW if enabled in the BIAS configuration.
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void Grab(BIAS::Image< unsigned char > &image, GrabSource source=GRAB_COLOR, int x=0, int y=0, int width=0, int height=0)
Copies the contents of the framebuffer to an image.
GrabSource
Frame buffer data that can be retrieved by Grab.
Configuration for a rendering context.