Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRenderingContext.cpp

Example for drawing on the GPU Using the OpenGLFramwork

,glfBatch,glfScreen,glfVertexBuffer,glfElementBuffer

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleRenderingContext.cpp
@relates glfPBuffer,glfBatch,glfScreen,glfVertexBuffer,glfElementBuffer
@brief Example for drawing on the GPU Using the OpenGLFramwork
@ingroup g_examples
@ingroup g_openglframework
@author MIP
*/
#include <Base/Image/ImageIO.hh>
#include <OpenGLFramework/RenderingContext/glfPBuffer.hh>
#include <OpenGLFramework/Base/glfException.hh>
#include <OpenGLFramework/Base/glfBatch.hh>
#include <OpenGLFramework/Base/glfScreen.hh>
#include <OpenGLFramework/Base/glfViewport.hh>
#include <OpenGLFramework/Base/glfVertexBuffer.hh>
#include <OpenGLFramework/Base/glfElementBuffer.hh>
#include <vector>
using namespace std;
using namespace BIAS;
/** \cond HIDDEN_SYMBOLS */
// vertex structure
struct Vertex {
Vertex(float x, float y, float z, unsigned char r, unsigned char g, unsigned char b) {
position[0] = x;
position[1] = y;
position[2] = z;
color[0] = r;
color[1] = g;
color[2] = b;
color[3] = 255;
}
float position[3];
unsigned char color[4];
};
/** \endcond */
int main(int argc, char* argv[])
{
// only need glfRenderingContext interface.
glfPBuffer pbuffer;
glfRenderingContext* context = &pbuffer;
try {
// fill configuration
cfg.width = 640;
cfg.height = 480;
cfg.doubleBuffer = 0;
cfg.redSize = 8;
cfg.greenSize = 8;
cfg.blueSize = 8;
cfg.alphaSize = 0;
cfg.depthSize = 16;
cfg.stencilSize = 0;
// initialize context
context->Init(cfg);
// get real configuration
context->GetConfig(realCfg);
// print config
cout << "--- Requested Config ---" << endl;
cfg.Dump(cout);
cout << endl << "--- Real Config ---" << endl;
realCfg.Dump(cout);
// clear screen
glfScreen& screen = glfScreen::GetInstance();
screen.ClearColorBuffer(1.0f, 0.5f, 0.0f, 0.0f);
screen.ClearDepthBuffer();
// create vertices
vector<Vertex> vertices;
vertices.push_back(Vertex( 0.0f, -0.5f, 0.5f, 255, 0, 0));
vertices.push_back(Vertex(-0.5f, 0.5f, 0.5f, 255, 0, 0));
vertices.push_back(Vertex( 0.5f, 0.5f, 0.5f, 255, 0, 0));
vertices.push_back(Vertex( 0.2f, -0.2f, 0.8f, 0, 255, 0));
vertices.push_back(Vertex( 0.0f, 0.2f, 0.8f, 0, 255, 0));
vertices.push_back(Vertex( 0.6f, 0.2f, 0.8f, 0, 255, 0));
// create vertex format
glfVertexFormat vertexFormat;
vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_POSITION, GL_FLOAT, 3);
vertexFormat.AddAttribute(glfVertexFormat::ATTRIB_COLOR, GL_UNSIGNED_BYTE, 4);
// create vertex buffer
glfVertexBuffer vertexBuffer;
vertexBuffer.Create(vertices, vertexFormat);
// create element buffer
std::vector<unsigned char> indices;
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(3);
indices.push_back(4);
indices.push_back(5);
indices.push_back(6);
glfElementBuffer elementBuffer;
elementBuffer.Create(indices, GL_TRIANGLES);
// create depth buffer mode
glfDepthBufferMode depthBufferMode;
depthBufferMode.SetDepthTest(true);
// create viewport
glfViewport viewport;
viewport.SetOrigin(0, 0);
viewport.SetSize(640, 480);
// create and draw batch
glfBatch batch;
batch.SetDepthBufferMode(&depthBufferMode);
batch.SetVertexBuffer(&vertexBuffer);
batch.SetElementBuffer(&elementBuffer);
batch.SetViewport(&viewport);
batch.Draw();
// grab framebuffer and write to files
context->Grab(image, glfRenderingContext::GRAB_COLOR);
//if (ImageIO::Save("color.mip", image) != 0) {
if (ImageIO::Save("color.mip", image) != 0) {
return -1;
}
context->Grab(image, glfRenderingContext::GRAB_DEPTH);
//if (ImageIO::Save("depth.mip", image) != 0) {
if (ImageIO::Save("depth.mip", image) != 0) {
return -1;
}
context->Grab(image, glfRenderingContext::GRAB_STENCIL);
//if (ImageIO::Save("stencil.mip", image) != 0) {
if (ImageIO::Save("stencil.mip", image) != 0) {
return -1;
}
// clean up and exit
context->Destroy();
} catch (glfException& e) {
context->Destroy();
std::cout << "Error: " << e.GetMessageString() << std::endl;
return -1;
}
return 0;
}