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

Very simple GL application with a single scene

Author
Jan Woetzel
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003, 2004 (see file CONTACTS 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 ExampleSimpleGLApp1.cpp
* @ingroup g_examples
* @brief Very simple GL application with a single scene
* @author Jan Woetzel
*/
#include <Gui/SimpleGLApp.hh>
#include <Gui/SceneGL.hh>
// global helpers
GLfloat light_position0[] = {1.0f, 1.0, 3.0f, 1.0f}; // w=0: direction
void SetMaterial();
// your simple wrapper for a (possibly global) display function
/** \cond HIDDEN_SYMBOLS */
class MyScene : public BIAS::SceneGL {
public:
void Display(){
BIASASSERT(this->pc!=NULL); // should be set in ctor
// clear scene, either with canvas' function
// this->pc->DisplayClear();
// or optionally by hand- may be more efficient by tighter buffer mask:
glClearColor(0.2,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set MODELVIEW_PROJECTION
// internal+external virtual camera parameters
// with mouse interaction
this->pc->DisplayCameraView();
//
// more of your fancy GL calls, here:
//
this->pc->DisplayCoordCross();
// a lit geometry that depnds on mode:
glPushMatrix();
glTranslatef(0, 0. ,-1.);
SetMaterial();
glColor4f( 0.9f, 0.2f, 0.5f, 1.0f ); // paintcolor
// select geometry based on mode
const int geoMode = BIAS::SimpleGLFrame::mode %4;
switch(geoMode){
case 0:
glFrontFace(GL_CW);
glutSolidTeapot(1);
glFrontFace(GL_CCW);
break;
case 1: glutSolidSphere(1,20,20);break;
case 2: glutSolidCube(1); break;
case 3: glutSolidCone(1, 1, 20, 20); break;
}
glPopMatrix();
// the light position:
glPushMatrix();
glTranslatef(light_position0[0], light_position0[1], light_position0[2] );
glColor3f(1, 1, 0);
glDisable(GL_LIGHTING);
glutSolidSphere(0.2,32,32);
glPopMatrix();
// ...
}
};
// your simple application
class MyApp : public BIAS::SimpleGLApp {
public:
MyApp() : SimpleGLApp(){
this->scene = new MyScene();
}
};
IMPLEMENT_APP(MyApp)
// helper
void SetMaterial(){
// light source proeprties for enabled lighting:
// specified pos by w=1, direction by w=0:
GLfloat light_maincolor0[] = {1.0f, 1.0, 1.0f, 1.0f};
GLfloat light_ambientcolor0[] = {0.4f, 0.2f, 0.2f, 1.0f};
// GL create light 0
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_maincolor0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_maincolor0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambientcolor0);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING); // may be switched on/off for every object
glEnable(GL_DEPTH_TEST); // depth buffer occlusions of objects
// material properties for enabled lighting:
GLfloat paintcolor0[] = { 0.9f, 0.2f, 0.5f, 1.0f};
GLfloat specularcolor0[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat shininess0[] = { 70.0f }; // 1..128, large values make smaller specular highlights
// activate material properties:
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specularcolor0);
glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, shininess0);
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, paintcolor0);
glColor4fv(&paintcolor0[0]);
}
/** \endcond */