home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / Z.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  8.7 KB  |  411 lines

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