| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/GLExtensions> |
|---|
| 15 | #include <osg/GL> |
|---|
| 16 | #include <osg/State> |
|---|
| 17 | #include <osg/Point> |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | |
|---|
| 20 | using namespace osg; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | #ifndef GL_VERSION_1_4 |
|---|
| 24 | # define GL_POINT_SIZE_MIN 0x8126 |
|---|
| 25 | # define GL_POINT_SIZE_MAX 0x8127 |
|---|
| 26 | # define GL_POINT_FADE_THRESHOLD_SIZE 0x8128 |
|---|
| 27 | # define GL_POINT_DISTANCE_ATTENUATION 0x8129 |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | Point::Point() |
|---|
| 31 | { |
|---|
| 32 | _size = 1.0f; |
|---|
| 33 | _fadeThresholdSize = 1.0f; |
|---|
| 34 | _distanceAttenuation = Vec3(1, 0.0, 0.0); |
|---|
| 35 | |
|---|
| 36 | _minSize = 0.0; |
|---|
| 37 | _maxSize = 100.0; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | Point::Point(float size) |
|---|
| 41 | { |
|---|
| 42 | _size = size; |
|---|
| 43 | _fadeThresholdSize = 1.0f; |
|---|
| 44 | _distanceAttenuation = Vec3(1, 0.0, 0.0); |
|---|
| 45 | |
|---|
| 46 | _minSize = 0.0; |
|---|
| 47 | _maxSize = 100.0; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | Point::~Point() |
|---|
| 51 | { |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | void Point::setSize( float size ) |
|---|
| 56 | { |
|---|
| 57 | _size = size; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | void Point::setFadeThresholdSize(float fadeThresholdSize) |
|---|
| 62 | { |
|---|
| 63 | _fadeThresholdSize = fadeThresholdSize; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void Point::setDistanceAttenuation(const Vec3& distanceAttenuation) |
|---|
| 68 | { |
|---|
| 69 | _distanceAttenuation = distanceAttenuation; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void Point::setMinSize(float minSize) |
|---|
| 73 | { |
|---|
| 74 | _minSize = minSize; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void Point::setMaxSize(float maxSize) |
|---|
| 78 | { |
|---|
| 79 | _maxSize = maxSize; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void Point::apply(State& state) const |
|---|
| 83 | { |
|---|
| 84 | #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE |
|---|
| 85 | glPointSize(_size); |
|---|
| 86 | |
|---|
| 87 | const unsigned int contextID = state.getContextID(); |
|---|
| 88 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 89 | |
|---|
| 90 | if (!extensions->isPointParametersSupported()) |
|---|
| 91 | return; |
|---|
| 92 | |
|---|
| 93 | extensions->glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, (const GLfloat*)&_distanceAttenuation); |
|---|
| 94 | extensions->glPointParameterf(GL_POINT_FADE_THRESHOLD_SIZE, _fadeThresholdSize); |
|---|
| 95 | extensions->glPointParameterf(GL_POINT_SIZE_MIN, _minSize); |
|---|
| 96 | extensions->glPointParameterf(GL_POINT_SIZE_MAX, _maxSize); |
|---|
| 97 | #else |
|---|
| 98 | OSG_NOTICE<<"Warning: Point::apply(State&) - not supported."<<std::endl; |
|---|
| 99 | #endif |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | typedef buffered_value< ref_ptr<Point::Extensions> > BufferedExtensions; |
|---|
| 104 | static BufferedExtensions s_extensions; |
|---|
| 105 | |
|---|
| 106 | Point::Extensions* Point::getExtensions(unsigned int contextID,bool createIfNotInitalized) |
|---|
| 107 | { |
|---|
| 108 | if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); |
|---|
| 109 | return s_extensions[contextID].get(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void Point::setExtensions(unsigned int contextID,Extensions* extensions) |
|---|
| 113 | { |
|---|
| 114 | s_extensions[contextID] = extensions; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | Point::Extensions::Extensions(unsigned int contextID) |
|---|
| 118 | { |
|---|
| 119 | setupGLExtensions(contextID); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | Point::Extensions::Extensions(const Extensions& rhs): |
|---|
| 123 | Referenced() |
|---|
| 124 | { |
|---|
| 125 | _isPointParametersSupported = rhs._isPointParametersSupported; |
|---|
| 126 | _isPointSpriteCoordOriginSupported = rhs._isPointSpriteCoordOriginSupported; |
|---|
| 127 | _glPointParameteri = rhs._glPointParameteri; |
|---|
| 128 | _glPointParameterf = rhs._glPointParameterf; |
|---|
| 129 | _glPointParameterfv = rhs._glPointParameterfv; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void Point::Extensions::lowestCommonDenominator(const Extensions& rhs) |
|---|
| 133 | { |
|---|
| 134 | if (!rhs._isPointParametersSupported) _isPointParametersSupported = false; |
|---|
| 135 | if (!rhs._isPointSpriteCoordOriginSupported) _isPointSpriteCoordOriginSupported = false; |
|---|
| 136 | if (!rhs._glPointParameteri) _glPointParameteri = 0; |
|---|
| 137 | if (!rhs._glPointParameterf) _glPointParameterf = 0; |
|---|
| 138 | if (!rhs._glPointParameterfv) _glPointParameterfv = 0; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void Point::Extensions::setupGLExtensions(unsigned int contextID) |
|---|
| 142 | { |
|---|
| 143 | _isPointParametersSupported = OSG_GL3_FEATURES || |
|---|
| 144 | strncmp((const char*)glGetString(GL_VERSION),"1.4",3)>=0 || |
|---|
| 145 | isGLExtensionSupported(contextID,"GL_ARB_point_parameters") || |
|---|
| 146 | isGLExtensionSupported(contextID,"GL_EXT_point_parameters") || |
|---|
| 147 | isGLExtensionSupported(contextID,"GL_SGIS_point_parameters"); |
|---|
| 148 | |
|---|
| 149 | _isPointSpriteCoordOriginSupported = OSG_GL3_FEATURES || strncmp((const char*)glGetString(GL_VERSION),"2.0",3)>=0; |
|---|
| 150 | |
|---|
| 151 | setGLExtensionFuncPtr(_glPointParameteri, "glPointParameteri", "glPointParameteriARB"); |
|---|
| 152 | if (!_glPointParameteri) setGLExtensionFuncPtr(_glPointParameteri, "glPointParameteriEXT", "glPointParameteriSGIS"); |
|---|
| 153 | |
|---|
| 154 | setGLExtensionFuncPtr(_glPointParameterf, "glPointParameterf", "glPointParameterfARB"); |
|---|
| 155 | if (!_glPointParameterf) setGLExtensionFuncPtr(_glPointParameterf, "glPointParameterfEXT", "glPointParameterfSGIS"); |
|---|
| 156 | |
|---|
| 157 | setGLExtensionFuncPtr(_glPointParameterfv, "glPointParameterfv", "glPointParameterfvARB"); |
|---|
| 158 | if (!_glPointParameterfv) setGLExtensionFuncPtr(_glPointParameterfv, "glPointParameterfvEXT", "glPointParameterfvSGIS"); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void Point::Extensions::glPointParameteri(GLenum pname, GLint param) const |
|---|
| 162 | { |
|---|
| 163 | if (_glPointParameteri) |
|---|
| 164 | { |
|---|
| 165 | _glPointParameteri(pname, param); |
|---|
| 166 | } |
|---|
| 167 | else |
|---|
| 168 | { |
|---|
| 169 | OSG_WARN<<"Error: glPointParameteri not supported by OpenGL driver"<<std::endl; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void Point::Extensions::glPointParameterf(GLenum pname, GLfloat param) const |
|---|
| 174 | { |
|---|
| 175 | if (_glPointParameterf) |
|---|
| 176 | { |
|---|
| 177 | _glPointParameterf(pname, param); |
|---|
| 178 | } |
|---|
| 179 | else |
|---|
| 180 | { |
|---|
| 181 | OSG_WARN<<"Error: glPointParameterf not supported by OpenGL driver"<<std::endl; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void Point::Extensions::glPointParameterfv(GLenum pname, const GLfloat *params) const |
|---|
| 186 | { |
|---|
| 187 | if (_glPointParameterfv) |
|---|
| 188 | { |
|---|
| 189 | _glPointParameterfv(pname, params); |
|---|
| 190 | } |
|---|
| 191 | else |
|---|
| 192 | { |
|---|
| 193 | OSG_WARN<<"Error: glPointParameterfv not supported by OpenGL driver"<<std::endl; |
|---|
| 194 | } |
|---|
| 195 | } |
|---|