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 #include <X11Renderer.h>
00038 #include <ColorMap.h>
00039 #include <string.h>
00040
00041 #include <db.H>
00042
00043 #include <Matrix.h>
00044 #include <Vector.h>
00045 #include <View.h>
00046 #include <Projection.h>
00047 #include <Viewport.h>
00048 #include <Clipping.h>
00049 #include <WindowDevice.h>
00050 #include <Scan.h>
00051
00052 #define WIRE_MODE 1
00053 #define FILL_MODE 0
00054
00055
00056
00057 X11Renderer::X11Renderer(char *title, int xLoc, int yLoc, int width, int height,
00058 ColorMap &_theMap)
00059 :Renderer(_theMap)
00060 {
00061 theView = new View;
00062 theProjection = new Projection;
00063 theClipping = new Clipping;
00064 theViewport = new Viewport;
00065 theScan = new ScanLineConverter;
00066 theDevice = new WindowDevice;
00067
00068 theScan->setDevice(*theDevice);
00069 theScan->setProjection(*theProjection);
00070 theViewport->setDevice(*theDevice);
00071
00072 theDevice->WINOPEN(title, xLoc, yLoc, width, height);
00073 theScan->setFillMode(0);
00074
00075 aFile = 0;
00076 }
00077
00078
00079 X11Renderer::X11Renderer(char *title, int xLoc, int yLoc, int width, int height,
00080 ColorMap &_theMap,
00081 char *fileName)
00082 :Renderer(_theMap)
00083 {
00084 theView = new View;
00085 theProjection = new Projection;
00086 theClipping = new Clipping;
00087 theViewport = new Viewport;
00088 theScan = new ScanLineConverter;
00089 theDevice = new WindowDevice;
00090
00091 theScan->setDevice(*theDevice);
00092 theScan->setProjection(*theProjection);
00093 theViewport->setDevice(*theDevice);
00094
00095 theDevice->WINOPEN(title, xLoc, yLoc, width, height);
00096 theScan->setFillMode(0);
00097
00098
00099 if (fileName != 0) {
00100 if (strlen(fileName) > MAX_FILENAMELENGTH)
00101 g3ErrorHandler->warning("WARNING - X11Renderer::X11Renderer() - fileName %s too long, max %d\n",
00102 fileName, MAX_FILENAMELENGTH);
00103 else {
00104 strcpy(theFileName, fileName);
00105 theFile.open(fileName, ios::out);
00106 if (!theFile) {
00107 g3ErrorHandler->warning("WARNING - X11Renderer::X11Renderer() - could not open file %s\n",fileName);
00108 aFile = 0;
00109 } else {
00110 aFile = 1;
00111 theFile << title << endl;
00112 theFile << xLoc << " " << yLoc << " " << width << " " << height << endl;
00113 }
00114 }
00115 }
00116 }
00117
00118 X11Renderer::~X11Renderer()
00119 {
00120 delete theView;
00121 delete theProjection;
00122 delete theClipping;
00123 delete theViewport;
00124 delete theScan;
00125 delete theDevice;
00126
00127 if (aFile == 1)
00128 theFile.close();
00129 }
00130
00131
00132 int
00133 X11Renderer::clearImage(void)
00134 {
00135 theDevice->CLEAR();
00136 return 0;
00137 }
00138
00139 int
00140 X11Renderer::startImage(void)
00141 {
00142 theView->update();
00143 theProjection->update();
00144 theClipping->update();
00145 theViewport->update();
00146 theScan->update();
00147 theDevice->STARTIMAGE();
00148
00149 if (aFile == 1) {
00150 theFile << "StartImage\n";
00151 theFile << "VRP " << theView->vrp[0] << " " << theView->vrp[1] << " " << theView->vrp[2] << " " << endl;
00152 theFile << "VPN " << theView->vpn[0] << " " << theView->vpn[1] << " " << theView->vpn[2] << " " << endl;
00153 theFile << "VUV " << theView->vuv[0] << " " << theView->vuv[1] << " " << theView->vuv[2] << " " << endl;
00154 theFile << "COP " << theProjection->cop[0] << " " << theProjection->cop[1] << " " << theProjection->cop[2] << " " << endl;
00155 theFile << "PROJECTIONMODE " << theProjection->projection_mode << endl;
00156 theFile << "VPWINDOW " << theProjection->vpwindow[0] << " " << theProjection->vpwindow[1] << " "
00157 << theProjection->vpwindow[2] << " " << theProjection->vpwindow[3] << " " << endl;
00158 theFile << "PLANES " << theProjection->planedist[0] << " " << theProjection->planedist[2] << " " << endl;
00159 theFile << "PORTWINDOW " << theViewport->portwindow[0] << " " << theViewport->portwindow[1] << " "
00160 << theViewport->portwindow[2] << " " << theViewport->portwindow[3] << " " << endl;
00161
00162 }
00163
00164 return 0;
00165 }
00166
00167 int
00168 X11Renderer::doneImage(void)
00169 {
00170 theDevice->ENDIMAGE();
00171
00172 if (aFile == 1) {
00173 theFile << "DoneImage\n";
00174 }
00175 return 0;
00176 }
00177
00178
00179 int
00180 X11Renderer::drawPoint(const Vector &pos1, float V1, int numPixels)
00181 {
00182 float r, g, b;
00183 r = theMap->getRed(V1);
00184 g = theMap->getGreen(V1);
00185 b = theMap->getBlue(V1);
00186
00187 if (aFile == 1) {
00188 theFile << "Point\n" << pos1(0) << " " << pos1(1) << " " << pos1(2)
00189 << " " << r << " " << g << " " << b << " " << endl;
00190 }
00191
00192 return 0;
00193 }
00194
00195
00196 int
00197 X11Renderer::drawPoint(const Vector &pos1, const Vector &rgb, int numPixels)
00198 {
00199 float r, g, b;
00200 r = rgb(0);
00201 g = rgb(1);
00202 b = rgb(2);
00203
00204 if (aFile == 1 ) {
00205 theFile << "Point\n" << pos1(0) << " " << pos1(1) << " " << pos1(2)
00206 << " " << r << " " << g << " " << b << " " << endl;
00207 }
00208
00209 return 0;
00210 }
00211
00212
00213
00214
00215 int
00216 X11Renderer::drawLine(const Vector &pos1, const Vector &pos2,
00217 float V1, float V2, int width, int style)
00218 {
00219 FACE *theFace = new FACE();
00220
00221 int size;
00222 float x,y,z, r, g, b;
00223 MYPOINT *point;
00224
00225
00226 size = pos1.Size();
00227 if (size == 1) {
00228 x = pos1(0);
00229 y = 0;
00230 z = 0;
00231 } else if (size == 2) {
00232 x = pos1(0);
00233 y = pos1(1);
00234 z = 0;
00235 } else {
00236 x = pos1(0);
00237 y = pos1(1);
00238 z = pos1(2);
00239 }
00240 point = new MYPOINT(1,x,y,z);
00241 r = theMap->getRed(V1);
00242 g = theMap->getGreen(V1);
00243 b = theMap->getBlue(V1);
00244 point->r = r;
00245 point->g = g;
00246 point->b = b;
00247
00248 theFace->AddPoint(*point);
00249
00250 if (aFile == 1) {
00251 theFile << "Line\n" << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endl;
00252 }
00253
00254 size = pos2.Size();
00255 if (size == 1) {
00256 x = pos2(0);
00257 y = 0;
00258 z = 0;
00259 } else if (size == 2) {
00260 x = pos2(0);
00261 y = pos2(1);
00262 z = 0;
00263 } else {
00264 x = pos2(0);
00265 y = pos2(1);
00266 z = pos2(2);
00267 }
00268 point = new MYPOINT(2,x,y,z);
00269 r = theMap->getRed(V2);
00270 g = theMap->getGreen(V2);
00271 b = theMap->getBlue(V2);
00272 point->r = r;
00273 point->g = g;
00274 point->b = b;
00275
00276 theFace->AddPoint(*point);
00277
00278 if (aFile == 1) {
00279 theFile << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endl;
00280 }
00281
00282 FACE &res1 = theView->transform(*theFace);
00283
00284 FACE &res2 = theProjection->transform(res1);
00285
00286 FACE &res3 = theClipping->transform(res2);
00287
00288 FACE &res4 = theViewport->transform(res3);
00289
00290 theScan->scanLine(res4);
00291 return 0;
00292 }
00293
00294 int
00295 X11Renderer::drawLine(const Vector &pos1, const Vector &pos2,
00296 const Vector &rgb1, const Vector &rgb2,
00297 int width, int style)
00298 {
00299 FACE *theFace = new FACE();
00300
00301 int size;
00302 float x,y,z, r, g, b;
00303 MYPOINT *point;
00304
00305
00306 size = pos1.Size();
00307 if (size == 1) {
00308 x = pos1(0);
00309 y = 0;
00310 z = 0;
00311 } else if (size == 2) {
00312 x = pos1(0);
00313 y = pos1(1);
00314 z = 0;
00315 } else {
00316 x = pos1(0);
00317 y = pos1(1);
00318 z = pos1(2);
00319 }
00320 point = new MYPOINT(1,x,y,z);
00321 r = rgb1(0);
00322 g = rgb1(1);
00323 b = rgb1(2);
00324 point->r = r;
00325 point->g = g;
00326 point->b = b;
00327
00328 theFace->AddPoint(*point);
00329
00330 if (aFile == 1) {
00331 theFile << "Line\n" << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endl;
00332 }
00333
00334 size = pos2.Size();
00335 if (size == 1) {
00336 x = pos2(0);
00337 y = 0;
00338 z = 0;
00339 } else if (size == 2) {
00340 x = pos2(0);
00341 y = pos2(1);
00342 z = 0;
00343 } else {
00344 x = pos2(0);
00345 y = pos2(1);
00346 z = pos2(2);
00347 }
00348 point = new MYPOINT(2,x,y,z);
00349 r = rgb2(0);
00350 g = rgb2(1);
00351 b = rgb2(2);
00352 point->r = r;
00353 point->g = g;
00354 point->b = b;
00355
00356 theFace->AddPoint(*point);
00357
00358 if (aFile == 1) {
00359 theFile << x << " " << y << " " << z << " " << r << " " << g << " " << b << " " << endl;
00360 }
00361
00362 FACE &res1 = theView->transform(*theFace);
00363
00364 FACE &res2 = theProjection->transform(res1);
00365
00366 FACE &res3 = theClipping->transform(res2);
00367
00368 FACE &res4 = theViewport->transform(res3);
00369
00370 theScan->scanLine(res4);
00371 return 0;
00372 }
00373
00374
00375
00376
00377
00378 int
00379 X11Renderer::drawPolygon(const Matrix &pos, const Vector &data)
00380
00381 {
00382 #ifdef _G3DEBUG
00383 if (pos.noCols() != 3) {
00384 g3ErrorHandler->warning("X11Renderer::drawPolygon - matrix needs 3 cols\n");
00385 return -1;
00386 }
00387 if (pos.noRows() != data.Size()) {
00388 g3ErrorHandler->warning("X11Renderer::drawPolygon - matrix & vector incompatable\n");
00389 return -1;
00390 }
00391 #endif
00392
00393 FACE *theFace = new FACE();
00394
00395 float posX,posY,posZ, r, g, b;
00396 double value;
00397 MYPOINT *point;
00398
00399
00400 int numRows = pos.noRows();
00401 for (int i=0; i<numRows; i++) {
00402 posX = pos(i,0);
00403 posY = pos(i,1);
00404 posZ = pos(i,2);
00405 value = data(i);
00406 r = theMap->getRed(value);
00407 g = theMap->getGreen(value);
00408 b = theMap->getBlue(value);
00409
00410 if (aFile == 1) {
00411 theFile << posX << " " << posY << " " << posZ << " " << r
00412 << " " << g << " " << b << " " << endl;
00413 }
00414
00415 point = new MYPOINT(1,posX,posY,posZ);
00416 point->r = r;
00417 point->g = g;
00418 point->b = b;
00419
00420 theFace->AddPoint(*point);
00421 }
00422
00423
00424 FACE &res1 = theView->transform(*theFace);
00425
00426 FACE &res2 = theProjection->transform(res1);
00427
00428 FACE &res3 = theClipping->transform(res2);
00429
00430 FACE &res4 = theViewport->transform(res3);
00431
00432 theScan->scanPolygon(res4);
00433 return 0;
00434 }
00435
00436
00437 int
00438 X11Renderer::drawPolygon(const Matrix &pos, const Matrix &data)
00439
00440 {
00441 #ifdef _G3DEBUG
00442 if (pos.noCols() != 3 || data.noCols() != 3) {
00443 g3ErrorHandler->warning("X11Renderer::drawPolygon - matrix needs 3 cols\n");
00444 return -1;
00445 }
00446 if (pos.noRows() != data.noRows()) {
00447 g3ErrorHandler->warning("X11Renderer::drawPolygon - matrix & vector incompatable\n");
00448 return -1;
00449 }
00450 #endif
00451
00452 FACE *theFace = new FACE();
00453
00454 float posX,posY,posZ, r, g, b;
00455 MYPOINT *point;
00456
00457
00458 int numRows = pos.noRows();
00459 for (int i=0; i<numRows; i++) {
00460 posX = pos(i,0);
00461 posY = pos(i,1);
00462 posZ = pos(i,2);
00463 r = data(i,0);
00464 g = data(i,1);
00465 b = data(i,2);
00466
00467 if (aFile == 1) {
00468 theFile << posX << " " << posY << " " << posZ << " " << r
00469 << " " << g << " " << b << " " << endl;
00470 }
00471
00472 point = new MYPOINT(1,posX,posY,posZ);
00473 point->r = r;
00474 point->g = g;
00475 point->b = b;
00476
00477 theFace->AddPoint(*point);
00478 }
00479
00480
00481 FACE &res1 = theView->transform(*theFace);
00482
00483 FACE &res2 = theProjection->transform(res1);
00484
00485 FACE &res3 = theClipping->transform(res2);
00486
00487 FACE &res4 = theViewport->transform(res3);
00488
00489 theScan->scanPolygon(res4);
00490 return 0;
00491 }
00492
00493
00494
00495 int
00496 X11Renderer::drawText(const Vector &pos, char *text, int length,
00497 char horizontalJustify, char verticalJustify)
00498 {
00499 MYPOINT *point;
00500
00501
00502 int size = pos.Size();
00503 float x,y,z;
00504 if (size == 1) {
00505 x = pos(0);
00506 y = 0;
00507 z = 0;
00508 } else if (size == 2) {
00509 x = pos(0);
00510 y = pos(1);
00511 z = 0;
00512 } else {
00513 x = pos(0);
00514 y = pos(1);
00515 z = pos(2);
00516 }
00517 point = new MYPOINT(1,x,y,z);
00518
00519 MYPOINT *res =0;
00520 res = theView->transformP(point);
00521 if (res != 0) {
00522 res = theProjection->transformP(res);
00523 }
00524 if (res != 0) {
00525 res = theClipping->transformP(res);
00526 }
00527
00528 if (res != 0) {
00529 res = theViewport->transformP(res);
00530 }
00531 if (res != 0) {
00532 float x = res->p[0];
00533 float y = res->p[1];
00534 theDevice->drawText(x,y, text, length);
00535 }
00536
00537 if (res != 0)
00538 delete res;
00539
00540 return 0;
00541 }
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 int
00571 X11Renderer::displayFace(FACE &theFace)
00572 {
00573
00574 FACE &res1 = theView->transform(theFace);
00575
00576 FACE &res2 = theProjection->transform(res1);
00577
00578 FACE &res3 = theClipping->transform(res2);
00579
00580 FACE &res4 = theViewport->transform(res3);
00581
00582 theScan->scanPolygon(res4);
00583 return 0;
00584 }
00585
00586
00587
00588
00589 int
00590 X11Renderer::setVRP(float x, float y, float z)
00591 {
00592 theView->vrp[0] = x;
00593 theView->vrp[1] = y;
00594 theView->vrp[2] = z;
00595
00596 return 0;
00597 }
00598
00599 int
00600 X11Renderer::setVPN(float x, float y, float z)
00601 {
00602 theView->vpn[0] = x;
00603 theView->vpn[1] = y;
00604 theView->vpn[2] = z;
00605
00606 return 0;
00607 }
00608
00609 int
00610 X11Renderer::setVUP(float x, float y, float z)
00611 {
00612 theView->vuv[0] = x;
00613 theView->vuv[1] = y;
00614 theView->vuv[2] = z;
00615
00616 return 0;
00617 }
00618
00619 int
00620 X11Renderer::setViewWindow(float umin, float umax, float vmin, float vmax)
00621 {
00622 if (umin > umax || vmin > vmax) {
00623 cerr << "X11Renderer::setViewWindow() - invalid window ";
00624 cerr << umin << " "<< umax << " "<< vmin << " "<< vmax << endl;
00625 return -1;
00626 }
00627
00628 theProjection->vpwindow[0] = umin;
00629 theProjection->vpwindow[1] = umax;
00630 theProjection->vpwindow[2] = vmin;
00631 theProjection->vpwindow[3] = vmax;
00632
00633 return 0;
00634 }
00635
00636 int
00637 X11Renderer::setPlaneDist(float anear, float afar)
00638 {
00639
00640 if ((anear < 0.0) || (afar < 0.0)) {
00641 cerr << "X11Renderer::setPlaneDist() - invalid planes";
00642 cerr << anear << " " << afar << endl;
00643 return -1;
00644 }
00645
00646 theProjection->planedist[0] = anear;
00647 theProjection->planedist[2] = afar;
00648
00649 return 0;
00650 }
00651
00652 int
00653 X11Renderer::setProjectionMode(char *newMode)
00654 {
00655 int projectionMode = 0;
00656 if ((strcmp(newMode, "parallel") == 0) || (strcmp(newMode, "Parallel") == 0))
00657 projectionMode = PARALLEL_MODE;
00658 else if ((strcmp(newMode, "perspective") == 0) || (strcmp(newMode, "Perspective") == 0))
00659
00660 theProjection->projection_mode = projectionMode;
00661 return 0;
00662 }
00663
00664 int
00665 X11Renderer::setFillMode(char *newMode)
00666 {
00667 int fillMode = 0;
00668 if ((strcmp(newMode, "wire") == 0) || (strcmp(newMode, "Wire") == 0))
00669 fillMode = WIRE_MODE;
00670 else if ((strcmp(newMode, "fill") == 0) || (strcmp(newMode, "Fill") == 0))
00671 fillMode = FILL_MODE;
00672
00673 theScan->setFillMode(fillMode);
00674 return 0;
00675 }
00676
00677
00678 int
00679 X11Renderer::setPRP(float u, float v, float n){
00680 theProjection->cop[0] = u;
00681 theProjection->cop[1] = v;
00682 theProjection->cop[2] = n;
00683
00684 return 0;
00685 }
00686
00687 int
00688 X11Renderer::setPortWindow(float left, float right,
00689 float bottom, float top)
00690 {
00691 if (left < -1 || right > 1 || bottom < -1 || top > 1
00692 || left > right || bottom > top) {
00693
00694 cerr << "X11Renderer::setPortWindow() - bounds invalid ";
00695 cerr << left << " "<< right << " "<< bottom << " "<< top << endl;
00696 return -1;
00697 }
00698
00699 theViewport->portwindow[0] = left;
00700 theViewport->portwindow[1] = right;
00701 theViewport->portwindow[2] = bottom;
00702 theViewport->portwindow[3] = top;
00703
00704 return 0;
00705 }
00706