00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "controlEngine.h"
00020 #include "object3d_instance.h"
00021 #include "systray.h"
00022 #include <math.h>
00023 #include "jitter.h"
00024
00025 extern ControlEngine ce;
00026
00068 ControlEngine::ControlEngine()
00069 {
00070 MyRegistry reg(FALSE);
00071 reg="HKCU\\Software\\3d controls";
00072 if (reg.TestKey())
00073 {
00074 MyString version;
00075 reg.GetValue("version",version,"?");
00076 if (version!=VERSION)
00077 {
00078 MyString text,caption;
00079 caption<<"3D Controls v"<<VERSION;
00080 text<<"Your 3D Engine version is different of the last running version launched (v"<<version<<")\nWhould you want to use default configuration ?";
00081 if (MessageBox(NULL,text,caption,MB_YESNO|MB_ICONINFORMATION)==IDYES)
00082 {
00083 reg.DeleteKey();
00084 }
00085 }
00086 }
00087 reg="\\light";
00088 reg.GetValue("move0",&lightsMove[0],DEFAULT_LIGHT_MOVE0);
00089 reg.GetValue("move1",&lightsMove[1],DEFAULT_LIGHT_MOVE1);
00090 reg.GetValue("move2",&lightsMove[2],DEFAULT_LIGHT_MOVE2);
00091 reg.GetValue("enable0",&lights[0].isEnable,DEFAULT_LIGHT_ENABLE0);
00092 reg.GetValue("enable1",&lights[1].isEnable,DEFAULT_LIGHT_ENABLE1);
00093 reg.GetValue("enable2",&lights[2].isEnable,DEFAULT_LIGHT_ENABLE2);
00094
00095 reg="..\\fog";
00096 reg.GetValue("enable",&fog.isEnable,DEFAULT_FOG_ENABLE);
00097 reg.GetValue("start",&fog.start,DEFAULT_FOG_START);
00098 reg.GetValue("end",&fog.end,DEFAULT_FOG_END);
00099 reg.GetValue("density",&fog.density,DEFAULT_FOG_DENSITY);
00100 reg.GetValue("mode",(int *)&fog.mode,DEFAULT_FOG_MODE);
00101
00102 reg="..";
00103 reg.GetValue("enableAntiAliasing",&enableAntiAliasing,DEFAULT_ANTIALIASING);
00104 reg.GetValue("antiAliasingQuality",&antiAliasingQuality,DEFAULT_ANTIALIASING_QUALITY);
00105 reg.GetValue("enableRepere",&enableRepere,DEFAULT_VIEW_REPERE);
00106 reg.GetValue("enableLightView",&enableLightView,DEFAULT_VIEW_LIGHT);
00107 reg.GetValue("enablePicking",&enablePicking,DEFAULT_PICKING);
00108 reg.GetValue("enableDelta",&enableDelta,DEFAULT_DELTA);
00109 reg.GetValue("enableBackGround",&enableBackGround,DEFAULT_VIEW_BACKGROUND);
00110 reg.GetValue("enableFPS",&enableFPS,DEFAULT_VIEW_FPS);
00111 reg.GetValue("enableInertia",&enableInertia,DEFAULT_INERTIA);
00112 reg.GetValue("enableMonitor",&monitor.enable,DEFAULT_MONITOR);
00113 reg.GetValue("enableViewInertia",&enableViewInertia,DEFAULT_VIEW_INERTIA);
00114
00115 reg.GetValue("enableFullScreen",&screenMain.enableFullScreen,DEFAULT_FULLSCREEN);
00116 reg.GetValue("enableTitle",&screenMain.enableTitle,DEFAULT_TITLED);
00117 reg.GetValue("enableRegion",&screenMain.region.enable,DEFAULT_VIEW_REGION);
00118
00119 reg.GetValue("polygonModeFront",(int *)&polygonMode.front,DEFAULT_POLYGON_FRONT);
00120 reg.GetValue("polygonModeBack",(int *)&polygonMode.back,DEFAULT_POLYGON_BACK);
00121
00122 int dx,dy;
00123 reg.GetValue("dxWin",&dx,DEFAULT_XSIZE);
00124 reg.GetValue("dyWin",&dy,DEFAULT_YSIZE);
00125 dxWin=dx;
00126 dyWin=dy;
00127
00128 reg.GetValue("timerRefresh",&refreshTime,DEFAULT_TIMER_REFRESH);
00129 reg.GetValue("cpuLimit",&cpuLimit,DEFAULT_CPU_LIMIT);
00130
00131 depth = 15;
00132
00133 hInst = NULL;
00134
00135 aspect = 0;
00136 enableCursor = FALSE;
00137 usingInertia = 0;
00138 for (int i=0;i<3;i++) eventInertia[i].Init(i);
00139
00140 tipFPS = 0;
00141 InitFont();
00142 fog.color.Set(0.22f,0.43f,0.64f,1);
00143
00144
00146 masterContainer = new MasterContainer;
00147 masterContainer->Create();
00148
00149 Push(masterContainer);
00150 Face trashFace;
00151 trashFace.name="Trash";
00152 trash.Add(trashFace);
00153
00154
00155
00156 univers = new Univers;
00157 univers->Create();
00158
00159 taskWindow = new TaskWindow;
00160 taskWindow->Create();
00161 Pop();
00162 InitMonitor();
00163 }
00164
00170 ControlEngine::~ControlEngine()
00171 {
00172 MyRegistry reg(TRUE);
00173 reg="HKCU\\Software\\3d controls";
00174 reg="\\light";
00175 reg.SetValue("move0",lightsMove[0]);
00176 reg.SetValue("move1",lightsMove[1]);
00177 reg.SetValue("move2",lightsMove[2]);
00178 reg.SetValue("enable0",lights[0].isEnable);
00179 reg.SetValue("enable1",lights[1].isEnable);
00180 reg.SetValue("enable2",lights[2].isEnable);
00181
00182 reg="..\\fog";
00183 reg.SetValue("enable",fog.isEnable);
00184 reg.SetValue("start",fog.start);
00185 reg.SetValue("end",fog.end);
00186 reg.SetValue("density",fog.density);
00187 reg.SetValue("mode",fog.mode);
00188
00189 reg="..";
00190 reg.SetValue("version",VERSION);
00191 reg.SetValue("enableAntiAliasing",enableAntiAliasing);
00192 reg.SetValue("antiAliasingQuality",antiAliasingQuality);
00193 reg.SetValue("enableRepere",enableRepere);
00194 reg.SetValue("enableLightView",enableLightView);
00195 reg.SetValue("enablePicking",enablePicking);
00196 reg.SetValue("enableDelta",enableDelta);
00197 reg.SetValue("enableBackGround",enableBackGround);
00198 reg.SetValue("enableFPS",enableFPS);
00199 reg.SetValue("enableInertia",enableInertia);
00200 reg.SetValue("enableMonitor",monitor.enable);
00201 reg.SetValue("enableViewInertia",enableViewInertia);
00202
00203 reg.SetValue("enableFullScreen",screenMain.enableFullScreen);
00204 reg.SetValue("enableTitle",screenMain.enableTitle);
00205 reg.SetValue("enableRegion",screenMain.region.enable);
00206
00207 reg.SetValue("polygonModeFront",(int)polygonMode.front);
00208 reg.SetValue("polygonModeBack",(int)polygonMode.back);
00209
00210 reg.SetValue("dxWin",screenMain.dxNormal);
00211 reg.SetValue("dyWin",screenMain.dyNormal);
00212
00213 reg.SetValue("timerRefresh",refreshTime);
00214 reg.SetValue("cpuLimit",cpuLimit);
00215
00216 EndFont();
00217 Stop();
00218 delete taskWindow;
00219 delete univers;
00220
00221 }
00222
00223 #include <freetype/freetype.h>
00224
00225 static FT_Library FTlib = NULL;
00226 static int init_done = 0;
00227 static int maxTextSize = 0;
00228 static GLuint activeTextureID = 0;
00229
00230
00231
00232
00233
00234 int FTLibraryInit ()
00235 {
00236 int err;
00237
00238 if (!maxTextSize) glGetIntegerv (GL_MAX_TEXTURE_SIZE, (GLint*) &maxTextSize);
00239 if (!maxTextSize)
00240 {
00241 glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextSize);
00242 if (maxTextSize > 512) maxTextSize = 512;
00243 }
00244
00245 if (FTlib != 0 ) return TRUE;
00246 err = FT_Init_FreeType( &FTlib);
00247 if (err)
00248 {
00249 FTlib = 0;
00250 return FALSE;
00251 }
00252 return TRUE;
00253 }
00254
00255 void FTLibraryFree ()
00256 {
00257 if (init_done)
00258 {
00259 FT_Done_FreeType (FTlib);
00260 FTlib= 0;
00261 }
00262 }
00263
00269 BOOL ControlEngine::InitFont()
00270 {
00271 fontTexture = new FTGLTextureFont(FONT_FILE);
00272 fontExtrude = new FTGLExtrdFont(FONT_FILE);
00273 fontPixmap = new FTGLPixmapFont(FONT_FILE);
00274 fontOutline = new FTGLOutlineFont(FONT_FILE);
00275 fontPolygon = new FTGLPolygonFont(FONT_FILE);
00276 fontBitmap = new FTGLBitmapFont(FONT_FILE);
00277
00278 if (!fontPolygon->FaceSize(16)) return FALSE;
00279 if (!fontBitmap->FaceSize(16)) return FALSE;
00280 if (!fontOutline->FaceSize(16)) return FALSE;
00281 if (!fontTexture->FaceSize(16)) return FALSE;
00282 if (!fontExtrude->FaceSize(16)) return FALSE;
00283 if (!fontPixmap->FaceSize(16)) return FALSE;
00284
00285 fontPolygon->CharMap(ft_encoding_unicode);
00286 fontBitmap->CharMap(ft_encoding_unicode);
00287 fontOutline->CharMap(ft_encoding_unicode);
00288 fontTexture->CharMap(ft_encoding_unicode);
00289 fontExtrude->CharMap(ft_encoding_unicode);
00290 fontPixmap->CharMap(ft_encoding_unicode);
00291
00292 return TRUE;
00293 }
00294
00298 void ControlEngine::EndFont()
00299 {
00300 if (fontPolygon) delete fontPolygon;
00301 if (fontBitmap) delete fontBitmap;
00302 if (fontExtrude) delete fontExtrude;
00303 if (fontTexture) delete fontTexture;
00304 if (fontPixmap) delete fontPixmap;
00305 if (fontOutline) delete fontOutline;
00306 }
00307
00309
00310 void ControlEngine::InitMonitor()
00311 {
00312 monitor.fileName="tools/perfMon.txt";
00313 monitor.Begin("freeTime",TRUE);
00314 }
00315
00322 void ControlEngine::Start(HINSTANCE hInstance)
00323 {
00324
00325 LoadLibrary("riched32.dll");
00326 InitCommonControls();
00327
00328 hInst = hInstance;
00329 lights[0].id = GL_LIGHT0;
00330 lights[1].id = GL_LIGHT1;
00331 lights[2].id = GL_LIGHT2;
00332 lights[3].id = GL_LIGHT3;
00333
00334
00335 WNDCLASS wc ;
00336 memset (&wc, 0, sizeof(wc)) ;
00337 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
00338 wc.hInstance = hInst;
00339 wc.hbrBackground = NULL;
00340
00341
00342 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
00343 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_SYSTRAY));
00344 wc.lpfnWndProc = SysTrayProc;
00345 screenSystray.Setup(&wc,"Systray","3D Controls - Systray");
00346 screenSystray.Create(hInst);
00347
00348
00349 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
00350 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DCONTROLS));
00351 wc.lpfnWndProc = ControlEngineProc;
00352 screenMain.Setup(&wc,"Render","3D Controls - Render");
00353 screenMain.SetStyle(WS_OVERLAPPEDWINDOW);
00354 screenMain.SetSize((int)dxWin,(int)dyWin,TRUE);
00355 screenMain.Create(hInst);
00356 screenMain.BindOpenGL(TRUE);
00357 Object3D::hDC=screenMain.hDC;
00358 InitTextures();
00359 InitLights();
00360 ShowCursor(FALSE);
00361 screenMain.Show(TRUE);
00362 Setup((int)dxWin,(int)dyWin);
00363
00364 Timer::Init(screenMain.hWnd);
00365 SetTimerRefresh(refreshTime);
00366
00367
00368 Control::inertia.Start();
00369 cursor.Create();
00370
00371 }
00372
00376 void ControlEngine::Stop()
00377 {
00378 PostQuitMessage(0);
00379 screenMain.Destroy();
00380 screenSystray.Destroy();
00381 }
00382
00386 void ControlEngine::Loop()
00387 {
00388 MSG msg;
00389 while (GetMessage(&msg, NULL, 0, 0))
00390 {
00391 TranslateMessage(&msg);
00392 DispatchMessage(&msg);
00393 }
00394 }
00395
00401 void ControlEngine::InitViewPort(int deltaX,int deltaY)
00402 {
00403 glViewport(0, 0, deltaX,deltaY);
00404 glGetIntegerv(GL_VIEWPORT,viewPort);
00405
00406 glMatrixMode(GL_PROJECTION);
00407 aspect = (GLdouble)deltaX/(GLdouble)deltaY;
00408 glLoadIdentity();
00409 SetPerspective(0,0);
00410 }
00411
00417 void ControlEngine::Setup(int deltaX, int deltaY)
00418 {
00419 InitViewPort(deltaX,deltaY);
00420 glMatrixMode(GL_MODELVIEW);
00421 glLoadIdentity();
00422 gluLookAt(0,0,depth,0,0,0,0,1,0);
00423
00424 polygonMode.Use();
00425 glShadeModel(GL_SMOOTH);
00426
00427
00428
00429
00430
00431 glClearDepth(1);
00432 glEnable(GL_DEPTH_TEST);
00433 glDepthFunc(GL_LEQUAL);
00434
00435 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00436
00437 fog.Use();
00438
00439 glClearColor(0.22f,0.43f,0.64f,0);
00440 glClearAccum(0.0, 0.0, 0.0, 0.0);
00441
00442 glEnable(GL_LINE_SMOOTH);
00443 glEnable(GL_POLYGON_SMOOTH);
00444 glEnable(GL_POINT_SMOOTH);
00445
00446
00447 glEnable(GL_NORMALIZE);
00448 glEnable(GL_AUTO_NORMAL);
00449 }
00450
00451 void ControlEngine::Refresh(BOOL setSize)
00452 {
00453 Setup((int)dxWin,(int)dyWin);
00454 if (setSize) screenMain.SetSize((int)dxWin,(int)dyWin,FALSE);
00455 screenMain.Update(FALSE);
00456 screenMain.Show(TRUE);
00457 }
00458
00467 void ControlEngine::SetTimerRefresh(int value)
00468 {
00469 BindTimer(timerRefresh,OnRefresh,value);
00470 }
00471
00475 void ControlEngine::DrawCursors()
00476 {
00477 if (!enableCursor) return;
00478 enableCursor = FALSE;
00479
00480 glGetDoublev (GL_PROJECTION_MATRIX,projMatrix);
00481 glGetDoublev (GL_MODELVIEW_MATRIX,modelMatrix);
00482
00483 Color color;
00484 if (usingInertia)
00485 {
00486 color.Set(0,0,1);
00487 DrawCursor(pickingInertia.mouse.pos,color,FALSE);
00488 }
00489 color.SetFull();
00490 DrawCursor(pickingCursor.mouse.pos,color,TRUE);
00491 }
00492
00499 void ControlEngine::DrawCursor(Point2D<int> mousePos,Color color,BOOL isDrawInertia)
00500 {
00501 Point2D<int> hotSpot;
00502 hotSpot = cursor.GetHotSpot(cursor.curType);
00503 cursor.Rotate(univers->faceNode->elem->angle);
00504 cursor.Draw(mousePos.x+hotSpot.x,mousePos.y+hotSpot.y);
00505 }
00506
00515 void ControlEngine::Draw2DText(int xCenter,int yCenter,int x,int y,char *text,Color *color)
00516 {
00517 Color colorFull;
00518 if (!color)
00519 {
00520 color=&colorFull;
00521 color->SetFull();
00522 }
00523 Material material;
00524 material.emission=*color;
00525 material.Use();
00526
00527 Point3D<GLdouble> pos;
00528 switch (xCenter)
00529 {
00530 case -1: break;
00531 case 0: x+=deltaXwin/2; break;
00532 case 1: x+=deltaXwin; break;
00533 }
00534 switch (yCenter)
00535 {
00536 case -1: y=deltaYwin-y; break;
00537 case 0: y=deltaYwin/2-y; break;
00538 case 1: break;
00539 }
00540
00541 if (gluUnProject(x,y,0,modelMatrix,projMatrix,viewPort,&pos.x,&pos.y,&pos.z)==GL_TRUE)
00542 {
00543 glPushMatrix();
00544 pos.glTranslated();
00545 glScalef(0.001f,0.001f,1);
00546 fontTexture->Render(text);
00547 glPopMatrix();
00548 }
00549 }
00550
00554 BOOL ControlEngine::DrawOrigineRepere()
00555 {
00556 if (!enableRepere) return FALSE;
00557 Object3D::DrawRepere();
00558 return TRUE;
00559 }
00560
00565 BOOL ControlEngine::DrawFPS()
00566 {
00567 if (!enableFPS)
00568 {
00569 if (tipFPS)
00570 {
00571 delete tipFPS;
00572 tipFPS=NULL;
00573 }
00574 return FALSE;
00575 }
00576 if (!tipFPS)
00577 {
00578 tipFPS = new Tip;
00579 ce.Push(ce.masterContainer);
00580 tipFPS->Create();
00581 tipFPS->XDockLeft(2,80);
00582 tipFPS->YDockTop(2,20);
00583 ce.Pop();
00584 }
00585 static MyString fps;
00586 fps.Format("%.1f fps",GiveFPS());
00587 tipFPS->SetText(fps);
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 return TRUE;
00600 }
00601
00608 float ControlEngine::GiveFPS()
00609 {
00610 #define SIZE_LIST 25 // nb d'element a prendre en compt
00611 #define TIME_TO_VIEW 250 // temp de changement de valeur min
00612
00613 static MyStack<int> lstTime;
00614 static DWORD oldTick=0;
00615 static int sumTime=0;
00616 static DWORD lastViewTime=0;
00617 static float lastViewValue=0;
00618
00619 DWORD tick = GetTickCount();
00620 if (!oldTick) { lastViewTime = oldTick = tick; return 0; }
00621 int delta = tick-oldTick;
00622
00623 oldTick = tick;
00624 sumTime+=delta;
00625 lstTime.Push(delta);
00626
00627 if (lstTime.GetNbElem()>SIZE_LIST)
00628 {
00629 sumTime-=lstTime.FrontPop();
00630 if ((tick-lastViewTime)> TIME_TO_VIEW)
00631 {
00632 lastViewValue = (SIZE_LIST*1000)/(float)sumTime;
00633 lastViewTime=tick;
00634 }
00635 return lastViewValue;
00636 }
00637 else return 0;
00638 }
00639
00647 void ControlEngine::ApplyScaleView()
00648 {
00649 GLdouble scaling=12.4/dyWin;
00650 glScaled(scaling,scaling,scaling);
00651 }
00652
00656 void ControlEngine::SetPerspective(float xJitter,float yJitter)
00657 {
00658
00659 double angle=45;
00660 if (!xJitter&&!yJitter) gluPerspective(angle, aspect, 0.01, 1000);
00661 else accPerspective(angle,aspect , 0.01, 1000, xJitter , yJitter, 0.0, 0.0, 1.0);
00662 }
00663
00664 BOOL ControlEngine::ProjectWindowToObject(Point3D<GLdouble> &windowPos,Point3D<GLdouble> *objectPos)
00665 {
00666 return gluUnProject(windowPos.x,deltaYwin-windowPos.y,windowPos.z,modelMatrix,projMatrix,viewPort,&objectPos->x,&objectPos->y,&objectPos->z)==GL_TRUE?TRUE:FALSE;
00667 }
00668
00669 BOOL ControlEngine::ProjectWindowToObject(GLdouble windowPosX,GLdouble windowPosY,GLdouble windowPosZ,GLdouble *objectPosX,GLdouble *objectPosY,GLdouble *objectPosZ)
00670 {
00671 GLdouble _objectPosX,_objectPosY,_objectPosZ;
00672 if (!objectPosX) objectPosX=&_objectPosX;
00673 if (!objectPosY) objectPosY=&_objectPosY;
00674 if (!objectPosZ) objectPosZ=&_objectPosZ;
00675 return gluUnProject(windowPosX,deltaYwin-windowPosY,windowPosZ,modelMatrix,projMatrix,viewPort,objectPosX,objectPosY,objectPosZ)==GL_TRUE?TRUE:FALSE;
00676 }
00677
00678 BOOL ControlEngine::ProjectObjectToWindow(Point3D<GLdouble> &objectPos,Point3D<GLdouble> *windowPos)
00679 {
00680 int result = gluProject(objectPos.x,objectPos.y,objectPos.z,modelMatrix,projMatrix,viewPort,&windowPos->x,&windowPos->y,&windowPos->z);
00681 windowPos->y=deltaYwin-windowPos->y;
00682 return result==GL_TRUE?TRUE:FALSE;
00683 }
00684
00685 BOOL ControlEngine::ProjectObjectToWindow(GLdouble objectPosX,GLdouble objectPosY,GLdouble objectPosZ,GLdouble *windowPosX,GLdouble *windowPosY,GLdouble *windowPosZ)
00686 {
00687 GLdouble _windowPosX,_windowPosY,_windowPosZ;
00688 if (!windowPosX) windowPosX=&_windowPosX;
00689 if (!windowPosY) windowPosY=&_windowPosY;
00690 if (!windowPosZ) windowPosZ=&_windowPosZ;
00691 int result = gluProject(objectPosX,objectPosY,objectPosZ,modelMatrix,projMatrix,viewPort,windowPosX,windowPosY,windowPosZ);
00692 *windowPosY=deltaYwin-*windowPosY;
00693 return result==GL_TRUE?TRUE:FALSE;
00694 }
00695
00701 void ControlEngine::InitLights()
00702 {
00703 Light::SetLighting(TRUE);
00704 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
00705
00706 lights[0].material.SetDS(1,0,0);
00707 lights[0].position.Set(-0,0,10,TRUE);
00708 lights[0].cutoff = 20;
00709 lights[0].exponent = 50;
00710
00711 BOOL isEnable;
00712 isEnable=lights[1].isEnable;
00713 lights[1]=lights[0];
00714 lights[1].isEnable=isEnable;
00715 lights[1].material.SetDS(0,1,0);
00716
00717 isEnable=lights[2].isEnable;
00718 lights[2]=lights[1];
00719 lights[2].isEnable=isEnable;
00720 lights[2].material.SetDS(1,1,1);
00721 lights[2].attenuationCst.Set(4);
00722
00723 lights[3].isEnable=TRUE;
00724 lights[3].material.SetDS(.5,.5,.5);
00725 lights[3].material.ambient.Set(.5,.5,.5);
00726 lights[3].position.Set(0,0,(GLfloat)depth,FALSE);
00727
00728 MajLights();
00729 }
00730
00737 void ControlEngine::InitTextures()
00738 {
00739 texture.min.FromFile("min","texture/control/button minus.bmp");
00740 texture.max.FromFile("max","texture/control/button max.bmp");
00741 texture.maxAlternate.FromFile("alternate","texture/control/button max alternate.bmp");
00742
00743 texture.rotateFace.FromFile("rotated face","texture/control/button rotate face.bmp");
00744 texture.close.FromFile("close","texture/control/button close.bmp");
00745 texture.iconBase.FromFile("baseIcon","texture/control/base icon.bmp");
00746 texture.tropique.FromFile("tropique","texture/other/virus.jpg");
00747 texture.arrow.FromFile("arrow","texture/control/arrow left.png");
00748 texture.arrowOblique.FromFile("arrow oblique","texture/control/arrow top left.png");
00749 texture.plus.FromFile("plus","texture/control/plus.png");
00750 texture.minus.FromFile("minus","texture/control/minus.png");
00751 texture.grip.FromFile("grip","texture/control/grip.bmp");
00752
00753 texture.degradeeFocus.Begin("degradeeOn",2,2);
00754 texture.degradeeFocus.SetPixel(0,0,10,36,106);
00755 texture.degradeeFocus.SetPixel(1,0,166,202,240);
00756 texture.degradeeFocus.SetPixel(0,1,10,36,106);
00757 texture.degradeeFocus.SetPixel(1,1,10,36,106);
00758 texture.degradeeFocus.End();
00759
00760 texture.degradeeUnFocus.Begin("degradeeOff",2,2);
00761 texture.degradeeUnFocus.SetPixel(0,0,128,128,128);
00762 texture.degradeeUnFocus.SetPixel(1,0,192,192,192);
00763 texture.degradeeUnFocus.SetPixel(0,1,128,128,128);
00764 texture.degradeeUnFocus.SetPixel(1,1,128,128,128);
00765 texture.degradeeUnFocus.End();
00766
00767 texture.cursorArrow.FromFile("cursorArrow","texture/cursor/arrow.png");
00768 texture.cursorWait.FromFile("cursorWait","texture/cursor/wait.png");
00769 texture.cursorText.FromFile("cursorArrow","texture/cursor/text.png");
00770 texture.cursorMoving.FromFile("cursorWait","texture/cursor/moving.png");
00771 texture.cursorPush.FromFile("cursorPush","texture/cursor/push.png");
00772
00773 texture.background.FromFile("background","texture/background/venus.png");
00774
00775
00776 }
00777
00781 void ControlEngine::MajLights()
00782 {
00783 for (int i=0;i<4;i++) lights[i].Use();
00784
00785 boxLights[0].material.emission = lights[0].material.specular;
00786 boxLights[0].delta.Set(1,1,1);
00787 boxLights[0].coor.z = 10;
00788
00789 boxLights[1]=boxLights[0];
00790 boxLights[1].material.emission = lights[1].material.specular;
00791
00792 boxLights[2]=boxLights[0];
00793 boxLights[2].material.emission = lights[2].material.specular;
00794 }
00795
00800 void ControlEngine::Draw()
00801 {
00802 monitor.Reset("text");
00803 monitor.Reset("testPicking");
00804 monitor.End("freeTime");
00805 monitor.Begin("frame",TRUE);
00806 monitor.Begin("initScene",TRUE);
00807 monitor.Reset("textRender");
00808 monitor.Reset("textRenderCenter");
00809 monitor.Reset("textAlign");
00810
00811
00812
00813
00814 int jitterNum = antiAliasingQuality;
00815 BOOL isFirst = TRUE;
00816 if (!enableAntiAliasing) jitterNum=0;
00817 Jitter *jitter = &jitters[jitterNum];
00818 int jitterIndice;
00819 Recorder record;
00820 for (jitterIndice = 0; jitterIndice < jitter->nb ; jitterIndice++)
00821 {
00822 if (enableAntiAliasing) glEnable(GL_MULTISAMPLE_ARB);
00823 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00824 if (enableAntiAliasing) SetPerspective(jitter->tab[jitterIndice].x,jitter->tab[jitterIndice].y);
00825 glMatrixMode(GL_MODELVIEW);
00826 glLoadIdentity();
00827 gluLookAt(0,0,depth,0,0,0,0,1,0);
00828
00829 DrawLight();
00830 DrawBackGround();
00831
00832
00833 glGetDoublev (GL_PROJECTION_MATRIX,projMatrix);
00834 glGetDoublev (GL_MODELVIEW_MATRIX,modelMatrix);
00835
00836
00837 DrawOrigineRepere();
00838 ApplyScaleView();
00839
00840 monitor.End("initScene");
00841 monitor.Begin("draw",TRUE);
00842
00843 if (enableAntiAliasing)
00844 {
00845 if (isFirst)
00846 {
00847 record.BeginRecord(TRUE);
00848 DrawScene(FALSE);
00849 DrawCursors();
00850 record.EndRecord();
00851 glAccum(GL_LOAD,1.0f/(float)jitter->nb);
00852 isFirst=FALSE;
00853 }
00854 else
00855 {
00856 record.Play();
00857 glAccum(GL_ACCUM, 1.0f/(float)jitter->nb);
00858 }
00859 }
00860 else
00861 {
00862 DrawScene(FALSE);
00863 DrawCursors();
00864 }
00865 }
00866 if (enableAntiAliasing) glAccum (GL_RETURN, 1.0f);
00867
00868 monitor.End("draw");
00869 monitor.Begin("picking",TRUE);
00870 monitor.Begin("drawPicking",TRUE);
00871 if (enablePicking)
00872 {
00873 pickingCursor.Test();
00874 if (usingInertia) pickingInertia.Test();
00875 }
00876 DrawFPS();
00877
00878 monitor.End("picking");
00879 monitor.End("frame");
00880 monitor.AddRecord();
00881 monitor.Begin("freeTime",TRUE);
00882 }
00883
00888 BOOL ControlEngine::DrawBackGround()
00889 {
00890 if (!enableBackGround) return FALSE;
00891
00892 glPushMatrix();
00893 univers->faceNode->elem->angle.glRotated();
00894
00895
00896
00897
00898
00899
00900 glDisable(GL_BLEND);
00901 glDisable(GL_TEXTURE_2D);
00902
00903 Material material;
00904 material.SetNull();
00905
00906 GLUquadric *newQuadric = gluNewQuadric();
00907 gluQuadricNormals(newQuadric,GLU_NONE);
00908 material.emission.Set(1,1,1);
00909 material.Use();
00910 glEnable(GL_TEXTURE_2D);
00911
00912 glBindTexture(GL_TEXTURE_2D,texture.background.id);
00913 gluQuadricOrientation(newQuadric, GLU_INSIDE);
00914 gluQuadricTexture(newQuadric,TRUE);
00915 gluQuadricDrawStyle(newQuadric, GLU_FILL);
00916
00917
00918 gluSphere(newQuadric,univers->faceNode->elem->computePosition.z*0.1+200,50,50);
00919 gluDeleteQuadric(newQuadric);
00920 glDisable(GL_TEXTURE_2D);
00921 material.emission.SetNull();
00922 material.Use();
00923 glPopMatrix();
00924
00925
00926 return FALSE;
00927 }
00928
00932 void ControlEngine::DrawScene(BOOL isPicking)
00933 {
00934
00935 primitive.Draw(isPicking);
00936 }
00937
00941 void ControlEngine::DrawLight()
00942 {
00943 static float count =0;
00944 count+=0.04f;
00945
00946
00947 if (lights[0].IsEnabled())
00948 {
00949 if (lightsMove[0])
00950 {
00951 lights[0].position.x=0;
00952 lights[0].position.y=cos(count)*10.0f;
00953 lights[0].position.z=sin(count)*10.0f;
00954 lights[0].direction.Set(-lights[0].position.x,-lights[0].position.y,-lights[0].position.z);
00955 }
00956 if (enableLightView)
00957 {
00958 boxLights[0].coor = lights[0].position;
00959 boxLights[0].CenterIt();
00960 boxLights[0].Draw();
00961 }
00962 }
00963
00964 if (lights[1].IsEnabled())
00965 {
00966 if (lightsMove[1])
00967 {
00968 lights[1].position.x=sin(count)*10.0f;
00969 lights[1].position.y=0;
00970 lights[1].position.z=cos(count)*10.0f;
00971 lights[1].direction.Set(-lights[1].position.x,-lights[1].position.y,-lights[1].position.z);
00972 }
00973 if (enableLightView)
00974 {
00975 boxLights[1].coor = lights[1].position;
00976 boxLights[1].CenterIt();
00977 boxLights[1].Draw();
00978 }
00979 }
00980
00981 if (lights[2].IsEnabled())
00982 {
00983 if (lightsMove[2])
00984 {
00985 lights[2].position.x=sin(count)*10.0f;
00986 lights[2].position.y=cos(count)*10.0f;
00987 lights[2].position.z=(GLfloat)depth;
00988 lights[2].direction.Set(0,0,-1);
00989 }
00990 else
00991 {
00992 lights[2].position.x=10.0f*(-1+2.0f*((float)pickingCursor.mouse.pos.x/(float)deltaXwin));
00993 lights[2].position.y=10.0f*(1-2.0f*((float)pickingCursor.mouse.pos.y/(float)deltaYwin));
00994 lights[2].position.z=(GLfloat)depth;
00995 }
00996 if (enableLightView)
00997 {
00998 boxLights[2].coor = lights[2].position;
00999 boxLights[2].CenterIt();
01000 boxLights[2].Draw();
01001 }
01002 }
01003 MajLights();
01004 }
01005
01016 double ControlEngine::ComputeRealZ(double logicZ)
01017 {
01018 double y10=438.5964912;
01019 double y100=5434.782609;
01020 double alpha = (y100-y10)/90.0;
01021 double cst = y100-100.0*alpha;
01022 double zLineaire = 1.0/(2.0-logicZ);
01023 double realZdist =(zLineaire-cst)/alpha;
01024 double zDist = realZdist*0.001015;
01025 return zDist;
01026 }
01027
01032 int ControlEngine::OnRefresh(Timer *timer)
01033 {
01034
01035
01036
01037
01038
01039 InvalidateRect(screenMain.hWnd,NULL,FALSE);
01040 return 0;
01041 }
01042
01046 LRESULT CALLBACK ControlEngineProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
01047 {
01048 return ce.Proc(hDlg,message,wParam,lParam);
01049 }
01050
01056 LRESULT CALLBACK ScreenShotProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
01057 {
01058 switch (message)
01059 {
01060 case WM_CREATE:
01061 return TRUE;
01062
01063 case WM_PAINT:
01064 PAINTSTRUCT ps;
01065 BeginPaint(hWnd,&ps);
01066 EndPaint(hWnd,&ps);
01067 return TRUE;
01068 }
01069 return DefWindowProc(hWnd,message,wParam,lParam);
01070 }
01071
01072 void ControlEngine::ScreenShot(TypeScreenShot type)
01073 {
01074 static BOOL isFirst = TRUE;
01075 if (isFirst)
01076 {
01077 isFirst=FALSE;
01078 WNDCLASS wc ;
01079 memset (&wc, 0, sizeof(wc)) ;
01080 wc.style = CS_HREDRAW | CS_VREDRAW;
01081 wc.lpfnWndProc = ScreenShotProc;
01082 wc.hInstance = hInst;
01083 wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_DCONTROLS));
01084 wc.hCursor = LoadCursor (NULL, IDC_ARROW);
01085 wc.hbrBackground = NULL;
01086 wc.lpszClassName = TEXT ("ScreenShot");
01087 if (!RegisterClass(&wc) ) MessageBox (NULL, "ScreenShot - Window","!Failed",MB_OK) ;
01088 }
01089
01090 MyString title("3D Control - ScreenShot - ");
01091 switch (type)
01092 {
01093 case ssRGB: title<<"RGB"; break;
01094 case ssDepth: title<<"Depth"; break;
01095 case ssBlending: title<<"blending"; break;
01096 case ssLuminance: title<<"luminance"; break;
01097 }
01098 HWND hWndScreenShot = CreateWindow("ScreenShot",title,WS_OVERLAPPEDWINDOW,0,0,(int)dxWin,(int)dyWin,NULL,NULL,hInst,NULL);
01099 MoveWindow(hWndScreenShot,400,0,(int)dxWin,(int)dyWin+28,FALSE);
01100 ShowWindow (hWndScreenShot,SW_SHOW);
01101
01102 HDC hDc = GetDC(hWndScreenShot);
01103 Point3D<GLfloat> pos;
01104 Color color;
01105 GLfloat value;
01106 for (pos.x=0;pos.x<dxWin;pos.x++)
01107 for (pos.y=0;pos.y<dyWin;pos.y++)
01108 {
01109 switch (type)
01110 {
01111 case ssLuminance:
01112 glReadPixels((GLint)pos.x,(GLint)pos.y,1,1,GL_LUMINANCE,GL_FLOAT,&value);
01113 SetPixel(hDc,(int)pos.x,(int)dyWin-(int)pos.y,RGB(value*255,value*255,value*255));
01114 break;
01115 case ssRGB:
01116 glReadPixels((GLint)pos.x,(GLint)pos.y,1,1,GL_RGBA,GL_FLOAT,&color);
01117 SetPixel(hDc,(int)pos.x,(int)dyWin-(int)pos.y,RGB(color.r*255,color.g*255,color.b*255));
01118 break;
01119 case ssDepth:
01120 glReadPixels((GLint)pos.x,(GLint)pos.y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pos.z);
01121 value=(pos.z-0.999f)*1000.0f;
01122 SetPixel(hDc,(int)pos.x,(int)dyWin-(int)pos.y,RGB(value*255,value*255,value*255));
01123 break;
01124 case ssBlending:
01125 glReadPixels((GLint)pos.x,(GLint)pos.y,1,1,GL_ALPHA,GL_FLOAT,&value);
01126 SetPixel(hDc,(int)pos.x,(int)dyWin-(int)pos.y,RGB(value*255,value*255,value*255));
01127 break;
01128 }
01129
01130 }
01131 }
01132
01136 LRESULT ControlEngine::Proc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
01137 {
01138 LRESULT result=FALSE;
01139
01140
01141 static Timer keyDouble;
01142 static Control control_LMouseDown,control_RMouseDown,control_MMouseDown;
01143
01144 switch(message)
01145 {
01146 case WM_SETCURSOR:
01147 ShowMouse(FALSE);
01148 enableCursor=TRUE;
01149 result=TRUE;
01150 break;
01151
01152 case WM_KILLFOCUS:
01153 ShowMouse(TRUE);
01154 result=TRUE;
01155 break;
01156
01157 case WM_CREATE:
01158 pickingCursor.controlHover=univers;
01159 pickingCursor.controlContainer=univers;
01160 pickingInertia.controlHover=univers;
01161 pickingInertia.controlContainer=univers;
01162 univers->OverIn(univers,-1);
01163 result=TRUE;
01164 break;
01165
01166 case WM_DESTROY:
01167 Stop();
01168 PostQuitMessage(0);
01169 result=FALSE;
01170 break;
01171
01172 case WM_TIMER:
01173 result=Timer::Call((int)wParam);
01174 break;
01175
01176 case WM_PAINT:
01177 PAINTSTRUCT ps;
01178 BeginPaint(hWnd,&ps);
01179 EndPaint(hWnd,&ps);
01180 if (!screenMain.region.enable) SetWindowRgn(hWnd,NULL,FALSE);
01181 screenMain.SwapBuffers();
01182 screenMain.RegionReset();
01183 Draw();
01184 screenMain.RegionUse();
01185 result=TRUE;
01186 break;
01187
01188 case WM_SIZE:
01189 dxWin = LOWORD(lParam);
01190 dyWin = HIWORD(lParam);
01191 MyDebug::SendEvent("Size","%f, %f",dxWin,dyWin);
01192 Refresh(TRUE);
01193 result=FALSE;
01194
01195 case WM_NCMOUSEMOVE:
01196 ShowMouse(TRUE);
01197 enableCursor=FALSE;
01198 result=TRUE;
01199 break;
01200
01201
01202
01203 case WM_MOUSEWHEEL:
01204 pickingCursor.SetDeltaWheel(GET_WHEEL_DELTA_WPARAM(wParam));
01205 if (!pickingCursor.controlHover->OnMouseWheel(&pickingCursor.mouse,pickingCursor.faceHover))
01206 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01207 pickingCursor.controlContainer->OnMouseWheel(&pickingCursor.mouse,pickingCursor.faceContainer);
01208 result=TRUE;
01209 break;
01210
01211 case WM_MOUSEMOVE:
01212 enableCursor=TRUE;
01213 pickingCursor.mouse.leftBtn = (wParam&MK_LBUTTON)==MK_LBUTTON?TRUE:FALSE;
01214 pickingCursor.mouse.midBtn = (wParam&MK_MBUTTON)==MK_MBUTTON?TRUE:FALSE;
01215 pickingCursor.mouse.rightBtn = (wParam&MK_RBUTTON)==MK_RBUTTON?TRUE:FALSE;
01216 pickingCursor.SetMousePos(GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
01217 if (pickingCursor.isMoving)
01218 if (!pickingCursor.controlHover->OnMouseMove(&pickingCursor.mouse,pickingCursor.faceHover))
01219 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01220 pickingCursor.controlContainer->OnMouseMove(&pickingCursor.mouse,pickingCursor.faceContainer);
01221 result=TRUE;
01222 break;
01223
01224 case WM_LBUTTONDBLCLK:
01225 if (!pickingCursor.controlHover->OnLButtonDouble(&pickingCursor.mouse,pickingCursor.faceHover))
01226 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01227 pickingCursor.controlContainer->OnLButtonDouble(&pickingCursor.mouse,pickingCursor.faceContainer);
01228 result=TRUE;
01229 break;
01230
01231 case WM_MBUTTONDBLCLK:
01232 if (!pickingCursor.controlHover->OnMButtonDouble(&pickingCursor.mouse,pickingCursor.faceHover))
01233 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01234 pickingCursor.controlContainer->OnMButtonDouble(&pickingCursor.mouse,pickingCursor.faceContainer);
01235 result=TRUE;
01236 break;
01237
01238 case WM_RBUTTONDBLCLK:
01239 if (!pickingCursor.controlHover->OnRButtonDouble(&pickingCursor.mouse,pickingCursor.faceHover))
01240 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01241 pickingCursor.controlContainer->OnRButtonDouble(&pickingCursor.mouse,pickingCursor.faceContainer);
01242 result=TRUE;
01243 break;
01244
01245 case WM_LBUTTONUP:
01246 ReleaseCapture();
01247 pickingCursor.mouse.leftBtn=FALSE;
01248 if (!pickingCursor.controlHover->OnLButtonUp(&pickingCursor.mouse,pickingCursor.faceHover))
01249 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01250 pickingCursor.controlContainer->OnLButtonUp(&pickingCursor.mouse,pickingCursor.faceContainer);
01251 result=TRUE;
01252 break;
01253
01254 case WM_MBUTTONUP:
01255 pickingCursor.mouse.midBtn=FALSE;
01256 if (!pickingCursor.controlHover->OnMButtonUp(&pickingCursor.mouse,pickingCursor.faceHover))
01257 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01258 pickingCursor.controlContainer->OnMButtonUp(&pickingCursor.mouse,pickingCursor.faceContainer);
01259 result=TRUE;
01260 break;
01261
01262 case WM_RBUTTONUP:
01263 pickingCursor.mouse.rightBtn=FALSE;
01264 if (!pickingCursor.controlHover->OnRButtonUp(&pickingCursor.mouse,pickingCursor.faceHover))
01265 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01266 pickingCursor.controlContainer->OnRButtonUp(&pickingCursor.mouse,pickingCursor.faceContainer);
01267 result=TRUE;
01268 break;
01269
01270 case WM_LBUTTONDOWN:
01271 SetCapture(hWnd);
01272 pickingCursor.mouse.leftBtn=TRUE;
01273 if (!pickingCursor.controlHover->OnLButtonDown(&pickingCursor.mouse,pickingCursor.faceHover))
01274 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01275 pickingCursor.controlContainer->OnLButtonDown(&pickingCursor.mouse,pickingCursor.faceContainer);
01276 result=TRUE;
01277 break;
01278
01279 case WM_MBUTTONDOWN:
01280 pickingCursor.mouse.midBtn=TRUE;
01281 if (!pickingCursor.controlHover->OnMButtonDown(&pickingCursor.mouse,pickingCursor.faceHover))
01282 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01283 pickingCursor.controlContainer->OnMButtonDown(&pickingCursor.mouse,pickingCursor.faceContainer);
01284 result=TRUE;
01285 break;
01286
01287 case WM_RBUTTONDOWN:
01288 pickingCursor.mouse.rightBtn=TRUE;
01289 if (!pickingCursor.controlHover->OnRButtonDown(&pickingCursor.mouse,pickingCursor.faceHover))
01290 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01291 pickingCursor.controlContainer->OnRButtonDown(&pickingCursor.mouse,pickingCursor.faceContainer);
01292 result=TRUE;
01293 break;
01294
01295 case WM_KEYDOWN:
01296 {
01297 short code = (short)wParam;
01298 double counter = keyDouble.GetDeltaTime(TRUE);
01299 BOOL isSame = code==key.code;
01300 key.code= code;
01301 key.letter=(char)wParam;
01302 key.isPush=TRUE;
01303 if (!pickingCursor.controlHover->OnKeyDown(&key,pickingCursor.faceHover))
01304 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01305 pickingCursor.controlContainer->OnKeyDown(&key,pickingCursor.faceContainer);
01306 if (isSame && counter<=DEFAULT_DOUBLE_CLICK)
01307 {
01308 if (!pickingCursor.controlHover->OnKeyDouble(&key,pickingCursor.faceHover))
01309 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01310 pickingCursor.controlContainer->OnKeyDouble(&key,pickingCursor.faceContainer);
01311 }
01312 }
01313 result=FALSE;
01314 break;
01315
01316 case WM_KEYUP:
01317 key.code=(short)wParam;
01318 key.letter=(char)wParam;
01319 key.isPush=FALSE;
01320 if (!pickingCursor.controlHover->OnKeyUp(&key,pickingCursor.faceHover))
01321 if (pickingCursor.controlHover!=pickingCursor.controlContainer)
01322 pickingCursor.controlContainer->OnKeyUp(&key,pickingCursor.faceContainer);
01323 result=FALSE;
01324 break;
01325
01326
01327
01328 default: result = DefWindowProc(hWnd,message,wParam,lParam);
01329 }
01330
01331 return result;
01332 }
01333
01340 BOOL ControlEngine::RegionAdd(double x,double y)
01341 {
01342 GLdouble modelMatrixCur[16];
01343 glGetDoublev (GL_MODELVIEW_MATRIX,modelMatrixCur);
01344 Point2D<double> pos(x,y);
01345 return screenMain.region.AddElem(pos,modelMatrixCur,projMatrix,viewPort);
01346 }
01347
01354 BOOL ControlEngine::RegionGet(double x,double y,Point3D<double> *pt)
01355 {
01356 GLdouble modelMatrixCur[16];
01357 glGetDoublev (GL_MODELVIEW_MATRIX,modelMatrixCur);
01358 Point2D<double> pos(x,y);
01359 return screenMain.region.Compute(pos,modelMatrixCur,projMatrix,viewPort,pt);
01360 }
01361
01362
01363 BOOL ControlEngine::Push(Control *control)
01364 {
01365 if (!control) return FALSE;
01366 stack.Push(control);
01367 primitive.Push();
01368 primitive.GoTo(control->faceNode);
01369 return TRUE;
01370 }
01371
01372 Control * ControlEngine::Pop()
01373 {
01374 if (!stack.GetNbElem()) return NULL;
01375 Control *control = stack.Pop();
01376 primitive.Pop();
01377 return control;
01378 }
01379
01380 Control * ControlEngine::Peek()
01381 {
01382 if (!stack.GetNbElem()) return NULL;
01383 return stack.Peek();
01384 }