llfloaterimagepreview.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:27k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file llfloaterimagepreview.cpp
  3.  * @brief LLFloaterImagePreview class implementation
  4.  *
  5.  * $LicenseInfo:firstyear=2004&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2004-2010, Linden Research, Inc.
  8.  * 
  9.  * Second Life Viewer Source Code
  10.  * The source code in this file ("Source Code") is provided by Linden Lab
  11.  * to you under the terms of the GNU General Public License, version 2.0
  12.  * ("GPL"), unless you have obtained a separate licensing agreement
  13.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  14.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16.  * 
  17.  * There are special exceptions to the terms and conditions of the GPL as
  18.  * it is applied to this Source Code. View the full text of the exception
  19.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  20.  * online at
  21.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22.  * 
  23.  * By copying, modifying or distributing this software, you acknowledge
  24.  * that you have read and understood your obligations described above,
  25.  * and agree to abide by those obligations.
  26.  * 
  27.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29.  * COMPLETENESS OR PERFORMANCE.
  30.  * $/LicenseInfo$
  31.  */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llfloaterimagepreview.h"
  34. #include "llimagebmp.h"
  35. #include "llimagetga.h"
  36. #include "llimagejpeg.h"
  37. #include "llimagepng.h"
  38. #include "llagent.h"
  39. #include "llbutton.h"
  40. #include "llcombobox.h"
  41. #include "lldrawable.h"
  42. #include "lldrawpoolavatar.h"
  43. #include "llrender.h"
  44. #include "llface.h"
  45. #include "llfocusmgr.h"
  46. #include "lltextbox.h"
  47. #include "lltoolmgr.h"
  48. #include "llui.h"
  49. #include "llviewercamera.h"
  50. #include "llviewerwindow.h"
  51. #include "llviewerobjectlist.h"
  52. #include "llvoavatar.h"
  53. #include "pipeline.h"
  54. #include "lluictrlfactory.h"
  55. #include "llviewertexturelist.h"
  56. #include "llstring.h"
  57. const S32 PREVIEW_BORDER_WIDTH = 2;
  58. const S32 PREVIEW_RESIZE_HANDLE_SIZE = S32(RESIZE_HANDLE_WIDTH * OO_SQRT2) + PREVIEW_BORDER_WIDTH;
  59. const S32 PREVIEW_HPAD = PREVIEW_RESIZE_HANDLE_SIZE;
  60. const S32 PREF_BUTTON_HEIGHT = 16 + 7 + 16;
  61. const S32 PREVIEW_TEXTURE_HEIGHT = 300;
  62. //-----------------------------------------------------------------------------
  63. // LLFloaterImagePreview()
  64. //-----------------------------------------------------------------------------
  65. LLFloaterImagePreview::LLFloaterImagePreview(const std::string& filename) : 
  66. LLFloaterNameDesc(filename),
  67. mAvatarPreview(NULL),
  68. mSculptedPreview(NULL),
  69. mLastMouseX(0),
  70. mLastMouseY(0),
  71. mImagep(NULL)
  72. {
  73. loadImage(mFilenameAndPath);
  74. }
  75. //-----------------------------------------------------------------------------
  76. // postBuild()
  77. //-----------------------------------------------------------------------------
  78. BOOL LLFloaterImagePreview::postBuild()
  79. {
  80. if (!LLFloaterNameDesc::postBuild())
  81. {
  82. return FALSE;
  83. }
  84. LLCtrlSelectionInterface* iface = childGetSelectionInterface("clothing_type_combo");
  85. if (iface)
  86. {
  87. iface->selectFirstItem();
  88. }
  89. childSetCommitCallback("clothing_type_combo", onPreviewTypeCommit, this);
  90. mPreviewRect.set(PREVIEW_HPAD, 
  91. PREVIEW_TEXTURE_HEIGHT,
  92. getRect().getWidth() - PREVIEW_HPAD, 
  93. PREVIEW_HPAD + PREF_BUTTON_HEIGHT + PREVIEW_HPAD);
  94. mPreviewImageRect.set(0.f, 1.f, 1.f, 0.f);
  95. childHide("bad_image_text");
  96. if (mRawImagep.notNull() && gAgent.getRegion() != NULL)
  97. {
  98. mAvatarPreview = new LLImagePreviewAvatar(256, 256);
  99. mAvatarPreview->setPreviewTarget("mPelvis", "mUpperBodyMesh0", mRawImagep, 2.f, FALSE);
  100. mSculptedPreview = new LLImagePreviewSculpted(256, 256);
  101. mSculptedPreview->setPreviewTarget(mRawImagep, 2.0f);
  102. if (mRawImagep->getWidth() * mRawImagep->getHeight () <= LL_IMAGE_REZ_LOSSLESS_CUTOFF * LL_IMAGE_REZ_LOSSLESS_CUTOFF)
  103. childEnable("lossless_check");
  104. }
  105. else
  106. {
  107. mAvatarPreview = NULL;
  108. mSculptedPreview = NULL;
  109. childShow("bad_image_text");
  110. childDisable("clothing_type_combo");
  111. childDisable("ok_btn");
  112. }
  113. getChild<LLUICtrl>("ok_btn")->setCommitCallback(boost::bind(&LLFloaterNameDesc::onBtnOK, this));
  114. return TRUE;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // LLFloaterImagePreview()
  118. //-----------------------------------------------------------------------------
  119. LLFloaterImagePreview::~LLFloaterImagePreview()
  120. {
  121. clearAllPreviewTextures();
  122. mRawImagep = NULL;
  123. mImagep = NULL ;
  124. }
  125. //static 
  126. //-----------------------------------------------------------------------------
  127. // onPreviewTypeCommit()
  128. //-----------------------------------------------------------------------------
  129. void LLFloaterImagePreview::onPreviewTypeCommit(LLUICtrl* ctrl, void* userdata)
  130. {
  131. LLFloaterImagePreview *fp =(LLFloaterImagePreview *)userdata;
  132. if (!fp->mAvatarPreview || !fp->mSculptedPreview)
  133. {
  134. return;
  135. }
  136. S32 which_mode = 0;
  137. LLCtrlSelectionInterface* iface = fp->childGetSelectionInterface("clothing_type_combo");
  138. if (iface)
  139. {
  140. which_mode = iface->getFirstSelectedIndex();
  141. }
  142. switch(which_mode)
  143. {
  144. case 0:
  145. break;
  146. case 1:
  147. fp->mAvatarPreview->setPreviewTarget("mSkull", "mHairMesh0", fp->mRawImagep, 0.4f, FALSE);
  148. break;
  149. case 2:
  150. fp->mAvatarPreview->setPreviewTarget("mSkull", "mHeadMesh0", fp->mRawImagep, 0.4f, FALSE);
  151. break;
  152. case 3:
  153. fp->mAvatarPreview->setPreviewTarget("mChest", "mUpperBodyMesh0", fp->mRawImagep, 1.0f, FALSE);
  154. break;
  155. case 4:
  156. fp->mAvatarPreview->setPreviewTarget("mKneeLeft", "mLowerBodyMesh0", fp->mRawImagep, 1.2f, FALSE);
  157. break;
  158. case 5:
  159. fp->mAvatarPreview->setPreviewTarget("mSkull", "mHeadMesh0", fp->mRawImagep, 0.4f, TRUE);
  160. break;
  161. case 6:
  162. fp->mAvatarPreview->setPreviewTarget("mChest", "mUpperBodyMesh0", fp->mRawImagep, 1.2f, TRUE);
  163. break;
  164. case 7:
  165. fp->mAvatarPreview->setPreviewTarget("mKneeLeft", "mLowerBodyMesh0", fp->mRawImagep, 1.2f, TRUE);
  166. break;
  167. case 8:
  168. fp->mAvatarPreview->setPreviewTarget("mKneeLeft", "mSkirtMesh0", fp->mRawImagep, 1.3f, FALSE);
  169. break;
  170. case 9:
  171. fp->mSculptedPreview->setPreviewTarget(fp->mRawImagep, 2.0f);
  172. break;
  173. default:
  174. break;
  175. }
  176. fp->mAvatarPreview->refresh();
  177. fp->mSculptedPreview->refresh();
  178. }
  179. //-----------------------------------------------------------------------------
  180. // clearAllPreviewTextures()
  181. //-----------------------------------------------------------------------------
  182. void LLFloaterImagePreview::clearAllPreviewTextures()
  183. {
  184. if (mAvatarPreview)
  185. {
  186. mAvatarPreview->clearPreviewTexture("mHairMesh0");
  187. mAvatarPreview->clearPreviewTexture("mUpperBodyMesh0");
  188. mAvatarPreview->clearPreviewTexture("mLowerBodyMesh0");
  189. mAvatarPreview->clearPreviewTexture("mHeadMesh0");
  190. mAvatarPreview->clearPreviewTexture("mUpperBodyMesh0");
  191. mAvatarPreview->clearPreviewTexture("mLowerBodyMesh0");
  192. mAvatarPreview->clearPreviewTexture("mSkirtMesh0");
  193. }
  194. }
  195. //-----------------------------------------------------------------------------
  196. // draw()
  197. //-----------------------------------------------------------------------------
  198. void LLFloaterImagePreview::draw()
  199. {
  200. LLFloater::draw();
  201. LLRect r = getRect();
  202. if (mRawImagep.notNull())
  203. {
  204. LLCtrlSelectionInterface* iface = childGetSelectionInterface("clothing_type_combo");
  205. U32 selected = 0;
  206. if (iface)
  207. selected = iface->getFirstSelectedIndex();
  208. if (selected <= 0)
  209. {
  210. gl_rect_2d_checkerboard(mPreviewRect);
  211. LLGLDisable gls_alpha(GL_ALPHA_TEST);
  212. if(mImagep.notNull())
  213. {
  214. gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, mImagep->getTexName());
  215. }
  216. else
  217. {
  218. mImagep = LLViewerTextureManager::getLocalTexture(mRawImagep.get(), FALSE) ;
  219. gGL.getTexUnit(0)->unbind(mImagep->getTarget()) ;
  220. gGL.getTexUnit(0)->bindManual(LLTexUnit::TT_TEXTURE, mImagep->getTexName());
  221. stop_glerror();
  222. gGL.getTexUnit(0)->setTextureFilteringOption(LLTexUnit::TFO_BILINEAR);
  223. gGL.getTexUnit(0)->setTextureAddressMode(LLTexUnit::TAM_CLAMP);
  224. if (mAvatarPreview)
  225. {
  226. mAvatarPreview->setTexture(mImagep->getTexName());
  227. mSculptedPreview->setTexture(mImagep->getTexName());
  228. }
  229. }
  230. gGL.color3f(1.f, 1.f, 1.f);
  231. gGL.begin( LLRender::QUADS );
  232. {
  233. gGL.texCoord2f(mPreviewImageRect.mLeft, mPreviewImageRect.mTop);
  234. gGL.vertex2i(PREVIEW_HPAD, PREVIEW_TEXTURE_HEIGHT);
  235. gGL.texCoord2f(mPreviewImageRect.mLeft, mPreviewImageRect.mBottom);
  236. gGL.vertex2i(PREVIEW_HPAD, PREVIEW_HPAD + PREF_BUTTON_HEIGHT + PREVIEW_HPAD);
  237. gGL.texCoord2f(mPreviewImageRect.mRight, mPreviewImageRect.mBottom);
  238. gGL.vertex2i(r.getWidth() - PREVIEW_HPAD, PREVIEW_HPAD + PREF_BUTTON_HEIGHT + PREVIEW_HPAD);
  239. gGL.texCoord2f(mPreviewImageRect.mRight, mPreviewImageRect.mTop);
  240. gGL.vertex2i(r.getWidth() - PREVIEW_HPAD, PREVIEW_TEXTURE_HEIGHT);
  241. }
  242. gGL.end();
  243. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  244. stop_glerror();
  245. }
  246. else
  247. {
  248. if ((mAvatarPreview) && (mSculptedPreview))
  249. {
  250. gGL.color3f(1.f, 1.f, 1.f);
  251. if (selected == 9)
  252. {
  253. gGL.getTexUnit(0)->bind(mSculptedPreview);
  254. }
  255. else
  256. {
  257. gGL.getTexUnit(0)->bind(mAvatarPreview);
  258. }
  259. gGL.begin( LLRender::QUADS );
  260. {
  261. gGL.texCoord2f(0.f, 1.f);
  262. gGL.vertex2i(PREVIEW_HPAD, PREVIEW_TEXTURE_HEIGHT);
  263. gGL.texCoord2f(0.f, 0.f);
  264. gGL.vertex2i(PREVIEW_HPAD, PREVIEW_HPAD + PREF_BUTTON_HEIGHT + PREVIEW_HPAD);
  265. gGL.texCoord2f(1.f, 0.f);
  266. gGL.vertex2i(r.getWidth() - PREVIEW_HPAD, PREVIEW_HPAD + PREF_BUTTON_HEIGHT + PREVIEW_HPAD);
  267. gGL.texCoord2f(1.f, 1.f);
  268. gGL.vertex2i(r.getWidth() - PREVIEW_HPAD, PREVIEW_TEXTURE_HEIGHT);
  269. }
  270. gGL.end();
  271. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  272. }
  273. }
  274. }
  275. }
  276. //-----------------------------------------------------------------------------
  277. // loadImage()
  278. //-----------------------------------------------------------------------------
  279. bool LLFloaterImagePreview::loadImage(const std::string& src_filename)
  280. {
  281. std::string exten = gDirUtilp->getExtension(src_filename);
  282. U32 codec = IMG_CODEC_INVALID;
  283. std::string temp_str;
  284. if( exten == "bmp")
  285. {
  286. codec = IMG_CODEC_BMP;
  287. }
  288. else if( exten == "tga")
  289. {
  290. codec = IMG_CODEC_TGA;
  291. }
  292. else if( exten == "jpg" || exten == "jpeg")
  293. {
  294. codec = IMG_CODEC_JPEG;
  295. }
  296. else if( exten == "png" )
  297. {
  298. codec = IMG_CODEC_PNG;
  299. }
  300. LLPointer<LLImageRaw> raw_image = new LLImageRaw;
  301. switch (codec)
  302. {
  303. case IMG_CODEC_BMP:
  304. {
  305. LLPointer<LLImageBMP> bmp_image = new LLImageBMP;
  306. if (!bmp_image->load(src_filename))
  307. {
  308. return false;
  309. }
  310. if (!bmp_image->decode(raw_image, 0.0f))
  311. {
  312. return false;
  313. }
  314. }
  315. break;
  316. case IMG_CODEC_TGA:
  317. {
  318. LLPointer<LLImageTGA> tga_image = new LLImageTGA;
  319. if (!tga_image->load(src_filename))
  320. {
  321. return false;
  322. }
  323. if (!tga_image->decode(raw_image))
  324. {
  325. return false;
  326. }
  327. if( (tga_image->getComponents() != 3) &&
  328. (tga_image->getComponents() != 4) )
  329. {
  330. tga_image->setLastError( "Image files with less than 3 or more than 4 components are not supported." );
  331. return false;
  332. }
  333. }
  334. break;
  335. case IMG_CODEC_JPEG:
  336. {
  337. LLPointer<LLImageJPEG> jpeg_image = new LLImageJPEG;
  338. if (!jpeg_image->load(src_filename))
  339. {
  340. return false;
  341. }
  342. if (!jpeg_image->decode(raw_image, 0.0f))
  343. {
  344. return false;
  345. }
  346. }
  347. break;
  348. case IMG_CODEC_PNG:
  349. {
  350. LLPointer<LLImagePNG> png_image = new LLImagePNG;
  351. if (!png_image->load(src_filename))
  352. {
  353. return false;
  354. }
  355. if (!png_image->decode(raw_image, 0.0f))
  356. {
  357. return false;
  358. }
  359. }
  360. break;
  361. default:
  362. return false;
  363. }
  364. raw_image->biasedScaleToPowerOfTwo(1024);
  365. mRawImagep = raw_image;
  366. return true;
  367. }
  368. //-----------------------------------------------------------------------------
  369. // handleMouseDown()
  370. //-----------------------------------------------------------------------------
  371. BOOL LLFloaterImagePreview::handleMouseDown(S32 x, S32 y, MASK mask)
  372. {
  373. if (mPreviewRect.pointInRect(x, y))
  374. {
  375. bringToFront( x, y );
  376. gFocusMgr.setMouseCapture(this);
  377. gViewerWindow->hideCursor();
  378. mLastMouseX = x;
  379. mLastMouseY = y;
  380. return TRUE;
  381. }
  382. return LLFloater::handleMouseDown(x, y, mask);
  383. }
  384. //-----------------------------------------------------------------------------
  385. // handleMouseUp()
  386. //-----------------------------------------------------------------------------
  387. BOOL LLFloaterImagePreview::handleMouseUp(S32 x, S32 y, MASK mask)
  388. {
  389. gFocusMgr.setMouseCapture(FALSE);
  390. gViewerWindow->showCursor();
  391. return LLFloater::handleMouseUp(x, y, mask);
  392. }
  393. //-----------------------------------------------------------------------------
  394. // handleHover()
  395. //-----------------------------------------------------------------------------
  396. BOOL LLFloaterImagePreview::handleHover(S32 x, S32 y, MASK mask)
  397. {
  398. MASK local_mask = mask & ~MASK_ALT;
  399. if (mAvatarPreview && hasMouseCapture())
  400. {
  401. if (local_mask == MASK_PAN)
  402. {
  403. // pan here
  404. LLCtrlSelectionInterface* iface = childGetSelectionInterface("clothing_type_combo");
  405. if (iface && iface->getFirstSelectedIndex() <= 0)
  406. {
  407. mPreviewImageRect.translate((F32)(x - mLastMouseX) * -0.005f * mPreviewImageRect.getWidth(), 
  408. (F32)(y - mLastMouseY) * -0.005f * mPreviewImageRect.getHeight());
  409. }
  410. else
  411. {
  412. mAvatarPreview->pan((F32)(x - mLastMouseX) * -0.005f, (F32)(y - mLastMouseY) * -0.005f);
  413. mSculptedPreview->pan((F32)(x - mLastMouseX) * -0.005f, (F32)(y - mLastMouseY) * -0.005f);
  414. }
  415. }
  416. else if (local_mask == MASK_ORBIT)
  417. {
  418. F32 yaw_radians = (F32)(x - mLastMouseX) * -0.01f;
  419. F32 pitch_radians = (F32)(y - mLastMouseY) * 0.02f;
  420. mAvatarPreview->rotate(yaw_radians, pitch_radians);
  421. mSculptedPreview->rotate(yaw_radians, pitch_radians);
  422. }
  423. else 
  424. {
  425. LLCtrlSelectionInterface* iface = childGetSelectionInterface("clothing_type_combo");
  426. if (iface && iface->getFirstSelectedIndex() <= 0)
  427. {
  428. F32 zoom_amt = (F32)(y - mLastMouseY) * -0.002f;
  429. mPreviewImageRect.stretch(zoom_amt);
  430. }
  431. else
  432. {
  433. F32 yaw_radians = (F32)(x - mLastMouseX) * -0.01f;
  434. F32 zoom_amt = (F32)(y - mLastMouseY) * 0.02f;
  435. mAvatarPreview->rotate(yaw_radians, 0.f);
  436. mAvatarPreview->zoom(zoom_amt);
  437. mSculptedPreview->rotate(yaw_radians, 0.f);
  438. mSculptedPreview->zoom(zoom_amt);
  439. }
  440. }
  441. LLCtrlSelectionInterface* iface = childGetSelectionInterface("clothing_type_combo");
  442. if (iface && iface->getFirstSelectedIndex() <= 0)
  443. {
  444. if (mPreviewImageRect.getWidth() > 1.f)
  445. {
  446. mPreviewImageRect.stretch((1.f - mPreviewImageRect.getWidth()) * 0.5f);
  447. }
  448. else if (mPreviewImageRect.getWidth() < 0.1f)
  449. {
  450. mPreviewImageRect.stretch((0.1f - mPreviewImageRect.getWidth()) * 0.5f);
  451. }
  452. if (mPreviewImageRect.getHeight() > 1.f)
  453. {
  454. mPreviewImageRect.stretch((1.f - mPreviewImageRect.getHeight()) * 0.5f);
  455. }
  456. else if (mPreviewImageRect.getHeight() < 0.1f)
  457. {
  458. mPreviewImageRect.stretch((0.1f - mPreviewImageRect.getHeight()) * 0.5f);
  459. }
  460. if (mPreviewImageRect.mLeft < 0.f)
  461. {
  462. mPreviewImageRect.translate(-mPreviewImageRect.mLeft, 0.f);
  463. }
  464. else if (mPreviewImageRect.mRight > 1.f)
  465. {
  466. mPreviewImageRect.translate(1.f - mPreviewImageRect.mRight, 0.f);
  467. }
  468. if (mPreviewImageRect.mBottom < 0.f)
  469. {
  470. mPreviewImageRect.translate(0.f, -mPreviewImageRect.mBottom);
  471. }
  472. else if (mPreviewImageRect.mTop > 1.f)
  473. {
  474. mPreviewImageRect.translate(0.f, 1.f - mPreviewImageRect.mTop);
  475. }
  476. }
  477. else
  478. {
  479. mAvatarPreview->refresh();
  480. mSculptedPreview->refresh();
  481. }
  482. LLUI::setMousePositionLocal(this, mLastMouseX, mLastMouseY);
  483. }
  484. if (!mPreviewRect.pointInRect(x, y) || !mAvatarPreview || !mSculptedPreview)
  485. {
  486. return LLFloater::handleHover(x, y, mask);
  487. }
  488. else if (local_mask == MASK_ORBIT)
  489. {
  490. gViewerWindow->setCursor(UI_CURSOR_TOOLCAMERA);
  491. }
  492. else if (local_mask == MASK_PAN)
  493. {
  494. gViewerWindow->setCursor(UI_CURSOR_TOOLPAN);
  495. }
  496. else
  497. {
  498. gViewerWindow->setCursor(UI_CURSOR_TOOLZOOMIN);
  499. }
  500. return TRUE;
  501. }
  502. //-----------------------------------------------------------------------------
  503. // handleScrollWheel()
  504. //-----------------------------------------------------------------------------
  505. BOOL LLFloaterImagePreview::handleScrollWheel(S32 x, S32 y, S32 clicks)
  506. {
  507. if (mPreviewRect.pointInRect(x, y) && mAvatarPreview)
  508. {
  509. mAvatarPreview->zoom((F32)clicks * -0.2f);
  510. mAvatarPreview->refresh();
  511. mSculptedPreview->zoom((F32)clicks * -0.2f);
  512. mSculptedPreview->refresh();
  513. }
  514. return TRUE;
  515. }
  516. //-----------------------------------------------------------------------------
  517. // onMouseCaptureLost()
  518. //-----------------------------------------------------------------------------
  519. // static
  520. void LLFloaterImagePreview::onMouseCaptureLostImagePreview(LLMouseHandler* handler)
  521. {
  522. gViewerWindow->showCursor();
  523. }
  524. //-----------------------------------------------------------------------------
  525. // LLImagePreviewAvatar
  526. //-----------------------------------------------------------------------------
  527. LLImagePreviewAvatar::LLImagePreviewAvatar(S32 width, S32 height) : LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE)
  528. {
  529. mNeedsUpdate = TRUE;
  530. mTargetJoint = NULL;
  531. mTargetMesh = NULL;
  532. mCameraDistance = 0.f;
  533. mCameraYaw = 0.f;
  534. mCameraPitch = 0.f;
  535. mCameraZoom = 1.f;
  536. mDummyAvatar = (LLVOAvatar*)gObjectList.createObjectViewer(LL_PCODE_LEGACY_AVATAR, gAgent.getRegion());
  537. mDummyAvatar->initInstance();
  538. mDummyAvatar->createDrawable(&gPipeline);
  539. mDummyAvatar->mIsDummy = TRUE;
  540. mDummyAvatar->mSpecialRenderMode = 2;
  541. mDummyAvatar->setPositionAgent(LLVector3::zero);
  542. mDummyAvatar->slamPosition();
  543. mDummyAvatar->updateJointLODs();
  544. mDummyAvatar->updateGeometry(mDummyAvatar->mDrawable);
  545. // gPipeline.markVisible(mDummyAvatar->mDrawable, *LLViewerCamera::getInstance());
  546. mTextureName = 0;
  547. }
  548. LLImagePreviewAvatar::~LLImagePreviewAvatar()
  549. {
  550. mDummyAvatar->markDead();
  551. }
  552. void LLImagePreviewAvatar::setPreviewTarget(const std::string& joint_name, const std::string& mesh_name, LLImageRaw* imagep, F32 distance, BOOL male) 
  553. mTargetJoint = mDummyAvatar->mRoot.findJoint(joint_name);
  554. // clear out existing test mesh
  555. if (mTargetMesh)
  556. {
  557. mTargetMesh->setTestTexture(0);
  558. }
  559. if (male)
  560. {
  561. mDummyAvatar->setVisualParamWeight( "male", 1.f );
  562. mDummyAvatar->updateVisualParams();
  563. mDummyAvatar->updateGeometry(mDummyAvatar->mDrawable);
  564. }
  565. else
  566. {
  567. mDummyAvatar->setVisualParamWeight( "male", 0.f );
  568. mDummyAvatar->updateVisualParams();
  569. mDummyAvatar->updateGeometry(mDummyAvatar->mDrawable);
  570. }
  571. mDummyAvatar->mRoot.setVisible(FALSE, TRUE);
  572. mTargetMesh = (LLViewerJointMesh*)mDummyAvatar->mRoot.findJoint(mesh_name);
  573. mTargetMesh->setTestTexture(mTextureName);
  574. mTargetMesh->setVisible(TRUE, FALSE);
  575. mCameraDistance = distance;
  576. mCameraZoom = 1.f;
  577. mCameraPitch = 0.f;
  578. mCameraYaw = 0.f;
  579. mCameraOffset.clearVec();
  580. }
  581. //-----------------------------------------------------------------------------
  582. // clearPreviewTexture()
  583. //-----------------------------------------------------------------------------
  584. void LLImagePreviewAvatar::clearPreviewTexture(const std::string& mesh_name)
  585. {
  586. if (mDummyAvatar)
  587. {
  588. LLViewerJointMesh *mesh = (LLViewerJointMesh*)mDummyAvatar->mRoot.findJoint(mesh_name);
  589. // clear out existing test mesh
  590. if (mesh)
  591. {
  592. mesh->setTestTexture(0);
  593. }
  594. }
  595. }
  596. //-----------------------------------------------------------------------------
  597. // update()
  598. //-----------------------------------------------------------------------------
  599. BOOL LLImagePreviewAvatar::render()
  600. {
  601. mNeedsUpdate = FALSE;
  602. LLVOAvatar* avatarp = mDummyAvatar;
  603. glMatrixMode(GL_PROJECTION);
  604. gGL.pushMatrix();
  605. glLoadIdentity();
  606. glOrtho(0.0f, mFullWidth, 0.0f, mFullHeight, -1.0f, 1.0f);
  607. glMatrixMode(GL_MODELVIEW);
  608. gGL.pushMatrix();
  609. glLoadIdentity();
  610. LLGLSUIDefault def;
  611. gGL.color4f(0.15f, 0.2f, 0.3f, 1.f);
  612. gl_rect_2d_simple( mFullWidth, mFullHeight );
  613. glMatrixMode(GL_PROJECTION);
  614. gGL.popMatrix();
  615. glMatrixMode(GL_MODELVIEW);
  616. gGL.popMatrix();
  617. gGL.flush();
  618. LLVector3 target_pos = mTargetJoint->getWorldPosition();
  619. LLQuaternion camera_rot = LLQuaternion(mCameraPitch, LLVector3::y_axis) * 
  620. LLQuaternion(mCameraYaw, LLVector3::z_axis);
  621. LLQuaternion av_rot = avatarp->mPelvisp->getWorldRotation() * camera_rot;
  622. LLViewerCamera::getInstance()->setOriginAndLookAt(
  623. target_pos + ((LLVector3(mCameraDistance, 0.f, 0.f) + mCameraOffset) * av_rot), // camera
  624. LLVector3::z_axis, // up
  625. target_pos + (mCameraOffset  * av_rot) ); // point of interest
  626. stop_glerror();
  627. LLViewerCamera::getInstance()->setAspect((F32)mFullWidth / mFullHeight);
  628. LLViewerCamera::getInstance()->setView(LLViewerCamera::getInstance()->getDefaultFOV() / mCameraZoom);
  629. LLViewerCamera::getInstance()->setPerspective(FALSE, mOrigin.mX, mOrigin.mY, mFullWidth, mFullHeight, FALSE);
  630. LLVertexBuffer::unbind();
  631. avatarp->updateLOD();
  632. if (avatarp->mDrawable.notNull())
  633. {
  634. LLGLDepthTest gls_depth(GL_TRUE, GL_TRUE);
  635. // make sure alpha=0 shows avatar material color
  636. LLGLDisable no_blend(GL_BLEND);
  637. LLDrawPoolAvatar *avatarPoolp = (LLDrawPoolAvatar *)avatarp->mDrawable->getFace(0)->getPool();
  638. avatarPoolp->renderAvatars(avatarp);  // renders only one avatar
  639. }
  640. gGL.color4f(1,1,1,1);
  641. return TRUE;
  642. }
  643. //-----------------------------------------------------------------------------
  644. // refresh()
  645. //-----------------------------------------------------------------------------
  646. void LLImagePreviewAvatar::refresh()
  647. mNeedsUpdate = TRUE; 
  648. }
  649. //-----------------------------------------------------------------------------
  650. // rotate()
  651. //-----------------------------------------------------------------------------
  652. void LLImagePreviewAvatar::rotate(F32 yaw_radians, F32 pitch_radians)
  653. {
  654. mCameraYaw = mCameraYaw + yaw_radians;
  655. mCameraPitch = llclamp(mCameraPitch + pitch_radians, F_PI_BY_TWO * -0.8f, F_PI_BY_TWO * 0.8f);
  656. }
  657. //-----------------------------------------------------------------------------
  658. // zoom()
  659. //-----------------------------------------------------------------------------
  660. void LLImagePreviewAvatar::zoom(F32 zoom_amt)
  661. {
  662. mCameraZoom = llclamp(mCameraZoom + zoom_amt, 1.f, 10.f);
  663. }
  664. void LLImagePreviewAvatar::pan(F32 right, F32 up)
  665. {
  666. mCameraOffset.mV[VY] = llclamp(mCameraOffset.mV[VY] + right * mCameraDistance / mCameraZoom, -1.f, 1.f);
  667. mCameraOffset.mV[VZ] = llclamp(mCameraOffset.mV[VZ] + up * mCameraDistance / mCameraZoom, -1.f, 1.f);
  668. }
  669. //-----------------------------------------------------------------------------
  670. // LLImagePreviewSculpted
  671. //-----------------------------------------------------------------------------
  672. LLImagePreviewSculpted::LLImagePreviewSculpted(S32 width, S32 height) : LLViewerDynamicTexture(width, height, 3, ORDER_MIDDLE, FALSE)
  673. {
  674. mNeedsUpdate = TRUE;
  675. mCameraDistance = 0.f;
  676. mCameraYaw = 0.f;
  677. mCameraPitch = 0.f;
  678. mCameraZoom = 1.f;
  679. mTextureName = 0;
  680. LLVolumeParams volume_params;
  681. volume_params.setType(LL_PCODE_PROFILE_CIRCLE, LL_PCODE_PATH_CIRCLE);
  682. volume_params.setSculptID(LLUUID::null, LL_SCULPT_TYPE_SPHERE);
  683. F32 const HIGHEST_LOD = 4.0f;
  684. mVolume = new LLVolume(volume_params,  HIGHEST_LOD);
  685. }
  686. LLImagePreviewSculpted::~LLImagePreviewSculpted()
  687. {
  688. }
  689. void LLImagePreviewSculpted::setPreviewTarget(LLImageRaw* imagep, F32 distance)
  690. mCameraDistance = distance;
  691. mCameraZoom = 1.f;
  692. mCameraPitch = 0.f;
  693. mCameraYaw = 0.f;
  694. mCameraOffset.clearVec();
  695. if (imagep)
  696. {
  697. mVolume->sculpt(imagep->getWidth(), imagep->getHeight(), imagep->getComponents(), imagep->getData(), 0);
  698. }
  699. const LLVolumeFace &vf = mVolume->getVolumeFace(0);
  700. U32 num_indices = vf.mIndices.size();
  701. U32 num_vertices = vf.mVertices.size();
  702. mVertexBuffer = new LLVertexBuffer(LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_NORMAL, 0);
  703. mVertexBuffer->allocateBuffer(num_vertices, num_indices, TRUE);
  704. LLStrider<LLVector3> vertex_strider;
  705. LLStrider<LLVector3> normal_strider;
  706. LLStrider<U16> index_strider;
  707. mVertexBuffer->getVertexStrider(vertex_strider);
  708. mVertexBuffer->getNormalStrider(normal_strider);
  709. mVertexBuffer->getIndexStrider(index_strider);
  710. // build vertices and normals
  711. for (U32 i = 0; i < num_vertices; i++)
  712. {
  713. *(vertex_strider++) = vf.mVertices[i].mPosition;
  714. LLVector3 normal = vf.mVertices[i].mNormal;
  715. normal.normalize();
  716. *(normal_strider++) = normal;
  717. }
  718. // build indices
  719. for (U16 i = 0; i < num_indices; i++)
  720. {
  721. *(index_strider++) = vf.mIndices[i];
  722. }
  723. }
  724. //-----------------------------------------------------------------------------
  725. // render()
  726. //-----------------------------------------------------------------------------
  727. BOOL LLImagePreviewSculpted::render()
  728. {
  729. mNeedsUpdate = FALSE;
  730. LLGLSUIDefault def;
  731. LLGLDisable no_blend(GL_BLEND);
  732. LLGLEnable cull(GL_CULL_FACE);
  733. LLGLDepthTest depth(GL_TRUE);
  734. glMatrixMode(GL_PROJECTION);
  735. gGL.pushMatrix();
  736. glLoadIdentity();
  737. glOrtho(0.0f, mFullWidth, 0.0f, mFullHeight, -1.0f, 1.0f);
  738. glMatrixMode(GL_MODELVIEW);
  739. gGL.pushMatrix();
  740. glLoadIdentity();
  741. gGL.color4f(0.15f, 0.2f, 0.3f, 1.f);
  742. gl_rect_2d_simple( mFullWidth, mFullHeight );
  743. glMatrixMode(GL_PROJECTION);
  744. gGL.popMatrix();
  745. glMatrixMode(GL_MODELVIEW);
  746. gGL.popMatrix();
  747. glClear(GL_DEPTH_BUFFER_BIT);
  748. LLVector3 target_pos(0, 0, 0);
  749. LLQuaternion camera_rot = LLQuaternion(mCameraPitch, LLVector3::y_axis) * 
  750. LLQuaternion(mCameraYaw, LLVector3::z_axis);
  751. LLQuaternion av_rot = camera_rot;
  752. LLViewerCamera::getInstance()->setOriginAndLookAt(
  753. target_pos + ((LLVector3(mCameraDistance, 0.f, 0.f) + mCameraOffset) * av_rot), // camera
  754. LLVector3::z_axis, // up
  755. target_pos + (mCameraOffset  * av_rot) ); // point of interest
  756. stop_glerror();
  757. LLViewerCamera::getInstance()->setAspect((F32) mFullWidth / mFullHeight);
  758. LLViewerCamera::getInstance()->setView(LLViewerCamera::getInstance()->getDefaultFOV() / mCameraZoom);
  759. LLViewerCamera::getInstance()->setPerspective(FALSE, mOrigin.mX, mOrigin.mY, mFullWidth, mFullHeight, FALSE);
  760. const LLVolumeFace &vf = mVolume->getVolumeFace(0);
  761. U32 num_indices = vf.mIndices.size();
  762. mVertexBuffer->setBuffer(LLVertexBuffer::MAP_VERTEX | LLVertexBuffer::MAP_NORMAL);
  763. gPipeline.enableLightsAvatar();
  764. gGL.pushMatrix();
  765. const F32 SCALE = 1.25f;
  766. gGL.scalef(SCALE, SCALE, SCALE);
  767. const F32 BRIGHTNESS = 0.9f;
  768. gGL.color3f(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS);
  769. mVertexBuffer->draw(LLRender::TRIANGLES, num_indices, 0);
  770. gGL.popMatrix();
  771. return TRUE;
  772. }
  773. //-----------------------------------------------------------------------------
  774. // refresh()
  775. //-----------------------------------------------------------------------------
  776. void LLImagePreviewSculpted::refresh()
  777. mNeedsUpdate = TRUE; 
  778. }
  779. //-----------------------------------------------------------------------------
  780. // rotate()
  781. //-----------------------------------------------------------------------------
  782. void LLImagePreviewSculpted::rotate(F32 yaw_radians, F32 pitch_radians)
  783. {
  784. mCameraYaw = mCameraYaw + yaw_radians;
  785. mCameraPitch = llclamp(mCameraPitch + pitch_radians, F_PI_BY_TWO * -0.8f, F_PI_BY_TWO * 0.8f);
  786. }
  787. //-----------------------------------------------------------------------------
  788. // zoom()
  789. //-----------------------------------------------------------------------------
  790. void LLImagePreviewSculpted::zoom(F32 zoom_amt)
  791. {
  792. mCameraZoom = llclamp(mCameraZoom + zoom_amt, 1.f, 10.f);
  793. }
  794. void LLImagePreviewSculpted::pan(F32 right, F32 up)
  795. {
  796. mCameraOffset.mV[VY] = llclamp(mCameraOffset.mV[VY] + right * mCameraDistance / mCameraZoom, -1.f, 1.f);
  797. mCameraOffset.mV[VZ] = llclamp(mCameraOffset.mV[VZ] + up * mCameraDistance / mCameraZoom, -1.f, 1.f);
  798. }