| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/GLExtensions> |
|---|
| 14 | #include <osg/TexEnvCombine> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | |
|---|
| 20 | TexEnvCombine::TexEnvCombine(): |
|---|
| 21 | _needsTexEnvCrossbar(false), |
|---|
| 22 | _combine_RGB(GL_MODULATE), |
|---|
| 23 | _combine_Alpha(GL_MODULATE), |
|---|
| 24 | _source0_RGB(GL_TEXTURE), |
|---|
| 25 | _source1_RGB(GL_PREVIOUS_ARB), |
|---|
| 26 | _source2_RGB(GL_CONSTANT_ARB), |
|---|
| 27 | _source0_Alpha(GL_TEXTURE), |
|---|
| 28 | _source1_Alpha(GL_PREVIOUS_ARB), |
|---|
| 29 | _source2_Alpha(GL_CONSTANT_ARB), |
|---|
| 30 | _operand0_RGB(GL_SRC_COLOR), |
|---|
| 31 | _operand1_RGB(GL_SRC_COLOR), |
|---|
| 32 | _operand2_RGB(GL_SRC_ALPHA), |
|---|
| 33 | _operand0_Alpha(GL_SRC_ALPHA), |
|---|
| 34 | _operand1_Alpha(GL_SRC_ALPHA), |
|---|
| 35 | _operand2_Alpha(GL_SRC_ALPHA), |
|---|
| 36 | _scale_RGB(1.0), |
|---|
| 37 | _scale_Alpha(1.0), |
|---|
| 38 | _constantColor(0.0f,0.0f,0.0f,0.0f) |
|---|
| 39 | { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | TexEnvCombine::~TexEnvCombine() |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void TexEnvCombine::apply(State& state) const |
|---|
| 47 | { |
|---|
| 48 | #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE |
|---|
| 49 | unsigned int contextID = state.getContextID(); |
|---|
| 50 | |
|---|
| 51 | static bool s_isTexEnvCombineSupported = |
|---|
| 52 | isGLExtensionSupported(contextID,"GL_ARB_texture_env_combine"); |
|---|
| 53 | |
|---|
| 54 | static bool s_isTexEnvCrossbarSupported = |
|---|
| 55 | isGLExtensionSupported(contextID,"GL_ARB_texture_env_crossbar"); |
|---|
| 56 | |
|---|
| 57 | static bool s_isNVTexEnvCrossbarSupported = |
|---|
| 58 | isGLExtensionSupported(contextID,"GL_NV_texture_env_combine4"); |
|---|
| 59 | |
|---|
| 60 | static bool s_isTexEnvDot3Supported = |
|---|
| 61 | isGLExtensionSupported(contextID,"GL_ARB_texture_env_dot3"); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | bool needsTexEnvDot3 = (_combine_RGB==DOT3_RGB) || |
|---|
| 65 | (_combine_RGB==DOT3_RGBA); |
|---|
| 66 | |
|---|
| 67 | bool supported = s_isTexEnvCombineSupported; |
|---|
| 68 | if (_needsTexEnvCrossbar && !(s_isTexEnvCrossbarSupported || s_isNVTexEnvCrossbarSupported)) supported = false; |
|---|
| 69 | if (needsTexEnvDot3 && !s_isTexEnvDot3Supported) supported = false; |
|---|
| 70 | |
|---|
| 71 | if (supported) |
|---|
| 72 | { |
|---|
| 73 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB); |
|---|
| 74 | |
|---|
| 75 | glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, _combine_RGB); |
|---|
| 76 | |
|---|
| 77 | if (_combine_RGB!=DOT3_RGBA) |
|---|
| 78 | glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, _combine_Alpha); |
|---|
| 79 | |
|---|
| 80 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB,_source0_RGB ); |
|---|
| 81 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, _source1_RGB); |
|---|
| 82 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB,_source2_RGB ); |
|---|
| 83 | |
|---|
| 84 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, _source0_Alpha); |
|---|
| 85 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, _source1_Alpha); |
|---|
| 86 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, _source2_Alpha); |
|---|
| 87 | |
|---|
| 88 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, _operand0_RGB); |
|---|
| 89 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, _operand1_RGB); |
|---|
| 90 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, _operand2_RGB); |
|---|
| 91 | |
|---|
| 92 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, _operand0_Alpha); |
|---|
| 93 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, _operand1_Alpha); |
|---|
| 94 | glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, _operand2_Alpha); |
|---|
| 95 | |
|---|
| 96 | glTexEnvf( GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, _scale_RGB); |
|---|
| 97 | glTexEnvf( GL_TEXTURE_ENV, GL_ALPHA_SCALE, _scale_Alpha); |
|---|
| 98 | |
|---|
| 99 | glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, _constantColor.ptr()); |
|---|
| 100 | } |
|---|
| 101 | else |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
|---|
| 106 | } |
|---|
| 107 | #else |
|---|
| 108 | OSG_NOTICE<<"Warning: TexEnvCombine::apply(State&) - not supported."<<std::endl; |
|---|
| 109 | #endif |
|---|
| 110 | } |
|---|
| 111 | void TexEnvCombine::setCombine_RGB(GLint cm) { _combine_RGB = cm; } |
|---|
| 112 | void TexEnvCombine::setCombine_Alpha(GLint cm) { _combine_Alpha = cm; } |
|---|
| 113 | |
|---|
| 114 | void TexEnvCombine::setSource0_RGB(GLint sp) { _source0_RGB = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 115 | void TexEnvCombine::setSource1_RGB(GLint sp) { _source1_RGB = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 116 | void TexEnvCombine::setSource2_RGB(GLint sp) { _source2_RGB = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 117 | |
|---|
| 118 | void TexEnvCombine::setSource0_Alpha(GLint sp) { _source0_Alpha = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 119 | void TexEnvCombine::setSource1_Alpha(GLint sp) { _source1_Alpha = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 120 | void TexEnvCombine::setSource2_Alpha(GLint sp) { _source2_Alpha = sp; computeNeedForTexEnvCombiners(); } |
|---|
| 121 | |
|---|
| 122 | void TexEnvCombine::setOperand0_RGB(GLint op) { _operand0_RGB = op; } |
|---|
| 123 | void TexEnvCombine::setOperand1_RGB(GLint op) { _operand1_RGB = op; } |
|---|
| 124 | void TexEnvCombine::setOperand2_RGB(GLint op) { _operand2_RGB = op; } |
|---|
| 125 | |
|---|
| 126 | static GLint Valid_Operand_Alpha(GLint op, const char* functionName) |
|---|
| 127 | { |
|---|
| 128 | if (op==TexEnvCombine::SRC_ALPHA || op==TexEnvCombine::ONE_MINUS_SRC_ALPHA) return op; |
|---|
| 129 | |
|---|
| 130 | OSG_WARN<<"Warning:: TexEnvCombine::"<<functionName<<"("<<op<<") invalid parameter value,"<<std::endl<< |
|---|
| 131 | " must be SRC_ALPHA or ONE_MINUS_SRC_ALPHA, resetting to SRC_ALPHA."<<std::endl; |
|---|
| 132 | return TexEnvCombine::SRC_ALPHA; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void TexEnvCombine::setOperand0_Alpha(GLint op) |
|---|
| 136 | { |
|---|
| 137 | _operand0_Alpha = Valid_Operand_Alpha(op,"setOperand0_Alpha"); |
|---|
| 138 | } |
|---|
| 139 | void TexEnvCombine::setOperand1_Alpha(GLint op) |
|---|
| 140 | { |
|---|
| 141 | _operand1_Alpha = Valid_Operand_Alpha(op,"setOperand1_Alpha"); |
|---|
| 142 | } |
|---|
| 143 | void TexEnvCombine::setOperand2_Alpha(GLint op) |
|---|
| 144 | { |
|---|
| 145 | _operand2_Alpha = Valid_Operand_Alpha(op,"setOperand2_Alpha"); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void TexEnvCombine::setScale_RGB(float scale) { _scale_RGB = scale; } |
|---|
| 149 | void TexEnvCombine::setScale_Alpha(float scale) { _scale_Alpha = scale; } |
|---|