Changeset 2974 for trunk/packages/vizservers/nanovis/VolumeRenderer.cpp
- Timestamp:
- Apr 19, 2012, 11:09:40 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanovis/VolumeRenderer.cpp
r2973 r2974 15 15 */ 16 16 #include <stdlib.h> 17 #include <assert.h>18 #include <time.h>19 #include <sys/time.h>20 17 #include <float.h> 21 18 … … 23 20 24 21 #include <GL/glew.h> 25 #include <GL/glut.h>26 22 27 23 #include <tcl.h> 28 29 #include <R2/R2FilePath.h>30 24 31 25 #include "nanovis.h" 32 26 #include "VolumeRenderer.h" 33 27 #include "ConvexPolygon.h" 34 35 28 #include "NvStdVertexShader.h" 36 29 #include "Trace.h" 37 #include "Grid.h" 38 39 #define NUMDIGITS 6 40 41 VolumeRenderer::VolumeRenderer() : 42 _sliceMode(false), 43 _volumeMode(true) 30 31 VolumeRenderer::VolumeRenderer() 44 32 { 45 33 initShaders(); 46 34 47 std::string path = R2FilePath::getInstance()->getPath("Font.bmp");48 if (path.empty()) {49 ERROR("can't find Font.bmp\n");50 assert(!path.empty());51 }52 initFont(path.c_str());53 35 _volumeInterpolator = new VolumeInterpolator(); 54 36 } … … 209 191 (double)olcolor[0], (double)olcolor[1], (double)olcolor[2], 210 192 1.5); 211 }212 glPopMatrix();213 214 //draw labels215 glPushMatrix();216 glTranslatef(shift_4d.x, shift_4d.y, shift_4d.z);217 if (volPtr->outline()) {218 //drawLabel(i);219 193 } 220 194 glPopMatrix(); … … 491 465 glEnd(); 492 466 493 #ifdef notdef494 /* Rappture doesn't supply axis units yet. So turn labeling off until we495 * can display the proper units with the distance of each bounding box496 * dimension.497 */498 glColor4f(1.0f, 1.0f, 0.0f, 1.0f);499 500 if (NanoVis::fonts != NULL) {501 double mv[16], prjm[16];502 int viewport[4];503 double wx, wy, wz;504 double dx, dy, dz;505 506 glGetDoublev(GL_MODELVIEW_MATRIX, mv);507 glGetDoublev(GL_PROJECTION_MATRIX, prjm);508 glGetIntegerv(GL_VIEWPORT, viewport);509 510 NanoVis::fonts->begin();511 dx = x1 - x0;512 dy = y1 - y0;513 dz = z1 - z0;514 if (gluProject((x0 + x1) * 0.5, y0, z0, mv, prjm, viewport,515 &wx, &wy, &wz)) {516 char buff[20];517 double min, max;518 519 NanoVis::grid->xAxis.GetDataLimits(min, max);520 glLoadIdentity();521 glTranslatef((int)wx, viewport[3] - (int) wy, 0.0f);522 const char *units;523 units = NanoVis::grid->xAxis.GetUnits();524 sprintf(buff, "%.*g %s", NUMDIGITS, max - min, units);525 NanoVis::fonts->draw(buff);526 }527 if (gluProject(x0, (y0 + y1) * 0.5, z0, mv, prjm, viewport, &528 wx, &wy, &wz)) {529 char buff[20];530 double min, max;531 532 NanoVis::grid->yAxis.GetDataLimits(min, max);533 glLoadIdentity();534 glTranslatef((int)wx, viewport[3] - (int) wy, 0.0f);535 const char *units;536 units = NanoVis::grid->yAxis.GetUnits();537 sprintf(buff, "%.*g %s", NUMDIGITS, max - min, units);538 NanoVis::fonts->draw(buff);539 }540 if (gluProject(x0, y0, (z0 + z1) * 0.5, mv, prjm, viewport,541 &wx, &wy, &wz)) {542 glLoadIdentity();543 glTranslatef((int)wx, viewport[3] - (int) wy, 0.0f);544 545 double min, max;546 NanoVis::grid->zAxis.GetDataLimits(min, max);547 const char *units;548 units = NanoVis::grid->zAxis.GetUnits();549 char buff[20];550 sprintf(buff, "%.*g %s", NUMDIGITS, max - min, units);551 NanoVis::fonts->draw(buff);552 }553 NanoVis::fonts->end();554 };555 #endif556 467 glPopMatrix(); 557 468 glPopAttrib(); … … 625 536 zFar = zMin; 626 537 } 627 628 bool629 VolumeRenderer::initFont(const char *filename)630 {631 FILE *f;632 unsigned short int bfType;633 int bfOffBits;634 short int biPlanes;635 short int biBitCount;636 int biSizeImage;637 int width, height;638 int i;639 unsigned char temp;640 unsigned char* data;641 /* make sure the file is there and open it read-only (binary) */642 f = fopen(filename, "rb");643 if (f == NULL) {644 ERROR("can't open font file \"%s\"\n", filename);645 return false;646 }647 648 if (fread(&bfType, sizeof(short int), 1, f) != 1) {649 ERROR("can't read %lu bytes from font file \"%s\"\n",650 (unsigned long)sizeof(short int), filename);651 goto error;652 }653 654 /* check if file is a bitmap */655 if (bfType != 19778) {656 ERROR("not a bmp file.\n");657 goto error;658 }659 660 /* get the file size */661 /* skip file size and reserved fields of bitmap file header */662 fseek(f, 8, SEEK_CUR);663 664 /* get the position of the actual bitmap data */665 if (fread(&bfOffBits, sizeof(int), 1, f) != 1) {666 ERROR("error reading file.\n");667 goto error;668 }669 //TRACE("Data at Offset: %ld\n", bfOffBits);670 671 /* skip size of bitmap info header */672 fseek(f, 4, SEEK_CUR);673 674 /* get the width of the bitmap */675 if (fread(&width, sizeof(int), 1, f) != 1) {676 ERROR("error reading file.\n");677 goto error;678 }679 //TRACE("Width of Bitmap: %d\n", texture->width);680 681 /* get the height of the bitmap */682 if (fread(&height, sizeof(int), 1, f) != 1) {683 ERROR("error reading file.\n");684 goto error;685 }686 //TRACE("Height of Bitmap: %d\n", texture->height);687 688 /* get the number of planes (must be set to 1) */689 if (fread(&biPlanes, sizeof(short int), 1, f) != 1) {690 ERROR("error reading file.\n");691 goto error;692 }693 if (biPlanes != 1) {694 ERROR("number of Planes not 1!\n");695 goto error;696 }697 698 /* get the number of bits per pixel */699 if (fread(&biBitCount, sizeof(short int), 1, f) != 1) {700 ERROR("error reading file.\n");701 goto error;702 }703 704 //TRACE("Bits per Pixel: %d\n", biBitCount);705 if (biBitCount != 24) {706 ERROR("Bits per Pixel not 24\n");707 goto error;708 }709 710 /* calculate the size of the image in bytes */711 biSizeImage = width * height * 3 * sizeof(unsigned char);712 data = (unsigned char*) malloc(biSizeImage);713 if (data == NULL) {714 ERROR("Can't allocate memory for image\n");715 goto error;716 }717 718 /* seek to the actual data */719 fseek(f, bfOffBits, SEEK_SET);720 if (fread(data, biSizeImage, 1, f) != 1) {721 ERROR("Error loading file!\n");722 goto error;723 }724 fclose(f);725 726 /* swap red and blue (bgr -> rgb) */727 for (i = 0; i < biSizeImage; i += 3) {728 temp = data[i];729 data[i] = data[i + 2];730 data[i + 2] = temp;731 }732 733 //insert alpha channel734 unsigned char* data_with_alpha;735 data_with_alpha = (unsigned char*)736 malloc(width*height*4*sizeof(unsigned char));737 for (int i = 0; i < height; i++) {738 for (int j = 0; j < width; j++) {739 unsigned char r, g, b, a;740 r = data[3*(i*width+j)];741 g = data[3*(i*width+j)+1];742 b = data[3*(i*width+j)+2];743 744 if (r==0 && g==0 && b==0)745 a = 0;746 else747 a = 255;748 749 data_with_alpha[4*(i*width+j)] = r;750 data_with_alpha[4*(i*width+j) + 1] = g;751 data_with_alpha[4*(i*width+j) + 2] = b;752 data_with_alpha[4*(i*width+j) + 3] = a;753 }754 }755 free(data);756 757 //create opengl texture758 glGenTextures(1, &_fontTexture);759 glBindTexture(GL_TEXTURE_2D, _fontTexture);760 //glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);761 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data_with_alpha);762 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);763 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);764 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);765 766 free(data_with_alpha);767 768 buildFont();769 return (glGetError()==0);770 771 error:772 fclose(f);773 return false;774 }775 776 void777 VolumeRenderer::drawLabel(Volume* vol)778 {779 //glEnable(GL_TEXTURE_2D);780 glDisable(GL_TEXTURE_2D);781 glEnable(GL_DEPTH_TEST);782 783 //x784 glColor3f(0.5, 0.5, 0.5);785 786 int length = vol->label[0].size();787 glPushMatrix();788 789 glTranslatef(.5 * vol->aspectRatioWidth,790 vol->aspectRatioHeight,791 -0.1 * vol->aspectRatioDepth);792 glRotatef(180, 0, 0, 1);793 glRotatef(90, 1, 0, 0);794 795 glScalef(0.0008, 0.0008, 0.0008);796 for (int i = 0; i < length; i++) {797 glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[0].c_str()[i]);798 glTranslatef(0.04, 0., 0.);799 }800 glPopMatrix();801 802 //y803 length = vol->label[1].size();804 glPushMatrix();805 glTranslatef(vol->aspectRatioWidth,806 0.5*vol->aspectRatioHeight,807 -0.1*vol->aspectRatioDepth);808 glRotatef(90, 0, 1, 0);809 glRotatef(90, 0, 0, 1);810 811 glScalef(0.0008, 0.0008, 0.0008);812 for (int i = 0; i < length; i++) {813 glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[1].c_str()[i]);814 glTranslatef(0.04, 0., 0.);815 }816 glPopMatrix();817 818 //z819 length = vol->label[2].size();820 glPushMatrix();821 glTranslatef(0.,822 1. * vol->aspectRatioHeight,823 0.5 * vol->aspectRatioDepth);824 glRotatef(90, 0, 1, 0);825 826 glScalef(0.0008, 0.0008, 0.0008);827 for (int i = 0; i < length; i++) {828 glutStrokeCharacter(GLUT_STROKE_ROMAN, vol->label[2].c_str()[i]);829 glTranslatef(0.04, 0., 0.);830 }831 glPopMatrix();832 833 glDisable(GL_TEXTURE_2D);834 }835 836 void837 VolumeRenderer::buildFont()838 {839 GLfloat cx, cy; /* the character coordinates in our texture */840 _fontBase = glGenLists(256);841 glBindTexture(GL_TEXTURE_2D, _fontTexture);842 for (int loop = 0; loop < 256; loop++) {843 cx = (float) (loop % 16) / 16.0f;844 cy = (float) (loop / 16) / 16.0f;845 glNewList(_fontBase + loop, GL_COMPILE);846 glBegin(GL_QUADS);847 glTexCoord2f(cx, 1 - cy - 0.0625f);848 glVertex3f(0, 0, 0);849 glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f);850 glVertex3f(0.04, 0, 0);851 glTexCoord2f(cx + 0.0625f, 1 - cy);852 glVertex3f(0.04, 0.04, 0);853 glTexCoord2f(cx, 1 - cy);854 glVertex3f(0, 0.04, 0);855 glEnd();856 glTranslated(0.04, 0, 0);857 glEndList();858 }859 }860 861 void862 VolumeRenderer::glPrint(char* string, int set)863 {864 if (set > 1) {865 set = 1;866 }867 glBindTexture(GL_TEXTURE_2D, _fontTexture);868 glListBase(_fontBase - 32 + (128 * set));869 glCallLists(strlen(string), GL_BYTE, string);870 }
Note: See TracChangeset
for help on using the changeset viewer.