GLWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include "Radiant.h"
  24. #include "GLWidget.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // idGLWidget
  32. class idMiniDrawVert {
  33. public:
  34. idVec3 xyz;
  35. idVec2 st;
  36. idMiniDrawVert(float x, float y, float z, float s, float t) : xyz(x,y,z), st(s, t) {
  37. };
  38. };
  39. static idMiniDrawVert cubeData[] = {
  40. idMiniDrawVert(-1.0, -1.0, +1.0, 0.0, 0.0),
  41. idMiniDrawVert(+1.0, -1.0, +1.0, 1.0, 0.0),
  42. idMiniDrawVert(+1.0, +1.0, +1.0, 1.0, 1.0),
  43. idMiniDrawVert(-1.0, +1.0, +1.0, 0.0, 1.0),
  44. idMiniDrawVert(-1.0, -1.0, -1.0, 1.0, 0.0),
  45. idMiniDrawVert(-1.0, +1.0, +1.0, 1.0, 1.0),
  46. idMiniDrawVert(+1.0, +1.0, -1.0, 0.0, 1.0),
  47. idMiniDrawVert(+1.0, -1.0, -1.0, 0.0, 0.0),
  48. idMiniDrawVert(-1.0, +1.0, -1.0, 0.0, 1.0),
  49. idMiniDrawVert(-1.0, +1.0, +1.0, 0.0, 0.0),
  50. idMiniDrawVert(+1.0, +1.0, +1.0, 1.0, 0.0),
  51. idMiniDrawVert(+1.0, +1.0, -1.0, 1.0, 1.0),
  52. idMiniDrawVert(-1.0, -1.0, -1.0, 1.0, 1.0),
  53. idMiniDrawVert(+1.0, -1.0, -1.0, 0.0, 1.0),
  54. idMiniDrawVert(+1.0, -1.0, +1.0, 0.0, 0.0),
  55. idMiniDrawVert(-1.0, -1.0, +1.0, 1.0, 0.0),
  56. idMiniDrawVert(+1.0, -1.0, -1.0, 1.0, 0.0),
  57. idMiniDrawVert(+1.0, +1.0, -1.0, 1.0, 1.0),
  58. idMiniDrawVert(+1.0, +1.0, +1.0, 0.0, 1.0),
  59. idMiniDrawVert(+1.0, -1.0, +1.0, 0.0, 0.0),
  60. idMiniDrawVert(-1.0, -1.0, -1.0, 0.0, 0.0),
  61. idMiniDrawVert(-1.0, -1.0, +1.0, 1.0, 0.0),
  62. idMiniDrawVert(-1.0, +1.0, +1.0, 1.0, 1.0),
  63. idMiniDrawVert(-1.0, +1.0, -1.0, 0.0, 1.0)
  64. };
  65. static int cubeSides = sizeof(cubeData) / sizeof(idMiniDrawVert);
  66. static int numQuads = cubeSides / 4;
  67. void glTexturedBox(idVec3 &point, float size, const idMaterial *mat) {
  68. qglTranslatef(point.x, point.y, point.z);
  69. for (int i = 0; i < numQuads; i++) {
  70. qglBegin(GL_QUADS);
  71. for (int j = 0; j < 4; j++) {
  72. idVec3 v = cubeData[i * 4 + j].xyz;
  73. v *= size;
  74. qglTexCoord2fv(cubeData[i * 4 + j].st.ToFloatPtr());
  75. qglVertex3fv(v.ToFloatPtr());
  76. }
  77. qglEnd();
  78. }
  79. }
  80. idGLWidget::idGLWidget()
  81. {
  82. initialized = false;
  83. drawable = NULL;
  84. }
  85. idGLWidget::~idGLWidget()
  86. {
  87. }
  88. BEGIN_MESSAGE_MAP(idGLWidget, CWnd)
  89. //{{AFX_MSG_MAP(idGLWidget)
  90. ON_WM_PAINT()
  91. ON_WM_LBUTTONDOWN()
  92. ON_WM_LBUTTONUP()
  93. ON_WM_MBUTTONDOWN()
  94. ON_WM_MBUTTONUP()
  95. ON_WM_MOUSEMOVE()
  96. ON_WM_MOUSEWHEEL()
  97. ON_WM_RBUTTONDOWN()
  98. ON_WM_RBUTTONUP()
  99. ON_WM_TIMER()
  100. ON_WM_ERASEBKGND()
  101. //}}AFX_MSG_MAP
  102. END_MESSAGE_MAP()
  103. /////////////////////////////////////////////////////////////////////////////
  104. // idGLWidget message handlers
  105. BOOL idGLWidget::PreCreateWindow(CREATESTRUCT& cs)
  106. {
  107. // TODO: Add your specialized code here and/or call the base class
  108. return CWnd::PreCreateWindow(cs);
  109. }
  110. BOOL idGLWidget::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  111. {
  112. if (CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext) == -1) {
  113. return FALSE;
  114. }
  115. CDC *dc = GetDC();
  116. QEW_SetupPixelFormat(dc->m_hDC, false);
  117. ReleaseDC(dc);
  118. return TRUE;
  119. }
  120. void idGLWidget::OnPaint()
  121. {
  122. if (!initialized) {
  123. CDC *dc = GetDC();
  124. QEW_SetupPixelFormat(dc->m_hDC, false);
  125. ReleaseDC(dc);
  126. initialized = true;
  127. }
  128. CPaintDC dc(this); // device context for painting
  129. CRect rect;
  130. GetClientRect(rect);
  131. if (!qwglMakeCurrent(dc.m_hDC, win32.hGLRC)) {
  132. }
  133. qglViewport(0, 0, rect.Width(), rect.Height());
  134. qglScissor(0, 0, rect.Width(), rect.Height());
  135. qglMatrixMode(GL_PROJECTION);
  136. qglLoadIdentity();
  137. qglClearColor (0.4f, 0.4f, 0.4f, 0.7f);
  138. qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. qglDisable(GL_DEPTH_TEST);
  140. qglDisable(GL_BLEND);
  141. qglOrtho(0, rect.Width(), 0, rect.Height(), -256, 256);
  142. if (drawable) {
  143. drawable->draw(1, 1, rect.Width()-1, rect.Height()-1);
  144. } else {
  145. qglViewport(0, 0, rect.Width(), rect.Height());
  146. qglScissor(0, 0, rect.Width(), rect.Height());
  147. qglMatrixMode(GL_PROJECTION);
  148. qglLoadIdentity();
  149. qglClearColor (0.4f, 0.4f, 0.4f, 0.7f);
  150. qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  151. }
  152. qwglSwapBuffers(dc);
  153. qglFlush();
  154. qwglMakeCurrent(win32.hDC, win32.hGLRC);
  155. }
  156. extern bool Sys_KeyDown(int key);
  157. void idGLDrawable::buttonDown(int _button, float x, float y) {
  158. pressX = x;
  159. pressY = y;
  160. button = _button;
  161. if (button == MK_RBUTTON) {
  162. handleMove = true;
  163. }
  164. }
  165. void idGLDrawable::buttonUp(int button, float x, float y) {
  166. handleMove = false;
  167. }
  168. extern float fDiff(float f1, float f2);
  169. void idGLDrawable::mouseMove(float x, float y) {
  170. if (handleMove) {
  171. Update();
  172. if (Sys_KeyDown(VK_MENU)) {
  173. // scale
  174. float *px = &x;
  175. float *px2 = &pressX;
  176. if (fDiff(y, pressY) > fDiff(x, pressX)) {
  177. px = &y;
  178. px2 = &pressY;
  179. }
  180. if (*px > *px2) {
  181. // zoom in
  182. scale += 0.1f;
  183. if ( scale > 10.0f ) {
  184. scale = 10.0f;
  185. }
  186. } else if (*px < *px2) {
  187. // zoom out
  188. scale -= 0.1f;
  189. if ( scale <= 0.001f ) {
  190. scale = 0.001f;
  191. }
  192. }
  193. *px2 = *px;
  194. ::SetCursorPos(pressX, pressY);
  195. } else if (Sys_KeyDown(VK_SHIFT)) {
  196. // rotate
  197. } else {
  198. // origin
  199. if (x != pressX) {
  200. xOffset += (x - pressX);
  201. pressX = x;
  202. }
  203. if (y != pressY) {
  204. yOffset -= (y - pressY);
  205. pressY = y;
  206. }
  207. //::SetCursorPos(pressX, pressY);
  208. }
  209. }
  210. }
  211. void idGLDrawable::draw(int x, int y, int w, int h) {
  212. GL_State( GLS_DEFAULT );
  213. qglViewport(x, y, w, h);
  214. qglScissor(x, y, w, h);
  215. qglMatrixMode(GL_PROJECTION);
  216. qglClearColor( 0.1f, 0.1f, 0.1f, 0.0f );
  217. qglClear(GL_COLOR_BUFFER_BIT);
  218. qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  219. qglLineWidth(0.5);
  220. qglColor3f(1, 1, 1);
  221. globalImages->BindNull();
  222. qglBegin(GL_LINE_LOOP);
  223. qglColor3f(1, 0, 0);
  224. qglVertex2f(x + 3, y + 3);
  225. qglColor3f(0, 1, 0);
  226. qglVertex2f(x + 3, h - 3);
  227. qglColor3f(0, 0, 1);
  228. qglVertex2f(w - 3, h - 3);
  229. qglColor3f(1, 1, 1);
  230. qglVertex2f(w - 3, y + 3);
  231. qglEnd();
  232. }
  233. static int viewAngle = -98;
  234. void idGLDrawableMaterial::buttonDown(int button, float x, float y) {
  235. idGLDrawable::buttonDown(button, x, y);
  236. //viewAngle += (button == MK_LBUTTON) ? 15 : -15;
  237. }
  238. void idGLDrawableMaterial::mouseMove(float x, float y) {
  239. if (handleMove) {
  240. Update();
  241. bool doScale = Sys_KeyDown(VK_MENU);
  242. bool doLight = Sys_KeyDown(VK_SHIFT);
  243. if (doScale || doLight) {
  244. // scale
  245. float *px = &x;
  246. float *px2 = &pressX;
  247. if (fDiff(y, pressY) > fDiff(x, pressX)) {
  248. px = &y;
  249. px2 = &pressY;
  250. }
  251. if (*px > *px2) {
  252. // zoom in
  253. if (doScale) {
  254. scale += 0.1f;
  255. if ( scale > 10.0f ) {
  256. scale = 10.0f;
  257. }
  258. } else {
  259. light += 0.05f;
  260. if ( light > 2.0f ) {
  261. light = 2.0f;
  262. }
  263. }
  264. } else if (*px < *px2) {
  265. // zoom out
  266. if (doScale) {
  267. scale -= 0.1f;
  268. if ( scale <= 0.001f ) {
  269. scale = 0.001f;
  270. }
  271. } else {
  272. light -= 0.05f;
  273. if ( light < 0.0f ) {
  274. light = 0.0f;
  275. }
  276. }
  277. }
  278. *px2 = *px;
  279. ::SetCursorPos(pressX, pressY);
  280. } else {
  281. // origin
  282. if (x != pressX) {
  283. xOffset += (x - pressX);
  284. pressX = x;
  285. }
  286. if (y != pressY) {
  287. yOffset -= (y - pressY);
  288. pressY = y;
  289. }
  290. //::SetCursorPos(pressX, pressY);
  291. }
  292. }
  293. }
  294. void idGLDrawableMaterial::draw(int x, int y, int w, int h) {
  295. const idMaterial *mat = material;
  296. if (mat) {
  297. qglViewport(x, y, w, h);
  298. qglScissor(x, y, w, h);
  299. qglMatrixMode(GL_PROJECTION);
  300. qglClearColor( 0.1f, 0.1f, 0.1f, 0.0f );
  301. qglClear(GL_COLOR_BUFFER_BIT);
  302. if (worldDirty) {
  303. InitWorld();
  304. renderLight_t parms;
  305. idDict spawnArgs;
  306. spawnArgs.Set("classname", "light");
  307. spawnArgs.Set("name", "light_1");
  308. spawnArgs.Set("origin", "0 0 0");
  309. idStr str;
  310. sprintf(str, "%f %f %f", light, light, light);
  311. spawnArgs.Set("_color", str);
  312. gameEdit->ParseSpawnArgsToRenderLight( &spawnArgs, &parms );
  313. lightDef = world->AddLightDef( &parms );
  314. idImage *img = (mat->GetNumStages() > 0) ? mat->GetStage(0)->texture.image : mat->GetEditorImage();
  315. if (img == NULL) {
  316. common->Warning("Unable to load image for preview for %s", mat->GetName());
  317. return;
  318. }
  319. int width = img->uploadWidth;
  320. int height = img->uploadHeight;
  321. width *= scale;
  322. height *= scale;
  323. srfTriangles_t *tris = worldModel->AllocSurfaceTriangles( 4, 6 );
  324. tris->numVerts = 4;
  325. tris->numIndexes = 6;
  326. tris->indexes[0] = 0;
  327. tris->indexes[1] = 1;
  328. tris->indexes[2] = 2;
  329. tris->indexes[3] = 3;
  330. tris->indexes[4] = 1;
  331. tris->indexes[5] = 0;
  332. tris->verts[0].xyz.x = 64;
  333. tris->verts[0].xyz.y = -xOffset + 0 - width / 2;
  334. tris->verts[0].xyz.z = yOffset + 0 - height / 2;
  335. tris->verts[0].st.x = 1;
  336. tris->verts[0].st.y = 1;
  337. tris->verts[1].xyz.x = 64;
  338. tris->verts[1].xyz.y = -xOffset + width / 2;
  339. tris->verts[1].xyz.z = yOffset + height / 2;
  340. tris->verts[1].st.x = 0;
  341. tris->verts[1].st.y = 0;
  342. tris->verts[2].xyz.x = 64;
  343. tris->verts[2].xyz.y = -xOffset + 0 - width / 2;
  344. tris->verts[2].xyz.z = yOffset + height / 2;
  345. tris->verts[2].st.x = 1;
  346. tris->verts[2].st.y = 0;
  347. tris->verts[3].xyz.x = 64;
  348. tris->verts[3].xyz.y = -xOffset + width / 2;
  349. tris->verts[3].xyz.z = yOffset + 0 - height / 2;
  350. tris->verts[3].st.x = 0;
  351. tris->verts[3].st.y = 1;
  352. tris->verts[0].normal = tris->verts[1].xyz.Cross(tris->verts[3].xyz);
  353. tris->verts[1].normal = tris->verts[2].normal = tris->verts[3].normal = tris->verts[0].normal;
  354. AddTris(tris, mat);
  355. worldModel->FinishSurfaces();
  356. renderEntity_t worldEntity;
  357. memset( &worldEntity, 0, sizeof( worldEntity ) );
  358. if ( mat->HasGui() ) {
  359. worldEntity.gui[ 0 ] = mat->GlobalGui();
  360. }
  361. worldEntity.hModel = worldModel;
  362. worldEntity.axis = mat3_default;
  363. worldEntity.shaderParms[0] = 1;
  364. worldEntity.shaderParms[1] = 1;
  365. worldEntity.shaderParms[2] = 1;
  366. worldEntity.shaderParms[3] = 1;
  367. modelDef = world->AddEntityDef( &worldEntity );
  368. worldDirty = false;
  369. }
  370. renderView_t refdef;
  371. // render it
  372. renderSystem->BeginFrame(w, h);
  373. memset( &refdef, 0, sizeof( refdef ) );
  374. refdef.vieworg.Set(viewAngle, 0, 0);
  375. refdef.viewaxis = idAngles(0,0,0).ToMat3();
  376. refdef.shaderParms[0] = 1;
  377. refdef.shaderParms[1] = 1;
  378. refdef.shaderParms[2] = 1;
  379. refdef.shaderParms[3] = 1;
  380. refdef.width = SCREEN_WIDTH;
  381. refdef.height = SCREEN_HEIGHT;
  382. refdef.fov_x = 90;
  383. refdef.fov_y = 2 * atan((float)h / w) * idMath::M_RAD2DEG;
  384. refdef.time = eventLoop->Milliseconds();
  385. world->RenderScene( &refdef );
  386. int frontEnd, backEnd;
  387. renderSystem->EndFrame( &frontEnd, &backEnd );
  388. qglMatrixMode( GL_MODELVIEW );
  389. qglLoadIdentity();
  390. }
  391. }
  392. void idGLDrawableMaterial::setMedia(const char *name) {
  393. idImage *img = NULL;
  394. if (name && *name) {
  395. material = declManager->FindMaterial(name);
  396. if (material) {
  397. const shaderStage_t *stage = (material->GetNumStages() > 0) ? material->GetStage(0) : NULL;
  398. if (stage) {
  399. img = stage->texture.image;
  400. } else {
  401. img = material->GetEditorImage();
  402. }
  403. }
  404. } else {
  405. material = NULL;
  406. }
  407. // set scale to get a good fit
  408. if (material && img) {
  409. float size = (img->uploadWidth > img->uploadHeight) ? img->uploadWidth : img->uploadHeight;
  410. // use 128 as base scale of 1.0
  411. scale = 128.0 / size;
  412. } else {
  413. scale = 1.0;
  414. }
  415. xOffset = 0.0;
  416. yOffset = 0.0;
  417. worldDirty = true;
  418. }
  419. idGLDrawableModel::idGLDrawableModel(const char *name) {
  420. worldModel = renderModelManager->FindModel( name );
  421. light = 1.0;
  422. worldDirty = true;
  423. }
  424. idGLDrawableModel::idGLDrawableModel() {
  425. worldModel = renderModelManager->DefaultModel();
  426. light = 1.0;
  427. }
  428. void idGLDrawableModel::setMedia(const char *name) {
  429. worldModel = renderModelManager->FindModel(name);
  430. worldDirty = true;
  431. xOffset = 0.0;
  432. yOffset = 0.0;
  433. zOffset = -128;
  434. rotation.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  435. radius = 2.6f;
  436. lastPress.Zero();
  437. }
  438. void idGLDrawableModel::SetSkin( const char *skin ) {
  439. skinStr = skin;
  440. }
  441. void idGLDrawableModel::buttonDown(int _button, float x, float y) {
  442. pressX = x;
  443. pressY = y;
  444. lastPress.y = -( float )( 2 * x - rect.z ) / rect.z;
  445. lastPress.x = -( float )( 2 * y - rect.w ) / rect.w;
  446. lastPress.z = 0.0f;
  447. button = _button;
  448. if (button == MK_RBUTTON || button == MK_LBUTTON) {
  449. handleMove = true;
  450. }
  451. }
  452. void idGLDrawableModel::mouseMove(float x, float y) {
  453. if (handleMove) {
  454. Update();
  455. if (button == MK_LBUTTON) {
  456. float cury = ( float )( 2 * x - rect.z ) / rect.z;
  457. float curx = ( float )( 2 * y - rect.w ) / rect.w;
  458. idVec3 to( -curx, -cury, 0.0f );
  459. to.ProjectSelfOntoSphere( radius );
  460. lastPress.ProjectSelfOntoSphere( radius );
  461. idVec3 axis;
  462. axis.Cross( to, lastPress );
  463. float len = ( lastPress - to ).Length() / ( 2.0f * radius );
  464. len = idMath::ClampFloat( -1.0f, 1.0f, len );
  465. float phi = 2.0f * asin ( len ) ;
  466. axis.Normalize();
  467. axis *= sin( phi / 2.0f );
  468. idQuat rot( axis.z, axis.y, axis.x, cos( phi / 2.0f ) );
  469. rot.Normalize();
  470. rotation *= rot;
  471. rotation.Normalize();
  472. lastPress = to;
  473. lastPress.z = 0.0f;
  474. } else {
  475. bool doScale = Sys_KeyDown(VK_MENU);
  476. bool doLight = Sys_KeyDown(VK_SHIFT);
  477. if (doLight) {
  478. // scale
  479. float *px = &x;
  480. float *px2 = &pressX;
  481. if (fDiff(y, pressY) > fDiff(x, pressX)) {
  482. px = &y;
  483. px2 = &pressY;
  484. }
  485. if (*px > *px2) {
  486. light += 0.05f;
  487. if ( light > 2.0f ) {
  488. light = 2.0f;
  489. }
  490. } else if (*px < *px2) {
  491. light -= 0.05f;
  492. if ( light < 0.0f ) {
  493. light = 0.0f;
  494. }
  495. }
  496. *px2 = *px;
  497. ::SetCursorPos(pressX, pressY);
  498. } else {
  499. // origin
  500. if (x != pressX) {
  501. if (doScale) {
  502. zOffset += (x - pressX);
  503. } else {
  504. xOffset += (x - pressX);
  505. }
  506. pressX = x;
  507. }
  508. if (y != pressY) {
  509. if (doScale) {
  510. zOffset -= (y - pressY);
  511. } else {
  512. yOffset -= (y - pressY);
  513. }
  514. pressY = y;
  515. }
  516. //::SetCursorPos(pressX, pressY);
  517. }
  518. }
  519. }
  520. }
  521. void idGLDrawableModel::draw(int x, int y, int w, int h) {
  522. if ( !worldModel ) {
  523. return;
  524. }
  525. if ( worldModel->IsDynamicModel() != DM_STATIC ) {
  526. //return;
  527. }
  528. rect.Set( x, y, w, h );
  529. qglViewport(x, y, w, h);
  530. qglScissor(x, y, w, h);
  531. qglMatrixMode(GL_PROJECTION);
  532. qglClearColor( 0.1f, 0.1f, 0.1f, 0.0f );
  533. qglClear(GL_COLOR_BUFFER_BIT);
  534. if (worldDirty) {
  535. //InitWorld();
  536. world->InitFromMap( NULL );
  537. renderLight_t parms;
  538. idDict spawnArgs;
  539. spawnArgs.Set("classname", "light");
  540. spawnArgs.Set("name", "light_1");
  541. spawnArgs.Set("origin", "-128 0 0");
  542. idStr str;
  543. sprintf(str, "%f %f %f", light, light, light);
  544. spawnArgs.Set("_color", str);
  545. gameEdit->ParseSpawnArgsToRenderLight( &spawnArgs, &parms );
  546. lightDef = world->AddLightDef( &parms );
  547. renderEntity_t worldEntity;
  548. memset( &worldEntity, 0, sizeof( worldEntity ) );
  549. spawnArgs.Clear();
  550. spawnArgs.Set("classname", "func_static");
  551. spawnArgs.Set("name", spawnArgs.GetString("model"));
  552. spawnArgs.Set("origin", "0 0 0");
  553. if ( skinStr.Length() ) {
  554. spawnArgs.Set( "skin", skinStr );
  555. }
  556. gameEdit->ParseSpawnArgsToRenderEntity(&spawnArgs, &worldEntity);
  557. worldEntity.hModel = worldModel;
  558. worldEntity.axis = rotation.ToMat3();
  559. worldEntity.shaderParms[0] = 1;
  560. worldEntity.shaderParms[1] = 1;
  561. worldEntity.shaderParms[2] = 1;
  562. worldEntity.shaderParms[3] = 1;
  563. modelDef = world->AddEntityDef( &worldEntity );
  564. worldDirty = false;
  565. }
  566. renderView_t refdef;
  567. // render it
  568. renderSystem->BeginFrame(w, h);
  569. memset( &refdef, 0, sizeof( refdef ) );
  570. refdef.vieworg.Set(zOffset, xOffset, -yOffset);
  571. refdef.viewaxis = idAngles(0,0,0).ToMat3();
  572. refdef.shaderParms[0] = 1;
  573. refdef.shaderParms[1] = 1;
  574. refdef.shaderParms[2] = 1;
  575. refdef.shaderParms[3] = 1;
  576. refdef.width = SCREEN_WIDTH;
  577. refdef.height = SCREEN_HEIGHT;
  578. refdef.fov_x = 90;
  579. refdef.fov_y = 2 * atan((float)h / w) * idMath::M_RAD2DEG;
  580. refdef.time = eventLoop->Milliseconds();
  581. world->RenderScene( &refdef );
  582. int frontEnd, backEnd;
  583. renderSystem->EndFrame( &frontEnd, &backEnd );
  584. qglMatrixMode( GL_MODELVIEW );
  585. qglLoadIdentity();
  586. }
  587. void idGLWidget::OnLButtonDown(UINT nFlags, CPoint point)
  588. {
  589. SetCapture();
  590. if (drawable) {
  591. if ( drawable->ScreenCoords() ) {
  592. ClientToScreen(&point);
  593. }
  594. drawable->buttonDown(MK_LBUTTON, point.x, point.y);
  595. }
  596. }
  597. void idGLWidget::OnLButtonUp(UINT nFlags, CPoint point)
  598. {
  599. if (drawable) {
  600. if ( drawable->ScreenCoords() ) {
  601. ClientToScreen(&point);
  602. }
  603. drawable->buttonUp(MK_LBUTTON, point.x, point.y);
  604. }
  605. ReleaseCapture();
  606. }
  607. void idGLWidget::OnMButtonDown(UINT nFlags, CPoint point)
  608. {
  609. SetCapture();
  610. if (drawable) {
  611. if ( drawable->ScreenCoords() ) {
  612. ClientToScreen(&point);
  613. }
  614. drawable->buttonDown(MK_MBUTTON, point.x, point.y);
  615. }
  616. }
  617. void idGLWidget::OnMButtonUp(UINT nFlags, CPoint point)
  618. {
  619. if (drawable) {
  620. if ( drawable->ScreenCoords() ) {
  621. ClientToScreen(&point);
  622. }
  623. drawable->buttonUp(MK_MBUTTON, point.x, point.y);
  624. }
  625. ReleaseCapture();
  626. }
  627. void idGLWidget::OnMouseMove(UINT nFlags, CPoint point)
  628. {
  629. if (drawable) {
  630. if ( drawable->ScreenCoords() ) {
  631. ClientToScreen(&point);
  632. }
  633. drawable->mouseMove(point.x, point.y);
  634. RedrawWindow();
  635. }
  636. }
  637. BOOL idGLWidget::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  638. {
  639. if (drawable) {
  640. float f = drawable->getScale();
  641. if ( zDelta > 0.0f ) {
  642. f += 0.1f;
  643. } else {
  644. f -= 0.1f;
  645. }
  646. if ( f <= 0.0f ) {
  647. f = 0.1f;
  648. }
  649. if ( f > 5.0f ) {
  650. f = 5.0f;
  651. }
  652. drawable->setScale(f);
  653. }
  654. return TRUE;
  655. }
  656. void idGLWidget::OnRButtonDown(UINT nFlags, CPoint point)
  657. {
  658. SetCapture();
  659. if (drawable) {
  660. if ( drawable->ScreenCoords() ) {
  661. ClientToScreen(&point);
  662. }
  663. drawable->buttonDown(MK_RBUTTON, point.x, point.y);
  664. }
  665. }
  666. void idGLWidget::OnRButtonUp(UINT nFlags, CPoint point)
  667. {
  668. if (drawable) {
  669. if ( drawable->ScreenCoords() ) {
  670. ClientToScreen(&point);
  671. }
  672. drawable->buttonUp(MK_RBUTTON, point.x, point.y);
  673. }
  674. ReleaseCapture();
  675. }
  676. void idGLWidget::setDrawable(idGLDrawable *d) {
  677. drawable = d;
  678. if (d->getRealTime()) {
  679. SetTimer(1, d->getRealTime(), NULL);
  680. }
  681. }
  682. void idGLWidget::OnTimer(UINT nIDEvent) {
  683. if (drawable && drawable->getRealTime()) {
  684. Invalidate(FALSE);
  685. } else {
  686. KillTimer(1);
  687. }
  688. }
  689. idGLDrawable::idGLDrawable() {
  690. scale = 1.0;
  691. xOffset = 0.0;
  692. yOffset = 0.0;
  693. handleMove = false;
  694. realTime = 0;
  695. }
  696. void idGLDrawableConsole::draw(int x, int y, int w, int h) {
  697. qglPushAttrib( GL_ALL_ATTRIB_BITS );
  698. qglClearColor( 0.1f, 0.1f, 0.1f, 0.0f );
  699. qglScissor( 0, 0, w, h );
  700. qglClear( GL_COLOR_BUFFER_BIT );
  701. renderSystem->BeginFrame( w, h );
  702. console->Draw( true );
  703. renderSystem->EndFrame( NULL, NULL );
  704. qglPopAttrib();
  705. }
  706. void idGLConsoleWidget::init() {
  707. setDrawable(&console);
  708. }
  709. void idGLConsoleWidget::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  710. {
  711. sysEvent_t ev;
  712. memset( &ev, 0, sizeof( ev ) );
  713. ev.evType = SE_KEY;
  714. ev.evValue2 = 1;
  715. ev.evValue = nChar;
  716. ::console->ProcessEvent( &ev, true );
  717. }
  718. BEGIN_MESSAGE_MAP(idGLConsoleWidget, idGLWidget)
  719. //{{AFX_MSG_MAP(idGLConsoleWidget)
  720. ON_WM_PAINT()
  721. ON_WM_KEYDOWN()
  722. ON_WM_KEYUP()
  723. ON_WM_CHAR()
  724. ON_WM_LBUTTONDOWN()
  725. //}}AFX_MSG_MAP
  726. END_MESSAGE_MAP()
  727. void idGLConsoleWidget::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
  728. {
  729. sysEvent_t ev;
  730. memset( &ev, 0, sizeof( ev ) );
  731. ev.evType = SE_KEY;
  732. ev.evValue2 = 0;
  733. ev.evValue = nChar;
  734. ::console->ProcessEvent( &ev, true );
  735. }
  736. void idGLConsoleWidget::OnPaint() {
  737. idGLWidget::OnPaint();
  738. }
  739. void idGLConsoleWidget::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  740. {
  741. sysEvent_t ev;
  742. memset( &ev, 0, sizeof( ev ) );
  743. ev.evType = SE_CHAR;
  744. ev.evValue = nChar;
  745. ::console->ProcessEvent( &ev, true );
  746. }
  747. void idGLConsoleWidget::OnLButtonDown(UINT nFlags, CPoint point) {
  748. SetFocus();
  749. }
  750. BOOL idGLWidget::OnEraseBkgnd(CDC* pDC)
  751. {
  752. return FALSE;
  753. //return CWnd::OnEraseBkgnd(pDC);
  754. }
  755. idGLDrawableWorld::idGLDrawableWorld() {
  756. world = NULL;
  757. worldModel = NULL;
  758. InitWorld();
  759. }
  760. idGLDrawableWorld::~idGLDrawableWorld() {
  761. delete world;
  762. }
  763. void idGLDrawableWorld::AddTris(srfTriangles_t *tris, const idMaterial *mat) {
  764. modelSurface_t surf;
  765. surf.geometry = tris;
  766. surf.shader = mat;
  767. worldModel->AddSurface( surf );
  768. }
  769. void idGLDrawableWorld::draw(int x, int y, int w, int h) {
  770. }
  771. void idGLDrawableWorld::InitWorld() {
  772. if ( world == NULL ) {
  773. world = renderSystem->AllocRenderWorld();
  774. }
  775. if ( worldModel == NULL ) {
  776. worldModel = renderModelManager->AllocModel();
  777. }
  778. world->InitFromMap( NULL );
  779. worldModel->InitEmpty( va( "GLWorldModel_%i", Sys_Milliseconds() ) );
  780. }