00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include <OSGConfig.h>
00047 #include <OSGSimpleGeometry.h>
00048 #include <OSGMaterialChunk.h>
00049
00050 #include "OSGDisplayCalibration.h"
00051
00052 OSG_USING_NAMESPACE
00053
00054
00055
00056
00057
00062
00063
00064
00065
00066
00067
00068
00069
00070 void DisplayCalibration::initMethod (void)
00071 {
00072 }
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 DisplayCalibration::DisplayCalibration(void) :
00086 Inherited(),
00087 _changed(true),
00088 _cmPort(NullFC),
00089 _dsPort(NullFC),
00090 _dsPort2(NullFC)
00091 {
00092 }
00093
00094 DisplayCalibration::DisplayCalibration(const DisplayCalibration &source) :
00095 Inherited(source),
00096 _changed(true),
00097 _cmPort(NullFC),
00098 _dsPort(NullFC),
00099 _dsPort2(NullFC)
00100 {
00101 }
00102
00103 DisplayCalibration::~DisplayCalibration(void)
00104 {
00105 if(_cmPort != osg::NullFC)
00106 {
00107 subRefCP(_cmPort);
00108 subRefCP(_dsPort);
00109 subRefCP(_dsPort2);
00110 subRefCP(_cmRoot);
00111 subRefCP(_dsRoot);
00112 subRefCP(_ds2Root);
00113 subRefCP(_cam);
00114 subRefCP(_dsBack);
00115 subRefCP(_ds2Back);
00116 }
00117 }
00118
00119
00120
00123 void DisplayCalibration::calibrate(ViewportPtr port,RenderActionBase *ract)
00124 {
00125 WindowPtr window=port->getParent();
00126 if(!getEnabled())
00127 return;
00128
00129 createCMViewports(port);
00130
00131 window->addPort(_cmPort);
00132 _cmPort->render(ract);
00133 window->subPort(_cmPort);
00134
00135 if(getScaleDown() < 1.0f)
00136 {
00137 window->addPort(_dsPort);
00138 _dsPort->render(ract);
00139 window->subPort(_dsPort);
00140 window->addPort(_dsPort2);
00141 _dsPort2->render(ract);
00142 window->subPort(_dsPort2);
00143 }
00144
00145 }
00146
00147 void DisplayCalibration::changed(BitVector whichField, UInt32 origin)
00148 {
00149 _changed = true;
00150 Inherited::changed(whichField, origin);
00151 }
00152
00153 void DisplayCalibration::dump( UInt32 ,
00154 const BitVector ) const
00155 {
00156 SLOG << "Dump DisplayCalibration NI" << std::endl;
00157 }
00158
00159 void DisplayCalibration::createCMViewports(ViewportPtr port)
00160 {
00161 WindowPtr window=port->getParent();
00162
00163
00164 if(_cmPort != osg::NullFC &&
00165 !_changed &&
00166 _vpLeft == port->getPixelLeft() &&
00167 _vpRight == port->getPixelRight() &&
00168 _vpBottom == port->getPixelBottom() &&
00169 _vpTop == port->getPixelTop())
00170 return;
00171
00172 if(getScaleDown()>1.0)
00173 setScaleDown(1.0);
00174
00175 _changed = false;
00176 _vpLeft = port->getPixelLeft();
00177 _vpBottom = port->getPixelBottom();
00178 _vpRight = port->getPixelRight();
00179 _vpTop = port->getPixelTop();
00180
00181
00182 if(_cmPort != osg::NullFC) {
00183 subRefCP(_cmPort);
00184 subRefCP(_dsPort);
00185 subRefCP(_dsPort2);
00186 subRefCP(_cmRoot);
00187 subRefCP(_dsRoot);
00188 subRefCP(_ds2Root);
00189 subRefCP(_cam);
00190 subRefCP(_dsBack);
00191 subRefCP(_ds2Back);
00192 }
00193
00194 Matrix m;
00195 ImagePtr img = Image::create();
00196 beginEditCP(img);
00197 img->set(GL_RGB,1,1);
00198
00199 endEditCP(img);
00200
00201 int extension;
00202 extension = osg::Window::registerExtension("GL_ARB_fragment_program");
00203 if(window->hasExtension(extension))
00204 {
00205 SINFO << "Use fragment program for color crrection" << std::endl;
00206 _useFragmentProgram = true;
00207 }
00208 else
00209 {
00210 SINFO << "Use reg combiner for color crrection" << std::endl;
00211 _useFragmentProgram = false;
00212 }
00213 extension = Window::registerExtension("GL_ARB_texture_non_power_of_two" );
00214 if(window->hasExtension(extension))
00215 _nonPowerOfTwo = true;
00216 else
00217 _nonPowerOfTwo = false;
00218
00219 TextureChunkPtr tex = TextureChunk::create();
00220
00221 if(_useFragmentProgram)
00222 {
00223
00224
00225 beginEditCP(tex);
00226 tex->setImage(img);
00227 tex->setMinFilter(GL_NEAREST);
00228 tex->setScale(false);
00229 tex->setEnvMode(GL_REPLACE);
00230 endEditCP (tex);
00231
00232
00233
00234
00235
00236 UChar8 rgammadata[] =
00237 { 0, 80, 160, 255 };
00238
00239 _rgammaimg = Image::create();
00240 beginEditCP(_rgammaimg);
00241 _rgammaimg->set(Image::OSG_L_PF,4,1,1,1,1,0,rgammadata);
00242 endEditCP(_rgammaimg);
00243
00244 _rgammachunk = TextureChunk::create();
00245 beginEditCP(_rgammachunk);
00246 _rgammachunk->setImage(_rgammaimg);
00247 _rgammachunk->setMinFilter(GL_LINEAR);
00248 _rgammachunk->setMagFilter(GL_LINEAR);
00249 _rgammachunk->setWrapS(GL_CLAMP_TO_EDGE);
00250 _rgammachunk->setWrapT(GL_CLAMP_TO_EDGE);
00251 endEditCP (_rgammachunk);
00252
00253 UChar8 ggammadata[] =
00254 { 0, 80, 160, 255 };
00255
00256 _ggammaimg = Image::create();
00257 beginEditCP(_ggammaimg);
00258 _ggammaimg->set(Image::OSG_L_PF,4,1,1,1,1,0,ggammadata);
00259 endEditCP(_ggammaimg);
00260
00261 _ggammachunk = TextureChunk::create();
00262 beginEditCP(_ggammachunk);
00263 _ggammachunk->setImage(_ggammaimg);
00264 _ggammachunk->setMinFilter(GL_LINEAR);
00265 _ggammachunk->setMagFilter(GL_LINEAR);
00266 _ggammachunk->setWrapS(GL_CLAMP_TO_EDGE);
00267 _ggammachunk->setWrapT(GL_CLAMP_TO_EDGE);
00268 endEditCP (_ggammachunk);
00269
00270 UChar8 bgammadata[] =
00271 { 0, 80, 160, 255 };
00272
00273 _bgammaimg = Image::create();
00274 beginEditCP(_bgammaimg);
00275 _bgammaimg->set(Image::OSG_L_PF,4,1,1,1,1,0,bgammadata);
00276 endEditCP(_bgammaimg);
00277
00278 _bgammachunk = TextureChunk::create();
00279 beginEditCP(_bgammachunk);
00280 _bgammachunk->setImage(_bgammaimg);
00281 _bgammachunk->setMinFilter(GL_LINEAR);
00282 _bgammachunk->setMagFilter(GL_LINEAR);
00283 _bgammachunk->setWrapS(GL_CLAMP_TO_EDGE);
00284 _bgammachunk->setWrapT(GL_CLAMP_TO_EDGE);
00285 endEditCP (_bgammachunk);
00286
00287
00288 for(int j=0; j < 3; ++j)
00289 {
00290 ImagePtr img;
00291 switch(j)
00292 {
00293 case 0: img = _rgammaimg; break;
00294 case 1: img = _ggammaimg; break;
00295 case 2: img = _bgammaimg; break;
00296 }
00297 beginEditCP(img);
00298 if(getGammaRamp().size() == 0)
00299 {
00300 img->set(Image::OSG_L_PF, 256, 1);
00301 UInt8 *data = img->getData();
00302 for(int i=0; i < 256; i++)
00303 {
00304 data[i] = (UInt8)(pow(i/255.0,1.0)*255);
00305 }
00306 }
00307 else
00308 {
00309 img->set(Image::OSG_L_PF, getGammaRamp().size(), 1);
00310 UInt8 *data = img->getData();
00311 for(int i=0; i < getGammaRamp().size(); i++)
00312 {
00313 data[i] = (UInt8)(getGammaRamp()[i][j] * 255);
00314 }
00315 }
00316 endEditCP(img);
00317 }
00318
00319
00320 m.setValue(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
00321
00322 _fragProgram = FragmentProgramChunk::create();
00323 std::string prog(
00324 "!!ARBfp1.0\n"
00325 "PARAM mat0 = program.local[1];\n"
00326 "PARAM mat1 = program.local[2];\n"
00327 "PARAM mat2 = program.local[3];\n"
00328 "PARAM mat3 = program.local[4];\n"
00329 "PARAM gamma = program.local[5];\n"
00330 "TEMP source;\n"
00331 "TEMP target;\n"
00332 "\n"
00333 "# get the grabbed texture's color\n"
00334 "TEX source, fragment.texcoord[0], texture[0], 2D;\n"
00335 "\n"
00336 "# gamma map it\n"
00337 "POW source.r, source.r, gamma.r;\n"
00338 "POW source.g, source.g, gamma.r;\n"
00339 "POW source.b, source.b, gamma.r;\n"
00340 "\n"
00341 "# do the matrix transform\n"
00342 "DP4 target.x, mat0, source;\n"
00343 "DP4 target.y, mat1, source;\n"
00344 "DP4 target.z, mat2, source;\n"
00345 "\n"
00346 "# map it through the target gamma\n"
00347 "TEX target.r, target.r, texture[1], 1D;\n"
00348 "TEX target.g, target.g, texture[2], 1D;\n"
00349 "TEX target.b, target.b, texture[3], 1D;\n"
00350 "\n"
00351 "# mov it to the output\n"
00352 "MOV result.color, target;\n"
00353 "END\n");
00354
00355 beginEditCP(_fragProgram);
00356 _fragProgram->setProgram(prog);
00357 endEditCP(_fragProgram);
00358 }
00359 else
00360 {
00361
00362
00363 beginEditCP(tex);
00364 tex->setImage(img);
00365 tex->setWrapS(GL_CLAMP_TO_EDGE);
00366 tex->setWrapT(GL_CLAMP_TO_EDGE);
00367
00368 tex->setMinFilter(GL_LINEAR);
00369 tex->setMagFilter(GL_LINEAR);
00370
00371
00372 tex->setScale(false);
00373 tex->setEnvMode(GL_REPLACE);
00374 tex->setShaderOperation(GL_TEXTURE_2D);
00375 tex->setInternalFormat(GL_RGB8);
00376 endEditCP (tex);
00377
00378
00379
00380
00381
00382 UChar8 _argammadata[] =
00383 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00384 80, 0, 0, 80, 0, 0, 80, 0, 0, 80, 0, 0,
00385 160, 0, 0, 160, 0, 0, 160, 0, 0, 160, 0, 0,
00386 255, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0
00387 };
00388
00389 _argammaimg = Image::create();
00390 beginEditCP(_argammaimg);
00391 _argammaimg->set(GL_RGB,4,4,1,1,1,0,_argammadata);
00392 endEditCP(_argammaimg);
00393
00394 _argammachunk = TextureChunk::create();
00395 beginEditCP(_argammachunk);
00396 _argammachunk->setImage(_argammaimg);
00397 _argammachunk->setMinFilter(GL_NEAREST);
00398 _argammachunk->setMagFilter(GL_NEAREST);
00399 _argammachunk->setWrapS(GL_CLAMP_TO_EDGE);
00400 _argammachunk->setWrapT(GL_CLAMP_TO_EDGE);
00401 _argammachunk->setShaderOperation(GL_DEPENDENT_AR_TEXTURE_2D_NV);
00402 _argammachunk->setShaderInput (GL_TEXTURE0_ARB);
00403 _argammachunk->setInternalFormat(GL_RGB8);
00404 endEditCP (_argammachunk);
00405
00406 UChar8 _gbgammadata[] =
00407 { 0, 0, 0, 0, 80, 0, 0,160, 0, 0,255, 0,
00408 0, 0, 80, 0, 80, 80, 0,160, 80, 0,255, 80,
00409 0, 0,160, 0, 80,160, 0,160,160, 0,255,160,
00410 0, 0,255, 0, 80,255, 0,160,255, 0,255,255
00411 };
00412
00413 _gbgammaimg = Image::create();
00414 beginEditCP(_gbgammaimg);
00415 _gbgammaimg->set(GL_RGB,4,4,1,1,1,0,_gbgammadata);
00416 endEditCP(_gbgammaimg);
00417
00418 _gbgammachunk = TextureChunk::create();
00419 beginEditCP(_gbgammachunk);
00420 _gbgammachunk->setImage(_gbgammaimg);
00421 _gbgammachunk->setMinFilter(GL_NEAREST);
00422 _gbgammachunk->setMagFilter(GL_NEAREST);
00423 _gbgammachunk->setWrapS(GL_CLAMP_TO_EDGE);
00424 _gbgammachunk->setWrapT(GL_CLAMP_TO_EDGE);
00425 _gbgammachunk->setShaderOperation(GL_DEPENDENT_GB_TEXTURE_2D_NV);
00426 _gbgammachunk->setShaderInput (GL_TEXTURE0_ARB);
00427 _gbgammachunk->setInternalFormat(GL_RGB8);
00428 endEditCP (_gbgammachunk);
00429
00430
00431
00432 m.setValue(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
00433 m.setValue(1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1);
00434
00435 _regCombiner = RegisterCombinersChunk::create();
00436 }
00437
00438
00439
00440 ChunkMaterialPtr mat = ChunkMaterial::create();
00441 beginEditCP(mat);
00442 mat->addChunk(tex);
00443 if(_useFragmentProgram)
00444 {
00445 mat->addChunk(_rgammachunk);
00446 mat->addChunk(_ggammachunk);
00447 mat->addChunk(_bgammachunk);
00448 mat->addChunk(_fragProgram);
00449 }
00450 else
00451 {
00452 mat->addChunk(_argammachunk);
00453 mat->addChunk(_gbgammachunk);
00454 mat->addChunk(_regCombiner);
00455 }
00456 endEditCP(mat);
00457
00458
00459
00460 GeoPTypesUI8Ptr types = GeoPTypesUI8::create();
00461 GeoPLengthsPtr lens = GeoPLengthsUI32::create();
00462 beginEditCP(types);
00463 types->addValue(GL_QUADS);
00464 endEditCP(types);
00465
00466 beginEditCP(lens);
00467 lens->addValue(4);
00468 endEditCP(lens);
00469
00470 GeoPositions3fPtr pos = GeoPositions3f::create();
00471 beginEditCP(pos);
00472 pos->addValue(Pnt3f(-1, -1, -0.5));
00473 pos->addValue(Pnt3f( 1, -1, -0.5));
00474 pos->addValue(Pnt3f( 1, 1, -0.5));
00475 pos->addValue(Pnt3f(-1, 1, -0.5));
00476 endEditCP(pos);
00477 _positions=pos;
00478
00479 _texcoords = GeoTexCoords2f::create();
00480 beginEditCP(_texcoords);
00481 _texcoords->addValue(Vec2f(0,0));
00482 _texcoords->addValue(Vec2f(1,0));
00483 _texcoords->addValue(Vec2f(1,1));
00484 _texcoords->addValue(Vec2f(0,1));
00485 endEditCP(_texcoords);
00486
00487 GeometryPtr geo = Geometry::create();
00488 beginEditCP(geo);
00489 geo->setMaterial(mat);
00490 geo->setPositions(pos);
00491 geo->setTypes(types);
00492 geo->setTexCoords(_texcoords);
00493 geo->setLengths(lens);
00494 endEditCP (geo);
00495
00496 NodePtr cube = Node::create();
00497 beginEditCP(cube);
00498 cube->setCore(geo);
00499 endEditCP(cube);
00500
00501 ComponentTransformPtr trans = ComponentTransform::create();
00502 beginEditCP(trans);
00503 trans->setTranslation(Vec3f(getScaleDown()-1, getScaleDown()-1, 0));
00504 trans->setScale(Vec3f(getScaleDown(), getScaleDown(), 1.0));
00505 endEditCP(trans);
00506
00507 _cmRoot = Node::create();
00508 addRefCP(_cmRoot);
00509 beginEditCP(_cmRoot);
00510 _cmRoot->setCore(trans);
00511 _cmRoot->addChild(cube);
00512 endEditCP(_cmRoot);
00513
00514 GroupPtr group = Group::create();
00515 NodePtr gr = Node::create();
00516 beginEditCP(gr);
00517 gr->setCore(group);
00518 gr->addChild(_cmRoot);
00519 endEditCP(gr);
00520
00521 _cam = MatrixCamera::create();
00522 addRefCP(_cam);
00523 beginEditCP(_cam);
00524 _cam->setNear(0.1);
00525 _cam->setFar(10);
00526 _cam->setModelviewMatrix(Matrix::identity());
00527 _cam->setProjectionMatrix(Matrix::identity());
00528 endEditCP(_cam);
00529
00530
00531 _dsRoot = makePlane(1, 1, 1, 1);
00532 addRefCP(_dsRoot);
00533 beginEditCP(_dsRoot);
00534 _dsRoot->setActive(false);
00535 endEditCP(_dsRoot);
00536
00537
00538
00539 _cmBack = TextureGrabBackground::create();
00540 addRefCP(_cmBack);
00541
00542 beginEditCP(_cmBack);
00543 _cmBack->setTexture(tex);
00544 _cmBack->setColor(Color3f(0,0,0));
00545 endEditCP(_cmBack);
00546
00547 _cmPort = Viewport::create();
00548 addRefCP(_cmPort);
00549
00550 beginEditCP(_cmPort);
00551 _cmPort->setCamera(_cam);
00552 _cmPort->setRoot(_cmRoot);
00553 _cmPort->setBackground(_cmBack);
00554 _cmPort->setLeft(port->getLeft());
00555 _cmPort->setRight(port->getRight());
00556 _cmPort->setBottom(port->getBottom());
00557 _cmPort->setTop(port->getTop());
00558 endEditCP(_cmPort);
00559
00560
00561
00562 ImagePtr img2 = Image::create();
00563 beginEditCP(img2);
00564 img2->set(GL_RGB,1,1);
00565 endEditCP(img2);
00566
00567 TextureChunkPtr tex2 = TextureChunk::create();
00568 beginEditCP(tex2);
00569 tex2->setImage(img2);
00570 tex2->setMinFilter(GL_NEAREST);
00571 tex2->setMagFilter(GL_NEAREST);
00572 tex2->setScale(false);
00573 tex2->setEnvMode(GL_REPLACE);
00574 endEditCP(tex2);
00575
00576 _dsBack = TextureGrabBackground::create();
00577 addRefCP(_dsBack);
00578 beginEditCP(_dsBack);
00579 _dsBack->setTexture(tex2);
00580 _dsBack->setColor(Color3f(1,0,0));
00581 endEditCP(_dsBack);
00582
00583
00584 _dsPort = Viewport::create();
00585 addRefCP(_dsPort);
00586 beginEditCP(_dsPort);
00587 _dsPort->setCamera(_cam);
00588 _dsPort->setRoot(_dsRoot);
00589 _dsPort->setBackground(_dsBack);
00590 _dsPort->setLeft(port->getLeft());
00591 _dsPort->setRight(port->getRight());
00592 _dsPort->setBottom(port->getBottom());
00593 _dsPort->setTop(port->getTop());
00594 endEditCP(_dsPort);
00595
00596
00597
00598 _ds2Back = SolidBackground::create();
00599 addRefCP(_ds2Back);
00600 beginEditCP(_ds2Back);
00601 _ds2Back->setColor(Color3f(1,0,0));
00602 endEditCP(_ds2Back);
00603
00604 ChunkMaterialPtr mat2 = ChunkMaterial::create();
00605 MaterialChunkPtr matc = MaterialChunk::create();
00606
00607 beginEditCP(mat2);
00608 mat2->addChunk(tex2);
00609 endEditCP(mat2);
00610
00611 GeometryPtr geo2 = Geometry::create();
00612
00613 GeoPLengthsPtr lens2 = GeoPLengthsUI32::create();
00614 beginEditCP(lens2);
00615 lens->addValue(4);
00616 endEditCP(lens2);
00617
00618 _positionsScale = GeoPositions3f::create();
00619 beginEditCP(_positionsScale);
00620 _positionsScale->addValue(Pnt3f(-1, -1, -0.5));
00621 _positionsScale->addValue(Pnt3f( 1, -1, -0.5));
00622 _positionsScale->addValue(Pnt3f( 1, 1, -0.5));
00623 _positionsScale->addValue(Pnt3f(-1, 1, -0.5));
00624 endEditCP(_positionsScale);
00625
00626 _texcoordsScale = GeoTexCoords2f::create();
00627 beginEditCP(_texcoordsScale);
00628 _texcoordsScale->addValue(Vec2f(0,0));
00629 _texcoordsScale->addValue(Vec2f(1,0));
00630 _texcoordsScale->addValue(Vec2f(1,1));
00631 _texcoordsScale->addValue(Vec2f(0,1));
00632 endEditCP(_texcoordsScale);
00633
00634 beginEditCP(geo2);
00635 geo2->setMaterial(mat2);
00636 geo2->setPositions(_positionsScale);
00637 geo2->setTypes(types);
00638 geo2->setTexCoords(_texcoordsScale);
00639 geo2->setLengths(lens2);
00640 endEditCP(geo2);
00641
00642 _ds2Root = Node::create();
00643 beginEditCP(_ds2Root);
00644 _ds2Root->setCore(geo2);
00645 endEditCP(_ds2Root);
00646
00647 _dsPort2 = Viewport::create();
00648 addRefCP(_dsPort2);
00649 beginEditCP(_dsPort2);
00650 _dsPort2->setCamera(_cam);
00651 _dsPort2->setRoot(_ds2Root);
00652 _dsPort2->setBackground(_ds2Back);
00653 _dsPort2->setLeft(0);
00654 _dsPort2->setRight(1);
00655 _dsPort2->setBottom(0);
00656 _dsPort2->setTop(1);
00657 endEditCP(_dsPort2);
00658
00659 updateMatrix();
00660 updateGamma();
00661
00662
00663
00664
00665
00666
00667 UInt32 x,y;
00668 Int32 left=0,top=1,bottom=0,right=1;
00669 left = port->getPixelLeft();
00670 right = port->getPixelRight();
00671 bottom = port->getPixelBottom();
00672 top = port->getPixelTop();
00673
00674 Real32 h=top-bottom+1;
00675 Real32 w=right-left+1;
00676 Real32
00677 tw = osgnextpower2((UInt32)w),
00678 th = osgnextpower2((UInt32)h);
00679
00680 beginEditCP(_texcoords);
00681 beginEditCP(_positions);
00682 _positions->clear();
00683 _texcoords->clear();
00684
00685 if(getGrid().size() == 0 ||
00686 getGrid().size() != getGridWidth() * getGridHeight() ||
00687 getGridWidth() < 2 ||
00688 getGridHeight() < 2)
00689 {
00690 if(!_nonPowerOfTwo)
00691 {
00692 _texcoords->addValue(Vec2f(0,0));
00693 _texcoords->addValue(Vec2f(w/tw,0));
00694 _texcoords->addValue(Vec2f(w/tw,h/th));
00695 _texcoords->addValue(Vec2f(0,h/th));
00696 }
00697 else
00698 {
00699 _texcoords->addValue(Vec2f(0,0));
00700 _texcoords->addValue(Vec2f(1,0));
00701 _texcoords->addValue(Vec2f(1,1));
00702 _texcoords->addValue(Vec2f(0,1));
00703 }
00704 _positions->addValue(Pnt3f(-1, -1, -0.5));
00705 _positions->addValue(Pnt3f( 1, -1, -0.5));
00706 _positions->addValue(Pnt3f( 1, 1, -0.5));
00707 _positions->addValue(Pnt3f(-1, 1, -0.5));
00708 }
00709 else
00710 {
00711 UInt32 i;
00712 bool absolute=false;
00713 for(i=0;i<getGrid().size();++i)
00714 if(getGrid()[i][0]>1 ||
00715 getGrid()[i][1]>1)
00716 {
00717 absolute=true;
00718 break;
00719 }
00720 for(int y=0 ; y<getGridHeight()-1 ; ++y)
00721 {
00722 for(int x=0 ; x<getGridWidth()-1 ; ++x)
00723 {
00724
00725 Vec2f tex[4];
00726 tex[0] = Vec2f( x, y);
00727 tex[1] = Vec2f(x+1, y);
00728 tex[2] = Vec2f(x+1,y+1);
00729 tex[3] = Vec2f( x,y+1);
00730
00731
00732 Vec2f pos[4];
00733 pos[0] = getGrid()[y*getGridWidth()+x];
00734 pos[1] = getGrid()[y*getGridWidth()+x+1];
00735 pos[2] = getGrid()[(y+1)*getGridWidth()+x+1];
00736 pos[3] = getGrid()[(y+1)*getGridWidth()+x];
00737 for(i=0 ; i<4 ; ++i)
00738 {
00739
00740 tex[i][0] /= getGridWidth() - 1;
00741 tex[i][1] /= getGridHeight() - 1;
00742
00743 if(!_nonPowerOfTwo)
00744 {
00745 tex[i][0] *= w/(float)tw;
00746 tex[i][1] *= h/(float)th;
00747 }
00748 if(absolute)
00749 {
00750 pos[i][0] /= (w-1);
00751 pos[i][1] /= (h-1);
00752 }
00753 _texcoords->addValue(tex[i]);
00754 Pnt3f p(pos[i][0],pos[i][1],0);
00755 _positions->addValue(p*2-Vec3f(1,1,1));
00756 }
00757 }
00758 }
00759 }
00760 endEditCP(_texcoords);
00761
00762 beginEditCP(lens);
00763 lens->clear();
00764 lens->addValue(_positions->getSize());
00765 endEditCP(lens);
00766
00767 endEditCP(_positions);
00768
00769
00770
00771
00772
00773 UInt32 w2 = UInt32(((right - left) + 1) * getScaleDown());
00774 UInt32 h2 = UInt32(((top - bottom) + 1) * getScaleDown());
00775 UInt32 lefts = Int32(left * getScaleDown());
00776 UInt32 rights = w2 - 1;
00777 UInt32 bottoms = Int32(bottom * getScaleDown());
00778 UInt32 tops = h2 - 1;
00779
00780 _dsPort->setSize(lefts,bottoms,rights,tops);
00781 _dsPort2->setSize(left,bottom,right,top);
00782
00783 Real32 h3= tops - bottoms + 1;
00784 Real32 w3= rights - lefts + 1;
00785 Real32 tw2 = osgnextpower2((UInt32) w2);
00786 Real32 th2 = osgnextpower2((UInt32) h2);
00787
00788 beginEditCP(_texcoordsScale);
00789 _texcoordsScale->clear();
00790 _texcoordsScale->addValue(Vec2f(0,0));
00791 _texcoordsScale->addValue(Vec2f(w3/tw2,0));
00792 _texcoordsScale->addValue(Vec2f(w3/tw2,h3/th2));
00793 _texcoordsScale->addValue(Vec2f(0,h3/th2));
00794 endEditCP(_texcoordsScale);
00795
00796 beginEditCP(_positionsScale);
00797 _positionsScale->clear();
00798 _positionsScale->addValue(Pnt3f(-1, -1, -0.5));
00799 _positionsScale->addValue(Pnt3f( 1, -1, -0.5));
00800 _positionsScale->addValue(Pnt3f( 1, 1, -0.5));
00801 _positionsScale->addValue(Pnt3f(-1, 1, -0.5));
00802 endEditCP(_positionsScale);
00803
00804 }
00805
00806 void DisplayCalibration::updateGamma()
00807 {
00808 UInt16 x,y;
00809 UInt8 *data;
00810 UInt32 res = getGammaRamp().size();
00811
00812 if(res == 0)
00813 res = 256;
00814
00815 if(_useFragmentProgram)
00816 {
00817 beginEditCP(_fragProgram, FragmentProgramChunk::ParamValuesFieldMask);
00818
00819 _fragProgram->setParameter(5, Vec4f(getGamma(),getGamma(),getGamma(),0));
00820
00821 endEditCP(_fragProgram, FragmentProgramChunk::ParamValuesFieldMask);
00822 }
00823 else
00824 {
00825 _argammaimg->set(GL_RGB,res,res);
00826
00827 beginEditCP(_argammaimg);
00828 data = _argammaimg->getData();
00829
00830 memset(data, 0, res * res * 3);
00831
00832 for(y = 0; y < res; ++y)
00833 {
00834 UInt8 v = (int)(pow(y/(Real32)(res-1),getGamma())*255+0.5);
00835 for(x = 0; x < res; ++x)
00836 {
00837 *data++ = v;
00838 *data++ = 0;
00839 *data++ = 0;
00840 }
00841 }
00842 endEditCP(_argammaimg);
00843
00844 beginEditCP(_argammachunk);
00845 _argammachunk->setImage(_argammaimg);
00846 endEditCP(_argammachunk);
00847
00848 _gbgammaimg->set(GL_RGB,res,res);
00849
00850 beginEditCP(_gbgammaimg);
00851 data = _gbgammaimg->getData();
00852
00853 memset(data, 0, res * res * 3);
00854
00855 for(y = 0; y < res; ++y)
00856 {
00857 UInt8 vy = (int)(pow(y/(Real32)(res-1),getGamma())*255+0.5);
00858 for(x = 0; x < res; ++x)
00859 {
00860 UInt8 vx = (int)(pow(x/(Real32)(res-1),getGamma())*255+0.5);
00861 *data++ = 0;
00862 *data++ = vx;
00863 *data++ = vy;
00864 }
00865 }
00866 endEditCP(_gbgammaimg);
00867
00868 beginEditCP(_gbgammachunk);
00869 _gbgammachunk->setImage(_gbgammaimg);
00870 endEditCP(_gbgammachunk);
00871 }
00872 }
00873
00874 void DisplayCalibration::updateMatrix()
00875 {
00876 if(_useFragmentProgram)
00877 {
00878 beginEditCP(_fragProgram, FragmentProgramChunk::ParamValuesFieldMask);
00879
00880 _fragProgram->setParameter(1, getColorMatrix()[0]);
00881 _fragProgram->setParameter(2, getColorMatrix()[1]);
00882 _fragProgram->setParameter(3, getColorMatrix()[2]);
00883 _fragProgram->setParameter(4, getColorMatrix()[3]);
00884
00885 endEditCP(_fragProgram, FragmentProgramChunk::ParamValuesFieldMask);
00886
00887 }
00888 else
00889 {
00890 #if 0
00891 Color4f
00892 m1(m[0][0]/2+.5,m[0][1]/2+.5,m[0][2]/2+.5,0),
00893 m2(m[1][0]/2+.5,m[1][1]/2+.5,m[1][2]/2+.5,0),
00894 m3(m[2][0]/2+.5,m[2][1]/2+.5,m[2][2]/2+.5,0),
00895 m4(m[3][0]/2+.5,m[3][1]/2+.5,m[3][2]/2+.5,0);
00896 #endif
00897 Color4f
00898 m1(getColorMatrix()[0][0]/2+.5,
00899 getColorMatrix()[0][1]/2+.5,
00900 getColorMatrix()[0][2]/2+.5,0),
00901 m2(getColorMatrix()[1][0]/2+.5,
00902 getColorMatrix()[1][1]/2+.5,
00903 getColorMatrix()[1][2]/2+.5,0),
00904 m3(getColorMatrix()[2][0]/2+.5,
00905 getColorMatrix()[2][1]/2+.5,
00906 getColorMatrix()[2][2]/2+.5,0),
00907 m4(getColorMatrix()[3][0]/2+.5,
00908 getColorMatrix()[3][1]/2+.5,
00909 getColorMatrix()[3][2]/2+.5,0);
00910
00911 Color4f selectR(1,0,0,0), selectG(0,1,0,0), selectB(0,0,1,0);
00912
00913 UInt16 ncomb = 0;
00914
00915 beginEditCP(_regCombiner);
00916
00917 _regCombiner->setCombinerRGB(ncomb,
00918 GL_TEXTURE1_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00919 GL_ZERO, GL_UNSIGNED_INVERT_NV, GL_RGB,
00920 GL_TEXTURE2_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00921 GL_ZERO, GL_UNSIGNED_INVERT_NV, GL_RGB,
00922 GL_DISCARD_NV, GL_DISCARD_NV, GL_TEXTURE0_ARB,
00923 GL_NONE, GL_NONE,
00924 GL_FALSE, GL_FALSE, GL_FALSE );
00925
00926 ncomb++;
00927
00928
00929
00930 _regCombiner->setCombinerColors(ncomb, m1, m2);
00931
00932 _regCombiner->setCombinerRGB(
00933 ncomb,
00934 GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00935 GL_CONSTANT_COLOR0_NV, GL_EXPAND_NORMAL_NV, GL_RGB,
00936 GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00937 GL_CONSTANT_COLOR1_NV, GL_EXPAND_NORMAL_NV, GL_RGB,
00938 GL_SPARE0_NV, GL_SPARE1_NV, GL_DISCARD_NV,
00939 GL_NONE, GL_NONE,
00940 GL_TRUE, GL_TRUE, GL_FALSE );
00941
00942 ncomb++;
00943
00944
00945
00946 _regCombiner->setCombinerColors(ncomb, m3, selectR);
00947
00948 _regCombiner->setCombinerRGB(ncomb,
00949 GL_TEXTURE0_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00950 GL_CONSTANT_COLOR0_NV, GL_EXPAND_NORMAL_NV, GL_RGB,
00951 GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00952 GL_CONSTANT_COLOR1_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00953 GL_TEXTURE3_ARB, GL_SPARE0_NV, GL_DISCARD_NV,
00954 GL_NONE, GL_NONE,
00955 GL_TRUE, GL_FALSE, GL_FALSE );
00956
00957 ncomb++;
00958
00959
00960
00961 _regCombiner->setColor0(selectG);
00962 _regCombiner->setColor1(selectB);
00963
00964
00965 _regCombiner->setFinalCombiner(
00966 GL_CONSTANT_COLOR0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00967 GL_SPARE1_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00968 GL_E_TIMES_F_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00969 GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00970 GL_CONSTANT_COLOR1_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00971 GL_TEXTURE3_ARB, GL_UNSIGNED_IDENTITY_NV, GL_RGB,
00972 GL_PRIMARY_COLOR_NV, GL_UNSIGNED_IDENTITY_NV, GL_ALPHA );
00973
00974 endEditCP(_regCombiner);
00975 }
00976 }
00977
00978
00979
00980
00981 #ifdef OSG_SGI_CC
00982 #pragma set woff 1174
00983 #endif
00984
00985 #ifdef OSG_LINUX_ICC
00986 #pragma warning( disable : 177 )
00987 #endif
00988
00989 namespace
00990 {
00991 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00992 static Char8 cvsid_hpp [] = OSGDISPLAYCALIBRATIONBASE_HEADER_CVSID;
00993 static Char8 cvsid_inl [] = OSGDISPLAYCALIBRATIONBASE_INLINE_CVSID;
00994
00995 static Char8 cvsid_fields_hpp[] = OSGDISPLAYCALIBRATIONFIELDS_HEADER_CVSID;
00996 }
00997
00998 #ifdef __sgi
00999 #pragma reset woff 1174
01000 #endif
01001