| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91,92 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.1.1.1 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoV1Texture2 |
| 47 |
| |
| 48 |
| Author(s) : Gavin Bell |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include "SoV1Texture2.h" |
| 55 |
#include <Inventor/SbPList.h> |
| 56 |
#include <Inventor/nodes/SoTexture2.h> |
| 57 |
#include <Inventor/nodes/SoTexture2Transform.h> |
| 58 |
#include <Inventor/sensors/SoNodeSensor.h> |
| 59 |
|
| 60 |
SO_NODE_SOURCE(SoV1Texture2); |
| 61 |
|
| 62 |
// |
| 63 |
// List of textures that we've created so we can share textures with |
| 64 |
// the same filename, just like Inventor 1 |
| 65 |
// |
| 66 |
SbPList *SoV1Texture2::textureList = NULL; |
| 67 |
|
| 68 |
//////////////////////////////////////////////////////////////////////// |
| 69 |
// |
| 70 |
// Description: |
| 71 |
// Default constructor |
| 72 |
// |
| 73 |
// Use: public |
| 74 |
|
| 75 |
SoV1Texture2::SoV1Texture2() |
| 76 |
// |
| 77 |
//////////////////////////////////////////////////////////////////////// |
| 78 |
{ |
| 79 |
SO_NODE_CONSTRUCTOR(SoV1Texture2); |
| 80 |
|
| 81 |
SO_NODE_ADD_FIELD(component, (INTENSITY)); |
| 82 |
SO_NODE_ADD_FIELD(filename, ("")); |
| 83 |
SO_NODE_ADD_FIELD(minFilter, (POINT)); |
| 84 |
SO_NODE_ADD_FIELD(magFilter, (POINT)); |
| 85 |
SO_NODE_ADD_FIELD(wrapS, (REPEAT)); |
| 86 |
SO_NODE_ADD_FIELD(wrapT, (REPEAT)); |
| 87 |
SO_NODE_ADD_FIELD(model, (MODULATE)); |
| 88 |
SO_NODE_ADD_FIELD(blendColor, (SbColor(1,1,1))); |
| 89 |
SO_NODE_ADD_FIELD(translation, (SbVec2f(0., 0.))); |
| 90 |
SO_NODE_ADD_FIELD(scaleFactor, (SbVec2f(1., 1.))); |
| 91 |
SO_NODE_ADD_FIELD(rotation, (0.0)); |
| 92 |
SO_NODE_ADD_FIELD(center, (SbVec2f(0.0, 0.0))); |
| 93 |
|
| 94 |
// Set up static info for enumerated type field |
| 95 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, POINT); |
| 96 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, BILINEAR); |
| 97 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, MIPMAP_POINT); |
| 98 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, MIPMAP_LINEAR); |
| 99 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, MIPMAP_BILINEAR); |
| 100 |
SO_NODE_DEFINE_ENUM_VALUE(MinFilter, MIPMAP_TRILINEAR); |
| 101 |
|
| 102 |
SO_NODE_DEFINE_ENUM_VALUE(MagFilter, POINT); |
| 103 |
SO_NODE_DEFINE_ENUM_VALUE(MagFilter, BILINEAR); |
| 104 |
|
| 105 |
// Set up enumerations for Texture2 model |
| 106 |
SO_NODE_DEFINE_ENUM_VALUE(Model, MODULATE); |
| 107 |
SO_NODE_DEFINE_ENUM_VALUE(Model, DECAL); |
| 108 |
SO_NODE_DEFINE_ENUM_VALUE(Model, BLEND); |
| 109 |
|
| 110 |
// Set up enumerations for Texture2 component |
| 111 |
SO_NODE_DEFINE_ENUM_VALUE(Component, NULL_COMPONENT); |
| 112 |
SO_NODE_DEFINE_ENUM_VALUE(Component, INTENSITY); |
| 113 |
SO_NODE_DEFINE_ENUM_VALUE(Component, TRANSPARENCY); |
| 114 |
|
| 115 |
SO_NODE_DEFINE_ENUM_VALUE(Wrap, REPEAT); |
| 116 |
SO_NODE_DEFINE_ENUM_VALUE(Wrap, CLAMP); |
| 117 |
|
| 118 |
// Set up info in enumerated type field |
| 119 |
SO_NODE_SET_SF_ENUM_TYPE(minFilter, MinFilter); |
| 120 |
SO_NODE_SET_SF_ENUM_TYPE(magFilter, MagFilter); |
| 121 |
SO_NODE_SET_SF_ENUM_TYPE(model, Model); |
| 122 |
SO_NODE_SET_SF_ENUM_TYPE(component, Component); |
| 123 |
SO_NODE_SET_SF_ENUM_TYPE(wrapS, Wrap); |
| 124 |
SO_NODE_SET_SF_ENUM_TYPE(wrapT, Wrap); |
| 125 |
|
| 126 |
// This upgrader returns a group, but it is an upgrade of a non-group |
| 127 |
isGroup = FALSE; |
| 128 |
} |
| 129 |
|
| 130 |
//////////////////////////////////////////////////////////////////////// |
| 131 |
// |
| 132 |
// Description: |
| 133 |
// Destructor. |
| 134 |
// |
| 135 |
// Use: private |
| 136 |
|
| 137 |
SoV1Texture2::~SoV1Texture2() |
| 138 |
// |
| 139 |
//////////////////////////////////////////////////////////////////////// |
| 140 |
{ |
| 141 |
} |
| 142 |
|
| 143 |
//////////////////////////////////////////////////////////////////////// |
| 144 |
// |
| 145 |
// Description: |
| 146 |
// Create a version 2.0 SoTexture2. |
| 147 |
// |
| 148 |
// Use: private |
| 149 |
|
| 150 |
SoNode * |
| 151 |
SoV1Texture2::createNewNode() |
| 152 |
// |
| 153 |
//////////////////////////////////////////////////////////////////////// |
| 154 |
{ |
| 155 |
SoTexture2 *tex; |
| 156 |
SbBool alreadyExists = findTexture2(tex); |
| 157 |
|
| 158 |
tex->ref(); |
| 159 |
|
| 160 |
if (!alreadyExists) { |
| 161 |
// Convert from old enums to new fields: |
| 162 |
|
| 163 |
// Easy ones first: |
| 164 |
SO_UPGRADER_COPY_FIELD(blendColor, tex); |
| 165 |
|
| 166 |
// Enum values for model changed between 1.0/2.0: |
| 167 |
if (!model.isDefault()) { |
| 168 |
switch (model.getValue()) { |
| 169 |
case MODULATE: |
| 170 |
tex->model = SoTexture2::MODULATE; |
| 171 |
break; |
| 172 |
case DECAL: |
| 173 |
tex->model = SoTexture2::DECAL; |
| 174 |
break; |
| 175 |
case BLEND: |
| 176 |
tex->model = SoTexture2::BLEND; |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
if (model.isIgnored()) tex->model.setIgnored(TRUE); |
| 181 |
|
| 182 |
// Enum values for wrapS changed between 1.0/2.0: |
| 183 |
if (!wrapS.isDefault()) { |
| 184 |
switch (wrapS.getValue()) { |
| 185 |
case REPEAT: |
| 186 |
tex->wrapS = SoTexture2::REPEAT; |
| 187 |
break; |
| 188 |
case CLAMP: |
| 189 |
tex->wrapS = SoTexture2::CLAMP; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
if (wrapS.isIgnored()) tex->wrapS.setIgnored(TRUE); |
| 194 |
|
| 195 |
// Enum values for wrapT changed between 1.0/2.0: |
| 196 |
if (!wrapT.isDefault()) { |
| 197 |
switch (wrapT.getValue()) { |
| 198 |
case REPEAT: |
| 199 |
tex->wrapT = SoTexture2::REPEAT; |
| 200 |
break; |
| 201 |
case CLAMP: |
| 202 |
tex->wrapT = SoTexture2::CLAMP; |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
if (wrapT.isIgnored()) tex->wrapT.setIgnored(TRUE); |
| 207 |
|
| 208 |
|
| 209 |
// Convert name to string in filename field |
| 210 |
if (! filename.isDefault()) |
| 211 |
tex->filename = filename.getValue().getString(); |
| 212 |
if (filename.isIgnored()) |
| 213 |
tex->filename.setIgnored(TRUE); |
| 214 |
|
| 215 |
// Ignore component field |
| 216 |
|
| 217 |
// And ignore min/magFilters; I could create a Complexity node |
| 218 |
// with textureQuality set and value/type ignored, but I don't |
| 219 |
// think it is worth the effort... |
| 220 |
} |
| 221 |
|
| 222 |
// These require us to create a Texture2Transform also: |
| 223 |
if (!translation.isDefault() || !scaleFactor.isDefault() || |
| 224 |
!rotation.isDefault()) { |
| 225 |
|
| 226 |
SoGroup *g = SO_UPGRADER_CREATE_NEW(SoGroup); |
| 227 |
g->ref(); |
| 228 |
|
| 229 |
SoTexture2Transform *xf = SO_UPGRADER_CREATE_NEW(SoTexture2Transform); |
| 230 |
g->addChild(xf); |
| 231 |
SO_UPGRADER_COPY_FIELD(translation, xf); |
| 232 |
SO_UPGRADER_COPY_FIELD(scaleFactor, xf); |
| 233 |
SO_UPGRADER_COPY_FIELD(rotation, xf); |
| 234 |
SO_UPGRADER_COPY_FIELD(center, xf); |
| 235 |
|
| 236 |
g->addChild(tex); |
| 237 |
tex->unref(); |
| 238 |
g->unrefNoDelete(); |
| 239 |
|
| 240 |
return g; |
| 241 |
} |
| 242 |
tex->unrefNoDelete(); |
| 243 |
|
| 244 |
return tex; |
| 245 |
} |
| 246 |
|
| 247 |
//////////////////////////////////////////////////////////////////////// |
| 248 |
// |
| 249 |
// Description: |
| 250 |
// Get a texture2 node with the right stuff in it. This imitates |
| 251 |
// the Inventor 1.0 behavior of sharing GL texture maps by |
| 252 |
// instancing Texture2 nodes with the same fields. It does this by |
| 253 |
// maintaining a list of all Texture2 nodes created by the |
| 254 |
// upgrader, and searching through the list for a match. |
| 255 |
// |
| 256 |
// Use: private |
| 257 |
|
| 258 |
SbBool |
| 259 |
SoV1Texture2::findTexture2(SoTexture2 *&tex) |
| 260 |
// |
| 261 |
//////////////////////////////////////////////////////////////////////// |
| 262 |
{ |
| 263 |
if (textureList == NULL) { |
| 264 |
textureList = new SbPList; |
| 265 |
} |
| 266 |
for (int i = 0; i < textureList->getLength(); i++) { |
| 267 |
tex = (SoTexture2 *)(*textureList)[i]; |
| 268 |
if (matches(tex)) |
| 269 |
return TRUE; |
| 270 |
} |
| 271 |
// Not found, create a new one and add to list: |
| 272 |
tex = SO_UPGRADER_CREATE_NEW(SoTexture2); |
| 273 |
textureList->append(tex); |
| 274 |
|
| 275 |
// And add a node sensor so we can remove the texture from the |
| 276 |
// list if it is deleted: |
| 277 |
SoNodeSensor *sensor = new SoNodeSensor; |
| 278 |
sensor->attach(tex); |
| 279 |
sensor->setDeleteCallback(nodeDeletedCB, tex); |
| 280 |
|
| 281 |
return FALSE; |
| 282 |
} |
| 283 |
|
| 284 |
//////////////////////////////////////////////////////////////////////// |
| 285 |
// |
| 286 |
// Description: |
| 287 |
// Returns TRUE if the given Texture2 node's fields match this |
| 288 |
// upgrader's fields. |
| 289 |
// |
| 290 |
// Use: private, static |
| 291 |
|
| 292 |
SbBool |
| 293 |
SoV1Texture2::matches(SoTexture2 *compare) |
| 294 |
// |
| 295 |
//////////////////////////////////////////////////////////////////////// |
| 296 |
{ |
| 297 |
if (compare->filename.getValue() != |
| 298 |
filename.getValue().getString()) return FALSE; |
| 299 |
|
| 300 |
int compareModel = compare->model.getValue(); |
| 301 |
switch (model.getValue()) { |
| 302 |
case MODULATE: |
| 303 |
if (compareModel != SoTexture2::MODULATE) return FALSE; |
| 304 |
break; |
| 305 |
case DECAL: |
| 306 |
if (compareModel != SoTexture2::DECAL) return FALSE; |
| 307 |
break; |
| 308 |
case BLEND: |
| 309 |
if (compareModel != SoTexture2::BLEND) return FALSE; |
| 310 |
break; |
| 311 |
} |
| 312 |
int compareWrap = compare->wrapS.getValue(); |
| 313 |
switch (wrapS.getValue()) { |
| 314 |
case REPEAT: |
| 315 |
if (compareWrap != SoTexture2::REPEAT) return FALSE; |
| 316 |
break; |
| 317 |
case CLAMP: |
| 318 |
if (compareWrap != SoTexture2::CLAMP) return FALSE; |
| 319 |
break; |
| 320 |
} |
| 321 |
compareWrap = compare->wrapT.getValue(); |
| 322 |
switch (wrapT.getValue()) { |
| 323 |
case REPEAT: |
| 324 |
if (compareWrap != SoTexture2::REPEAT) return FALSE; |
| 325 |
break; |
| 326 |
case CLAMP: |
| 327 |
if (compareWrap != SoTexture2::CLAMP) return FALSE; |
| 328 |
break; |
| 329 |
} |
| 330 |
return TRUE; |
| 331 |
} |
| 332 |
|
| 333 |
//////////////////////////////////////////////////////////////////////// |
| 334 |
// |
| 335 |
// Description: |
| 336 |
// Called by node sensor when a Texture2 we are keeping track of is |
| 337 |
// deleted. Removes the node from the list and deletes the sensor. |
| 338 |
// |
| 339 |
// Use: private, static |
| 340 |
|
| 341 |
void |
| 342 |
SoV1Texture2::nodeDeletedCB(void *node, SoSensor *sensor) |
| 343 |
// |
| 344 |
//////////////////////////////////////////////////////////////////////// |
| 345 |
{ |
| 346 |
textureList->remove(textureList->find(node)); |
| 347 |
delete sensor; |
| 348 |
} |