Blender V4.3
GHOST_C-Test.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13#include <math.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#define FALSE 0
19
20#include "GHOST_C-api.h"
21
22#if defined(WIN32) || defined(__APPLE__)
23# ifdef WIN32
24# include <GL/gl.h>
25# include <windows.h>
26# else /* WIN32 */
27/* __APPLE__ is defined */
28# include <AGL/gl.h>
29# endif /* WIN32 */
30#else /* defined(WIN32) || defined(__APPLE__) */
31# include <GL/gl.h>
32#endif /* defined(WIN32) || defined(__APPLE__) */
33
34static void gearsTimerProc(GHOST_TimerTaskHandle task, uint64_t time);
35bool processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData);
36
37static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
38static GLfloat fAngle = 0.0;
39static int sExitRequested = 0;
40static GHOST_SystemHandle shSystem = NULL;
41static GHOST_WindowHandle sMainWindow = NULL;
42static GHOST_WindowHandle sSecondaryWindow = NULL;
44static GHOST_WindowHandle sFullScreenWindow = NULL;
45static GHOST_TimerTaskHandle sTestTimer;
46static GHOST_TimerTaskHandle sGearsTimer;
47
48static void testTimerProc(GHOST_TimerTaskHandle task, uint64_t time)
49{
50 printf("timer1, time=%d\n", (int)time);
51}
52
53static void gearGL(
54 GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
55{
56 GLint i;
57 GLfloat r0, r1, r2;
58 GLfloat angle, da;
59 GLfloat u, v, len;
60 const double pi = 3.14159264;
61
62 r0 = inner_radius;
63 r1 = (float)(outer_radius - tooth_depth / 2.0);
64 r2 = (float)(outer_radius + tooth_depth / 2.0);
65
66 da = (float)(2.0 * pi / teeth / 4.0);
67
68 glShadeModel(GL_FLAT);
69 glNormal3f(0.0, 0.0, 1.0);
70
71 /* draw front face */
72 glBegin(GL_QUAD_STRIP);
73 for (i = 0; i <= teeth; i++) {
74 angle = (float)(i * 2.0 * pi / teeth);
75 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
76 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
77 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
78 glVertex3f((float)(r1 * cos(angle + 3 * da)),
79 (float)(r1 * sin(angle + 3 * da)),
80 (float)(width * 0.5));
81 }
82 glEnd();
83
84 /* draw front sides of teeth */
85 glBegin(GL_QUADS);
86 da = (float)(2.0 * pi / teeth / 4.0);
87 for (i = 0; i < teeth; i++) {
88 angle = (float)(i * 2.0 * pi / teeth);
89 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
90 glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
91 glVertex3f((float)(r2 * cos(angle + 2 * da)),
92 (float)(r2 * sin(angle + 2 * da)),
93 (float)(width * 0.5));
94 glVertex3f((float)(r1 * cos(angle + 3 * da)),
95 (float)(r1 * sin(angle + 3 * da)),
96 (float)(width * 0.5));
97 }
98 glEnd();
99
100 glNormal3f(0.0, 0.0, -1.0);
101
102 /* draw back face */
103 glBegin(GL_QUAD_STRIP);
104 for (i = 0; i <= teeth; i++) {
105 angle = (float)(i * 2.0 * pi / teeth);
106 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
107 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
108 glVertex3f((float)(r1 * cos(angle + 3 * da)),
109 (float)(r1 * sin(angle + 3 * da)),
110 (float)(-width * 0.5));
111 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
112 }
113 glEnd();
114
115 /* draw back sides of teeth */
116 glBegin(GL_QUADS);
117 da = (float)(2.0 * pi / teeth / 4.0);
118 for (i = 0; i < teeth; i++) {
119 angle = (float)(i * 2.0 * pi / teeth);
120 glVertex3f((float)(r1 * cos(angle + 3 * da)),
121 (float)(r1 * sin(angle + 3 * da)),
122 (float)(-width * 0.5));
123 glVertex3f((float)(r2 * cos(angle + 2 * da)),
124 (float)(r2 * sin(angle + 2 * da)),
125 (float)(-width * 0.5));
126 glVertex3f(
127 (float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
128 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
129 }
130 glEnd();
131
132 /* draw outward faces of teeth */
133 glBegin(GL_QUAD_STRIP);
134 for (i = 0; i < teeth; i++) {
135 angle = (float)(i * 2.0 * pi / teeth);
136 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
137 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
138 u = (float)(r2 * cos(angle + da) - r1 * cos(angle));
139 v = (float)(r2 * sin(angle + da) - r1 * sin(angle));
140 len = (float)(sqrt(u * u + v * v));
141 u /= len;
142 v /= len;
143 glNormal3f(v, -u, 0.0);
144 glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
145 glVertex3f(
146 (float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
147 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
148 glVertex3f((float)(r2 * cos(angle + 2 * da)),
149 (float)(r2 * sin(angle + 2 * da)),
150 (float)(width * 0.5));
151 glVertex3f((float)(r2 * cos(angle + 2 * da)),
152 (float)(r2 * sin(angle + 2 * da)),
153 (float)(-width * 0.5));
154 u = (float)(r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da));
155 v = (float)(r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da));
156 glNormal3f(v, -u, 0.0);
157 glVertex3f((float)(r1 * cos(angle + 3 * da)),
158 (float)(r1 * sin(angle + 3 * da)),
159 (float)(width * 0.5));
160 glVertex3f((float)(r1 * cos(angle + 3 * da)),
161 (float)(r1 * sin(angle + 3 * da)),
162 (float)(-width * 0.5));
163 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
164 }
165 glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(width * 0.5));
166 glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(-width * 0.5));
167 glEnd();
168
169 glShadeModel(GL_SMOOTH);
170
171 /* draw inside radius cylinder */
172 glBegin(GL_QUAD_STRIP);
173 for (i = 0; i <= teeth; i++) {
174 angle = (float)(i * 2.0 * pi / teeth);
175 glNormal3f((float)(-cos(angle)), (float)(-sin(angle)), 0.0);
176 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
177 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
178 }
179 glEnd();
180}
181
182static void drawGearGL(int id)
183{
184 static GLfloat pos[4] = {5.0f, 5.0f, 10.0f, 1.0f};
185 static GLfloat ared[4] = {0.8f, 0.1f, 0.0f, 1.0f};
186 static GLfloat agreen[4] = {0.0f, 0.8f, 0.2f, 1.0f};
187 static GLfloat ablue[4] = {0.2f, 0.2f, 1.0f, 1.0f};
188
189 glLightfv(GL_LIGHT0, GL_POSITION, pos);
190 glEnable(GL_CULL_FACE);
191 glEnable(GL_LIGHTING);
192 glEnable(GL_LIGHT0);
193 glEnable(GL_DEPTH_TEST);
194
195 switch (id) {
196 case 1:
197 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared);
198 gearGL(1.0f, 4.0f, 1.0f, 20, 0.7f);
199 break;
200 case 2:
201 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, agreen);
202 gearGL(0.5f, 2.0f, 2.0f, 10, 0.7f);
203 break;
204 case 3:
205 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ablue);
206 gearGL(1.3f, 2.0f, 0.5f, 10, 0.7f);
207 break;
208 default:
209 break;
210 }
211 glEnable(GL_NORMALIZE);
212}
213
214static void drawGL(void)
215{
216 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217
218 glPushMatrix();
219
220 glRotatef(view_rotx, 1.0, 0.0, 0.0);
221 glRotatef(view_roty, 0.0, 1.0, 0.0);
222 glRotatef(view_rotz, 0.0, 0.0, 1.0);
223
224 glPushMatrix();
225 glTranslatef(-3.0, -2.0, 0.0);
226 glRotatef(fAngle, 0.0, 0.0, 1.0);
227 drawGearGL(1);
228 glPopMatrix();
229
230 glPushMatrix();
231 glTranslatef(3.1f, -2.0f, 0.0f);
232 glRotatef((float)(-2.0 * fAngle - 9.0), 0.0, 0.0, 1.0);
233 drawGearGL(2);
234 glPopMatrix();
235
236 glPushMatrix();
237 glTranslatef(-3.1f, 2.2f, -1.8f);
238 glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
239 glRotatef((float)(2.0 * fAngle - 2.0), 0.0, 0.0, 1.0);
240 drawGearGL(3);
241 glPopMatrix();
242
243 glPopMatrix();
244}
245
246static void setViewPortGL(GHOST_WindowHandle hWindow)
247{
248 GHOST_RectangleHandle hRect = NULL;
249 GLfloat w, h;
250
252 hRect = GHOST_GetClientBounds(hWindow);
253
255 h = 1.0;
256
257 glViewport(0, 0, GHOST_GetWidthRectangle(hRect), GHOST_GetHeightRectangle(hRect));
258
259 glMatrixMode(GL_PROJECTION);
260 glLoadIdentity();
261 glFrustum(-w, w, -h, h, 5.0, 60.0);
262 // glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10);
263 glMatrixMode(GL_MODELVIEW);
264 glLoadIdentity();
265 glTranslatef(0.0, 0.0, -40.0);
266
267 glClearColor(.2f, 0.0f, 0.0f, 0.0f);
268 glClear(GL_COLOR_BUFFER_BIT);
269
271}
272
273bool processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData)
274{
275 bool handled = true;
276 int cursor;
277 int visibility;
278 GHOST_TEventKeyData *keyData = NULL;
279 GHOST_TEventWheelData *wheelData = NULL;
280 GHOST_DisplaySetting setting;
281 GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent);
282
283 switch (GHOST_GetEventType(hEvent)) {
284#if 0
286 break;
287 case GHOST_kEventCursorButton:
288 break;
290 break;
291#endif
292 case GHOST_kEventWheel: {
293 wheelData = (GHOST_TEventWheelData *)GHOST_GetEventData(hEvent);
294 if (wheelData->z > 0) {
295 view_rotz += 5.f;
296 }
297 else {
298 view_rotz -= 5.f;
299 }
300 break;
301 }
302
304 break;
305
306 case GHOST_kEventKeyDown: {
307 keyData = (GHOST_TEventKeyData *)GHOST_GetEventData(hEvent);
308 switch (keyData->key) {
309 case GHOST_kKeyC: {
310 cursor = sCursor;
311 cursor++;
312 if (cursor >= GHOST_kStandardCursorNumCursors) {
314 }
317 break;
318 }
319 case GHOST_kKeyF:
321 /* Begin full-screen mode. */
322 setting.bpp = 24;
323 setting.frequency = 85;
324 setting.xPixels = 640;
325 setting.yPixels = 480;
326
327 /*
328 * setting.bpp = 16;
329 * setting.frequency = 75;
330 * setting.xPixels = 640;
331 * setting.yPixels = 480;
332 */
333
335 &setting,
336
337 FALSE /* stereo flag */);
338 }
339 else {
342 }
343 break;
344 case GHOST_kKeyH: {
345 visibility = GHOST_GetCursorVisibility(window);
346 GHOST_SetCursorVisibility(window, !visibility);
347 break;
348 }
349 case GHOST_kKeyQ:
353 }
354 sExitRequested = 1;
355 case GHOST_kKeyT:
356 if (!sTestTimer) {
358 }
359 else {
361 sTestTimer = 0;
362 }
363 break;
364 case GHOST_kKeyW: {
365 if (sMainWindow) {
366 char *title = GHOST_GetTitle(sMainWindow);
367 char *ntitle = malloc(strlen(title) + 2);
368
369 sprintf(ntitle, "%s-", title);
371
372 free(ntitle);
373 free(title);
374 }
375 break;
376 }
377 default:
378 break;
379 }
380 break;
381 }
382
384 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
385 if (window2 == sMainWindow) {
386 sExitRequested = 1;
387 }
388 else {
389 if (sGearsTimer) {
391 sGearsTimer = 0;
392 }
394 }
395 break;
396 }
397
399 handled = false;
400 break;
402 handled = false;
403 break;
405 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
406 if (!GHOST_ValidWindow(shSystem, window2)) {
407 break;
408 }
409 setViewPortGL(window2);
410 drawGL();
412 break;
413 }
414 default:
415 handled = false;
416 break;
417 }
418 return handled;
419}
420
421int main(int argc, char **argv)
422{
423 GHOST_GPUSettings gpuSettings = {0};
424 char *title1 = "gears - main window";
425 char *title2 = "gears - secondary window";
426 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL);
427
428 /* Create the system */
431
432 if (shSystem) {
433 /* Create the main window */
435 NULL,
436 title1,
437 10,
438 64,
439 320,
440 200,
442 false,
443 GHOST_kDrawingContextTypeOpenGL,
444 gpuSettings);
445 if (!sMainWindow) {
446 printf("could not create main window\n");
447 exit(-1);
448 }
449
450 /* Create a secondary window */
452 NULL,
453 title2,
454 340,
455 64,
456 320,
457 200,
459 false,
460 GHOST_kDrawingContextTypeOpenGL,
461 gpuSettings);
462 if (!sSecondaryWindow) {
463 printf("could not create secondary window\n");
464 exit(-1);
465 }
466
467 /* Install a timer to have the gears running */
469
470 /* Enter main loop */
471 while (!sExitRequested) {
472 if (!GHOST_ProcessEvents(shSystem, false)) {
473#ifdef WIN32
474 /* If there were no events, be nice to other applications */
475 Sleep(10);
476#endif
477 }
479 }
480 }
481
482 /* Dispose windows */
485 }
488 }
489
490 /* Dispose the system */
492
493 return 0;
494}
495
496static void gearsTimerProc(GHOST_TimerTaskHandle hTask, uint64_t time)
497{
498 GHOST_WindowHandle hWindow = NULL;
499 fAngle += 2.0;
500 view_roty += 1.0;
501 hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask);
503 /* Running full screen */
505 }
506 else {
507 if (GHOST_ValidWindow(shSystem, hWindow)) {
508 GHOST_InvalidateWindow(hWindow);
509 }
510 }
511}
sqrt(x)+1/max(0
void BLI_kdtree_nd_ free(KDTree *tree)
static GLfloat fAngle
static void gearsTimerProc(GHOST_TimerTaskHandle task, uint64_t time)
static GHOST_WindowHandle sFullScreenWindow
static GHOST_SystemHandle shSystem
static GHOST_WindowHandle sMainWindow
static GLfloat view_roty
static GLfloat view_rotz
static void gearGL(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
static void setViewPortGL(GHOST_WindowHandle hWindow)
static GHOST_TimerTaskHandle sTestTimer
static void testTimerProc(GHOST_TimerTaskHandle task, uint64_t time)
static void drawGL(void)
#define FALSE
static GHOST_TimerTaskHandle sGearsTimer
static int sExitRequested
static GLfloat view_rotx
static void drawGearGL(int id)
static GHOST_TStandardCursor sCursor
static GHOST_WindowHandle sSecondaryWindow
bool processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData)
GHOST C-API function and type declarations.
int32_t GHOST_GetWidthRectangle(GHOST_RectangleHandle rectanglehandle)
bool GHOST_GetFullScreen(GHOST_SystemHandle systemhandle)
GHOST_EventConsumerHandle GHOST_CreateEventConsumer(GHOST_EventCallbackProcPtr eventCallback, GHOST_TUserDataPtr user_data)
GHOST_SystemHandle GHOST_CreateSystem(void)
GHOST_TSuccess GHOST_AddEventConsumer(GHOST_SystemHandle systemhandle, GHOST_EventConsumerHandle consumerhandle)
void GHOST_DisposeRectangle(GHOST_RectangleHandle rectanglehandle)
bool GHOST_ProcessEvents(GHOST_SystemHandle systemhandle, bool waitForEvent)
void GHOST_SetTitle(GHOST_WindowHandle windowhandle, const char *title)
GHOST_TSuccess GHOST_SetCursorShape(GHOST_WindowHandle windowhandle, GHOST_TStandardCursor cursorshape)
GHOST_TUserDataPtr GHOST_GetTimerTaskUserData(GHOST_TimerTaskHandle timertaskhandle)
GHOST_TSuccess GHOST_EndFullScreen(GHOST_SystemHandle systemhandle)
GHOST_TSuccess GHOST_InvalidateWindow(GHOST_WindowHandle windowhandle)
GHOST_TSuccess GHOST_DisposeWindow(GHOST_SystemHandle systemhandle, GHOST_WindowHandle windowhandle)
GHOST_TSuccess GHOST_RemoveTimer(GHOST_SystemHandle systemhandle, GHOST_TimerTaskHandle timertaskhandle)
int32_t GHOST_GetHeightRectangle(GHOST_RectangleHandle rectanglehandle)
GHOST_TSuccess GHOST_SwapWindowBuffers(GHOST_WindowHandle windowhandle)
GHOST_TEventDataPtr GHOST_GetEventData(GHOST_EventHandle eventhandle)
bool GHOST_GetCursorVisibility(GHOST_WindowHandle windowhandle)
GHOST_TSuccess GHOST_SetCursorVisibility(GHOST_WindowHandle windowhandle, bool visible)
GHOST_TSuccess GHOST_ActivateWindowDrawingContext(GHOST_WindowHandle windowhandle)
GHOST_WindowHandle GHOST_CreateWindow(GHOST_SystemHandle systemhandle, GHOST_WindowHandle parent_windowhandle, const char *title, int32_t left, int32_t top, uint32_t width, uint32_t height, GHOST_TWindowState state, bool is_dialog, GHOST_GPUSettings gpuSettings)
bool GHOST_ValidWindow(GHOST_SystemHandle systemhandle, GHOST_WindowHandle windowhandle)
GHOST_TEventType GHOST_GetEventType(GHOST_EventHandle eventhandle)
GHOST_WindowHandle GHOST_BeginFullScreen(GHOST_SystemHandle systemhandle, const GHOST_DisplaySetting *setting, const bool stereoVisual)
GHOST_WindowHandle GHOST_GetEventWindow(GHOST_EventHandle eventhandle)
char * GHOST_GetTitle(GHOST_WindowHandle windowhandle)
void GHOST_DispatchEvents(GHOST_SystemHandle systemhandle)
GHOST_RectangleHandle GHOST_GetClientBounds(GHOST_WindowHandle windowhandle)
GHOST_TimerTaskHandle GHOST_InstallTimer(GHOST_SystemHandle systemhandle, uint64_t delay, uint64_t interval, GHOST_TimerProcPtr timerProc, GHOST_TUserDataPtr user_data)
GHOST_TSuccess GHOST_DisposeSystem(GHOST_SystemHandle systemhandle)
@ GHOST_kWindowStateNormal
void * GHOST_TUserDataPtr
Definition GHOST_Types.h:85
GHOST_TStandardCursor
#define GHOST_kStandardCursorNumCursors
@ GHOST_kEventWindowClose
@ GHOST_kEventWheel
@ GHOST_kEventCursorMove
@ GHOST_kEventWindowActivate
@ GHOST_kEventWindowUpdate
@ GHOST_kEventWindowDeactivate
@ GHOST_kEventKeyDown
@ GHOST_kEventUnknown
@ GHOST_kEventKeyUp
#define GHOST_kStandardCursorFirstCursor
@ GHOST_kKeyT
@ GHOST_kKeyW
@ GHOST_kKeyC
@ GHOST_kKeyQ
@ GHOST_kKeyF
@ GHOST_kKeyH
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:125
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define printf
#define NULL
int len
draw_view in_light_buf[] float
ccl_device_inline float3 cos(float3 v)
int main()
unsigned __int64 uint64_t
Definition stdint.h:90