home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / frontend / flyEditor / flyRenderView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-12  |  9.3 KB  |  436 lines

  1. // flyRenderView.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "flyEditor.h"
  6. #include "flyEditorDoc.h"
  7. #include "flyEditorView.h"
  8. #include "flyRenderView.h"
  9. #include "LeftView.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. CFlyRenderView *rv=0;
  18. extern CFlyEditorView *lv;
  19. extern CLeftView *tv;
  20. extern int active_mode;
  21. extern bsp_object *active_sel;
  22. extern CDialogBar *dlgBar;
  23.  
  24. int anim_framerate=10;
  25. int drawflag=0;
  26. float zoom=100;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CFlyRenderView
  30.  
  31. IMPLEMENT_DYNCREATE(CFlyRenderView, CView)
  32.  
  33. CFlyRenderView::CFlyRenderView()
  34. {
  35.     timer=0;
  36.     render_type=0;
  37.     rv=this;
  38.     rot.load_identity();
  39.     flag=0;
  40.     mouse=0;
  41. }
  42.  
  43. CFlyRenderView::~CFlyRenderView()
  44. {
  45.     rv=0;
  46. }
  47.  
  48. BEGIN_MESSAGE_MAP(CFlyRenderView, CView)
  49.     //{{AFX_MSG_MAP(CFlyRenderView)
  50.     ON_WM_CREATE()
  51.     ON_WM_DESTROY()
  52.     ON_WM_SIZE()
  53.     ON_WM_TIMER()
  54.     ON_WM_ERASEBKGND()
  55.     ON_WM_LBUTTONDOWN()
  56.     ON_WM_LBUTTONUP()
  57.     ON_WM_MOUSEMOVE()
  58.     ON_WM_MOUSEWHEEL()
  59.     ON_WM_RBUTTONDOWN()
  60.     ON_WM_RBUTTONUP()
  61.     //}}AFX_MSG_MAP
  62. END_MESSAGE_MAP()
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CFlyRenderView drawing
  66.  
  67. void CFlyRenderView::OnDraw(CDC* pDC)
  68. {
  69.     CListCtrl& list=lv->GetListCtrl();
  70.     CTreeCtrl& tree=tv->GetTreeCtrl();
  71.     HTREEITEM item=tree.GetSelectedItem();
  72.     if (item==0) return;
  73.     DWORD type=tree.GetItemData(item);
  74.  
  75.     switch(type)
  76.     {
  77.     case TREE_TYPE_TEXTURES:
  78.         {
  79.         POSITION pos=list.GetFirstSelectedItemPosition();
  80.         if (pos==0)
  81.             break;
  82.         int i=list.GetNextSelectedItem(pos);
  83.         if (flyengine->piclib[i]->buf)
  84.             DrawPicture(pDC,flyengine->piclib[i]->buf[0],flyengine->piclib[i]->sx,flyengine->piclib[i]->sy,flyengine->piclib[i]->bytespixel);
  85.         }
  86.         break;
  87.     case TREE_TYPE_OBJECTS:
  88.         {
  89.         POSITION pos=list.GetFirstSelectedItemPosition();
  90.         if (pos==0)
  91.             break;
  92.         int i=list.GetNextSelectedItem(pos);
  93.         char buf[255];
  94.         list.GetItemText(i,0,buf,255);
  95.         DrawObject(buf);
  96.         }
  97.         break;
  98.     case TREE_TYPE_LIGHTMAPS:
  99.         {
  100.         POSITION pos=list.GetFirstSelectedItemPosition();
  101.         if (pos==0)
  102.             break;
  103.         int i=list.GetNextSelectedItem(pos);
  104.         DrawPicture(pDC,flyengine->lmpic[i]->bmp,flyengine->lmpic[i]->sizex,flyengine->lmpic[i]->sizey,3);
  105.         }
  106.         break;
  107.     case TREE_TYPE_SOUNDS:
  108.         break;
  109.  
  110.     default:
  111.         if (active_mode)
  112.         {
  113.             if (drawflag && type>255)
  114.                 DrawObject(active_sel);
  115.             else
  116.             if (rend) 
  117.                 rend->DrawView();
  118.         }
  119.         else
  120.         {
  121.             if (drawflag && type>255)
  122.                 DrawObject((bsp_object *)type);
  123.             else
  124.             if (rend) 
  125.                 rend->DrawView();
  126.         }
  127.         break;
  128.     }
  129. }
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CFlyRenderView diagnostics
  133.  
  134. CFlyEditorDoc* CFlyRenderView::GetDocument() // non-debug version is inline
  135. {
  136.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFlyEditorDoc)));
  137.     return (CFlyEditorDoc*)m_pDocument;
  138. }
  139.  
  140. #ifdef _DEBUG
  141. void CFlyRenderView::AssertValid() const
  142. {
  143.     CView::AssertValid();
  144. }
  145.  
  146. void CFlyRenderView::Dump(CDumpContext& dc) const
  147. {
  148.     CView::Dump(dc);
  149. }
  150. #endif //_DEBUG
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CFlyRenderView message handlers
  154.  
  155. int CFlyRenderView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  156. {
  157.     if (CView::OnCreate(lpCreateStruct) == -1)
  158.         return -1;
  159.  
  160.     return 0;
  161. }
  162.  
  163. void CFlyRenderView::OnDestroy() 
  164. {
  165.     CView::OnDestroy();
  166.  
  167.     if (timer)
  168.         {
  169.         KillTimer(rv->timer);
  170.         timer=0;
  171.         }
  172.     
  173.     GetDocument()->close_texture_cache();
  174.     free_render();
  175. }
  176.  
  177. void CFlyRenderView::OnTimer(UINT nIDEvent) 
  178. {
  179.     CListCtrl& list=lv->GetListCtrl();
  180.     CTreeCtrl& tree=tv->GetTreeCtrl();
  181.     HTREEITEM item=tree.GetSelectedItem();
  182.     DWORD type=0;
  183.     if (item!=0) 
  184.         type=tree.GetItemData(item);
  185.  
  186.     if (type==TREE_TYPE_OBJECTS)
  187.     {
  188.         if (rend) Invalidate(0);
  189.     }
  190.     else
  191.     if (type==TREE_TYPE_TEXTURES ||
  192.         type==TREE_TYPE_LIGHTMAPS ||
  193.         type==TREE_TYPE_SOUNDS)
  194.         ;
  195.     else
  196.     {
  197.         flyengine->step();
  198.     
  199.         if (active_mode)
  200.             lv->update_param_list();
  201.     
  202.         if (rend)
  203.             Invalidate(0);
  204.     }
  205.     
  206.     CView::OnTimer(nIDEvent);
  207. }
  208.  
  209. BOOL CFlyRenderView::OnEraseBkgnd(CDC* pDC) 
  210. {
  211.     CTreeCtrl& tree=tv->GetTreeCtrl();
  212.     HTREEITEM item=tree.GetSelectedItem();
  213.     if (item==0) return CView::OnEraseBkgnd(pDC);
  214.     DWORD type=tree.GetItemData(item);
  215.     if (rend==0 || 
  216.         type==TREE_TYPE_TEXTURES ||
  217.         type==TREE_TYPE_LIGHTMAPS ||
  218.         type==TREE_TYPE_OBJECTS ||
  219.         type==TREE_TYPE_SOUNDS)
  220.         return CView::OnEraseBkgnd(pDC);
  221.     else return 1;
  222. }
  223.  
  224. void CFlyRenderView::DrawObject(char *name)
  225. {
  226.     if (rend==0) return;
  227.     
  228.     mesh *o=(mesh *)flyengine->get_model_object(name);
  229.     if (o==0) return;
  230.  
  231.     if (timer==1 && strstr(o->name,".fao"))
  232.         {
  233.         int anim_time=((anim_mesh *)o)->nframes*1000/anim_framerate;
  234.         ((anim_mesh *)o)->set_key((timeGetTime()%anim_time)/(float)anim_time);
  235.         }
  236.  
  237.     float dist=(o->bbox.max-o->bbox.min).length();
  238.     o->color.vec(1,1,1,1);
  239.  
  240.     glClearColor(1,1,1,1);
  241.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  242.  
  243.     glMatrixMode( GL_PROJECTION );
  244.     glLoadIdentity();
  245.     gluPerspective( flyengine->camangle, flyengine->aspect, flyengine->viewmindist, flyengine->viewmaxdist);
  246.     glMatrixMode( GL_MODELVIEW );
  247.     glLoadIdentity();
  248.     glTranslatef(0,0,-dist-zoom);
  249.     glMultMatrixf((float *)&rot.m);
  250.     
  251.     o->draw();
  252.  
  253.     glFinish();
  254.     SwapBuffers(((renderGL *)rend)->hdc);
  255. }
  256.  
  257. void CFlyRenderView::DrawObject(bsp_object *obj)
  258. {
  259.     if (rend==0) return;
  260.  
  261.     glClearColor(1,1,1,1);
  262.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  263.  
  264.     if (obj)
  265.         {
  266.         glMatrixMode( GL_PROJECTION );
  267.         glLoadIdentity();
  268.         gluPerspective( flyengine->camangle, flyengine->aspect, flyengine->viewmindist, flyengine->viewmaxdist);
  269.         glMatrixMode( GL_MODELVIEW );
  270.         glLoadIdentity();
  271.         glTranslatef(0,0,-zoom);
  272.         glMultMatrixf((float *)&rot.m);
  273.         glTranslatef(-obj->pos.x,-obj->pos.y,-obj->pos.z);
  274.         obj->draw();
  275.         }
  276.  
  277.     glFinish();
  278.     SwapBuffers(((renderGL *)rend)->hdc);
  279. }
  280.  
  281. void CFlyRenderView::DrawPicture(CDC* pDC,unsigned char *buf,int sx,int sy,int bytespixel)
  282. {
  283.     struct tagBITMAPINFO bmi;
  284.     memset(&bmi,0,sizeof(bmi));
  285.  
  286.     bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  287.     bmi.bmiHeader.biCompression=BI_RGB;
  288.     bmi.bmiHeader.biXPelsPerMeter=6000;
  289.     bmi.bmiHeader.biYPelsPerMeter=6000;
  290.     bmi.bmiHeader.biClrUsed=0;
  291.     bmi.bmiHeader.biClrImportant=0;
  292.     bmi.bmiHeader.biSizeImage=0;
  293.     bmi.bmiHeader.biWidth=sx;
  294.     bmi.bmiHeader.biHeight=sy;
  295.     bmi.bmiHeader.biPlanes=1;
  296.     bmi.bmiHeader.biBitCount=bytespixel*8;
  297.  
  298.     int i,j,tx=sx*bytespixel;
  299.     if (tx%4) tx+=(4-tx%4);
  300.     unsigned char *buf2=new unsigned char[tx*sy],*c,cc;
  301.     for( i=0;i<sy;i++ )
  302.         {
  303.         c=&buf2[tx*(sy-i-1)];
  304.         memcpy(c,&buf[i*sx*bytespixel],sx*bytespixel);
  305.         for( j=0;j<sx;j++,c+=bytespixel )
  306.             {
  307.             cc=c[2];
  308.             c[2]=c[0];
  309.             c[0]=cc;
  310.             }
  311.         }
  312.  
  313.     if (flag%2)
  314.         {
  315.         RECT rect;
  316.         GetClientRect(&rect);
  317.         StretchDIBits(pDC->m_hDC,0,0,rect.right,rect.bottom,0,0,sx,sy,buf2,&bmi,DIB_RGB_COLORS,SRCCOPY);
  318.         }
  319.     else StretchDIBits(pDC->m_hDC,0,0,sx,sy,0,0,sx,sy,buf2,&bmi,DIB_RGB_COLORS,SRCCOPY);
  320.  
  321.     delete buf2;
  322. }
  323.  
  324. void CFlyRenderView::OnLButtonDown(UINT nFlags, CPoint point) 
  325. {
  326.     CTreeCtrl& tree=tv->GetTreeCtrl();
  327.     HTREEITEM item=tree.GetSelectedItem();
  328.     if (item==0) return;
  329.     DWORD type=tree.GetItemData(item);
  330.     if (type==TREE_TYPE_TEXTURES ||
  331.         type==TREE_TYPE_LIGHTMAPS)
  332.         {
  333.         flag++;
  334.         Invalidate(1);
  335.         }
  336.     else if (mouse==0 && 
  337.             (type==TREE_TYPE_OBJECTS || (drawflag && type>255)))
  338.     {
  339.         mouse=1;
  340.         mousepoint=point;
  341.         SetCapture();
  342.     }
  343.     
  344.     CView::OnLButtonDown(nFlags, point);
  345. }
  346.  
  347. void CFlyRenderView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  348. {
  349.     flyengine->noinput=(bActivate==0);
  350.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  351. }
  352.  
  353. void CFlyRenderView::OnSize(UINT nType, int cx, int cy) 
  354. {
  355.     CView::OnSize(nType, cx, cy);
  356.  
  357.     if (rend)
  358.         rend->ResizeView(cx,cy);
  359. }
  360.  
  361. void CFlyRenderView::OnLButtonUp(UINT nFlags, CPoint point) 
  362. {
  363.     if (mouse)
  364.     {
  365.         mouse=0;
  366.         ReleaseCapture();
  367.     }
  368.     CView::OnLButtonUp(nFlags, point);
  369. }
  370.  
  371. void CFlyRenderView::OnMouseMove(UINT nFlags, CPoint point) 
  372. {
  373.     if (mouse==1)
  374.     {
  375.         vector x(rot.m[0][0],rot.m[1][0],rot.m[2][0]);
  376.         vector y(rot.m[0][1],rot.m[1][1],rot.m[2][1]);
  377.         x.normalize();
  378.         y.normalize();
  379.         mat4x4 r;
  380.         r.load_identity();
  381.         r.rotate((point.x-mousepoint.x)*0.5f,y);
  382.         r.rotate((point.y-mousepoint.y)*0.5f,x);
  383.         rot=r*rot;
  384.         mousepoint=point;
  385.         Invalidate(0);
  386.     }
  387.     else if (mouse==2)
  388.     {
  389.         zoom+=(point.x-mousepoint.x)+(point.y-mousepoint.y);
  390.         mousepoint=point;
  391.         Invalidate(0);
  392.     }
  393.  
  394.     CView::OnMouseMove(nFlags, point);
  395. }
  396.  
  397.  
  398. BOOL CFlyRenderView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
  399. {
  400.     zoom-=zDelta*0.1f;    
  401.     if (zoom<10)
  402.         zoom=10;
  403.     else if (zoom>flyengine->viewmaxdist)
  404.             zoom=flyengine->viewmaxdist;
  405.     Invalidate(0);
  406.     return CView::OnMouseWheel(nFlags, zDelta, pt);
  407. }
  408.  
  409. void CFlyRenderView::OnRButtonDown(UINT nFlags, CPoint point) 
  410. {
  411.     CTreeCtrl& tree=tv->GetTreeCtrl();
  412.     HTREEITEM item=tree.GetSelectedItem();
  413.     if (item==0) return;
  414.     DWORD type=tree.GetItemData(item);
  415.     if (mouse==0 && 
  416.         (type==TREE_TYPE_OBJECTS || (drawflag && type>255)))    
  417.     {
  418.         mouse=2;
  419.         mousepoint=point;
  420.         SetCapture();
  421.     }
  422.     
  423.     CView::OnRButtonDown(nFlags, point);
  424. }
  425.  
  426. void CFlyRenderView::OnRButtonUp(UINT nFlags, CPoint point) 
  427. {
  428.     if (mouse)
  429.     {
  430.         mouse=0;
  431.         ReleaseCapture();
  432.     }
  433.     
  434.     CView::OnRButtonUp(nFlags, point);
  435. }
  436.