Z.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. #define PAGEFLIPS 2
  24. z_t z;
  25. /*
  26. =======================================================================================================================
  27. Z_Init
  28. =======================================================================================================================
  29. */
  30. void Z_Init(void) {
  31. z.origin[0] = 0;
  32. z.origin[1] = 20;
  33. z.origin[2] = 46;
  34. z.scale = 1;
  35. }
  36. /* MOUSE ACTIONS */
  37. static int cursorx, cursory;
  38. /*
  39. =======================================================================================================================
  40. Z_MouseDown
  41. =======================================================================================================================
  42. */
  43. void Z_MouseDown(int x, int y, int buttons) {
  44. idVec3 org, dir, vup, vright;
  45. brush_t *b;
  46. Sys_GetCursorPos(&cursorx, &cursory);
  47. vup[0] = 0;
  48. vup[1] = 0;
  49. vup[2] = 1 / z.scale;
  50. VectorCopy(z.origin, org);
  51. org[2] += (y - (z.height / 2)) / z.scale;
  52. org[1] = MIN_WORLD_COORD;
  53. b = selected_brushes.next;
  54. if (b != &selected_brushes) {
  55. org[0] = (b->mins[0] + b->maxs[0]) / 2;
  56. }
  57. dir[0] = 0;
  58. dir[1] = 1;
  59. dir[2] = 0;
  60. vright[0] = 0;
  61. vright[1] = 0;
  62. vright[2] = 0;
  63. // new mouse code for ZClip, I'll do this stuff before falling through into the standard ZWindow mouse code...
  64. //
  65. if (g_pParentWnd->GetZWnd()->m_pZClip) // should always be the case I think, but this is safer
  66. {
  67. bool bToggle = false;
  68. bool bSetTop = false;
  69. bool bSetBot = false;
  70. bool bReset = false;
  71. if (g_PrefsDlg.m_nMouseButtons == 2)
  72. {
  73. // 2 button mice...
  74. //
  75. bToggle = (GetKeyState(VK_F1) & 0x8000) != 0;
  76. bSetTop = (GetKeyState(VK_F2) & 0x8000) != 0;
  77. bSetBot = (GetKeyState(VK_F3) & 0x8000) != 0;
  78. bReset = (GetKeyState(VK_F4) & 0x8000) != 0;
  79. }
  80. else
  81. {
  82. // 3 button mice...
  83. //
  84. bToggle = (buttons == (MK_RBUTTON|MK_SHIFT|MK_CONTROL));
  85. bSetTop = (buttons == (MK_RBUTTON|MK_SHIFT));
  86. bSetBot = (buttons == (MK_RBUTTON|MK_CONTROL));
  87. bReset = (GetKeyState(VK_F4) & 0x8000) != 0;
  88. }
  89. if (bToggle)
  90. {
  91. g_pParentWnd->GetZWnd()->m_pZClip->Enable(!(g_pParentWnd->GetZWnd()->m_pZClip->IsEnabled()));
  92. Sys_UpdateWindows (W_ALL);
  93. return;
  94. }
  95. if (bSetTop)
  96. {
  97. g_pParentWnd->GetZWnd()->m_pZClip->SetTop(org[2]);
  98. Sys_UpdateWindows (W_ALL);
  99. return;
  100. }
  101. if (bSetBot)
  102. {
  103. g_pParentWnd->GetZWnd()->m_pZClip->SetBottom(org[2]);
  104. Sys_UpdateWindows (W_ALL);
  105. return;
  106. }
  107. if (bReset)
  108. {
  109. g_pParentWnd->GetZWnd()->m_pZClip->Reset();
  110. Sys_UpdateWindows (W_ALL);
  111. return;
  112. }
  113. }
  114. //
  115. // LBUTTON = manipulate selection shift-LBUTTON = select middle button = grab
  116. // texture ctrl-middle button = set entire brush to texture ctrl-shift-middle
  117. // button = set single face to texture
  118. //
  119. // see code above for these next 3, I just commented them here as well for clarity...
  120. //
  121. // ctrl-shift-RIGHT button = toggle ZClip on/off
  122. // shift-RIGHT button = set ZClip top marker
  123. // ctrl-RIGHT button = set ZClip bottom marker
  124. int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
  125. if
  126. (
  127. (buttons == MK_LBUTTON) ||
  128. (buttons == (MK_LBUTTON | MK_SHIFT)) ||
  129. (buttons == MK_MBUTTON) // || (buttons == (MK_MBUTTON|MK_CONTROL))
  130. ||
  131. (buttons == (nMouseButton | MK_SHIFT | MK_CONTROL))
  132. ) {
  133. Drag_Begin(x, y, buttons, vright, vup, org, dir);
  134. return;
  135. }
  136. // control mbutton = move camera
  137. if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {
  138. g_pParentWnd->GetCamera()->Camera().origin[2] = org[2];
  139. Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
  140. }
  141. }
  142. /*
  143. =======================================================================================================================
  144. Z_MouseUp
  145. =======================================================================================================================
  146. */
  147. void Z_MouseUp(int x, int y, int buttons) {
  148. Drag_MouseUp();
  149. }
  150. /*
  151. =======================================================================================================================
  152. Z_MouseMoved
  153. =======================================================================================================================
  154. */
  155. void Z_MouseMoved(int x, int y, int buttons) {
  156. if (!buttons) {
  157. return;
  158. }
  159. if (buttons == MK_LBUTTON) {
  160. Drag_MouseMoved(x, y, buttons);
  161. Sys_UpdateWindows(W_Z | W_CAMERA_IFON | W_XY);
  162. return;
  163. }
  164. // rbutton = drag z origin
  165. if (buttons == MK_RBUTTON) {
  166. Sys_GetCursorPos(&x, &y);
  167. if (y != cursory) {
  168. z.origin[2] += y - cursory;
  169. Sys_SetCursorPos(cursorx, cursory);
  170. Sys_UpdateWindows(W_Z);
  171. }
  172. return;
  173. }
  174. // control mbutton = move camera
  175. int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
  176. if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {
  177. g_pParentWnd->GetCamera()->Camera().origin[2] = z.origin[2] + (y - (z.height / 2)) / z.scale;
  178. Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
  179. }
  180. }
  181. /*
  182. =======================================================================================================================
  183. DRAWING £
  184. Z_DrawGrid
  185. =======================================================================================================================
  186. */
  187. void Z_DrawGrid(void) {
  188. float zz, zb, ze;
  189. int w, h;
  190. char text[32];
  191. w = z.width / 2 / z.scale;
  192. h = z.height / 2 / z.scale;
  193. zb = z.origin[2] - h;
  194. if (zb < region_mins[2]) {
  195. zb = region_mins[2];
  196. }
  197. zb = 64 * floor(zb / 64);
  198. ze = z.origin[2] + h;
  199. if (ze > region_maxs[2]) {
  200. ze = region_maxs[2];
  201. }
  202. ze = 64 * ceil(ze / 64);
  203. // draw major blocks
  204. qglColor3fv( g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR].ToFloatPtr() );
  205. qglBegin(GL_LINES);
  206. qglVertex2f(0, zb);
  207. qglVertex2f(0, ze);
  208. for (zz = zb; zz < ze; zz += 64) {
  209. qglVertex2f(-w, zz);
  210. qglVertex2f(w, zz);
  211. }
  212. qglEnd();
  213. // draw minor blocks
  214. if ( g_qeglobals.d_showgrid &&
  215. g_qeglobals.d_gridsize * z.scale >= 4 &&
  216. !g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].Compare( g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK] ) ) {
  217. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].ToFloatPtr());
  218. qglBegin(GL_LINES);
  219. for (zz = zb; zz < ze; zz += g_qeglobals.d_gridsize) {
  220. if (!((int)zz & 63)) {
  221. continue;
  222. }
  223. qglVertex2f(-w, zz);
  224. qglVertex2f(w, zz);
  225. }
  226. qglEnd();
  227. }
  228. // draw coordinate text if needed
  229. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDTEXT].ToFloatPtr());
  230. for (zz = zb; zz < ze; zz += 64) {
  231. qglRasterPos2f(-w + 1, zz);
  232. sprintf(text, "%i", (int)zz);
  233. qglCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
  234. }
  235. }
  236. #define CAM_HEIGHT 48 // height of main part
  237. #define CAM_GIZMO 8 // height of the gizmo
  238. /*
  239. =======================================================================================================================
  240. =======================================================================================================================
  241. */
  242. void ZDrawCameraIcon(void) {
  243. float x, y;
  244. int xCam = z.width / 4;
  245. x = 0;
  246. y = g_pParentWnd->GetCamera()->Camera().origin[2];
  247. qglColor3f(0.0, 0.0, 1.0);
  248. qglBegin(GL_LINE_STRIP);
  249. qglVertex3f(x - xCam, y, 0);
  250. qglVertex3f(x, y + CAM_GIZMO, 0);
  251. qglVertex3f(x + xCam, y, 0);
  252. qglVertex3f(x, y - CAM_GIZMO, 0);
  253. qglVertex3f(x - xCam, y, 0);
  254. qglVertex3f(x + xCam, y, 0);
  255. qglVertex3f(x + xCam, y - CAM_HEIGHT, 0);
  256. qglVertex3f(x - xCam, y - CAM_HEIGHT, 0);
  257. qglVertex3f(x - xCam, y, 0);
  258. qglEnd();
  259. }
  260. void ZDrawZClip()
  261. {
  262. float x,y;
  263. x = 0;
  264. y = g_pParentWnd->GetCamera()->Camera().origin[2];
  265. if (g_pParentWnd->GetZWnd()->m_pZClip) // should always be the case I think
  266. g_pParentWnd->GetZWnd()->m_pZClip->Paint();
  267. }
  268. GLbitfield glbitClear = GL_COLOR_BUFFER_BIT; // HACK
  269. /*
  270. =======================================================================================================================
  271. Z_Draw
  272. =======================================================================================================================
  273. */
  274. void Z_Draw(void) {
  275. brush_t *brush;
  276. float w, h;
  277. float top, bottom;
  278. idVec3 org_top, org_bottom, dir_up, dir_down;
  279. int xCam = z.width / 3;
  280. if (!active_brushes.next) {
  281. return; // not valid yet
  282. }
  283. // clear
  284. qglViewport(0, 0, z.width, z.height);
  285. qglScissor(0, 0, z.width, z.height);
  286. qglClearColor
  287. (
  288. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
  289. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
  290. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
  291. 0
  292. );
  293. /*
  294. * GL Bug £
  295. * When not using hw acceleration, gl will fault if we clear the depth buffer bit
  296. * on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT only after
  297. * Z_Draw() has been called once. Yeah, right. £
  298. * qglClear(glbitClear);
  299. */
  300. qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  301. //
  302. // glbitClear |= GL_DEPTH_BUFFER_BIT;
  303. // qglClear(GL_DEPTH_BUFFER_BIT);
  304. //
  305. qglMatrixMode(GL_PROJECTION);
  306. qglLoadIdentity();
  307. w = z.width / 2 / z.scale;
  308. h = z.height / 2 / z.scale;
  309. qglOrtho(-w, w, z.origin[2] - h, z.origin[2] + h, -8, 8);
  310. globalImages->BindNull();
  311. qglDisable(GL_DEPTH_TEST);
  312. qglDisable(GL_BLEND);
  313. // now draw the grid
  314. Z_DrawGrid();
  315. // draw stuff
  316. qglDisable(GL_CULL_FACE);
  317. qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  318. globalImages->BindNull();
  319. // draw filled interiors and edges
  320. dir_up[0] = 0;
  321. dir_up[1] = 0;
  322. dir_up[2] = 1;
  323. dir_down[0] = 0;
  324. dir_down[1] = 0;
  325. dir_down[2] = -1;
  326. VectorCopy(z.origin, org_top);
  327. org_top[2] = 4096;
  328. VectorCopy(z.origin, org_bottom);
  329. org_bottom[2] = -4096;
  330. for (brush = active_brushes.next; brush != &active_brushes; brush = brush->next) {
  331. if
  332. (
  333. brush->mins[0] >= z.origin[0] ||
  334. brush->maxs[0] <= z.origin[0] ||
  335. brush->mins[1] >= z.origin[1] ||
  336. brush->maxs[1] <= z.origin[1]
  337. ) {
  338. continue;
  339. }
  340. if (!Brush_Ray(org_top, dir_down, brush, &top)) {
  341. continue;
  342. }
  343. top = org_top[2] - top;
  344. if (!Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
  345. continue;
  346. }
  347. bottom = org_bottom[2] + bottom;
  348. //q = declManager->FindMaterial(brush->brush_faces->texdef.name);
  349. qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
  350. qglBegin(GL_QUADS);
  351. qglVertex2f(-xCam, bottom);
  352. qglVertex2f(xCam, bottom);
  353. qglVertex2f(xCam, top);
  354. qglVertex2f(-xCam, top);
  355. qglEnd();
  356. qglColor3f(1, 1, 1);
  357. qglBegin(GL_LINE_LOOP);
  358. qglVertex2f(-xCam, bottom);
  359. qglVertex2f(xCam, bottom);
  360. qglVertex2f(xCam, top);
  361. qglVertex2f(-xCam, top);
  362. qglEnd();
  363. }
  364. // now draw selected brushes
  365. for (brush = selected_brushes.next; brush != &selected_brushes; brush = brush->next) {
  366. if
  367. (
  368. !(
  369. brush->mins[0] >= z.origin[0] ||
  370. brush->maxs[0] <= z.origin[0] ||
  371. brush->mins[1] >= z.origin[1] ||
  372. brush->maxs[1] <= z.origin[1]
  373. )
  374. ) {
  375. if (Brush_Ray(org_top, dir_down, brush, &top)) {
  376. top = org_top[2] - top;
  377. if (Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
  378. bottom = org_bottom[2] + bottom;
  379. //q = declManager->FindMaterial(brush->brush_faces->texdef.name);
  380. qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
  381. qglBegin(GL_QUADS);
  382. qglVertex2f(-xCam, bottom);
  383. qglVertex2f(xCam, bottom);
  384. qglVertex2f(xCam, top);
  385. qglVertex2f(-xCam, top);
  386. qglEnd();
  387. }
  388. }
  389. }
  390. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_SELBRUSHES].ToFloatPtr());
  391. qglBegin(GL_LINE_LOOP);
  392. qglVertex2f(-xCam, brush->mins[2]);
  393. qglVertex2f(xCam, brush->mins[2]);
  394. qglVertex2f(xCam, brush->maxs[2]);
  395. qglVertex2f(-xCam, brush->maxs[2]);
  396. qglEnd();
  397. }
  398. ZDrawCameraIcon();
  399. ZDrawZClip();
  400. qglFinish();
  401. QE_CheckOpenGLForErrors();
  402. }