Source

Coin / src / nodes / SoTextureCombine.cpp

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

/*!
  \class SoTextureCombine SoTextureCombine.h Inventor/nodes/SoTextureCombine.h
  \brief The SoTextureCombine class is a node for setting texture combine functions.
  \ingroup nodes

  This node is more or less an exact mapping of the OpenGL
  ARB_texture_env_combine extension (included in OpenGL in OpenGL
  v1.3). See
  http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_env_combine.txt
  for more information about this extension.

  Basically this node lets you specify up to three sources, and a
  function to combine those sources. In addition to the function, it's
  possible to apply simple operands on the sources. In the
  documentation below, the sources will be called Arg0, Arg1, and
  Arg2, just like in the ARB_texture_env_mode specification.

  It's possible to specify both a color and an alpha operation.

  This node has many fields, but usually it is sufficient to set only
  one or very few fields. The selected operation decides which
  values you need to set. One common example is to add a light map
  to textured geometry. A lightmap can look like this:
  
  <center>
  <img src="http://doc.coin3d.org/images/Coin/nodes/lightmap.jpg">
  </center>
  
  The example below just shows how to apply the lightmap to a cube,
  with one light source on each side of the cube. Usually the texture
  coordinates are calculated so that a spot light or a point light is
  simulated.

  \verbatim

  Texture2 { filename "wood.jpg" }
  
  Switch {
    whichChild -3   # use to toggle lightmap on/off
    TextureUnit {
      unit 1
    }
    TextureCombine {
      rgbOperation ADD_SIGNED
      rgbSource [PREVIOUS, TEXTURE]
      rgbOperand [SRC_COLOR, SRC_COLOR ]
      alphaOperation REPLACE
      alphaSource [TEXTURE]
      alphaOperand [SRC_ALPHA]
    }
    Texture2 { filename "lightmap.jpg" }
    TextureUnit { unit 0 }
  }
  Cube { }

  \endverbatim

  The scene above in a viewer:

  <center>
  <img src="http://doc.coin3d.org/images/Coin/nodes/lightmap_screenshot.png">
  </center>
  
  In addition to the functions you can set in rgbOperation (or
  alphaOperation), it's possible to create more complex texture
  functions by combining two textures that have already been
  combined. You can use the SoSceneTexture2 node to create those
  textures. Below is an example that shows how to implement Arg0*Arg1
  + Arg2*Arg0, where Arg0 = texture1 RGB, Arg1 = texture2 RGB, Arg2 =
  texture 2 alpha:

  \verbatim 

  ShapeHints { vertexOrdering COUNTERCLOCKWISE shapeType SOLID }

  Separator {
    SceneTexture2 {
      size 256 256
      transparencyFunction NONE
      scene Separator {
        OrthographicCamera {
          height 2
          aspectRatio 1
          position 0 0 1
          viewportMapping LEAVE_ALONE
        }
        LightModel { model BASE_COLOR }
        Coordinate3 {
          point [ -1 -1 0, 1 -1 0, 1 1 0, -1 1 0 ] 
        }
        DEF texture1 Texture2 { filename "texture1.png" }
        TextureUnit { unit 1 }
        TextureCombine {
          rgbOperation MODULATE
          rgbSource [ PREVIOUS, TEXTURE ]
          rgbOperand [ SRC_COLOR, SRC_COLOR ]
          alphaOperation REPLACE
          alphaSource [TEXTURE]
          alphaOperand [ SRC_ALPHA ]
        }
        DEF texture2 Texture2 { filename "texture2_with_alpha.png" }
        TextureCoordinate2 {
          point [0 0, 1 0, 1 1, 0 1]
        }
        FaceSet { numVertices 4 }
      }
    }
    TextureUnit { unit 1 }
    TextureCombine {
      rgbOperation ADD
      rgbSource [ PREVIOUS, TEXTURE ]
      rgbOperand [ SRC_COLOR, SRC_COLOR ]
      alphaOperation REPLACE
      alphaSource [TEXTURE]
      alphaOperand [ SRC_ALPHA ]
    }
    SceneTexture2 {
      size 256 256
      transparencyFunction NONE
      scene Separator {
        OrthographicCamera {
          height 2
          aspectRatio 1
          position 0 0 1
          viewportMapping LEAVE_ALONE
        }
        LightModel { model BASE_COLOR }
        Coordinate3 {
          point [ -1 -1 0, 1 -1 0, 1 1 0, -1 1 0 ] 
        }
        USE texture1
        TextureUnit { unit 1 }
        TextureCombine {
          rgbOperation MODULATE
          rgbSource [ PREVIOUS, TEXTURE ]
          rgbOperand [ SRC_COLOR, SRC_ALPHA ]
          alphaOperation REPLACE
          alphaSource [TEXTURE]
          alphaOperand [ SRC_ALPHA ]
        }
        USE texture2
        TextureCoordinate2 {
          point [0 0, 1 0, 1 1, 0 1]
        }
        FaceSet { numVertices 4 }
      }
    }
    # map resulting texture onto a Cube
    Cube { }
  }
  \endverbatim

  It should be possible to create almost any kind of texture function
  using this scheme, at the cost of extra texture memory usage (the
  intermediate textures), of course.

  <b>FILE FORMAT/DEFAULTS:</b>
  \code
    TextureCombine {
        rgbSource [  ]
        alphaSource [  ]
        rgbOperand [  ]
        alphaOperand [  ]
        rgbOperation MODULATE
        alphaOperation MODULATE
        rgbScale 1
        alphaScale 1
        constantColor 1 1 1 1
    }
  \endcode

  \since Coin 2.3
*/

// *************************************************************************

/*!
  \enum SoTextureCombine::Source
  For enumerating combiner sources.
*/

/*!
  \enum SoTextureCombine::Operand
  For enumerating source operands.
*/

/*!
  \enum SoTextureCombine::Operation
  For enumerating combiner operations/functions.
*/


/*!
  \var SoTextureCombine::Source SoTextureCombine::PRIMARY_COLOR

  Choose primary color as source.
*/

/*!
  \var SoTextureCombine::Source SoTextureCombine::TEXTURE

  Choose texture as source.
*/

/*!
  \var SoTextureCombine::Source SoTextureCombine::CONSTANT

  Choose the constantColor field as source.
*/

/*!
  \var SoTextureCombine::Source SoTextureCombine::PREVIOUS

  Choose the previous unit's texture as source.
*/

/*!
  \var SoTextureCombine::Operand SoTextureCombine::SRC_COLOR

  Use the source color as operand.
*/

/*!
  \var SoTextureCombine::Operand SoTextureCombine::ONE_MINUS_SRC_COLOR

  Use one minus source color as operand.
*/

/*!
  \var SoTextureCombine::Operand SoTextureCombine::SRC_ALPHA

  Use the source alpha as operand.
*/

/*!
  \var SoTextureCombine::Operand SoTextureCombine::ONE_MINUS_SRC_ALPHA

  Use one minus source alpha as operand.
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::REPLACE

  dst = Arg0
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::MODULATE

  dst = Arg0 * Arg1
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::ADD

  dst = Arg0 + Arg1
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::ADD_SIGNED

  dst = Arg0 + Arg1 - 0.5
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::SUBTRACT

  dst = Arg0 - Arg1
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::INTERPOLATE

  dst = Arg0 * (Arg2) + Arg1 * (1-Arg2)
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::DOT3_RGB

  Dot product of Arg0 and Arg1
*/

/*!
  \var SoTextureCombine::Operation SoTextureCombine::DOT3_RGBA

  Dot product (including alpha) of Arg0 and Arg1
*/

/*!
  SoTextureCombine::rgbSource

  The color sources. This field is empty by default, but you can
  insert up to three values [Arg0, Arg1, Arg2]. When the field
  contains less than three values, the default [TEXTURE, PREVIOUS,
  CONSTANT], will be used for missing values.

  For texture unit 0, PREVIOUS maps to PRIMARY_COLOR,
*/

/*!
  SoTextureCombine::alphaSource

  The alpha sources. This field is empty by default, but you can
  insert up to three values [Arg0, Arg1, Arg2]. When the field
  contains less than three values, the default [TEXTURE, PREVIOUS,
  CONSTANT], will be used for missing values.

  For texture unit 0, PREVIOUS maps to PRIMARY_COLOR,
*/

/*!
  SoTextureCombine::rgbOperand

  The color operands. This field is empty by default, but you can
  insert up to three values. When the field contains less than three
  values, the default [SRC_COLOR, SRC_COLOR, SRC_COLOR] will be used
  for missing values.
*/

/*!
  SoTextureCombine::alphaOperand

  The alpha operands. This field is empty by default, but you can
  insert up to three values. When the field contains less than three
  values, the default [SRC_ALPHA, SRC_ALPHA, SRC_ALPHA] will be used
  for missing values. Please not that only SRC_ALPHA and
  ONE_MINUS_SRC_ALPHA are valid operands for alpha operations.
*/

/*!
  SoTextureCombine::rgbOperation

  The color operation. Default value is MODULATE.
*/

/*!
  SoTextureCombine::alphaOperation

  The alpha operation. Default value is MODULATE.
*/

/*!
  SoTextureCombine::rgbScale

  Scale color result by this value. Supported values are 1, 2, and 4.
*/

/*!
  SoTextureCombine::alphaScale

  Scale alpha result by this value. Supported values are 1, 2, and 4.
*/

/*!
  SoTextureCombine::constantColor

  The constant color (when CONSTANT is used as source). Default value
  is (1,1,1,1).
*/


#include <Inventor/nodes/SoTextureCombine.h>

#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoPickAction.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/elements/SoTextureCombineElement.h>
#include <Inventor/elements/SoTextureUnitElement.h>
#include <Inventor/elements/SoGLCacheContextElement.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/C/glue/gl.h>
#include <Inventor/misc/SoGLDriverDatabase.h>

#include "nodes/SoSubNodeP.h"

// *************************************************************************

SO_NODE_SOURCE(SoTextureCombine);

/*!
  Constructor.
*/
SoTextureCombine::SoTextureCombine(void)
{
  SO_NODE_INTERNAL_CONSTRUCTOR(SoTextureCombine);

  SO_NODE_ADD_FIELD(rgbSource, (TEXTURE));
  SO_NODE_ADD_FIELD(alphaSource, (TEXTURE));
  SO_NODE_ADD_FIELD(rgbOperand, (SRC_COLOR));
  SO_NODE_ADD_FIELD(alphaOperand, (SRC_ALPHA));

  this->rgbSource.setNum(0);
  this->rgbSource.setDefault(TRUE);
  this->alphaSource.setNum(0);
  this->alphaSource.setDefault(TRUE);
  this->rgbOperand.setNum(0);
  this->rgbOperand.setDefault(TRUE);
  this->alphaOperand.setNum(0);
  this->alphaOperand.setDefault(TRUE);

  SO_NODE_ADD_FIELD(rgbOperation, (MODULATE));
  SO_NODE_ADD_FIELD(alphaOperation, (MODULATE));

  SO_NODE_ADD_FIELD(rgbScale, (1.0f));
  SO_NODE_ADD_FIELD(alphaScale, (1.0f));

  SO_NODE_ADD_FIELD(constantColor, (1.0f, 1.0f, 1.0f, 1.0f));

  SO_NODE_DEFINE_ENUM_VALUE(Source, PRIMARY_COLOR);
  SO_NODE_DEFINE_ENUM_VALUE(Source, TEXTURE);
  SO_NODE_DEFINE_ENUM_VALUE(Source, CONSTANT);
  SO_NODE_DEFINE_ENUM_VALUE(Source, PREVIOUS);

  SO_NODE_DEFINE_ENUM_VALUE(Operand, SRC_COLOR);
  SO_NODE_DEFINE_ENUM_VALUE(Operand, ONE_MINUS_SRC_COLOR);
  SO_NODE_DEFINE_ENUM_VALUE(Operand, SRC_ALPHA);
  SO_NODE_DEFINE_ENUM_VALUE(Operand, ONE_MINUS_SRC_ALPHA);

  SO_NODE_DEFINE_ENUM_VALUE(Operation, REPLACE);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, MODULATE);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, ADD);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, ADD_SIGNED);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, SUBTRACT);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, INTERPOLATE);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, DOT3_RGB);
  SO_NODE_DEFINE_ENUM_VALUE(Operation, DOT3_RGBA);

  SO_NODE_SET_MF_ENUM_TYPE(rgbSource, Source);
  SO_NODE_SET_MF_ENUM_TYPE(alphaSource, Source);
  SO_NODE_SET_MF_ENUM_TYPE(rgbOperand, Operand);
  SO_NODE_SET_MF_ENUM_TYPE(alphaOperand, Operand);

  SO_NODE_SET_SF_ENUM_TYPE(rgbOperation, Operation);
  SO_NODE_SET_SF_ENUM_TYPE(alphaOperation, Operation);
}


/*!
  Destructor.
*/
SoTextureCombine::~SoTextureCombine()
{
}

// Doc from superclass.
void
SoTextureCombine::initClass(void)
{
  SO_NODE_INTERNAL_INIT_CLASS(SoTextureCombine, SO_FROM_COIN_2_2);

  SO_ENABLE(SoGLRenderAction, SoTextureCombineElement);
}

// Doc from superclass.
void
SoTextureCombine::GLRender(SoGLRenderAction * action)
{
  const cc_glglue * glue = cc_glglue_instance(action->getCacheContext());

  SoTextureCombine::Operation rgbaop =
    (SoTextureCombine::Operation) this->rgbOperation.getValue();

  SoTextureCombine::Operation alphaop =
    (SoTextureCombine::Operation) this->alphaOperation.getValue();

  SbBool supported = cc_glglue_glversion_matches_at_least(glue, 1, 3, 0);

  if (!supported) {
    supported = SoGLDriverDatabase::isSupported(glue, "GL_ARB_texture_env_combine");
    if (supported && (alphaop == DOT3_RGB || alphaop == DOT3_RGBA ||
                      rgbaop == DOT3_RGB || rgbaop == DOT3_RGBA)) {
      supported =
        SoGLDriverDatabase::isSupported(glue, "GL_ARB_texture_env_dot3");
    }
  }
  
  if (supported) {
    SoTextureCombine::doAction((SoAction*)action);
  }
  else {
    static int didwarn = 0;
    if (!didwarn) {
      SoDebugError::postWarning("SoTextureCombine::GLRender",
                                "Your OpenGL driver does not support the "
                                "required extensions to do texture combine.");
      didwarn = 1;
    }

  }
}

// Doc from superclass.
void
SoTextureCombine::doAction(SoAction * action)
{
  SoState * state = action->getState();
  int unit = SoTextureUnitElement::get(state);

  SoTextureCombineElement::Source rgbsource[3] = {
    SoTextureCombineElement::TEXTURE,
    SoTextureCombineElement::PREVIOUS,
    SoTextureCombineElement::CONSTANT
  };
  SoTextureCombineElement::Operand rgboperand[3] = {
    SoTextureCombineElement::SRC_COLOR,
    SoTextureCombineElement::SRC_COLOR,
    SoTextureCombineElement::SRC_COLOR
  };

  SoTextureCombineElement::Source alphasource[3] = {
    SoTextureCombineElement::TEXTURE,
    SoTextureCombineElement::PREVIOUS,
    SoTextureCombineElement::CONSTANT
  };
  SoTextureCombineElement::Operand alphaoperand[3] = {
    SoTextureCombineElement::SRC_ALPHA,
    SoTextureCombineElement::SRC_ALPHA,
    SoTextureCombineElement::SRC_ALPHA
  };
  int i;
  for (i = 0; i < this->rgbSource.getNum() && i < 3; i++) {
    rgbsource[i] = (SoTextureCombineElement::Source) this->rgbSource.getValues(0)[i];
  }
  for (i = 0; i < this->alphaSource.getNum() && i < 3; i++) {
    alphasource[i] = (SoTextureCombineElement::Source) this->alphaSource.getValues(0)[i];
  }
  for (i = 0; i < this->rgbOperand.getNum() && i < 3; i++) {
    rgboperand[i] = (SoTextureCombineElement::Operand) this->rgbOperand.getValues(0)[i];
  }
  for (i = 0; i < this->alphaOperand.getNum() && i < 3; i++) {
    alphaoperand[i] = (SoTextureCombineElement::Operand) this->alphaOperand.getValues(0)[i];
  }

  SbColor4f col;
  SbVec4f tmp = this->constantColor.getValue();
  col[0] = tmp[0];
  col[1] = tmp[1];
  col[2] = tmp[2];
  col[3] = tmp[3];


  const cc_glglue * glue = 
    cc_glglue_instance(SoGLCacheContextElement::get(state));
  int maxunits = cc_glglue_max_texture_units(glue);

  if (unit < maxunits) {
    SoTextureCombineElement::set(state, this, unit,
                                 (SoTextureCombineElement::Operation) this->rgbOperation.getValue(),
                                 (SoTextureCombineElement::Operation) this->alphaOperation.getValue(),
                                 rgbsource, alphasource,
                                 rgboperand, alphaoperand,
                                 col,
                                 this->rgbScale.getValue(),
                                 this->alphaScale.getValue());
  }
}


// Doc from superclass.
void
SoTextureCombine::callback(SoCallbackAction * action)
{
  // So far only SoGLRenderAction supports SoTextureCombineElement.  We
  // may never support multiple texture units for SoCallbackAction,
  // but we reimplement the method just in case
  inherited::callback(action);
}

// Doc from superclass.
void
SoTextureCombine::pick(SoPickAction * action)
{
  // So far only SoGLRenderAction supports SoTextureCombineElement.  We
  // may never support multiple texture units for SoPickAction, but we
  // reimplement the method just in case
  inherited::pick(action);
}