| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_TEXGEN |
|---|
| 15 | #define OSG_TEXGEN 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Plane> |
|---|
| 18 | #include <osg/StateAttribute> |
|---|
| 19 | |
|---|
| 20 | #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) || defined(OSG_GL3_AVAILABLE) |
|---|
| 21 | #define GL_OBJECT_LINEAR 0x2401 |
|---|
| 22 | #define GL_EYE_LINEAR 0x2400 |
|---|
| 23 | #define GL_SPHERE_MAP 0x2402 |
|---|
| 24 | #define GL_TEXTURE_GEN_S 0x0C60 |
|---|
| 25 | #define GL_TEXTURE_GEN_T 0x0C61 |
|---|
| 26 | #define GL_TEXTURE_GEN_R 0x0C62 |
|---|
| 27 | #define GL_TEXTURE_GEN_Q 0x0C63 |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | #ifndef GL_NORMAL_MAP_ARB |
|---|
| 31 | #define GL_NORMAL_MAP_ARB 0x8511 |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #ifndef GL_REFLECTION_MAP_ARB |
|---|
| 35 | #define GL_REFLECTION_MAP_ARB 0x8512 |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | namespace osg { |
|---|
| 39 | |
|---|
| 40 | /** TexGen encapsulates the OpenGL glTexGen (texture coordinate generation) |
|---|
| 41 | * state.*/ |
|---|
| 42 | class OSG_EXPORT TexGen : public StateAttribute |
|---|
| 43 | { |
|---|
| 44 | public : |
|---|
| 45 | |
|---|
| 46 | TexGen(); |
|---|
| 47 | |
|---|
| 48 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 49 | TexGen(const TexGen& texgen,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 50 | StateAttribute(texgen,copyop), |
|---|
| 51 | _mode(texgen._mode), |
|---|
| 52 | _plane_s(texgen._plane_s), |
|---|
| 53 | _plane_t(texgen._plane_t), |
|---|
| 54 | _plane_r(texgen._plane_r), |
|---|
| 55 | _plane_q(texgen._plane_q) {} |
|---|
| 56 | |
|---|
| 57 | META_StateAttribute(osg, TexGen, TEXGEN); |
|---|
| 58 | |
|---|
| 59 | virtual bool isTextureAttribute() const { return true; } |
|---|
| 60 | |
|---|
| 61 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 62 | virtual int compare(const StateAttribute& sa) const |
|---|
| 63 | { |
|---|
| 64 | // Check for equal types, then create the rhs variable |
|---|
| 65 | // used by the COMPARE_StateAttribute_Parameter macros below. |
|---|
| 66 | COMPARE_StateAttribute_Types(TexGen,sa) |
|---|
| 67 | |
|---|
| 68 | // Compare each parameter in turn against the rhs. |
|---|
| 69 | COMPARE_StateAttribute_Parameter(_mode) |
|---|
| 70 | COMPARE_StateAttribute_Parameter(_plane_s) |
|---|
| 71 | COMPARE_StateAttribute_Parameter(_plane_t) |
|---|
| 72 | COMPARE_StateAttribute_Parameter(_plane_r) |
|---|
| 73 | COMPARE_StateAttribute_Parameter(_plane_q) |
|---|
| 74 | |
|---|
| 75 | return 0; // Passed all the above comparison macros, so must be equal. |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 79 | { |
|---|
| 80 | usage.usesTextureMode(GL_TEXTURE_GEN_S); |
|---|
| 81 | usage.usesTextureMode(GL_TEXTURE_GEN_T); |
|---|
| 82 | |
|---|
| 83 | // Not happy with turning all tex gen parameters on |
|---|
| 84 | // as the OSG currently only supports 2D textures and therefore |
|---|
| 85 | // only S and T will be required, R&Q would be redundant... |
|---|
| 86 | // So commenting out the following until OSG supports 3D textures. |
|---|
| 87 | // I plan to revamp the OpenGL state management later so will |
|---|
| 88 | // tidy up then. Robert Osfield. Jan 2001. |
|---|
| 89 | |
|---|
| 90 | // The tidy up is now happening, but will have a think before |
|---|
| 91 | // resolving the below parameters. |
|---|
| 92 | |
|---|
| 93 | usage.usesTextureMode(GL_TEXTURE_GEN_R); |
|---|
| 94 | usage.usesTextureMode(GL_TEXTURE_GEN_Q); |
|---|
| 95 | return true; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | virtual void apply(State& state) const; |
|---|
| 99 | |
|---|
| 100 | enum Mode { |
|---|
| 101 | OBJECT_LINEAR = GL_OBJECT_LINEAR, |
|---|
| 102 | EYE_LINEAR = GL_EYE_LINEAR, |
|---|
| 103 | SPHERE_MAP = GL_SPHERE_MAP, |
|---|
| 104 | NORMAL_MAP = GL_NORMAL_MAP_ARB, |
|---|
| 105 | REFLECTION_MAP = GL_REFLECTION_MAP_ARB |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | inline void setMode( Mode mode ) { _mode = mode; } |
|---|
| 109 | |
|---|
| 110 | Mode getMode() const { return _mode; } |
|---|
| 111 | |
|---|
| 112 | enum Coord { |
|---|
| 113 | S, T, R, Q |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | void setPlane(Coord which, const Plane& plane); |
|---|
| 117 | |
|---|
| 118 | Plane& getPlane(Coord which); |
|---|
| 119 | |
|---|
| 120 | const Plane& getPlane(Coord which) const; |
|---|
| 121 | |
|---|
| 122 | /** Set the tex gen planes from specified matrix. |
|---|
| 123 | * Typical usage would be to pass in a projection |
|---|
| 124 | * matrix to set up projective texturing. |
|---|
| 125 | */ |
|---|
| 126 | void setPlanesFromMatrix(const Matrixd& matrix); |
|---|
| 127 | |
|---|
| 128 | protected : |
|---|
| 129 | |
|---|
| 130 | virtual ~TexGen( void ); |
|---|
| 131 | |
|---|
| 132 | Mode _mode; |
|---|
| 133 | |
|---|
| 134 | /** Additional texgen coefficients for GL_OBJECT_PLANE or |
|---|
| 135 | * GL_EYE_PLANE, */ |
|---|
| 136 | Plane _plane_s, _plane_t, _plane_r, _plane_q; |
|---|
| 137 | |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | #endif |
|---|