Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRenderingContext.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-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 /**
26  @example ExampleRenderingContext.cpp
27  @relates glfPBuffer,glfBatch,glfScreen,glfVertexBuffer,glfElementBuffer
28  @brief Example for drawing on the GPU Using the OpenGLFramwork
29  @ingroup g_examples
30  @ingroup g_openglframework
31  @author MIP
32 */
33 
34 
35 #include <Base/Image/ImageIO.hh>
36 #include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
37 #include <OpenGLFramework/Base/glfException.hh>
38 #include <OpenGLFramework/Base/glfBatch.hh>
39 #include <OpenGLFramework/Base/glfScreen.hh>
40 #include <OpenGLFramework/Base/glfViewport.hh>
41 #include <OpenGLFramework/Base/glfVertexBuffer.hh>
42 #include <OpenGLFramework/Base/glfElementBuffer.hh>
43 
44 #include <vector>
45 
46 using namespace std;
47 using namespace BIAS;
48 
49 /** \cond HIDDEN_SYMBOLS */
50 // vertex structure
51 struct Vertex {
52 
53  Vertex(float x, float y, float z, unsigned char r, unsigned char g, unsigned char b) {
54  position[0] = x;
55  position[1] = y;
56  position[2] = z;
57  color[0] = r;
58  color[1] = g;
59  color[2] = b;
60  color[3] = 255;
61  }
62 
63  float position[3];
64  unsigned char color[4];
65 };
66 /** \endcond */
67 
68 int main(int argc, char* argv[])
69 {
70  // only need glfRenderingContext interface.
71  glfPBuffer pbuffer;
72  glfRenderingContext* context = &pbuffer;
73 
74  try {
75 
76  // fill configuration
78  cfg.width = 640;
79  cfg.height = 480;
80  cfg.doubleBuffer = 0;
81  cfg.redSize = 8;
82  cfg.greenSize = 8;
83  cfg.blueSize = 8;
84  cfg.alphaSize = 0;
85  cfg.depthSize = 16;
86  cfg.stencilSize = 0;
87 
88  // initialize context
89  context->Init(cfg);
90 
91  // get real configuration
93  context->GetConfig(realCfg);
94 
95  // print config
96  cout << "--- Requested Config ---" << endl;
97  cfg.Dump(cout);
98  cout << endl << "--- Real Config ---" << endl;
99  realCfg.Dump(cout);
100 
101  // clear screen
102  glfScreen& screen = glfScreen::GetInstance();
103  screen.ClearColorBuffer(1.0f, 0.5f, 0.0f, 0.0f);
104  screen.ClearDepthBuffer();
105 
106  // create vertices
107  vector<Vertex> vertices;
108  vertices.push_back(Vertex( 0.0f, -0.5f, 0.5f, 255, 0, 0));
109  vertices.push_back(Vertex(-0.5f, 0.5f, 0.5f, 255, 0, 0));
110  vertices.push_back(Vertex( 0.5f, 0.5f, 0.5f, 255, 0, 0));
111  vertices.push_back(Vertex( 0.2f, -0.2f, 0.8f, 0, 255, 0));
112  vertices.push_back(Vertex( 0.0f, 0.2f, 0.8f, 0, 255, 0));
113  vertices.push_back(Vertex( 0.6f, 0.2f, 0.8f, 0, 255, 0));
114 
115  // create vertex format
116  glfVertexFormat vertexFormat;
117  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, 3);
118  vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_COLOR, GL_UNSIGNED_BYTE, 4);
119 
120  // create vertex buffer
121  glfVertexBuffer vertexBuffer;
122  vertexBuffer.Create(vertices, vertexFormat);
123 
124  // create element buffer
125  std::vector<unsigned char> indices;
126  indices.push_back(0);
127  indices.push_back(1);
128  indices.push_back(2);
129  indices.push_back(3);
130  indices.push_back(4);
131  indices.push_back(5);
132  indices.push_back(6);
133  glfElementBuffer elementBuffer;
134  elementBuffer.Create(indices, GL_TRIANGLES);
135 
136  // create depth buffer mode
137  glfDepthBufferMode depthBufferMode;
138  depthBufferMode.SetDepthTest(true);
139 
140  // create viewport
141  glfViewport viewport;
142  viewport.SetOrigin(0, 0);
143  viewport.SetSize(640, 480);
144 
145  // create and draw batch
146  glfBatch batch;
147  batch.SetDepthBufferMode(&depthBufferMode);
148  batch.SetVertexBuffer(&vertexBuffer);
149  batch.SetElementBuffer(&elementBuffer);
150  batch.SetViewport(&viewport);
151  batch.Draw();
152 
153  // grab framebuffer and write to files
154  Image<unsigned char> image;
155  context->Grab(image, glfRenderingContext::GRAB_COLOR);
156 
157  //if (ImageIO::Save("color.mip", image) != 0) {
158  if (ImageIO::Save("color.mip", image) != 0) {
159  return -1;
160  }
161  context->Grab(image, glfRenderingContext::GRAB_DEPTH);
162  //if (ImageIO::Save("depth.mip", image) != 0) {
163  if (ImageIO::Save("depth.mip", image) != 0) {
164  return -1;
165  }
166  context->Grab(image, glfRenderingContext::GRAB_STENCIL);
167  //if (ImageIO::Save("stencil.mip", image) != 0) {
168  if (ImageIO::Save("stencil.mip", image) != 0) {
169  return -1;
170  }
171 
172  // clean up and exit
173  context->Destroy();
174 
175  } catch (glfException& e) {
176  context->Destroy();
177  std::cout << "Error: " << e.GetMessageString() << std::endl;
178  return -1;
179  }
180 
181  return 0;
182 }
183 
An element buffer contains vertex indices that form primitives.
void SetVertexBuffer(const glfVertexBuffer *vertexBuffer)
Sets the vertex buffer.
Definition: glfBatch.cpp:129
void Dump(std::ostream &out=std::cout)
Defines the usage of the depth buffer.
void SetDepthBufferMode(const glfDepthBufferMode *depthBufferMode)
Sets the depth buffer mode.
Definition: glfBatch.cpp:106
void Draw()
Draws the batch.
Definition: glfBatch.cpp:218
A vertex buffer contains an array of vertices that can be used for rendering.
Exception class used for run-time errors in the OpenGLFramework.
Definition: glfException.hh:79
A batch represents a single Draw call including all parameters (render states).
Definition: glfBatch.hh:48
Represents the default render target of the current OpenGL context, i.e.
Definition: glfScreen.hh:40
void Create(int numVertices, const glfVertexFormat &format, const void *data=NULL, bool useBufferIfPossible=true)
Creates the vertex buffer for the given number of vertices and vertex format.
const std::string & GetMessageString() const
Returns the description of the error including the file name and line number where the error occured...
Interface for OpenGL rendering contexts.
void SetViewport(const glfViewport *viewport)
Sets the viewport.
Definition: glfBatch.cpp:65
void AddAttribute(Attribute attrib, int index, GLenum type, int size, bool normalized=false)
Adds a vertex attribute to the format.
class for setting viewports
Definition: glfViewport.hh:37
GLX pbuffer rendering context.
virtual void GetConfig(glfRenderingContextConfig &config)=0
Gets the currently used config of the rendering context.
void ClearDepthBuffer(float depth=1.0f)
Clears the depth buffer of the render target with the given value.
void Create(int numIndices, GLenum type, GLenum mode, const void *indices)
Creates the element buffer for the given number of indices.
A vertex format describes the attributes of a vertex.
void SetDepthTest(bool enable)
Sets whether to use depth buffer tests.
void SetSize(int width, int height)
Sets the size of the viewport in pixel coordinates.
Definition: glfViewport.cpp:51
void SetOrigin(int x, int y)
Sets the position of the upper-left corner of the viewport in pixel coordinates.
Definition: glfViewport.cpp:45
void SetElementBuffer(const glfElementBuffer *elementBuffer)
Sets the element buffer.
Definition: glfBatch.cpp:134
void ClearColorBuffer(float red=0.0f, float green=0.0f, float blue=0.0f, float alpha=0.0f)
Clears the color buffer of the render target with the given color.
virtual void Destroy()=0
Uninitializes the rendering context.
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.
Configuration for a rendering context.
virtual void Init(const glfRenderingContextConfig &config)=0
Initializes the rendering context with the given configuration.