PCMOpenGLRenderingEngine.h

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00027 
00028 #ifndef __PCM_OPENGLRENDERINGENGINE_H__
00029 #  define __PCM_OPENGLRENDERINGENGINE_H__
00030 
00031 //
00032 // Inner includes
00033 //
00034 
00035 #  include "PCMPremeditations.h"
00036 
00037 #  include "PCMLogger.h"
00038 
00039 #  include "PCMException.h"
00040 
00041 #  include "PCMPointer.h"
00042 
00043 //
00044 // Outer includes
00045 //
00046 
00047 #  include <list>
00048 
00049 #  include <set>
00050 
00051 #  include <vector>
00052 
00053 //
00054 // Scripting bind includes (for Squirrel)
00055 //
00056 
00057 #  include <squirrel/squirrel.h>
00058 #  include <squirrel/sqstdio.h>
00059 #  include <squirrel/sqstdaux.h>
00060 #  include <squirrel/sqplus.h>
00061 
00062 namespace ParCompMark
00063 {
00064 
00069   class OpenGLRenderingEngine
00070   {
00071 
00072         //
00073         // Unit test class
00074         //
00075 #  ifdef PARCOMPMARK_TEST
00076         friend class ParCompMarkTest::TestOpenGLRenderingEngine;
00077 #  endif
00078 
00079         //
00080         // Scripting binding (for Squirrel)
00081         //
00082 
00084 
00085 
00093    public:static void squirrelGlue()
00094         {
00095          ::SqPlus::SQClassDef < OpenGLRenderingEngine >
00096                 (_T("OpenGLRenderingEngine")).staticFunc(&OpenGLRenderingEngine::create,
00097                                                          _T("create")).
00098                 func(&OpenGLRenderingEngine::createCustomRenderer,
00099                   _T("createCustomRenderer")).func(&OpenGLRenderingEngine::perspective,
00100                                                   _T("perspective")).func(&OpenGLRenderingEngine::ortho2D,
00101                                                                           _T("ortho2D")).
00102                 func(&OpenGLRenderingEngine::viewport, _T("viewport")).func(&OpenGLRenderingEngine::pushModelView,
00103                                                                          _T("pushModelView")).
00104                 func(&OpenGLRenderingEngine::popModelView, _T("popModelView")).func(&OpenGLRenderingEngine::translate,
00105                                                                                  _T("translate")).
00106                 func(&OpenGLRenderingEngine::rotate, _T("rotate")).func(&OpenGLRenderingEngine::scale,
00107                                                                         _T("scale")).
00108                 func(&OpenGLRenderingEngine::setCameraPosition,
00109                   _T("setCameraPosition")).func(&OpenGLRenderingEngine::setCameraTarget,
00110                                                  _T("setCameraTarget")).
00111                 func(&OpenGLRenderingEngine::setCameraUpVector,
00112                   _T("setCameraUpVector")).func(&OpenGLRenderingEngine::setColor,
00113                                                  _T("setColor")).func(&OpenGLRenderingEngine::setAmbientMaterial,
00114                                                                         _T("setAmbientMaterial")).
00115                 func(&OpenGLRenderingEngine::setDiffuseMaterial,
00116                   _T("setDiffuseMaterial")).func(&OpenGLRenderingEngine::setSpecularMaterial,
00117                                                  _T("setSpecularMaterial")).
00118                 func(&OpenGLRenderingEngine::setDrawStyle,
00119                   _T("setDrawStyle")).func(&OpenGLRenderingEngine::setBackCulling,
00120                                           _T("setBackCulling")).func(&OpenGLRenderingEngine::setBlending,
00121                                                                          _T("setBlending")).
00122                 func(&OpenGLRenderingEngine::drawTriangle,
00123                   _T("drawTriangle")).func(&OpenGLRenderingEngine::generateRandomTriangles,
00124                                           _T("generateRandomTriangles")).func(&OpenGLRenderingEngine::renderObject,
00125                                                                                 _T("renderObject")).
00126                 func(&OpenGLRenderingEngine::renderObjectTriangles,
00127                   _T("renderObjectTriangles")).func(&OpenGLRenderingEngine::setAmbientLight,
00128                                                    _T("setAmbientLight")).
00129                 func(&OpenGLRenderingEngine::removeLightSources,
00130                   _T("removeLightSources")).func(&OpenGLRenderingEngine::addLightSource,
00131                                                  _T("addLightSource")).
00132                 func(&OpenGLRenderingEngine::setLightSourcePosition,
00133                   _T("setLightSourcePosition")).func(&OpenGLRenderingEngine::setLightSourceDiffuse,
00134                                                         _T("setLightSourceDiffuse")).
00135                 func(&OpenGLRenderingEngine::setLightSourceSpecular,
00136                   _T("setLightSourceSpecular")).func(&OpenGLRenderingEngine::createDisplayList,
00137                                                         _T("createDisplayList")).
00138                 func(&OpenGLRenderingEngine::finishDisplayList,
00139                   _T("finishDisplayList")).func(&OpenGLRenderingEngine::executeDisplayList,
00140                                                  _T("executeDisplayList")).func(&OpenGLRenderingEngine::drawSphere,
00141                                                                                 _T("drawSphere")).
00142                 func(&OpenGLRenderingEngine::drawCylinder, _T("drawCylinder")).func(&OpenGLRenderingEngine::drawDisk,
00143                                                                                  _T("drawDisk")).
00144                 func(&OpenGLRenderingEngine::drawTeapot, _T("drawTeapot")).func(&OpenGLRenderingEngine::drawCube,
00145                                                                                 _T("drawCube")).
00146                 func(&OpenGLRenderingEngine::drawTorus, _T("drawTorus")).func(&OpenGLRenderingEngine::drawDodecahedron,
00147                                                                           _T("drawDodecahedron")).
00148                 func(&OpenGLRenderingEngine::drawOctahedron,
00149                   _T("drawOctahedron")).func(&OpenGLRenderingEngine::drawTetrahedron,
00150                                                 _T("drawTetrahedron")).func(&OpenGLRenderingEngine::drawIcosahedron,
00151                                                                          _T("drawIcosahedron"));
00152         }
00153 
00155 
00156         //
00157         // Enumerations
00158         //
00159 
00160    public:
00161 
00163         enum DrawStyle
00164         {
00165 
00167          NONE = 0,
00168 
00170          POINT = 1,
00171 
00173          WIRE = 2,
00174 
00176          FILL = 3
00177         };
00178 
00179         //
00180         // Typedefs
00181         //
00182 
00183    public:
00184 
00186         typedef Pointer < OpenGLRenderingEngine,
00187         Mutex > Pointer;
00188 
00189    protected:
00190 
00192         typedef struct Camera
00193         {
00194          Real position[3];
00195          Real target[3];
00196 
00197          Real upVector[3];                                                               
00198           Camera()
00199          {
00200                 position[0] = position[1] = position[2] = 1.0;
00201                 target[0] = target[1] = target[2] = 0.0;
00202                 upVector[0] = 0.0;
00203                 upVector[1] = 1.0;
00204                 upVector[2] = 0.0;
00205          };
00206         } Camera;
00207 
00209         typedef struct ObjectData
00210 
00211         {                       
00213          int vertexCount;                                                       
00215          int vertexSize;                                                                                                                                                  
00217          int texSize;                                                                                                                                                                                                                                                    
00219          float *vertexCoordinates;                                                                                                                                                                                                                                                                                                                
00221          float *texCoordinates;                                                                                                                                                                                                                                                                                                                                                                                  
00223          float *normals;                                                                                                                                                                                                                                                                                                                                                                                                                                                
00224           ObjectData()
00225          {
00226                 vertexSize = 3;
00227                 texSize = 0;
00228                 vertexCoordinates = texCoordinates = 0;
00229          };
00230         } ObjectData;
00231 
00233         typedef struct DisplayList
00234 
00235         {                        
00237          u32 handle;                                                       
00239          u32 triangleCount;                                                                                                                        
00240           DisplayList()
00241          {
00242                 handle = 0;
00243                 triangleCount = 0;
00244          };
00245         } DisplayList;
00246 
00247         //
00248         // Variables
00249         //
00250 
00252 
00253 
00254    protected:
00255 
00261         Process * mParent;
00262 
00268         Camera mCamera;
00269 
00275         DrawStyle mDrawStyle;
00276 
00282         std::set < Renderer * >mRenderers;
00283 
00289         std::vector < Renderer * >mAutoRenderingQueue;
00290 
00296         std::vector < ObjectData > mObjectData;
00297 
00303         std::vector < DisplayList * >mDisplayLists;
00304 
00310         DisplayList *mCurrentDisplayList;
00311 
00317         u32 mLightCount;
00318 
00320 
00321         //
00322         // Constructors & destructor
00323         //
00324 
00326 
00327 
00328    public:
00329 
00333         OpenGLRenderingEngine();
00334 
00339         OpenGLRenderingEngine(Process * parent);
00340 
00344         virtual ~ OpenGLRenderingEngine();
00345 
00347 
00348         //
00349         // Getters & setters
00350         //
00351 
00353 
00354 
00355    public:
00356 
00362         Process * getParent()const;
00363 
00369         const OpenGLRenderingEngine::Camera & getCamera() const;
00370 
00376         const OpenGLRenderingEngine::DrawStyle & getDrawStyle() const;
00377 
00379 
00380         //
00381         // Class methods
00382         //
00383 
00385 
00386 
00387    public:
00388 
00394         static OpenGLRenderingEngine *create(const char *stringPtr);
00395 
00397 
00398         //
00399         // Methods
00400         //
00401 
00403 
00404 
00405    public:
00406 
00412         virtual Renderer * createCustomRenderer(const char *rendererName);
00413 
00421         virtual void resizeRenderers(const u32 width, const u32 height);
00422 
00430         virtual s32 getSortOrderFromRenderers();
00431 
00440         virtual void setAutoRenderOrder(const Renderer * renderer, const s32 & order);
00441 
00447         virtual void doAutoRendering();
00448 
00455         virtual void perspective(const double fovy, const double zNear, const double zFar);
00456 
00464         virtual void ortho2D(const double left, const double right, const double bottom,
00465                           const double top);
00466 
00474         virtual void viewport(const double left, const double top, const double width,
00475                           const double height);
00476 
00480         virtual void pushModelView();
00481 
00485         virtual void popModelView();
00486 
00493         virtual void translate(const double x, const double y, const double z);
00494 
00502         virtual void rotate(const double angle, const double x, const double y, const double z);
00503 
00510         virtual void scale(const double x, const double y, const double z);
00511 
00518         virtual void setCameraPosition(const double eyeX, const double eyeY, const double eyeZ);
00519 
00526         virtual void setCameraTarget(const double centerX, const double centerY,
00527                                   const double centerZ);
00528 
00535         virtual void setCameraUpVector(const double upX, const double upY, const double upZ);
00536 
00544         virtual void setColor(const double red, const double green, const double blue,
00545                           const double alpha);
00546 
00554         virtual void setAmbientMaterial(const double red, const double green, const double blue,
00555                                         const double alpha);
00556 
00564         virtual void setDiffuseMaterial(const double red, const double green, const double blue,
00565                                         const double alpha);
00566 
00575         virtual void setSpecularMaterial(const double red, const double green, const double blue,
00576                                          const double alpha, const int shininess);
00577 
00582         virtual void setDrawStyle(const unsigned drawStyle);
00583 
00588         virtual void setBackCulling(const bool isBackCulling);
00589 
00594         virtual void setBlending(const bool isOn);
00595 
00599         virtual void drawTriangle();
00600 
00610         virtual int generateRandomTriangles(const int dimension, const int count);
00611 
00617         virtual void renderObject(const u32 handle, const bool useVertexArrays);
00618 
00625         virtual void renderObjectTriangles(const u32 handle, const bool useVertexArrays,
00626                                          const u32 triangleCount);
00627 
00635         virtual void setAmbientLight(const double red, const double green, const double blue,
00636                                   const double alpha);
00637 
00641         virtual void removeLightSources();
00642 
00647         virtual int addLightSource();
00648 
00656         virtual void setLightSourcePosition(const int light, const double x, const double y,
00657                                          const double z);
00658 
00667         virtual void setLightSourceDiffuse(const int light, const double red, const double green,
00668                                          const double blue, const double alpha);
00669 
00678         virtual void setLightSourceSpecular(const int light, const double red, const double green,
00679                                          const double blue, const double alpha);
00680 
00685         virtual u32 createDisplayList();
00686 
00690         virtual void finishDisplayList();
00691 
00696         virtual void executeDisplayList(const u32 displayList);
00697 
00704         virtual void drawSphere(const double radius, const int slices, const int stacks);
00705 
00714         virtual void drawCylinder(const double baseRadius, const double topRadius,
00715                                 const double height, const int slices, const int stacks);
00716 
00724         virtual void drawDisk(const double innerRadius, const double outerRadius, const int slices,
00725                           const int loops);
00726 
00731         virtual void drawTeapot(const double size);
00732 
00737         virtual void drawCube(const double size);
00738 
00746         virtual void drawTorus(const double innerRadius, const double outerRadius, const int nsides,
00747                            const int rings);
00748 
00753         virtual void drawDodecahedron(const double size);
00754 
00759         virtual void drawOctahedron(const double size);
00760 
00765         virtual void drawTetrahedron(const double size);
00766 
00771         virtual void drawIcosahedron(const double size);
00772 
00773    protected:
00774 
00779         virtual void _registerRenderer(Renderer * renderer);
00780 
00786         virtual u32 _registerObject(ObjectData objectData);
00787 
00794         virtual void _renderObject(const u32 handle, const bool useVertexArrays,
00795                                  const u32 vertexCount);
00796 
00800         virtual void _refreshCamera();
00801 
00807         virtual bool _setGLUDrawStyle(const void *quadratic);
00808 
00814         virtual void _reportTriangles(const u32 & triangleCount);
00815 
00817 
00818   };
00819 
00820   //
00821   // Include header implementation
00822   //
00823 
00824 #  include "PCMOpenGLRenderingEngine_impl.h"
00825 
00826 }
00827 
00828 // Declare OpenGLRenderingEngine class (and its enums) for SqPlus binding
00830 DECLARE_INSTANCE_TYPE_NAME(ParCompMark::OpenGLRenderingEngine, OpenGLRenderingEngine);
00831 
00832 #endif