Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

ViewerOpenGL.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 1998-2000  Chris Morley
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 //
00020 
00021 # ifndef VIEWEROPENGL_H
00022 #   define VIEWEROPENGL_H
00023 
00024 #   ifdef HAVE_CONFIG_H
00025 #     include <config.h>
00026 #   endif
00027 
00028 #   include <stack>
00029 #   include <OpenVRML/Viewer.h>
00030 #   include "common.h"
00031 
00032 // Use the stencil buffer to set the SHAPE mask.
00033 #   define USE_STENCIL_SHAPE 0
00034 
00035 extern "C" struct GLUtesselator;
00036 
00037 namespace OpenVRML {
00038 
00039     namespace GL {
00040 
00041         class OPENVRML_GL_SCOPE ViewerOpenGL : public Viewer {
00042         public:
00043             enum { MAX_LIGHTS = 8 };
00044 
00045             enum LightType {
00046                 LIGHT_UNUSED,
00047                 LIGHT_DIRECTIONAL,
00048                 LIGHT_POSITIONAL
00049             };
00050 
00051             // Event types
00052             enum EventType {
00053                 EVENT_KEY_DOWN,
00054                 EVENT_MOUSE_MOVE,
00055                 EVENT_MOUSE_CLICK,
00056                 EVENT_MOUSE_DRAG,
00057                 EVENT_MOUSE_RELEASE
00058             };
00059 
00060             enum {
00061                 KEY_HOME,
00062                 KEY_LEFT,
00063                 KEY_UP,
00064                 KEY_RIGHT,
00065                 KEY_DOWN,
00066                 KEY_PAGE_UP,
00067                 KEY_PAGE_DOWN
00068             };
00069 
00070             struct EventInfo {
00071                 EventType event;
00072                 int what; // key or button number
00073                 int x, y;
00074             };
00075 
00076         protected:
00077             enum CursorStyle {
00078                 CURSOR_INHERIT,
00079                 CURSOR_INFO,
00080                 CURSOR_CYCLE,
00081                 CURSOR_UP_DOWN,
00082                 CURSOR_CROSSHAIR
00083             };
00084 
00085             class ModelviewMatrixStack {
00086                 size_t size;
00087                 std::stack<VrmlMatrix> spillover;
00088 
00089             public:
00090                 ModelviewMatrixStack();
00091 
00092                 void push();
00093                 void pop();
00094             };
00095 
00096             struct LightInfo {
00097                 LightType lightType;
00098                 int nestingLevel;
00099                 float radiusSquared;
00100                 float location[3];
00101             };
00102 
00103             ModelviewMatrixStack modelviewMatrixStack;
00104 
00105             // GL status
00106             bool d_GLinitialized;
00107             bool d_blend;
00108             bool d_lit;
00109             bool d_texture;
00110             bool d_wireframe;
00111 
00112             int d_window;
00113             int d_winWidth, d_winHeight;
00114             float d_background[3];
00115 
00116             // Groups used to be put in display lists, now these counters
00117             // trigger various initializations and clean ups
00118             int d_nObjects, d_nestedObjects;
00119 
00120             // Tessellation
00121             GLUtesselator * tesselator;
00122 
00123             // Pickable entities
00124             int d_nSensitive;
00125             int d_activeSensitive;
00126             int d_overSensitive;
00127 
00128             enum { MAXSENSITIVE = 1000 }; // make dynamic?...
00129             Node * d_sensitiveObject[MAXSENSITIVE];
00130 
00131             bool d_selectMode;
00132             double d_selectZ; // window Z coord of last selection
00133 
00134             // Lights
00135             LightInfo d_lightInfo[MAX_LIGHTS];
00136 
00137             // View manipulation
00138             float d_position[3];
00139             float d_zoom[3];
00140             float d_target[3];
00141             float d_orientation[4];
00142 
00143             int d_beginx, d_beginy;
00144             float d_scale;
00145             float d_translatex, d_translatey, d_translatez;
00146 
00147             // quaternion representations of last, current rotation
00148             float d_lastquat[4], d_curquat[4];
00149             bool d_rotationChanged;
00150             float d_rotationMatrix[4][4];
00151 
00152             bool d_rotating, d_scaling, d_translating;
00153             bool d_drawBSpheres;
00154             bool d_cull;
00155 
00156             double d_renderTime;
00157             double d_renderTime1;
00158 
00159             // Window system specific methods
00160 
00161             virtual void wsPostRedraw() = 0;
00162             virtual void wsSetCursor( CursorStyle c) = 0;
00163             virtual void wsSwapBuffers() = 0;
00164             virtual void wsSetTimer( double ) = 0;
00165 
00166             // Initialize OpenGL state
00167             void initialize();
00168 
00169             // Geometry insertion setup & cleanup methods
00170             void beginGeometry();
00171             void endGeometry();
00172 
00173             // User interaction
00174             void step(float, float, float);
00175             void rot(float x , float y, float z, float a);
00176             void zoom(float);
00177             void rot_trackball(float x1, float y1, float x2, float y2);
00178 
00179 #ifndef macintosh
00180             void handleKey(int);
00181 #endif
00182             void handleButton(EventInfo*);
00183             void handleMouseDrag(int, int);
00184 
00185             void insertExtrusionCaps(unsigned int mask, size_t nSpine, const float * c,
00186                                      size_t nCrossSection, const float * cs);
00187 
00188             // Check for pickable entity selection
00189             bool checkSensitive(int x, int y, EventType );
00190 
00191         public:
00192             explicit ViewerOpenGL(Browser & browser);
00193             virtual ~ViewerOpenGL();
00194 
00195             virtual RenderMode getRenderMode();
00196             virtual double getFrameRate();
00197 
00198             //
00199             virtual void resetUserNavigation();
00200             virtual void getUserNavigation(VrmlMatrix & M);
00201 
00202             // Scope dirlights, open/close display lists
00203             virtual Object beginObject(const char *name, bool retain);
00204             virtual void endObject();
00205 
00206             // Insert objects into the display list
00207             virtual Object insertBackground(size_t nGroundAngles = 0,
00208                                             const float * groundAngle = 0,
00209                                             const float * groundColor = 0,
00210                                             size_t nSkyAngles = 0,
00211                                             const float * skyAngle = 0,
00212                                             const float * skyColor = 0,
00213                                             int* whc = 0,
00214                                             unsigned char ** pixels = 0);
00215 
00216 
00217             virtual Object insertBox(float x, float y, float z);
00218 
00219             virtual Object insertCone(float h, float r, bool bottom, bool side);
00220             virtual Object insertCylinder(float h, float r, bool, bool, bool);
00221 
00222             virtual Object insertElevationGrid(unsigned int mask,
00223                                                size_t nx,
00224                                                size_t nz,
00225                                                const float * height,
00226                                                float dx,
00227                                                float dz,
00228                                                const float * tc,
00229                                                const float * normals,
00230                                                const float * colors);
00231 
00232             virtual Object insertExtrusion(unsigned int,
00233                                            size_t nOrientation,
00234                                            const float * orientation,
00235                                            size_t nScale,
00236                                            const float * scale,
00237                                            size_t nCrossSection,
00238                                            const float * crossSection,
00239                                            size_t nSpine,
00240                                            const float * spine);
00241 
00242             virtual Object insertLineSet(size_t, const float *, size_t, const long *,
00243                                          bool colorPerVertex,
00244                                          const float * color,
00245                                          size_t nColorIndex,
00246                                          const long * colorIndex);
00247 
00248             virtual Object insertPointSet(size_t npts, const float * points, const float * colors);
00249             virtual Object insertShell(unsigned int mask,
00250                                        size_t npoints, const float * points,
00251                                        size_t nfaces, const long * faces,
00252                                        const float * tc,
00253                                        size_t ntci, const long * tci,
00254                                        const float * normal,
00255                                        size_t nni, const long * ni,
00256                                        const float * color,
00257                                        size_t nci, const long * ci);
00258 
00259             virtual Object insertSphere(float radius);
00260 
00261             // Lights
00262             virtual Object insertDirLight(float a, float i, const float rgb[], const float xyz[]);
00263 
00264             virtual Object insertPointLight(float ambientIntensity,
00265                                             const float attenuation[],
00266                                             const float color[],
00267                                             float intensity,
00268                                             const float location[],
00269                                             float radius );
00270 
00271             virtual Object insertSpotLight(float ambientIntensity ,
00272                                            const float attenuation[],
00273                                            float beamWidth ,
00274                                            const float color[],
00275                                            float cutOffAngle ,
00276                                            const float direction[],
00277                                            float intensity,
00278                                            const float location[],
00279                                            float radius);
00280 
00281 
00282             // Lightweight copy
00283             virtual Object insertReference(Object existingObject);
00284 
00285             // Remove an object from the display list
00286             virtual void removeObject(Object key);
00287 
00288             virtual void enableLighting(bool);
00289 
00290             // Set attributes
00291             virtual void setColor(float r, float g, float b, float a = 1.0);
00292 
00293             virtual void setFog(const float * /*color*/,
00294                                 float   /*visibilityRange*/,
00295                                 const char * /*fogType*/);
00296 
00297             virtual void setMaterial(float ambientIntensity,
00298                                      const float diffuseColor[3],
00299                                      const float emissiveColor[3],
00300                                      float shininess,
00301                                      const float specularColor[3],
00302                                      float transparency);
00303 
00304             virtual void setMaterialMode( int nTexComponents, bool geometryColor );
00305 
00306             virtual void setSensitive(Node * object);
00307 
00308             virtual void scaleTexture(size_t /*w*/, size_t /*h*/,
00309                                       size_t /*newW*/, size_t /*newH*/,
00310                                       size_t /*nc*/,
00311                                       unsigned char* /*pixels*/);
00312 
00313             virtual TextureObject insertTexture(size_t w, size_t h, size_t nc,
00314                                                 bool repeat_s,
00315                                                 bool repeat_t,
00316                                                 const unsigned char *pixels,
00317                                                 bool retainHint = false);
00318 
00319             virtual TextureObject insertSubTexture(size_t xoffset, size_t yoffset,
00320                                                    size_t w, size_t h,
00321                                                    size_t whole_w, size_t whole_h,
00322                                                    size_t nc, bool repeat_s,
00323                                                    bool repeat_t,
00324                                                    const unsigned char *pixels,
00325                                                    bool retainHint = false);
00326 
00327             // Reference/remove a texture object
00328             virtual void insertTextureReference(TextureObject, int);
00329             virtual void removeTextureObject(TextureObject);
00330 
00331             virtual void setTextureTransform(const float center[2],
00332                                              float rotation,
00333                                              const float scale[2],
00334                                              const float translation[2]);
00335 
00336             virtual void setViewpoint(const float position[3],
00337                                       const float orientation[4],
00338                                       float fieldOfView,
00339                                       float avatarSize,
00340                                       float visLimit);
00341 
00342             virtual void transform(const VrmlMatrix & mat);
00343 
00344             // The viewer knows the current viewpoint
00345             virtual void transformPoints(int nPoints, float *points);
00346 
00347             virtual void drawBSphere(const BSphere & bs,
00348                                      BVolume::Intersection intersection);
00349 
00350 
00351 
00352             //
00353             // Viewer callbacks (not for public consumption)
00354 
00355             // Update the model.
00356             void update( double time = 0.0 );
00357 
00358             // Redraw the screen.
00359             virtual void redraw();
00360             void resize(int w, int h);
00361 
00362             // user interaction
00363 #ifdef macintosh /* in order to be able to send 'keys' from menus */
00364             void handleKey(int);
00365 #endif
00366 
00367             void input( EventInfo *);
00368         };
00369     }
00370 }
00371 
00372 # endif // VIEWEROPENGL_H