/Lynx6/RoboticArmLynx/RenderUtils.cpp

http://buglearn.googlecode.com/ · C++ · 160 lines · 139 code · 20 blank · 1 comment · 11 complexity · 8b6c83b201fef6b57fbe32eef454de2e MD5 · raw file

  1. #include "RenderUtils.h"
  2. int delta = 0;
  3. void *font = GLUT_BITMAP_TIMES_ROMAN_24;
  4. char s[1000];
  5. char buff1[8];
  6. void SaveAllBuffers(int w, int h, int num) {
  7. int thesize = sizeof(char) * w * h * 3;
  8. char* pixels = (char*)malloc( thesize );
  9. CvMat* img = cvCreateMat(h,w,CV_8UC3);
  10. int initWindowID = glutGetWindow();
  11. for( int idx = 0; idx < num; idx++ ) {
  12. _itoa_s(idx, buff1,8, 10 );
  13. string end(buff1);
  14. glutSetWindow(idx+1);
  15. glReadPixels( 0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels );
  16. int k = 0;
  17. for( int i = h-1; i >= 0; i-- ) {
  18. for( int j = 0; j < w; j++ ) {
  19. uchar r = (uchar) pixels[k];
  20. uchar g = (uchar) pixels[k+1];
  21. uchar b = (uchar) pixels[k+2];
  22. CvScalar scalar = cvScalar(b,g,r);
  23. cvSet2D(img,i,j,scalar);
  24. k+=3;
  25. }
  26. }
  27. string name = "ReadBufferWindow";
  28. name.append(end);
  29. string fname = ".png";
  30. name.append(fname);
  31. cvSaveImage( name.c_str(), img );
  32. glFinish();
  33. int breakhere = 1;
  34. }
  35. cvReleaseMat( &img );
  36. free( pixels );
  37. glutSetWindow(initWindowID);
  38. }
  39. void TestReadBuffer(int h, int w)
  40. {
  41. int thesize = sizeof(char) * w * h * 3;
  42. char* pixels = (char*)malloc( thesize );
  43. glReadPixels( 0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels );
  44. IplImage* img = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );
  45. cvZero(img);
  46. int step = img->widthStep/sizeof(uchar);
  47. int channels = img->nChannels;
  48. uchar* data = (uchar *)img->imageData;
  49. for( int k = 0; k < thesize; k+=3 ) {
  50. int i = h - k / 3 / h - 1;
  51. int j = (k / 3) % h;
  52. int r = (int) pixels[k];
  53. int g = (int) pixels[k+1];
  54. int b = (int) pixels[k+2];
  55. data[i*step+j*channels+0]=b; // B
  56. data[i*step+j*channels+1]=g; // G
  57. data[i*step+j*channels+2]=r; // R
  58. int breakehre = 1;
  59. }
  60. cvSaveImage( "TestReadBuffer.png", img );
  61. cvReleaseImage( &img );
  62. free( pixels );
  63. int breakhere = 1;
  64. }
  65. void DrawAxes(vector<float>& size)
  66. {
  67. glPushMatrix();
  68. // Draw and Label Axes
  69. glScalef(size[0],size[1],size[2]);
  70. glColor3f (1.0, 1.0, 1.0);
  71. glBegin(GL_LINES);
  72. glVertex3f(-1.8f, 0.0f,0.0f);
  73. glVertex3f(1.8f, 0.0f,0.0f);
  74. glEnd();
  75. glBegin(GL_LINES);
  76. glVertex3f(0.0f, -1.8f,0.0f);
  77. glVertex3f(0.0f, 1.8f,0.0f);
  78. glEnd();
  79. glBegin(GL_LINES);
  80. glVertex3f(0.0f, 0.0f, -1.8f);
  81. glVertex3f(0.0f, 0.0f, 1.8f);
  82. glEnd();
  83. sprintf(s,"+z");
  84. outputCharacter(0.0f, 0.0f, 1.98f, s);
  85. sprintf(s,"-z");
  86. outputCharacter(0.0f, 0.0f, -1.98f, s);
  87. sprintf(s,"+x");
  88. outputCharacter(1.9f, 0.0f, 0.0f, s);
  89. sprintf(s,"-x");
  90. outputCharacter(-1.9f, 0.0f, 0.0f, s);
  91. sprintf(s,"+y");
  92. outputCharacter(0.0f, 1.9f, 0.0f, s);
  93. sprintf(s,"-y");
  94. outputCharacter(0.0f, -1.99f, 0.0f, s);
  95. glPopMatrix();
  96. }
  97. void DrawCalibrationPoints(const vector<float>& ptsxyz) {
  98. glPushMatrix();
  99. glColor3f (1.0, 0.0, 1.0);
  100. int len = ptsxyz.size();
  101. for( int i = 0; i < len; i+=3 ) {
  102. sprintf(s,"*");
  103. float ptx = ptsxyz[i];
  104. float pty = ptsxyz[i+1];
  105. float ptz = ptsxyz[i+2];
  106. outputCharacter(ptx, pty, ptz, s);
  107. }
  108. glPopMatrix();
  109. }
  110. void outputCharacter(float x, float y, float z, char *string) {
  111. int len, i;
  112. glRasterPos3f(x, y, z);
  113. len = (int) strlen(string);
  114. for (i = 0; i < len; i++) {
  115. glutBitmapCharacter(font, string[i]);
  116. }
  117. }
  118. char buff[32];
  119. char frm[8];
  120. IplImage* ReadImgByNum(const string& basename, const string& end, int iFrame, int numChars) {
  121. _itoa_s( iFrame, buff, 20 );
  122. int num = 0; // number of nonzero entries
  123. char val = buff[num];
  124. while( val != 0 && num < numChars) {
  125. num++;
  126. val = buff[num];
  127. }
  128. int idx = 0;
  129. for( int i = 0; i < numChars; i++ ) { frm[i] = '0'; }
  130. for( int i = numChars-num; i<numChars; i++) {
  131. frm[i] = buff[i-numChars+num];
  132. }
  133. string csFrame;
  134. csFrame.assign( frm, numChars );
  135. string filename = basename;
  136. filename.append(csFrame);
  137. filename.append(end);
  138. IplImage* img = cvLoadImage( filename.c_str() );
  139. return img;
  140. }