ファイル情報

Rev. 600
サイズ 18,806 バイト
日時 2012-05-21 00:49:03
作者 rishitani
ログメッセージ

(メッセージはありません)

内容

  1. // -*-Mode: C++;-*-
  2. //
  3. // Generate/Render a mesh contour of ScalarObject
  4. //
  5. #include <common.h>
  6. #include "GLSLMapVolRenderer.hpp"
  7. #include "DensityMap.hpp"
  8. #include <qsys/ScrEventManager.hpp>
  9. #include <qsys/ViewEvent.hpp>
  10. #include <qsys/View.hpp>
  11. #include <qsys/Scene.hpp>
  12. #define CHK_GLERROR(MSG)\
  13. { \
  14. GLenum errc; \
  15. errc = glGetError(); \
  16. if (errc!=GL_NO_ERROR) \
  17. MB_DPRINTLN("%s GLError(%d): %s", MSG, errc, gluErrorString(errc)); \
  18. }
  19. using namespace xtal;
  20. using qlib::Matrix4D;
  21. using qlib::Matrix3D;
  22. using qsys::ScrEventManager;
  23. // default constructor
  24. GLSLMapVolRenderer::GLSLMapVolRenderer()
  25. : super_t()
  26. {
  27. // m_nBufSize = 100;
  28. m_bPBC = false;
  29. m_bAutoUpdate = true;
  30. m_pPO = NULL;
  31. m_nMapTexID = 0;
  32. m_nXfunTexID = 0;
  33. m_bMapTexOK = false;
  34. }
  35. // destructor
  36. GLSLMapVolRenderer::~GLSLMapVolRenderer()
  37. {
  38. // for safety, remove from event manager is needed here...
  39. ScrEventManager *pSEM = ScrEventManager::getInstance();
  40. pSEM->removeViewListener(this);
  41. }
  42. /////////////////////////////////
  43. const char *GLSLMapVolRenderer::getTypeName() const
  44. {
  45. return "gpu_mapvol";
  46. }
  47. double GLSLMapVolRenderer::getMaxExtent() const
  48. {
  49. GLSLMapVolRenderer *pthis = const_cast<GLSLMapVolRenderer *>(this);
  50. ScalarObject *pMap = (ScalarObject *) pthis->getClientObj().get();
  51. const double xmax = 100 * pMap->getColGridSize() / 2.0;
  52. const double ymax = 100 * pMap->getRowGridSize() / 2.0;
  53. const double zmax = 100 * pMap->getSecGridSize() / 2.0;
  54. return qlib::min(xmax, qlib::min(ymax, zmax));
  55. }
  56. void GLSLMapVolRenderer::setSceneID(qlib::uid_t nid)
  57. {
  58. super_t::setSceneID(nid);
  59. if (nid==qlib::invalid_uid)
  60. return;
  61. ScrEventManager *pSEM = ScrEventManager::getInstance();
  62. pSEM->addViewListener(nid, this);
  63. initShader();
  64. }
  65. qlib::uid_t GLSLMapVolRenderer::detachObj()
  66. {
  67. ScrEventManager *pSEM = ScrEventManager::getInstance();
  68. pSEM->removeViewListener(this);
  69. return super_t::detachObj();
  70. }
  71. void GLSLMapVolRenderer::viewChanged(qsys::ViewEvent &ev)
  72. {
  73. const int nType = ev.getType();
  74. if (nType!=qsys::ViewEvent::VWE_PROPCHG &&
  75. nType!=qsys::ViewEvent::VWE_PROPCHG_DRG)
  76. return;
  77. if (!m_bAutoUpdate && !m_bDragUpdate)
  78. return;
  79. if (!ev.getDescr().equals("center"))
  80. return;
  81. qsys::View *pView = ev.getTargetPtr();
  82. if (pView==NULL)
  83. return;
  84. Vector4D c = pView->getViewCenter();
  85. if (m_bDragUpdate) {
  86. if (nType==qsys::ViewEvent::VWE_PROPCHG ||
  87. nType==qsys::ViewEvent::VWE_PROPCHG_DRG) {
  88. setCenter(c);
  89. setDefaultPropFlag("center", false);
  90. }
  91. return;
  92. }
  93. if (m_bAutoUpdate) {
  94. if (nType==qsys::ViewEvent::VWE_PROPCHG) {
  95. setCenter(c);
  96. setDefaultPropFlag("center", false);
  97. }
  98. return;
  99. }
  100. return;
  101. }
  102. //////////////////////////////////////////////////////////////////
  103. void GLSLMapVolRenderer::initShader()
  104. {
  105. if (!qsys::View::hasVBO() || !qsys::View::hasVS()) {
  106. LOG_DPRINTLN("GPUMapMesh> ERROR: OpenGL GPU shading not supported.");
  107. MB_THROW(qlib::RuntimeException, "OpenGL GPU shading not supported");
  108. return;
  109. }
  110. // get GL context
  111. sysdep::OglDisplayContext *pOglDC = NULL;
  112. qsys::ScenePtr pScene = getScene();
  113. qsys::Scene::ViewIter vi = pScene->beginView();
  114. qsys::Scene::ViewIter vie = pScene->endView();
  115. for (; vi!=vie; ++vi) {
  116. qsys::ViewPtr pView = vi->second;
  117. gfx::DisplayContext *pDC = pView->getDisplayContext();
  118. pDC->setCurrent();
  119. pOglDC = dynamic_cast<sysdep::OglDisplayContext *>(pDC);
  120. if (pOglDC!=NULL)
  121. break;
  122. }
  123. if (pOglDC==NULL) {
  124. LOG_DPRINTLN("GPUMapMesh> ERROR: OpenGL GPU shading not supported.");
  125. MB_THROW(qlib::RuntimeException, "OpenGL GPU shading not supported");
  126. return;
  127. }
  128. // setup shaders
  129. if (m_pPO==NULL) {
  130. m_pPO = pOglDC->getProgramObject("gpu_mapvol");
  131. if (m_pPO==NULL) {
  132. m_pPO = pOglDC->createProgramObject("gpu_mapvol");
  133. if (m_pPO==NULL) {
  134. LOG_DPRINTLN("GPUMapMesh> ERROR: cannot create progobj (shader may not be supported).");
  135. return;
  136. }
  137. m_pPO->loadShader("vert",
  138. "%%CONFDIR%%/data/shaders/mapvol_vertex.glsl",
  139. GL_VERTEX_SHADER);
  140. m_pPO->loadShader("frag",
  141. "%%CONFDIR%%/data/shaders/mapvol_frag.glsl",
  142. GL_FRAGMENT_SHADER);
  143. m_pPO->link();
  144. m_pPO->enable();
  145. // initialize const values
  146. m_pPO->disable();
  147. }
  148. }
  149. LOG_DPRINTLN("Init MapVol shader OK.");
  150. // glGenBuffersARB(1, &m_nVBOID);
  151. // setup texture (map 3D tex)
  152. glGenTextures(1, &m_nMapTexID);
  153. glActiveTexture(GL_TEXTURE0);
  154. glEnable(GL_TEXTURE_3D);
  155. glBindTexture(GL_TEXTURE_3D, m_nMapTexID);
  156. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  157. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  158. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  159. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  160. glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  161. glBindTexture(GL_TEXTURE_3D, 0);
  162. glDisable(GL_TEXTURE_3D);
  163. // setup texture (xfer function 1D tex; unit 1)
  164. glGenTextures(1, &m_nXfunTexID);
  165. glActiveTexture(GL_TEXTURE1);
  166. glEnable(GL_TEXTURE_1D);
  167. glBindTexture(GL_TEXTURE_1D, m_nXfunTexID);
  168. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  169. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  170. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  171. glBindTexture(GL_TEXTURE_1D, 0);
  172. glDisable(GL_TEXTURE_1D);
  173. glActiveTexture(GL_TEXTURE0);
  174. }
  175. void GLSLMapVolRenderer::unloading()
  176. {
  177. // delete texture
  178. glDeleteTextures(1, &m_nMapTexID);
  179. glDeleteTextures(1, &m_nXfunTexID);
  180. // glDeleteBuffersARB(1, &m_nVBOID);
  181. // PO will be reused
  182. m_pPO = NULL;
  183. super_t::unloading();
  184. }
  185. void GLSLMapVolRenderer::invalidateDisplayCache()
  186. {
  187. m_bMapTexOK = false;
  188. super_t::invalidateDisplayCache();
  189. }
  190. /////////////////////////////////
  191. void GLSLMapVolRenderer::make3DTexMap(ScalarObject *pMap, DensityMap *pXtal)
  192. {
  193. if (m_pPO==NULL) {
  194. MB_DPRINTLN("MapVol> Error; shader not initialized!!");
  195. return;
  196. }
  197. const Vector4D cent = getCenter();
  198. const double extent = getExtent();
  199. //
  200. // col,row,sec
  201. //
  202. Vector4D vmin(cent.x()-extent, cent.y()-extent, cent.z()-extent);
  203. Vector4D vmax(cent.x()+extent, cent.y()+extent, cent.z()+extent);
  204. //
  205. // get origin / translate the origin to (0,0,0)
  206. //
  207. vmin -= pMap->getOrigin();
  208. vmax -= pMap->getOrigin();
  209. if (pXtal!=NULL) {
  210. const CrystalInfo &xt = pXtal->getXtalInfo();
  211. xt.orthToFrac(vmin);
  212. xt.orthToFrac(vmax);
  213. // check PBC
  214. m_bPBC = false;
  215. const double dimx = pMap->getColGridSize()*pMap->getColNo();
  216. const double dimy = pMap->getRowGridSize()*pMap->getRowNo();
  217. const double dimz = pMap->getSecGridSize()*pMap->getSecNo();
  218. const double cea = xt.a();
  219. const double ceb = xt.b();
  220. const double cec = xt.c();
  221. if (qlib::isNear4(dimx, cea) &&
  222. qlib::isNear4(dimy, ceb) &&
  223. qlib::isNear4(dimz, cec))
  224. m_bPBC = true;
  225. }
  226. if (pXtal!=NULL) {
  227. vmin.x() *= pXtal->getColInterval();
  228. vmin.y() *= pXtal->getRowInterval();
  229. vmin.z() *= pXtal->getSecInterval();
  230. vmax.x() *= pXtal->getColInterval();
  231. vmax.y() *= pXtal->getRowInterval();
  232. vmax.z() *= pXtal->getSecInterval();
  233. }
  234. else {
  235. vmin.x() /= pMap->getColGridSize();
  236. vmin.y() /= pMap->getRowGridSize();
  237. vmin.z() /= pMap->getSecGridSize();
  238. vmax.x() /= pMap->getColGridSize();
  239. vmax.y() /= pMap->getRowGridSize();
  240. vmax.z() /= pMap->getSecGridSize();
  241. }
  242. m_dgrid = qlib::min(pMap->getColGridSize(),
  243. qlib::min(pMap->getRowGridSize(),
  244. pMap->getSecGridSize()));
  245. if (!m_bPBC) {
  246. // limit XYZ in the available region of map
  247. vmin.x() = floor(qlib::max<double>(vmin.x(), pMap->getStartCol()));
  248. vmin.y() = floor(qlib::max<double>(vmin.y(), pMap->getStartRow()));
  249. vmin.z() = floor(qlib::max<double>(vmin.z(), pMap->getStartSec()));
  250. vmax.x() = floor(qlib::min<double>(vmax.x(), pMap->getStartCol()+pMap->getColNo()));
  251. vmax.y() = floor(qlib::min<double>(vmax.y(), pMap->getStartRow()+pMap->getRowNo()));
  252. vmax.z() = floor(qlib::min<double>(vmax.z(), pMap->getStartSec()+pMap->getSecNo()));
  253. }
  254. m_nMapColNo = pMap->getColNo();
  255. m_nMapRowNo = pMap->getRowNo();
  256. m_nMapSecNo = pMap->getSecNo();
  257. Vector4D vwi;
  258. vwi.x() = vmax.x() - vmin.x();
  259. vwi.y() = vmax.y() - vmin.y();
  260. vwi.z() = vmax.z() - vmin.z();
  261. m_nStCol = int(vmin.x());
  262. m_nStRow = int(vmin.y());
  263. m_nStSec = int(vmin.z());
  264. int stcol = m_nStCol - pMap->getStartCol();
  265. int strow = m_nStRow - pMap->getStartRow();
  266. int stsec = m_nStSec - pMap->getStartSec();
  267. int ncol = int(vmax.x() - vmin.x());
  268. int nrow = int(vmax.y() - vmin.y());
  269. int nsec = int(vmax.z() - vmin.z());
  270. bool bReuse;
  271. if (qlib::abs(m_maptmp.cols()-ncol)<1 &&
  272. qlib::abs(m_maptmp.rows()-nrow)<1 &&
  273. qlib::abs(m_maptmp.secs()-nsec)<1) {
  274. MB_DPRINTLN("reuse texture");
  275. ncol = m_maptmp.cols();
  276. nrow = m_maptmp.rows();
  277. nsec = m_maptmp.secs();
  278. bReuse = true;
  279. }
  280. else {
  281. m_maptmp.resize(ncol, nrow, nsec);
  282. bReuse = false;
  283. }
  284. m_nActCol = ncol;// = 8;
  285. m_nActRow = nrow;// = 8;
  286. m_nActSec = nsec;// = 8;
  287. MB_DPRINT("ncol: %d\n", ncol);
  288. MB_DPRINT("nrow: %d\n", nrow);
  289. MB_DPRINT("nsec: %d\n", nsec);
  290. //
  291. // Generate texture map
  292. //
  293. int i,j,k;
  294. // unsigned int s0,s1;
  295. for (k=0; k<nsec; k++)
  296. for (j=0; j<nrow; j++)
  297. for (i=0; i<ncol; i++){
  298. m_maptmp.at(i,j,k) = getMap(pMap, stcol+i, strow+j, stsec+k);
  299. //m_maptmp.at(i,j,k) = MapTmp::value_type( float(i)/float(ncol)*255.0 );
  300. //dataField[i + ncol*(j + nrow*k)] = float(i)/float(ncol);
  301. }
  302. glActiveTexture(GL_TEXTURE0);
  303. glEnable(GL_TEXTURE_3D);
  304. glBindTexture(GL_TEXTURE_3D, m_nMapTexID);
  305. glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
  306. glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
  307. if (!bReuse) {
  308. glTexImage3D(GL_TEXTURE_3D, 0,
  309. GL_LUMINANCE,
  310. ncol, nrow, nsec, 0,
  311. GL_LUMINANCE, GL_UNSIGNED_BYTE, m_maptmp.data());
  312. }
  313. else {
  314. glTexSubImage3D(GL_TEXTURE_3D,
  315. 0, // LOD
  316. 0, 0, 0, // offset
  317. ncol, nrow, nsec, // size
  318. GL_LUMINANCE, // format
  319. GL_UNSIGNED_BYTE, // type
  320. m_maptmp.data());
  321. }
  322. {
  323. //
  324. // calculate the contour level
  325. //
  326. const double siglevel = getSigLevel();
  327. const double level = pMap->getRmsdDensity() * siglevel;
  328. double lvtmp = floor( (level-pMap->getLevelBase()) / pMap->getLevelStep());
  329. unsigned int lv = (unsigned int)lvtmp;
  330. if (lvtmp<0) lv = 0;
  331. if (lvtmp>0xFF) lv = 0xFF;
  332. MB_DPRINTLN("set isolevel=%d", lv);
  333. m_isolevel = lv;
  334. }
  335. glBindTexture(GL_TEXTURE_3D, 0);
  336. genXfurFunMap();
  337. m_pPO->enable();
  338. // connect texture0 to the dataFieldTex uniform variable
  339. m_pPO->setUniform("dataFieldTex", 0);
  340. CHK_GLERROR("setUniform dataFieldTex");
  341. // connect texture1 to the xferFunTex uniform variable
  342. m_pPO->setUniform("xferFunTex", 1);
  343. CHK_GLERROR("setUniform xferFunTex");
  344. m_pPO->disable();
  345. MB_DPRINTLN("make3D texture OK.");
  346. m_bMapTexOK = true;
  347. }
  348. void GLSLMapVolRenderer::genXfurFunMap()
  349. {
  350. CHK_GLERROR("(reset)");
  351. glActiveTexture(GL_TEXTURE1);
  352. glEnable(GL_TEXTURE_1D);
  353. glBindTexture(GL_TEXTURE_1D, m_nXfunTexID);
  354. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  355. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  356. quint8 *pData = new quint8[4*256];
  357. for (int i=0; i<256; ++i) {
  358. double d = double(i)/255.0;
  359. d = pow(d, 1.5);
  360. double dr, dg, db;
  361. gfx::AbstractColor::HSBtoRGB(d, 1.0, 1.0, dr, dg, db);
  362. Vector4D col(dr, dg, db);
  363. if (i>m_isolevel)
  364. col.w() = d*2.0;
  365. else
  366. col.w() = d*0.1;
  367. pData[i*4 + 0] = (quint8) qlib::trunc(int(col.x()*255.0), 0, 255);
  368. pData[i*4 + 1] = (quint8) qlib::trunc(int(col.y()*255.0), 0, 255);
  369. pData[i*4 + 2] = (quint8) qlib::trunc(int(col.z()*255.0), 0, 255);
  370. pData[i*4 + 3] = (quint8) qlib::trunc(int(col.w()*255.0), 0, 255);
  371. }
  372. glTexImage1D(GL_TEXTURE_1D, 0,
  373. GL_RGBA,
  374. 256, 0,
  375. GL_RGBA, GL_UNSIGNED_BYTE, pData);
  376. CHK_GLERROR("glTexImage1D xferFunTex");
  377. glBindTexture(GL_TEXTURE_1D, 0);
  378. glDisable(GL_TEXTURE_1D);
  379. glActiveTexture(GL_TEXTURE0);
  380. }
  381. void GLSLMapVolRenderer::display(DisplayContext *pdc)
  382. {
  383. ScalarObject *pMap = static_cast<ScalarObject *>(getClientObj().get());
  384. DensityMap *pXtal = dynamic_cast<DensityMap *>(pMap);
  385. if (!m_bMapTexOK) {
  386. if (!pMap)
  387. return;
  388. make3DTexMap(pMap, pXtal);
  389. }
  390. pdc->color(getColor());
  391. pdc->setLineWidth(1.0);
  392. pdc->setLighting(false);
  393. pdc->pushMatrix();
  394. if (pXtal==NULL)
  395. pdc->translate(pMap->getOrigin());
  396. if (pXtal!=NULL) {
  397. Matrix3D orthmat = pXtal->getXtalInfo().getOrthMat();
  398. pdc->multMatrix(Matrix4D(orthmat));
  399. }
  400. Vector4D vtmp;
  401. if (pXtal!=NULL)
  402. vtmp = Vector4D(1.0/double(pXtal->getColInterval()),
  403. 1.0/double(pXtal->getRowInterval()),
  404. 1.0/double(pXtal->getSecInterval()));
  405. else
  406. vtmp = Vector4D(pMap->getColGridSize(),
  407. pMap->getRowGridSize(),
  408. pMap->getSecGridSize());
  409. pdc->scale(vtmp);
  410. vtmp = Vector4D(m_nStCol, m_nStRow, m_nStSec);
  411. pdc->translate(vtmp);
  412. // (0~nActCol) <==> (0~1)
  413. vtmp = Vector4D(m_nActCol, m_nActRow, m_nActSec);
  414. pdc->scale(vtmp);
  415. //renderCPU(pdc);
  416. renderGPU(pdc);
  417. pdc->popMatrix();
  418. }
  419. void calcBoundingBox( float& x_min, float& x_max,
  420. float& y_min, float& y_max,
  421. float& z_min, float& z_max )
  422. {
  423. // current modelview matrix (local -> camera)
  424. float m[ 16 ];
  425. glGetFloatv( GL_MODELVIEW_MATRIX, m );
  426. // print
  427. #if 0
  428. for ( int i = 0; i < 4; i++ ) {
  429. for ( int j = 0; j < 4; j++ ) {
  430. cout << m[ i + j * 4 ] << " ";
  431. }
  432. cout << endl;
  433. }
  434. #endif
  435. // find x_min and x_max
  436. x_min = m[ 3 * 4 + 0 ];
  437. x_max = m[ 3 * 4 + 0 ];
  438. for ( int i = 0; i < 3; i++ ) {
  439. if ( m[ i * 4 + 0 ] < 0 ) {
  440. x_min += m[ i * 4 + 0 ];
  441. } else {
  442. x_max += m[ i * 4 + 0 ];
  443. }
  444. }
  445. // find y_min and y_max
  446. y_min = m[ 3 * 4 + 1 ];
  447. y_max = m[ 3 * 4 + 1 ];
  448. for ( int i = 0; i < 3; i++ ) {
  449. if ( m[ i * 4 + 1 ] < 0 ) {
  450. y_min += m[ i * 4 + 1 ];
  451. } else {
  452. y_max += m[ i * 4 + 1 ];
  453. }
  454. }
  455. // find z_min and z_max
  456. z_min = m[ 3 * 4 + 2 ];
  457. z_max = m[ 3 * 4 + 2 ];
  458. for ( int i = 0; i < 3; i++ ) {
  459. if ( m[ i * 4 + 2 ] < 0 ) {
  460. z_min += m[ i * 4 + 2 ];
  461. } else {
  462. z_max += m[ i * 4 + 2 ];
  463. }
  464. }
  465. /*
  466. // near and far must be set before exec
  467. if ( z_max > -near_clipping_length ) {
  468. z_max = -near_clipping_length;
  469. }
  470. if ( z_min < -far_clipping_length ) {
  471. z_min = -far_clipping_length;
  472. }
  473. */
  474. // -far_clipping_length < z_min < z_max < -near_clipping_length ( < 0 )
  475. }
  476. static GLdouble clip_plane[ 6 ][ 4 ] = {
  477. { 1, 0, 0, 0 },
  478. { -1, 0, 0, 1 },
  479. { 0, 1, 0, 0 },
  480. { 0, -1, 0, 1 },
  481. { 0, 0, 1, 0 },
  482. { 0, 0, -1, 1 }
  483. };
  484. void enableClipPlane()
  485. {
  486. for ( int i = 0; i < 6; i++ ) {
  487. glClipPlane( GL_CLIP_PLANE0 + i, clip_plane[i] );
  488. glEnable( GL_CLIP_PLANE0 + i );
  489. }
  490. }
  491. void disableClipPlane()
  492. {
  493. for ( int i = 0; i < 6; i++ ) {
  494. glDisable( GL_CLIP_PLANE0 + i );
  495. }
  496. }
  497. void GLSLMapVolRenderer::renderGPU(DisplayContext *pdc)
  498. {
  499. if (m_pPO==NULL) {
  500. MB_DPRINTLN("MapVol> Error; shader not initialized!!");
  501. return;
  502. }
  503. ScalarObject *pMap = static_cast<ScalarObject *>(getClientObj().get());
  504. DensityMap *pXtal = dynamic_cast<DensityMap *>(pMap);
  505. int i,j,k;
  506. int ncol = m_nActCol;
  507. int nrow = m_nActRow;
  508. int nsec = m_nActSec;
  509. //////////////////////////////////////////
  510. // check bounding box of volume in camera coordinate
  511. float x_min, x_max, y_min, y_max, z_min, z_max;
  512. calcBoundingBox( x_min, x_max, y_min, y_max, z_min, z_max );
  513. // Set clipping planes to limit drawing into the target box
  514. enableClipPlane();
  515. const float zrange = z_max - z_min;
  516. const int nLayers = ::ceil(zrange/m_dgrid*2.0);
  517. float thickness = zrange / nLayers;
  518. MB_DPRINTLN("nlayer: %d, thickness: %f", nLayers, thickness);
  519. glDisable( GL_CULL_FACE );
  520. glDisable( GL_LIGHTING );
  521. //glEnable( GL_BLEND );
  522. //glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  523. //glActiveTexture( GL_TEXTURE0 );
  524. //glEnable( GL_TEXTURE_3D );
  525. //glBindTexture( GL_TEXTURE_3D, texture_data );
  526. // calc inverse modelview matrix
  527. // because glsl's ModelViewMatrixInverse is sometimes incorrect...
  528. float m[ 16 ];
  529. float mi[ 16 ];
  530. float d;
  531. glGetFloatv( GL_MODELVIEW_MATRIX, m );
  532. d = m[ 0 ] * ( m[ 5 ] * m[ 10 ] - m[ 9 ] * m[ 6 ] )
  533. + m[ 4 ] * ( m[ 9 ] * m[ 2 ] - m[ 1 ] * m[ 10 ] )
  534. + m[ 8 ] * ( m[ 1 ] * m[ 6 ] - m[ 5 ] * m[ 2 ] );
  535. mi[ 0 ] = m[ 5 ] * m[ 10 ] - m[ 9 ] * m[ 6 ];
  536. mi[ 1 ] = -( m[ 1 ] * m[ 10 ] - m[ 9 ] * m[ 2 ] );
  537. mi[ 2 ] = m[ 1 ] * m[ 6 ] - m[ 5 ] * m[ 2 ];
  538. mi[ 3 ] = 0.0f;
  539. mi[ 4 ] = -( m[ 4 ] * m[ 10 ] - m[ 8 ] * m[ 6 ] );
  540. mi[ 5 ] = m[ 0 ] * m[ 10 ] - m[ 8 ] * m[ 2 ];
  541. mi[ 6 ] = -( m[ 0 ] * m[ 6 ] - m[ 4 ] * m[ 2 ] );
  542. mi[ 7 ] = 0.0f;
  543. mi[ 8 ] = m[ 4 ] * m[ 9 ] - m[ 8 ] * m[ 5 ];
  544. mi[ 9 ] = -( m[ 0 ] * m[ 9 ] - m[ 8 ] * m[ 1 ] );
  545. mi[ 10 ] = m[ 0 ] * m[ 5 ] - m[ 4 ] * m[ 1 ];
  546. mi[ 11 ] = 0.0f;
  547. for ( int i = 0; i < 12; i++ ) {
  548. mi[ i ] /= d;
  549. }
  550. mi[ 12 ] = -( mi[ 0 ] * m[ 12 ] + mi[ 4 ] * m[ 13 ] + mi[ 8 ] * m[ 14 ] );
  551. mi[ 13 ] = -( mi[ 1 ] * m[ 12 ] + mi[ 5 ] * m[ 13 ] + mi[ 9 ] * m[ 14 ] );
  552. mi[ 14 ] = -( mi[ 2 ] * m[ 12 ] + mi[ 6 ] * m[ 13 ] + mi[ 10 ] * m[ 14 ] );
  553. mi[ 15 ] = 1.0f;
  554. //////////////////////////////
  555. glActiveTexture(GL_TEXTURE0);
  556. //glEnable(GL_TEXTURE_3D);
  557. glBindTexture(GL_TEXTURE_3D, m_nMapTexID);
  558. glActiveTexture(GL_TEXTURE1);
  559. glBindTexture(GL_TEXTURE_1D, m_nXfunTexID);
  560. if (m_pPO)
  561. m_pPO->enable();
  562. // set variables
  563. // m_pPO->setUniform( "dataFieldTex", 0 );
  564. m_pPO->setUniformF( "isolevel", float(m_isolevel)/255.0 );
  565. m_pPO->setUniformF( "thickness", thickness );
  566. m_pPO->setMatrix4fv( "modelview_matrix_inverse", 1, GL_FALSE, mi );
  567. // draw proxy polygons
  568. glPushMatrix();
  569. glBegin( GL_QUADS );
  570. for (int i=0; i<nLayers; i++) {
  571. float z = z_min + thickness * i;
  572. glVertex3f( x_min, y_min, z );
  573. glVertex3f( x_max, y_min, z );
  574. glVertex3f( x_max, y_max, z );
  575. glVertex3f( x_min, y_max, z );
  576. }
  577. glEnd();
  578. glPopMatrix();
  579. glActiveTexture( GL_TEXTURE0 );
  580. glDisable( GL_TEXTURE_3D );
  581. glBindTexture(GL_TEXTURE_3D, 0);
  582. if (m_pPO)
  583. m_pPO->disable();
  584. disableClipPlane();
  585. }
旧リポジトリブラウザで表示