C:/Users/thebigMuh/Documents/Visual Studio 2005/Projects/Volumizer/Volumizer/RayWnd.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 #include "Volumizer.h"
00008 
00009 HDC             rayHdc;
00010 HGLRC   rayHglrc;
00011 
00012 camera  cam;            
00013 vect    ldir;           
00014 vect    lpos;           
00015 
00019 void InitCam()
00020 {
00021         cam.rotx = 0.0f;
00022         cam.roty = 0.6f;
00023 
00024         cam.org.x = (float)(volx / 2);
00025         cam.org.y = (float)(voly / 2);
00026         cam.org.z = (float)(volz / 2);
00027 
00028         cam.pos.x = cam.org.x + (float)volx * 2.0f;
00029         cam.pos.y = cam.org.y;
00030         cam.pos.z = cam.org.z;
00031 
00032         cam.up.x = 0.0f;
00033         cam.up.y = 0.0f;
00034         cam.up.z = 1.0f;
00035 }
00036 
00040 void InitLight()
00041 {
00042         lpos.x = 0.625f;
00043         lpos.y = 0.25f;
00044 
00045         ldir.x = ldir.z = -1.0f;
00046         ldir.y = 1.0f;
00047 
00048         normalize(ldir);
00049 }
00050 
00054 ATOM RegisterRayClass(HINSTANCE hInstance)
00055 {
00056         WNDCLASSEX wcex;
00057 
00058         wcex.cbSize = sizeof(WNDCLASSEX);
00059 
00060         wcex.style                      = CS_HREDRAW | CS_VREDRAW;
00061         wcex.lpfnWndProc        = RayProc;
00062         wcex.cbClsExtra         = 0;
00063         wcex.cbWndExtra         = 0;
00064         wcex.hInstance          = hInstance;
00065         wcex.hIcon                      = NULL;
00066         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
00067         wcex.hbrBackground      = NULL;
00068         wcex.lpszMenuName       = NULL;
00069         wcex.lpszClassName      = L"RayWnd";
00070         wcex.hIconSm            = NULL;
00071 
00072         return RegisterClassEx(&wcex);
00073 }
00074 
00078 void InitRay()
00079 {
00080         rayHdc = GetDC(wnd_3d);
00081 
00082         PIXELFORMATDESCRIPTOR pfd = { 
00083     sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd 
00084     1,                                // version number 
00085     PFD_DRAW_TO_WINDOW |              // support window 
00086     PFD_SUPPORT_OPENGL |              // support OpenGL 
00087     PFD_DOUBLEBUFFER,                 // double buffered 
00088     PFD_TYPE_RGBA,                    // RGBA type 
00089     32,                               // 32-bit color depth 
00090     0, 0, 0, 0, 0, 0,                 // color bits ignored 
00091     0,                                // no alpha buffer 
00092     0,                                // shift bit ignored 
00093     0,                                // no accumulation buffer 
00094     0, 0, 0, 0,                       // accum bits ignored 
00095     32,                               // 32-bit z-buffer     
00096     0,                                // no stencil buffer 
00097     0,                                // no auxiliary buffer 
00098     PFD_MAIN_PLANE,                   // main layer 
00099     0,                                // reserved 
00100     0, 0, 0                           // layer masks ignored 
00101         }; 
00102         
00103         int  iPixelFormat; 
00104 
00106 // TODO: Insert better pfd selecting code... too lazy now. //
00108         
00109 
00110         // get the device context's best, available pixel format match 
00111         iPixelFormat = ChoosePixelFormat(rayHdc, &pfd); 
00112  
00113         // make that match the device context's current pixel format 
00114         SetPixelFormat(rayHdc, iPixelFormat, &pfd); 
00115 
00116     if (rayHglrc = wglCreateContext( rayHdc ) ) 
00117         { 
00118         // try to make it the thread's current rendering context 
00119         wglMakeCurrent(rayHdc, rayHglrc) ; 
00120     } 
00121 
00123 // All the basic things that never change //
00125 
00126         glEnable(GL_DEPTH_TEST);
00127         glEnable(GL_POINT_SMOOTH);
00128         glEnable(GL_LINE_SMOOTH);
00129         glEnable(GL_TEXTURE_2D);
00130 
00131         glEnable(GL_BLEND);
00132         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00133 
00134         glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
00135 
00136         glDepthFunc(GL_LEQUAL);
00137 
00138         glClearColor(0.0f,0.0f,0.0f,0.0f);
00139 
00140         glLineWidth(1.0f);
00141 
00142         // init our font display lists
00143         HFONT neuro;
00144 
00145         neuro = CreateFontW(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, L"Arial");
00146 
00147         SelectObject(rayHdc, neuro);
00148 
00149         wglUseFontOutlines(rayHdc, 0, 255, 1000, 0.0f, 0.0f,  
00150                 WGL_FONT_LINES, NULL); 
00151 }
00152 
00153 void DeInitRay()
00154 {
00156 // Kill the context & free it //
00158     if(rayHglrc = wglGetCurrentContext()) { 
00159  
00160         // obtain its associated device context 
00161         rayHdc = wglGetCurrentDC() ; 
00162  
00163         // make the rendering context not current 
00164         wglMakeCurrent(NULL, NULL) ; 
00165  
00166         // release the device context 
00167         ReleaseDC(wnd_3d, rayHdc) ; 
00168  
00169         // delete the rendering context 
00170         wglDeleteContext(rayHglrc); 
00171     } 
00172 }
00173 
00177 void DrawGrid()
00178 {
00179         glLineWidth(0.3f);
00180 
00181         glColor3f(0.5f, 0.5f, 0.5f);
00182 
00183         glBegin(GL_LINES);
00184 
00185         for (unsigned int x = 0; x <= volx; x += 10)
00186         {
00187                 glVertex3i(x, 0, 0);
00188                 glVertex3i(x, voly, 0);
00189 
00190                 glVertex3i(x, 0, 0);
00191                 glVertex3i(x, 0, volz);
00192         }
00193 
00194         for (unsigned int y = 0; y <= voly; y += 10)
00195         {
00196                 glVertex3i(0, y, 0);
00197                 glVertex3i(volx, y, 0);
00198 
00199                 glVertex3i(0, y, 0);
00200                 glVertex3i(0, y, volz);
00201         }
00202 
00203         for (unsigned int z = 0; z <= volz; z += 10)
00204         {
00205                 glVertex3i(0, 0, z);
00206                 glVertex3i(0, voly, z);
00207 
00208                 glVertex3i(0, 0, z);
00209                 glVertex3i(volx, 0, z);
00210         }
00211 
00212         glColor3f(1.0f, 0.0f, 0.0f);
00213         glVertex3f(0, 0, 0);
00214         glVertex3f((float)volx, 0, 0);
00215 
00216         glColor3f(0.0f, 1.0f, 0.0f);
00217         glVertex3f(0, 0, 0);
00218         glVertex3f(0, (float)voly, 0);
00219 
00220         glColor3f(0.0f, 0.0f, 1.0f);
00221         glVertex3f(0, 0, 0);
00222         glVertex3f(0, 0, (float)volz);
00223 
00224         glEnd();
00225 }
00226 
00230 void DrawLight()
00231 {
00232         glLineWidth(3.0f);
00233         glBegin(GL_LINES);
00234                 glColor3f(1.0f, 1.0f, 0.0f);
00235                 glVertex3f(ldir.x * -50.0f, ldir.y * -50.0f, ldir.z * -50.0f);
00236 
00237                 glColor3f(0.5f, 0.5f, 0.0f);
00238                 glVertex3f(0.0f, 0.0f, 0.0f);
00239         glEnd();
00240         glLineWidth(1.0f);
00241 }
00242 
00246 void DrawRay()
00247 {
00248         wglMakeCurrent(rayHdc, rayHglrc);
00249 
00250         RECT clientRect;
00251         GetClientRect(wnd_3d, &clientRect);
00252 
00253         glViewport(0, 0, clientRect.right, clientRect.bottom);
00254 
00255         glMatrixMode(GL_PROJECTION);
00256         glLoadIdentity();
00257 
00258         float windowaspect = (float)clientRect.right / (float)clientRect.bottom;
00259 
00260         gluPerspective(60.0, windowaspect, 1.0, 10000.0);
00261 
00262         vect dir;
00263         dir.x = cam.org.x - cam.pos.x;
00264         dir.y = cam.org.y - cam.pos.y;
00265         dir.z = cam.org.z - cam.pos.z;
00266 
00267         normalize(dir);
00268 
00269         vect up;
00270         up.x = 0.0f;
00271         up.y = 0.0f;
00272         up.z = 1.0f;
00273 
00274         vect right;
00275         right = cross(dir, up);
00276         normalize(right);
00277 
00278         cam.up = cross(right, dir);
00279 
00280         gluLookAt((double)cam.pos.x, (double)cam.pos.y, (double)cam.pos.z, 
00281                 (double)cam.org.x, (double)cam.org.y, (double)cam.org.z,
00282                 cam.up.x, cam.up.y, cam.up.z);
00283 
00284         glMatrixMode(GL_MODELVIEW);
00285         
00286         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00287 
00288         DrawGrid();
00289 
00290         DrawLight();
00291 
00292         glFinish();
00293 
00294         glMatrixMode(GL_PROJECTION);
00295         glLoadIdentity();
00296 
00297         gluOrtho2D(0, clientRect.right, clientRect.bottom, 0.0f);
00298 
00299         glMatrixMode(GL_MODELVIEW);
00300 
00301         glClear(GL_DEPTH_BUFFER_BIT);
00302 
00303         glFinish();
00304 
00305         SwapBuffers(rayHdc);
00306 }
00307 
00311 LRESULT CALLBACK RayProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00312 {
00313         PAINTSTRUCT ps;
00314         HDC hdc;
00315 
00316         static POINT spos;
00317 
00318         switch (message)
00319         {
00320         case WM_LBUTTONDOWN:
00321         case WM_MBUTTONDOWN:
00322         case WM_RBUTTONDOWN:
00323                 SetCapture(hWnd);
00324                 GetCursorPos(&spos);
00325                 break;
00326         case WM_LBUTTONUP:
00327         case WM_MBUTTONUP:
00328         case WM_RBUTTONUP:
00329                 ReleaseCapture();
00330 
00331                 RayRender();
00332 
00333                 break;
00334         case WM_MOUSEMOVE:
00335                 POINT mpos;
00336 
00337                 if (GetCapture() == hWnd)
00338                 {
00339                         GetCursorPos(&mpos);
00340                         
00342                         if ((wParam & MK_LBUTTON) && (wParam & MK_RBUTTON))
00343                         {
00344                                 lpos.x -= (float)(mpos.x - spos.x) / 500.0f;
00345                                 lpos.y += (float)(mpos.y - spos.y) / 500.0f;
00346 
00347                                 if (lpos.y >= 0.99f)
00348                                         lpos.y = 0.99f;
00349 
00350                                 if (lpos.y <= 0.01f)
00351                                         lpos.y = 0.01f;
00352 
00353                                 float t = 2.0f * lpos.y - 1.0f;
00354 
00355                                 float st = sqrt(1.0f - t*t);
00356 
00357                                 float phirad = (float)fmod(lpos.x, 1.0f) * -2.0f * (float)M_PI;
00358 
00359                                 ldir.x = st * cos(phirad);
00360                                 ldir.y = st * sin(phirad);
00361                                 ldir.z = t;
00362 
00363                                 normalize(ldir);
00364                         }
00365                         else if (wParam & MK_LBUTTON)
00366                         {
00368                                 cam.rotx += (float)(mpos.x - spos.x) / 500.0f;
00369                                 cam.roty += (float)(mpos.y - spos.y) / 500.0f;
00370 
00371                                 if (cam.roty >= 0.99f)
00372                                         cam.roty = 0.99f;
00373 
00374                                 if (cam.roty <= 0.01f)
00375                                         cam.roty = 0.01f;
00376 
00377                                 float t, st, phirad, dist;
00378 
00379                                 vect dir;
00380                                 dir.x = cam.org.x - cam.pos.x;
00381                                 dir.y = cam.org.y - cam.pos.y;
00382                                 dir.z = cam.org.z - cam.pos.z;
00383 
00384                                 dist = norm(dir);
00385 
00386                                 t = 2.0f * cam.roty - 1.0f;
00387                                 
00388                                 st = sqrt(1.0f - t*t);
00389                                 
00390                                 phirad = (float)fmod(cam.rotx, 1.0f) * -2.0f * (float)M_PI;
00391 
00392                                 cam.pos.x = cam.org.x + (st * cos(phirad)) * dist;
00393                                 cam.pos.y = cam.org.y + (st * sin(phirad)) * dist;
00394                                 cam.pos.z = cam.org.z + t * dist;
00395                         }
00396                         else if (wParam & MK_RBUTTON)
00397                         {
00399                                 vect dir;
00400                                 dir.x = cam.org.x - cam.pos.x;
00401                                 dir.y = cam.org.y - cam.pos.y;
00402                                 dir.z = cam.org.z - cam.pos.z;
00403 
00404                                 float dy = (float)(mpos.y - spos.y);
00405 
00406                                 cam.pos.x += dir.x * dy / 100.0f;
00407                                 cam.pos.y += dir.y * dy / 100.0f;
00408                                 cam.pos.z += dir.z * dy / 100.0f;
00409                         }
00410                         else if (wParam & MK_MBUTTON)
00411                         {
00413                                 vect dir;
00414                                 dir.x = cam.org.x - cam.pos.x;
00415                                 dir.y = cam.org.y - cam.pos.y;
00416                                 dir.z = cam.org.z - cam.pos.z;
00417 
00418                                 vect right = cross(cam.up, dir);
00419                                 normalize(right);
00420 
00421                                 vect up = cross(dir, right);
00422                                 normalize(up);
00423 
00424                                 right.x *= (float)(mpos.x - spos.x) / 5.0f;
00425                                 right.y *= (float)(mpos.x - spos.x) / 5.0f;
00426                                 right.z *= (float)(mpos.x - spos.x) / 5.0f;
00427 
00428                                 up.x *= (float)(mpos.y - spos.y) / 5.0f;
00429                                 up.y *= (float)(mpos.y - spos.y) / 5.0f;
00430                                 up.z *= (float)(mpos.y - spos.y) / 5.0f;
00431 
00432                                 cam.pos.x += right.x + up.x;
00433                                 cam.pos.y += right.y + up.y;
00434                                 cam.pos.z += right.z + up.z;
00435 
00436                                 cam.org.x += right.x + up.x;
00437                                 cam.org.y += right.y + up.y;
00438                                 cam.org.z += right.z + up.z;
00439                         }
00440 
00441                         DrawRay();
00442 
00443                         spos = mpos;
00444                 }
00445                 break;
00446         case WM_PAINT:
00447                 hdc = BeginPaint(hWnd, &ps);
00448                 DrawRay();
00449                 EndPaint(hWnd, &ps);
00450                 break;
00451         case WM_SHOWWINDOW:
00452                 InitRay();
00453                 break;
00454         case WM_DESTROY:
00455                 DeInitRay();
00456                 PostQuitMessage(0);
00457                 break;
00458         default:
00459                 return DefWindowProc(hWnd, message, wParam, lParam);
00460         }
00461         return 0;
00462 }
00463 

Generated on Wed Dec 5 12:07:30 2007 for Volumizer by  doxygen 1.5.4