Blender V2.61 - r43446
|
00001 00039 #include <stdlib.h> 00040 #include <stdio.h> 00041 #include <string.h> 00042 #include <math.h> 00043 00044 #define FALSE 0 00045 00046 #include "GHOST_C-api.h" 00047 00048 #if defined(WIN32) || defined(__APPLE__) 00049 #ifdef WIN32 00050 #include <windows.h> 00051 #include <GL/gl.h> 00052 #else /* WIN32 */ 00053 /* __APPLE__ is defined */ 00054 #include <AGL/gl.h> 00055 #endif /* WIN32 */ 00056 #else /* defined(WIN32) || defined(__APPLE__) */ 00057 #include <GL/gl.h> 00058 #endif /* defined(WIN32) || defined(__APPLE__) */ 00059 00060 00061 static void gearsTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time); 00062 int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData); 00063 00064 static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0; 00065 static GLfloat fAngle = 0.0; 00066 static int sExitRequested = 0; 00067 static GHOST_SystemHandle shSystem = NULL; 00068 static GHOST_WindowHandle sMainWindow = NULL; 00069 static GHOST_WindowHandle sSecondaryWindow = NULL; 00070 static GHOST_TStandardCursor sCursor = GHOST_kStandardCursorFirstCursor; 00071 static GHOST_WindowHandle sFullScreenWindow = NULL; 00072 static GHOST_TimerTaskHandle sTestTimer; 00073 static GHOST_TimerTaskHandle sGearsTimer; 00074 00075 static void testTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time) 00076 { 00077 printf("timer1, time=%d\n", (int)time); 00078 } 00079 00080 00081 static void gearGL(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth) 00082 { 00083 GLint i; 00084 GLfloat r0, r1, r2; 00085 GLfloat angle, da; 00086 GLfloat u, v, len; 00087 const double pi = 3.14159264; 00088 00089 r0 = inner_radius; 00090 r1 = (float)(outer_radius - tooth_depth/2.0); 00091 r2 = (float)(outer_radius + tooth_depth/2.0); 00092 00093 da = (float)(2.0*pi / teeth / 4.0); 00094 00095 glShadeModel(GL_FLAT); 00096 glNormal3f(0.0, 0.0, 1.0); 00097 00098 /* draw front face */ 00099 glBegin(GL_QUAD_STRIP); 00100 for (i=0;i<=teeth;i++) { 00101 angle = (float)(i * 2.0*pi / teeth); 00102 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5)); 00103 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5)); 00104 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5)); 00105 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5)); 00106 } 00107 glEnd(); 00108 00109 /* draw front sides of teeth */ 00110 glBegin(GL_QUADS); 00111 da = (float)(2.0*pi / teeth / 4.0); 00112 for (i=0;i<teeth;i++) { 00113 angle = (float)(i * 2.0*pi / teeth); 00114 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5)); 00115 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(width*0.5)); 00116 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(width*0.5)); 00117 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5)); 00118 } 00119 glEnd(); 00120 00121 glNormal3f(0.0, 0.0, -1.0); 00122 00123 /* draw back face */ 00124 glBegin(GL_QUAD_STRIP); 00125 for (i=0;i<=teeth;i++) { 00126 angle = (float)(i * 2.0*pi / teeth); 00127 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5)); 00128 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5)); 00129 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5)); 00130 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5)); 00131 } 00132 glEnd(); 00133 00134 /* draw back sides of teeth */ 00135 glBegin(GL_QUADS); 00136 da = (float)(2.0*pi / teeth / 4.0); 00137 for (i=0;i<teeth;i++) { 00138 angle = (float)(i * 2.0*pi / teeth); 00139 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5)); 00140 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(-width*0.5)); 00141 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(-width*0.5)); 00142 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5)); 00143 } 00144 glEnd(); 00145 00146 /* draw outward faces of teeth */ 00147 glBegin(GL_QUAD_STRIP); 00148 for (i=0;i<teeth;i++) { 00149 angle = (float)(i * 2.0*pi / teeth); 00150 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(width*0.5)); 00151 glVertex3f((float)(r1*cos(angle)), (float)(r1*sin(angle)), (float)(-width*0.5)); 00152 u = (float)(r2*cos(angle+da) - r1*cos(angle)); 00153 v = (float)(r2*sin(angle+da) - r1*sin(angle)); 00154 len = (float)(sqrt(u*u + v*v)); 00155 u /= len; 00156 v /= len; 00157 glNormal3f(v, -u, 0.0); 00158 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(width*0.5)); 00159 glVertex3f((float)(r2*cos(angle+da)), (float)(r2*sin(angle+da)), (float)(-width*0.5)); 00160 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0); 00161 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(width*0.5)); 00162 glVertex3f((float)(r2*cos(angle+2*da)), (float)(r2*sin(angle+2*da)), (float)(-width*0.5)); 00163 u = (float)(r1*cos(angle+3*da) - r2*cos(angle+2*da)); 00164 v = (float)(r1*sin(angle+3*da) - r2*sin(angle+2*da)); 00165 glNormal3f(v, -u, 0.0); 00166 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(width*0.5)); 00167 glVertex3f((float)(r1*cos(angle+3*da)), (float)(r1*sin(angle+3*da)), (float)(-width*0.5)); 00168 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0); 00169 } 00170 glVertex3f((float)(r1*cos(0.0)), (float)(r1*sin(0.0)), (float)(width*0.5)); 00171 glVertex3f((float)(r1*cos(0.0)), (float)(r1*sin(0.0)), (float)(-width*0.5)); 00172 glEnd(); 00173 00174 glShadeModel(GL_SMOOTH); 00175 00176 /* draw inside radius cylinder */ 00177 glBegin(GL_QUAD_STRIP); 00178 for (i=0;i<=teeth;i++) { 00179 angle = (float)(i * 2.0*pi / teeth); 00180 glNormal3f((float)(-cos(angle)), (float)(-sin(angle)), 0.0); 00181 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(-width*0.5)); 00182 glVertex3f((float)(r0*cos(angle)), (float)(r0*sin(angle)), (float)(width*0.5)); 00183 } 00184 glEnd(); 00185 } 00186 00187 00188 00189 static void drawGearGL(int id) 00190 { 00191 static GLfloat pos[4] = { 5.0f, 5.0f, 10.0f, 1.0f }; 00192 static GLfloat ared[4] = { 0.8f, 0.1f, 0.0f, 1.0f }; 00193 static GLfloat agreen[4] = { 0.0f, 0.8f, 0.2f, 1.0f }; 00194 static GLfloat ablue[4] = { 0.2f, 0.2f, 1.0f, 1.0f }; 00195 00196 glLightfv(GL_LIGHT0, GL_POSITION, pos); 00197 glEnable(GL_CULL_FACE); 00198 glEnable(GL_LIGHTING); 00199 glEnable(GL_LIGHT0); 00200 glEnable(GL_DEPTH_TEST); 00201 00202 switch (id) 00203 { 00204 case 1: 00205 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared); 00206 gearGL(1.0f, 4.0f, 1.0f, 20, 0.7f); 00207 break; 00208 case 2: 00209 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, agreen); 00210 gearGL(0.5f, 2.0f, 2.0f, 10, 0.7f); 00211 break; 00212 case 3: 00213 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ablue); 00214 gearGL(1.3f, 2.0f, 0.5f, 10, 0.7f); 00215 break; 00216 default: 00217 break; 00218 } 00219 glEnable(GL_NORMALIZE); 00220 } 00221 00222 00223 static void drawGL(void) 00224 { 00225 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00226 00227 glPushMatrix(); 00228 00229 glRotatef(view_rotx, 1.0, 0.0, 0.0); 00230 glRotatef(view_roty, 0.0, 1.0, 0.0); 00231 glRotatef(view_rotz, 0.0, 0.0, 1.0); 00232 00233 glPushMatrix(); 00234 glTranslatef(-3.0, -2.0, 0.0); 00235 glRotatef(fAngle, 0.0, 0.0, 1.0); 00236 drawGearGL(1); 00237 glPopMatrix(); 00238 00239 glPushMatrix(); 00240 glTranslatef(3.1f, -2.0f, 0.0f); 00241 glRotatef((float)(-2.0*fAngle-9.0), 0.0, 0.0, 1.0); 00242 drawGearGL(2); 00243 glPopMatrix(); 00244 00245 glPushMatrix(); 00246 glTranslatef(-3.1f, 2.2f, -1.8f); 00247 glRotatef(90.0f, 1.0f, 0.0f, 0.0f); 00248 glRotatef((float)(2.0*fAngle-2.0), 0.0, 0.0, 1.0); 00249 drawGearGL(3); 00250 glPopMatrix(); 00251 00252 glPopMatrix(); 00253 } 00254 00255 00256 static void setViewPortGL(GHOST_WindowHandle hWindow) 00257 { 00258 GHOST_RectangleHandle hRect = NULL; 00259 GLfloat w, h; 00260 00261 GHOST_ActivateWindowDrawingContext(hWindow); 00262 hRect = GHOST_GetClientBounds(hWindow); 00263 00264 w = (float)GHOST_GetWidthRectangle(hRect) / (float)GHOST_GetHeightRectangle(hRect); 00265 h = 1.0; 00266 00267 glViewport(0, 0, GHOST_GetWidthRectangle(hRect), GHOST_GetHeightRectangle(hRect)); 00268 00269 glMatrixMode(GL_PROJECTION); 00270 glLoadIdentity(); 00271 glFrustum(-w, w, -h, h, 5.0, 60.0); 00272 /* glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10); */ 00273 glMatrixMode(GL_MODELVIEW); 00274 glLoadIdentity(); 00275 glTranslatef(0.0, 0.0, -40.0); 00276 00277 glClearColor(.2f,0.0f,0.0f,0.0f); 00278 glClear(GL_COLOR_BUFFER_BIT); 00279 00280 GHOST_DisposeRectangle(hRect); 00281 } 00282 00283 00284 00285 int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData) 00286 { 00287 int handled = 1; 00288 int cursor; 00289 int visibility; 00290 GHOST_TEventKeyData* keyData = NULL; 00291 GHOST_TEventWheelData* wheelData = NULL; 00292 GHOST_DisplaySetting setting; 00293 GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent); 00294 00295 switch (GHOST_GetEventType(hEvent)) 00296 { 00297 /* 00298 case GHOST_kEventUnknown: 00299 break; 00300 case GHOST_kEventCursorButton: 00301 break; 00302 case GHOST_kEventCursorMove: 00303 break; 00304 */ 00305 case GHOST_kEventWheel: 00306 { 00307 wheelData = (GHOST_TEventWheelData*)GHOST_GetEventData(hEvent); 00308 if (wheelData->z > 0) 00309 { 00310 view_rotz += 5.f; 00311 } 00312 else 00313 { 00314 view_rotz -= 5.f; 00315 } 00316 } 00317 break; 00318 00319 case GHOST_kEventKeyUp: 00320 break; 00321 00322 case GHOST_kEventKeyDown: 00323 { 00324 keyData = (GHOST_TEventKeyData*)GHOST_GetEventData(hEvent); 00325 switch (keyData->key) 00326 { 00327 case GHOST_kKeyC: 00328 { 00329 cursor = sCursor; 00330 cursor++; 00331 if (cursor >= GHOST_kStandardCursorNumCursors) 00332 { 00333 cursor = GHOST_kStandardCursorFirstCursor; 00334 } 00335 sCursor = (GHOST_TStandardCursor)cursor; 00336 GHOST_SetCursorShape(window, sCursor); 00337 } 00338 break; 00339 case GHOST_kKeyF: 00340 if (!GHOST_GetFullScreen(shSystem)) 00341 { 00342 /* Begin fullscreen mode */ 00343 setting.bpp = 24; 00344 setting.frequency = 85; 00345 setting.xPixels = 640; 00346 setting.yPixels = 480; 00347 00348 /* 00349 setting.bpp = 16; 00350 setting.frequency = 75; 00351 setting.xPixels = 640; 00352 setting.yPixels = 480; 00353 */ 00354 00355 sFullScreenWindow = GHOST_BeginFullScreen(shSystem, &setting, 00356 00357 FALSE /* stereo flag */); 00358 } 00359 else 00360 { 00361 GHOST_EndFullScreen(shSystem); 00362 sFullScreenWindow = 0; 00363 } 00364 break; 00365 case GHOST_kKeyH: 00366 { 00367 visibility = GHOST_GetCursorVisibility(window); 00368 GHOST_SetCursorVisibility(window, !visibility); 00369 } 00370 break; 00371 case GHOST_kKeyQ: 00372 if (GHOST_GetFullScreen(shSystem)) 00373 { 00374 GHOST_EndFullScreen(shSystem); 00375 sFullScreenWindow = 0; 00376 } 00377 sExitRequested = 1; 00378 case GHOST_kKeyT: 00379 if (!sTestTimer) 00380 { 00381 sTestTimer = GHOST_InstallTimer(shSystem, 0, 1000, testTimerProc, NULL); 00382 } 00383 else 00384 { 00385 GHOST_RemoveTimer(shSystem, sTestTimer); 00386 sTestTimer = 0; 00387 } 00388 break; 00389 case GHOST_kKeyW: 00390 { 00391 if (sMainWindow) 00392 { 00393 char *title = GHOST_GetTitle(sMainWindow); 00394 char *ntitle = malloc(strlen(title)+2); 00395 00396 sprintf(ntitle, "%s-", title); 00397 GHOST_SetTitle(sMainWindow, ntitle); 00398 00399 free(ntitle); 00400 free(title); 00401 } 00402 } 00403 break; 00404 default: 00405 break; 00406 } 00407 } 00408 break; 00409 00410 case GHOST_kEventWindowClose: 00411 { 00412 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent); 00413 if (window2 == sMainWindow) 00414 { 00415 sExitRequested = 1; 00416 } 00417 else 00418 { 00419 if (sGearsTimer) 00420 { 00421 GHOST_RemoveTimer(shSystem, sGearsTimer); 00422 sGearsTimer = 0; 00423 } 00424 GHOST_DisposeWindow(shSystem, window2); 00425 } 00426 } 00427 break; 00428 00429 case GHOST_kEventWindowActivate: 00430 handled = 0; 00431 break; 00432 case GHOST_kEventWindowDeactivate: 00433 handled = 0; 00434 break; 00435 case GHOST_kEventWindowUpdate: 00436 { 00437 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent); 00438 if (!GHOST_ValidWindow(shSystem, window2)) 00439 break; 00440 setViewPortGL(window2); 00441 drawGL(); 00442 GHOST_SwapWindowBuffers(window2); 00443 } 00444 break; 00445 00446 default: 00447 handled = 0; 00448 break; 00449 } 00450 return handled; 00451 } 00452 00453 00454 int main(int argc, char** argv) 00455 { 00456 char* title1 = "gears - main window"; 00457 char* title2 = "gears - secondary window"; 00458 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL); 00459 00460 /* Create the system */ 00461 shSystem = GHOST_CreateSystem(); 00462 GHOST_AddEventConsumer(shSystem, consumer); 00463 00464 if (shSystem) 00465 { 00466 /* Create the main window */ 00467 sMainWindow = GHOST_CreateWindow(shSystem, 00468 title1, 00469 10, 00470 64, 00471 320, 00472 200, 00473 GHOST_kWindowStateNormal, 00474 GHOST_kDrawingContextTypeOpenGL, 00475 FALSE, 00476 FALSE); 00477 if (!sMainWindow) 00478 { 00479 printf("could not create main window\n"); 00480 exit(-1); 00481 } 00482 00483 /* Create a secondary window */ 00484 sSecondaryWindow = GHOST_CreateWindow(shSystem, 00485 title2, 00486 340, 00487 64, 00488 320, 00489 200, 00490 GHOST_kWindowStateNormal, 00491 GHOST_kDrawingContextTypeOpenGL, 00492 FALSE, 00493 FALSE); 00494 if (!sSecondaryWindow) 00495 { 00496 printf("could not create secondary window\n"); 00497 exit(-1); 00498 } 00499 00500 /* Install a timer to have the gears running */ 00501 sGearsTimer = GHOST_InstallTimer(shSystem, 00502 0, 00503 10, 00504 gearsTimerProc, 00505 sMainWindow); 00506 00507 /* Enter main loop */ 00508 while (!sExitRequested) 00509 { 00510 if (!GHOST_ProcessEvents(shSystem, 0)) 00511 { 00512 #ifdef WIN32 00513 /* If there were no events, be nice to other applications */ 00514 Sleep(10); 00515 #endif 00516 } 00517 GHOST_DispatchEvents(shSystem); 00518 } 00519 } 00520 00521 /* Dispose windows */ 00522 if (GHOST_ValidWindow(shSystem, sMainWindow)) 00523 { 00524 GHOST_DisposeWindow(shSystem, sMainWindow); 00525 } 00526 if (GHOST_ValidWindow(shSystem, sSecondaryWindow)) 00527 { 00528 GHOST_DisposeWindow(shSystem, sSecondaryWindow); 00529 } 00530 00531 /* Dispose the system */ 00532 GHOST_DisposeSystem(shSystem); 00533 GHOST_DisposeEventConsumer(consumer); 00534 00535 return 0; 00536 } 00537 00538 00539 static void gearsTimerProc(GHOST_TimerTaskHandle hTask, GHOST_TUns64 time) 00540 { 00541 GHOST_WindowHandle hWindow = NULL; 00542 fAngle += 2.0; 00543 view_roty += 1.0; 00544 hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask); 00545 if (GHOST_GetFullScreen(shSystem)) 00546 { 00547 /* Running full screen */ 00548 GHOST_InvalidateWindow(sFullScreenWindow); 00549 } 00550 else 00551 { 00552 if (GHOST_ValidWindow(shSystem, hWindow)) 00553 { 00554 GHOST_InvalidateWindow(hWindow); 00555 } 00556 } 00557 }