00001
00002
00003
00004 #include "stdafx.h"
00005 #include "3dvis.h"
00006
00007 #include "3dvisDoc.h"
00008 #include "3dvisView.h"
00009
00010
00011 #include "cimagedata.h"
00012 #include "ctgaloader.h"
00013 #include <string>
00014 using std::string;
00015
00016
00017
00018
00019 #ifdef _DEBUG
00020 #define new DEBUG_NEW
00021 #undef THIS_FILE
00022 static char THIS_FILE[] = __FILE__;
00023 #endif
00024
00025
00026 Data *data3D;
00027 Slice *slice;
00028 Raycaster *raycaster;
00029
00030
00032
00033
00034 IMPLEMENT_DYNCREATE(CMy3dvisView, CView)
00035
00036 BEGIN_MESSAGE_MAP(CMy3dvisView, CView)
00037
00038 ON_WM_CREATE()
00039 ON_WM_SIZE()
00040 ON_WM_DESTROY()
00041 ON_WM_TIMER()
00042 ON_COMMAND(ID_FILE_SAVE_IMAGE, OnFileSaveImage)
00043
00044
00045 END_MESSAGE_MAP()
00046
00047
00048
00049
00050 CMy3dvisView::CMy3dvisView()
00051 {
00052
00053 }
00054
00055 CMy3dvisView::~CMy3dvisView() {
00056 }
00057
00058
00059 BOOL CMy3dvisView::PreCreateWindow(CREATESTRUCT& cs)
00060 {
00061
00062
00063 cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
00064
00065
00066 return CView::PreCreateWindow(cs);
00067 }
00068
00070
00071
00072 void CMy3dvisView::OnDraw(CDC* pDC)
00073 {
00074 CMy3dvisDoc* pDoc = GetDocument();
00075 ASSERT_VALID(pDoc);
00076
00077
00078 ::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00079
00080 RenderScene();
00081
00082 ::glFinish();
00083
00084 ::SwapBuffers( m_pDC->GetSafeHdc() );
00085 }
00086
00087
00088
00089
00090
00091
00092 BOOL CMy3dvisView::SetupWindow() {
00093 m_pDC = new CClientDC(this);
00094
00095 if(m_pDC == NULL) {
00096 MessageBox("Error Obtaining DC");
00097 return FALSE;
00098 }
00099
00100 if(!SetupPixelFormat()) {
00101 return FALSE;
00102 }
00103
00104 m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
00105
00106 if(m_hRC == 0) {
00107 MessageBox("Error Creating RC");
00108 return FALSE;
00109 }
00110
00111 if(::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE) {
00112 MessageBox("Error making RC Current");
00113 return FALSE;
00114 }
00115
00116 return TRUE;
00117 }
00118
00119
00120 BOOL CMy3dvisView::SetupPixelFormat() {
00121 static PIXELFORMATDESCRIPTOR pfd = {
00122 sizeof(PIXELFORMATDESCRIPTOR),
00123 1,
00124 PFD_DRAW_TO_WINDOW |
00125 PFD_SUPPORT_OPENGL |
00126 PFD_DOUBLEBUFFER,
00127 PFD_TYPE_RGBA,
00128 24,
00129 0, 0, 0, 0, 0, 0,
00130 0,
00131 0,
00132 0,
00133 0, 0, 0, 0,
00134 16,
00135 0,
00136 0,
00137 PFD_MAIN_PLANE,
00138 0,
00139 0, 0, 0
00140 };
00141
00142
00143 int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
00144
00145 if(m_nPixelFormat == 0) {
00146 return FALSE;
00147 }
00148
00149 if(::SetPixelFormat(m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd) == FALSE) {
00150 return FALSE;
00151 }
00152
00153 return TRUE;
00154 }
00155
00156
00157
00158
00159
00160 BOOL CMy3dvisView::InitGL() {
00161
00162 if (!SetupWindow())
00163 return FALSE;
00164
00165
00166 ::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
00167
00168 ::glClearDepth(1.0f);
00169
00170 ::glEnable(GL_DEPTH_TEST);
00171
00172
00173 m_nTimerID = SetTimer(1, 50, NULL);
00174
00175 ASSERT(m_nTimerID != NULL);
00176
00177 return TRUE;
00178 }
00179
00180
00181
00182 void CMy3dvisView::RenderScene() {
00183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00184 glLoadIdentity();
00185
00186 if (!((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume && slice)
00187 slice->DrawSlice(m_width, m_height);
00188 else if (((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume && raycaster)
00189 raycaster->Render(m_width, m_height);
00190 }
00191
00192
00194
00195
00196 #ifdef _DEBUG
00197 void CMy3dvisView::AssertValid() const
00198 {
00199 CView::AssertValid();
00200 }
00201
00202 void CMy3dvisView::Dump(CDumpContext& dc) const
00203 {
00204 CView::Dump(dc);
00205 }
00206
00207 CMy3dvisDoc* CMy3dvisView::GetDocument()
00208 {
00209 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3dvisDoc)));
00210 return (CMy3dvisDoc*)m_pDocument;
00211 }
00212 #endif //_DEBUG
00213
00215
00216
00217 int CMy3dvisView::OnCreate(LPCREATESTRUCT lpCreateStruct)
00218 {
00219 if (CView::OnCreate(lpCreateStruct) == -1)
00220 return -1;
00221
00222
00223 InitGL();
00224
00225
00226 return 0;
00227 }
00228
00229 void CMy3dvisView::OnSize(UINT nType, int cx, int cy)
00230 {
00231 CView::OnSize(nType, cx, cy);
00232
00233
00234 if (cy == 0) {
00235 cy = 1;
00236 }
00237
00238 ::glViewport(0, 0, cx, cy);
00239
00240
00241 ::glMatrixMode(GL_PROJECTION);
00242 ::glLoadIdentity();
00243
00244
00245 ::glOrtho(0.0f, cx, 0.0f, cy, -1.0, 1.0);
00246
00247 ::glMatrixMode(GL_MODELVIEW);
00248 ::glLoadIdentity();
00249
00250 m_width = cx;
00251 m_height = cy;
00252 }
00253
00254
00255 void CMy3dvisView::OnDestroy()
00256 {
00257 CView::OnDestroy();
00258
00259
00260
00261 if(::wglMakeCurrent (0,0) == FALSE) {
00262 MessageBox("Could not make RC non-current");
00263 }
00264
00265 if(::wglDeleteContext (m_hRC)==FALSE) {
00266 MessageBox("Could not delete RC");
00267 }
00268
00269 if(m_pDC) {
00270 delete m_pDC;
00271 }
00272
00273 m_pDC = NULL;
00274
00275 }
00276
00277 void CMy3dvisView::OnTimer(UINT nIDEvent)
00278 {
00279
00280 ::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00281 RenderScene();
00282 ::glFinish();
00283 ::SwapBuffers(m_pDC->GetSafeHdc());
00284
00285 CView::OnTimer(nIDEvent);
00286 }
00287
00288
00289 void CMy3dvisView::OnFileSaveImage()
00290 {
00291
00292 if (!slice && !raycaster)
00293 return;
00294
00295 ImageData_t dat;
00296 dat.dBpp = 3;
00297 dat.cColorInfo = 0;
00298
00299
00300 if (!((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume)
00301 dat.pcImageData = (unsigned char *)slice->GetScreenShotImage(dat.dWidth, dat.dHeight);
00302 else if (((CMy3dvisApp *)AfxGetApp())->m_bRenderVolume)
00303 dat.pcImageData = (unsigned char *)raycaster->GetScreenShotImage(dat.dWidth, dat.dHeight);
00304
00305
00306 dat.dSize = dat.dWidth * dat.dHeight * 3;
00307
00308
00309 CTGALoader screenShot;
00310 if (!screenShot.SaveTgaImage(((CMy3dvisApp *)AfxGetApp())->programPath, &dat))
00311 MessageBox("couldn't take screenshot");
00312 else
00313 MessageBox("screenshot saved");
00314 }