z.c 8.7 KB

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