Z.CPP 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "stdafx.h"
  19. #include "qe3.h"
  20. #define PAGEFLIPS 2
  21. z_t z;
  22. /*
  23. ============
  24. Z_Init
  25. ============
  26. */
  27. void Z_Init (void)
  28. {
  29. z.origin[0] = 0;
  30. z.origin[1] = 20;
  31. z.origin[2] = 46;
  32. z.scale = 1;
  33. }
  34. /*
  35. ============================================================================
  36. MOUSE ACTIONS
  37. ============================================================================
  38. */
  39. static int cursorx, cursory;
  40. /*
  41. ==============
  42. Z_MouseDown
  43. ==============
  44. */
  45. void Z_MouseDown (int x, int y, int buttons)
  46. {
  47. vec3_t org, dir, vup, vright;
  48. brush_t *b;
  49. Sys_GetCursorPos (&cursorx, &cursory);
  50. vup[0] = 0; vup[1] = 0; vup[2] = 1/z.scale;
  51. VectorCopy (z.origin, org);
  52. org[2] += (y - (z.height/2))/z.scale;
  53. org[1] = -8192;
  54. b = selected_brushes.next;
  55. if (b != &selected_brushes)
  56. {
  57. org[0] = (b->mins[0] + b->maxs[0])/2;
  58. }
  59. dir[0] = 0; dir[1] = 1; dir[2] = 0;
  60. vright[0] = 0; vright[1] = 0; vright[2] = 0;
  61. // LBUTTON = manipulate selection
  62. // shift-LBUTTON = select
  63. // middle button = grab texture
  64. // ctrl-middle button = set entire brush to texture
  65. // ctrl-shift-middle button = set single face to texture
  66. int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
  67. if ( (buttons == MK_LBUTTON)
  68. || (buttons == (MK_LBUTTON | MK_SHIFT))
  69. || (buttons == MK_MBUTTON)
  70. // || (buttons == (MK_MBUTTON|MK_CONTROL))
  71. || (buttons == (nMouseButton|MK_SHIFT|MK_CONTROL)) )
  72. {
  73. Drag_Begin (x, y, buttons,
  74. vright, vup,
  75. org, dir);
  76. return;
  77. }
  78. // control mbutton = move camera
  79. if ((buttons == (MK_CONTROL|nMouseButton) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
  80. {
  81. g_pParentWnd->GetCamera()->Camera().origin[2] = org[2] ;
  82. Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
  83. }
  84. }
  85. /*
  86. ==============
  87. Z_MouseUp
  88. ==============
  89. */
  90. void Z_MouseUp (int x, int y, int buttons)
  91. {
  92. Drag_MouseUp ();
  93. }
  94. /*
  95. ==============
  96. Z_MouseMoved
  97. ==============
  98. */
  99. void Z_MouseMoved (int x, int y, int buttons)
  100. {
  101. if (!buttons)
  102. return;
  103. if (buttons == MK_LBUTTON)
  104. {
  105. Drag_MouseMoved (x, y, buttons);
  106. Sys_UpdateWindows (W_Z|W_CAMERA_IFON|W_XY);
  107. return;
  108. }
  109. // rbutton = drag z origin
  110. if (buttons == MK_RBUTTON)
  111. {
  112. Sys_GetCursorPos (&x, &y);
  113. if ( y != cursory)
  114. {
  115. z.origin[2] += y-cursory;
  116. Sys_SetCursorPos (cursorx, cursory);
  117. Sys_UpdateWindows (W_Z);
  118. }
  119. return;
  120. }
  121. // control mbutton = move camera
  122. int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
  123. if ((buttons == (MK_CONTROL|nMouseButton) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
  124. {
  125. g_pParentWnd->GetCamera()->Camera().origin[2] = (y - (z.height/2))/z.scale;
  126. Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
  127. }
  128. }
  129. /*
  130. ============================================================================
  131. DRAWING
  132. ============================================================================
  133. */
  134. /*
  135. ==============
  136. Z_DrawGrid
  137. ==============
  138. */
  139. void Z_DrawGrid (void)
  140. {
  141. float zz, zb, ze;
  142. int w, h;
  143. char text[32];
  144. w = z.width/2 / z.scale;
  145. h = z.height/2 / z.scale;
  146. zb = z.origin[2] - h;
  147. if (zb < region_mins[2])
  148. zb = region_mins[2];
  149. zb = 64 * floor (zb/64);
  150. ze = z.origin[2] + h;
  151. if (ze > region_maxs[2])
  152. ze = region_maxs[2];
  153. ze = 64 * ceil (ze/64);
  154. // draw major blocks
  155. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR]);
  156. qglBegin (GL_LINES);
  157. qglVertex2f (0, zb);
  158. qglVertex2f (0, ze);
  159. for (zz=zb ; zz<ze ; zz+=64)
  160. {
  161. qglVertex2f (-w, zz);
  162. qglVertex2f (w, zz);
  163. }
  164. qglEnd ();
  165. // draw minor blocks
  166. if (g_qeglobals.d_showgrid && g_qeglobals.d_gridsize*z.scale >= 4 &&
  167. g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR] != g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK])
  168. {
  169. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR]);
  170. qglBegin (GL_LINES);
  171. for (zz=zb ; zz<ze ; zz+=g_qeglobals.d_gridsize)
  172. {
  173. if ( ! ((int)zz & 63) )
  174. continue;
  175. qglVertex2f (-w, zz);
  176. qglVertex2f (w, zz);
  177. }
  178. qglEnd ();
  179. }
  180. // draw coordinate text if needed
  181. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDTEXT]);
  182. for (zz=zb ; zz<ze ; zz+=64)
  183. {
  184. qglRasterPos2f (-w+1, zz);
  185. sprintf (text, "%i",(int)zz);
  186. qglCallLists (strlen(text), GL_UNSIGNED_BYTE, text);
  187. }
  188. }
  189. #define CAM_HEIGHT 48 // height of main part
  190. #define CAM_GIZMO 8 // height of the gizmo
  191. void ZDrawCameraIcon (void)
  192. {
  193. float x, y;
  194. int xCam = z.width/4;
  195. x = 0;
  196. y = g_pParentWnd->GetCamera()->Camera().origin[2];
  197. qglColor3f (0.0, 0.0, 1.0);
  198. qglBegin(GL_LINE_STRIP);
  199. qglVertex3f (x-xCam,y,0);
  200. qglVertex3f (x,y+CAM_GIZMO,0);
  201. qglVertex3f (x+xCam,y,0);
  202. qglVertex3f (x,y-CAM_GIZMO,0);
  203. qglVertex3f (x-xCam,y,0);
  204. qglVertex3f (x+xCam,y,0);
  205. qglVertex3f (x+xCam,y-CAM_HEIGHT,0);
  206. qglVertex3f (x-xCam,y-CAM_HEIGHT,0);
  207. qglVertex3f (x-xCam,y,0);
  208. qglEnd ();
  209. }
  210. GLbitfield glbitClear = GL_COLOR_BUFFER_BIT; //HACK
  211. /*
  212. ==============
  213. Z_Draw
  214. ==============
  215. */
  216. void Z_Draw (void)
  217. {
  218. brush_t *brush;
  219. float w, h;
  220. double start, end;
  221. qtexture_t *q;
  222. float top, bottom;
  223. vec3_t org_top, org_bottom, dir_up, dir_down;
  224. int xCam = z.width/3;
  225. if (!active_brushes.next)
  226. return; // not valid yet
  227. if (z.timing)
  228. start = Sys_DoubleTime ();
  229. //
  230. // clear
  231. //
  232. qglViewport(0, 0, z.width, z.height);
  233. qglClearColor (
  234. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
  235. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
  236. g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
  237. 0);
  238. /* GL Bug */
  239. /* When not using hw acceleration, gl will fault if we clear the depth
  240. buffer bit on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT
  241. only after Z_Draw() has been called once. Yeah, right. */
  242. qglClear(glbitClear);
  243. glbitClear |= GL_DEPTH_BUFFER_BIT;
  244. qglMatrixMode(GL_PROJECTION);
  245. qglLoadIdentity ();
  246. w = z.width/2 / z.scale;
  247. h = z.height/2 / z.scale;
  248. qglOrtho (-w, w, z.origin[2]-h, z.origin[2]+h, -8, 8);
  249. qglDisable(GL_TEXTURE_2D);
  250. qglDisable(GL_TEXTURE_1D);
  251. qglDisable(GL_DEPTH_TEST);
  252. qglDisable(GL_BLEND);
  253. //
  254. // now draw the grid
  255. //
  256. Z_DrawGrid ();
  257. //
  258. // draw stuff
  259. //
  260. qglDisable(GL_CULL_FACE);
  261. qglShadeModel (GL_FLAT);
  262. qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  263. qglDisable(GL_TEXTURE_2D);
  264. qglDisable(GL_BLEND);
  265. qglDisable(GL_DEPTH_TEST);
  266. // draw filled interiors and edges
  267. dir_up[0] = 0 ; dir_up[1] = 0; dir_up[2] = 1;
  268. dir_down[0] = 0 ; dir_down[1] = 0; dir_down[2] = -1;
  269. VectorCopy (z.origin, org_top);
  270. org_top[2] = 4096;
  271. VectorCopy (z.origin, org_bottom);
  272. org_bottom[2] = -4096;
  273. for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)
  274. {
  275. if (brush->mins[0] >= z.origin[0]
  276. || brush->maxs[0] <= z.origin[0]
  277. || brush->mins[1] >= z.origin[1]
  278. || brush->maxs[1] <= z.origin[1])
  279. continue;
  280. if (!Brush_Ray (org_top, dir_down, brush, &top))
  281. continue;
  282. top = org_top[2] - top;
  283. if (!Brush_Ray (org_bottom, dir_up, brush, &bottom))
  284. continue;
  285. bottom = org_bottom[2] + bottom;
  286. q = Texture_ForName (brush->brush_faces->texdef.name);
  287. qglColor3f (q->color[0], q->color[1], q->color[2]);
  288. qglBegin (GL_QUADS);
  289. qglVertex2f (-xCam, bottom);
  290. qglVertex2f (xCam, bottom);
  291. qglVertex2f (xCam, top);
  292. qglVertex2f (-xCam, top);
  293. qglEnd ();
  294. qglColor3f (1,1,1);
  295. qglBegin (GL_LINE_LOOP);
  296. qglVertex2f (-xCam, bottom);
  297. qglVertex2f (xCam, bottom);
  298. qglVertex2f (xCam, top);
  299. qglVertex2f (-xCam, top);
  300. qglEnd ();
  301. }
  302. //
  303. // now draw selected brushes
  304. //
  305. for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
  306. {
  307. if ( !(brush->mins[0] >= z.origin[0]
  308. || brush->maxs[0] <= z.origin[0]
  309. || brush->mins[1] >= z.origin[1]
  310. || brush->maxs[1] <= z.origin[1]) )
  311. {
  312. if (Brush_Ray (org_top, dir_down, brush, &top))
  313. {
  314. top = org_top[2] - top;
  315. if (Brush_Ray (org_bottom, dir_up, brush, &bottom))
  316. {
  317. bottom = org_bottom[2] + bottom;
  318. q = Texture_ForName (brush->brush_faces->texdef.name);
  319. qglColor3f (q->color[0], q->color[1], q->color[2]);
  320. qglBegin (GL_QUADS);
  321. qglVertex2f (-xCam, bottom);
  322. qglVertex2f (xCam, bottom);
  323. qglVertex2f (xCam, top);
  324. qglVertex2f (-xCam, top);
  325. qglEnd ();
  326. }
  327. }
  328. }
  329. qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_SELBRUSHES]);
  330. qglBegin (GL_LINE_LOOP);
  331. qglVertex2f (-xCam, brush->mins[2]);
  332. qglVertex2f (xCam, brush->mins[2]);
  333. qglVertex2f (xCam, brush->maxs[2]);
  334. qglVertex2f (-xCam, brush->maxs[2]);
  335. qglEnd ();
  336. }
  337. ZDrawCameraIcon ();
  338. qglFinish();
  339. QE_CheckOpenGLForErrors();
  340. if (z.timing)
  341. {
  342. end = Sys_DoubleTime ();
  343. Sys_Printf ("z: %i ms\n", (int)(1000*(end-start)));
  344. }
  345. }