Blender V5.0
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
12
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 user_data);
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_TimerTaskHandle sTestTimer;
45static GHOST_TimerTaskHandle sGearsTimer;
46
47static void testTimerProc(GHOST_TimerTaskHandle task, uint64_t time)
48{
49 printf("timer1, time=%d\n", (int)time);
50}
51
52static void gearGL(
53 GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
54{
55 GLint i;
56 GLfloat r0, r1, r2;
57 GLfloat angle, da;
58 GLfloat u, v, len;
59 const double pi = 3.14159264;
60
61 r0 = inner_radius;
62 r1 = (float)(outer_radius - tooth_depth / 2.0);
63 r2 = (float)(outer_radius + tooth_depth / 2.0);
64
65 da = (float)(2.0 * pi / teeth / 4.0);
66
67 glShadeModel(GL_FLAT);
68 glNormal3f(0.0, 0.0, 1.0);
69
70 /* draw front face */
71 glBegin(GL_QUAD_STRIP);
72 for (i = 0; i <= teeth; i++) {
73 angle = (float)(i * 2.0 * pi / teeth);
74 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
75 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
76 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
77 glVertex3f((float)(r1 * cos(angle + 3 * da)),
78 (float)(r1 * sin(angle + 3 * da)),
79 (float)(width * 0.5));
80 }
81 glEnd();
82
83 /* draw front sides of teeth */
84 glBegin(GL_QUADS);
85 da = (float)(2.0 * pi / teeth / 4.0);
86 for (i = 0; i < teeth; i++) {
87 angle = (float)(i * 2.0 * pi / teeth);
88 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
89 glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
90 glVertex3f((float)(r2 * cos(angle + 2 * da)),
91 (float)(r2 * sin(angle + 2 * da)),
92 (float)(width * 0.5));
93 glVertex3f((float)(r1 * cos(angle + 3 * da)),
94 (float)(r1 * sin(angle + 3 * da)),
95 (float)(width * 0.5));
96 }
97 glEnd();
98
99 glNormal3f(0.0, 0.0, -1.0);
100
101 /* draw back face */
102 glBegin(GL_QUAD_STRIP);
103 for (i = 0; i <= teeth; i++) {
104 angle = (float)(i * 2.0 * pi / teeth);
105 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
106 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
107 glVertex3f((float)(r1 * cos(angle + 3 * da)),
108 (float)(r1 * sin(angle + 3 * da)),
109 (float)(-width * 0.5));
110 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
111 }
112 glEnd();
113
114 /* draw back sides of teeth */
115 glBegin(GL_QUADS);
116 da = (float)(2.0 * pi / teeth / 4.0);
117 for (i = 0; i < teeth; i++) {
118 angle = (float)(i * 2.0 * pi / teeth);
119 glVertex3f((float)(r1 * cos(angle + 3 * da)),
120 (float)(r1 * sin(angle + 3 * da)),
121 (float)(-width * 0.5));
122 glVertex3f((float)(r2 * cos(angle + 2 * da)),
123 (float)(r2 * sin(angle + 2 * da)),
124 (float)(-width * 0.5));
125 glVertex3f(
126 (float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
127 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
128 }
129 glEnd();
130
131 /* draw outward faces of teeth */
132 glBegin(GL_QUAD_STRIP);
133 for (i = 0; i < teeth; i++) {
134 angle = (float)(i * 2.0 * pi / teeth);
135 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
136 glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
137 u = (float)(r2 * cos(angle + da) - r1 * cos(angle));
138 v = (float)(r2 * sin(angle + da) - r1 * sin(angle));
139 len = (float)(sqrt(u * u + v * v));
140 u /= len;
141 v /= len;
142 glNormal3f(v, -u, 0.0);
143 glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
144 glVertex3f(
145 (float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
146 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
147 glVertex3f((float)(r2 * cos(angle + 2 * da)),
148 (float)(r2 * sin(angle + 2 * da)),
149 (float)(width * 0.5));
150 glVertex3f((float)(r2 * cos(angle + 2 * da)),
151 (float)(r2 * sin(angle + 2 * da)),
152 (float)(-width * 0.5));
153 u = (float)(r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da));
154 v = (float)(r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da));
155 glNormal3f(v, -u, 0.0);
156 glVertex3f((float)(r1 * cos(angle + 3 * da)),
157 (float)(r1 * sin(angle + 3 * da)),
158 (float)(width * 0.5));
159 glVertex3f((float)(r1 * cos(angle + 3 * da)),
160 (float)(r1 * sin(angle + 3 * da)),
161 (float)(-width * 0.5));
162 glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
163 }
164 glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(width * 0.5));
165 glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(-width * 0.5));
166 glEnd();
167
168 glShadeModel(GL_SMOOTH);
169
170 /* draw inside radius cylinder */
171 glBegin(GL_QUAD_STRIP);
172 for (i = 0; i <= teeth; i++) {
173 angle = (float)(i * 2.0 * pi / teeth);
174 glNormal3f((float)(-cos(angle)), (float)(-sin(angle)), 0.0);
175 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
176 glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
177 }
178 glEnd();
179}
180
181static void drawGearGL(int id)
182{
183 static GLfloat pos[4] = {5.0f, 5.0f, 10.0f, 1.0f};
184 static GLfloat ared[4] = {0.8f, 0.1f, 0.0f, 1.0f};
185 static GLfloat agreen[4] = {0.0f, 0.8f, 0.2f, 1.0f};
186 static GLfloat ablue[4] = {0.2f, 0.2f, 1.0f, 1.0f};
187
188 glLightfv(GL_LIGHT0, GL_POSITION, pos);
189 glEnable(GL_CULL_FACE);
190 glEnable(GL_LIGHTING);
191 glEnable(GL_LIGHT0);
192 glEnable(GL_DEPTH_TEST);
193
194 switch (id) {
195 case 1:
196 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared);
197 gearGL(1.0f, 4.0f, 1.0f, 20, 0.7f);
198 break;
199 case 2:
200 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, agreen);
201 gearGL(0.5f, 2.0f, 2.0f, 10, 0.7f);
202 break;
203 case 3:
204 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ablue);
205 gearGL(1.3f, 2.0f, 0.5f, 10, 0.7f);
206 break;
207 default:
208 break;
209 }
210 glEnable(GL_NORMALIZE);
211}
212
213static void drawGL(void)
214{
215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
216
217 glPushMatrix();
218
219 glRotatef(view_rotx, 1.0, 0.0, 0.0);
220 glRotatef(view_roty, 0.0, 1.0, 0.0);
221 glRotatef(view_rotz, 0.0, 0.0, 1.0);
222
223 glPushMatrix();
224 glTranslatef(-3.0, -2.0, 0.0);
225 glRotatef(fAngle, 0.0, 0.0, 1.0);
226 drawGearGL(1);
227 glPopMatrix();
228
229 glPushMatrix();
230 glTranslatef(3.1f, -2.0f, 0.0f);
231 glRotatef((float)(-2.0 * fAngle - 9.0), 0.0, 0.0, 1.0);
232 drawGearGL(2);
233 glPopMatrix();
234
235 glPushMatrix();
236 glTranslatef(-3.1f, 2.2f, -1.8f);
237 glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
238 glRotatef((float)(2.0 * fAngle - 2.0), 0.0, 0.0, 1.0);
239 drawGearGL(3);
240 glPopMatrix();
241
242 glPopMatrix();
243}
244
245static void setViewPortGL(GHOST_WindowHandle hWindow)
246{
247 GHOST_RectangleHandle hRect = NULL;
248 GLfloat w, h;
249
251 hRect = GHOST_GetClientBounds(hWindow);
252
254 h = 1.0;
255
256 glViewport(0, 0, GHOST_GetWidthRectangle(hRect), GHOST_GetHeightRectangle(hRect));
257
258 glMatrixMode(GL_PROJECTION);
259 glLoadIdentity();
260 glFrustum(-w, w, -h, h, 5.0, 60.0);
261 // glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10);
262 glMatrixMode(GL_MODELVIEW);
263 glLoadIdentity();
264 glTranslatef(0.0, 0.0, -40.0);
265
266 glClearColor(.2f, 0.0f, 0.0f, 0.0f);
267 glClear(GL_COLOR_BUFFER_BIT);
268
270}
271
272bool processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr user_data)
273{
274 bool handled = true;
275 int cursor;
276 int visibility;
277 GHOST_TEventKeyData *keyData = NULL;
278 GHOST_TEventWheelData *wheelData = NULL;
279 GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent);
280
281 switch (GHOST_GetEventType(hEvent)) {
282#if 0
284 break;
285 case GHOST_kEventCursorButton:
286 break;
288 break;
289#endif
290 case GHOST_kEventWheel: {
291 wheelData = (GHOST_TEventWheelData *)GHOST_GetEventData(hEvent);
292 if (wheelData->value > 0) {
293 view_rotz += 5.f;
294 }
295 else {
296 view_rotz -= 5.f;
297 }
298 break;
299 }
300
302 break;
303
304 case GHOST_kEventKeyDown: {
305 keyData = (GHOST_TEventKeyData *)GHOST_GetEventData(hEvent);
306 switch (keyData->key) {
307 case GHOST_kKeyC: {
308 cursor = sCursor;
309 cursor++;
310 if (cursor >= GHOST_kStandardCursorNumCursors) {
312 }
315 break;
316 }
317 case GHOST_kKeyH: {
318 visibility = GHOST_GetCursorVisibility(window);
319 GHOST_SetCursorVisibility(window, !visibility);
320 break;
321 }
322 case GHOST_kKeyQ:
323 sExitRequested = 1;
324 case GHOST_kKeyT:
325 if (!sTestTimer) {
327 }
328 else {
330 sTestTimer = 0;
331 }
332 break;
333 case GHOST_kKeyW: {
334 if (sMainWindow) {
335 char *title = GHOST_GetTitle(sMainWindow);
336 char *ntitle = malloc(strlen(title) + 2);
337
338 sprintf(ntitle, "%s-", title);
340
341 free(ntitle);
342 free(title);
343 }
344 break;
345 }
346 default:
347 break;
348 }
349 break;
350 }
351
353 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
354 if (window2 == sMainWindow) {
355 sExitRequested = 1;
356 }
357 else {
358 if (sGearsTimer) {
360 sGearsTimer = 0;
361 }
363 }
364 break;
365 }
366
368 handled = false;
369 break;
371 handled = false;
372 break;
374 GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
375 if (!GHOST_ValidWindow(shSystem, window2)) {
376 break;
377 }
378 setViewPortGL(window2);
380 drawGL();
382 break;
383 }
384 default:
385 handled = false;
386 break;
387 }
388 return handled;
389}
390
391int main(int argc, char **argv)
392{
393 GHOST_GPUSettings gpu_settings = {0};
394 char *title1 = "gears - main window";
395 char *title2 = "gears - secondary window";
396 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL);
397
398 /* Create the system */
401
402 if (shSystem) {
403 /* Create the main window */
405 NULL,
406 title1,
407 10,
408 64,
409 320,
410 200,
412 false,
413 GHOST_kDrawingContextTypeOpenGL,
414 gpu_settings);
415 if (!sMainWindow) {
416 printf("could not create main window\n");
417 exit(-1);
418 }
419
420 /* Create a secondary window */
422 NULL,
423 title2,
424 340,
425 64,
426 320,
427 200,
429 false,
430 GHOST_kDrawingContextTypeOpenGL,
431 gpu_settings);
432 if (!sSecondaryWindow) {
433 printf("could not create secondary window\n");
434 exit(-1);
435 }
436
437 /* Install a timer to have the gears running */
439
440 /* Enter main loop */
441 while (!sExitRequested) {
442 if (!GHOST_ProcessEvents(shSystem, false)) {
443#ifdef WIN32
444 /* If there were no events, be nice to other applications */
445 Sleep(10);
446#endif
447 }
449 }
450 }
451
452 /* Dispose windows */
455 }
458 }
459
460 /* Dispose the system */
462
463 return 0;
464}
465
466static void gearsTimerProc(GHOST_TimerTaskHandle hTask, uint64_t time)
467{
468 GHOST_WindowHandle hWindow = NULL;
469 fAngle += 2.0;
470 view_roty += 1.0;
471 hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask);
472 if (GHOST_ValidWindow(shSystem, hWindow)) {
473 GHOST_InvalidateWindow(hWindow);
474 }
475}
void BLI_kdtree_nd_ free(KDTree *tree)
static GLfloat fAngle
static void gearsTimerProc(GHOST_TimerTaskHandle task, uint64_t time)
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)
bool processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr user_data)
static void drawGL(void)
static GHOST_TimerTaskHandle sGearsTimer
static int sExitRequested
static GLfloat view_rotx
static void drawGearGL(int id)
static GHOST_TStandardCursor sCursor
static GHOST_WindowHandle sSecondaryWindow
GHOST C-API function and type declarations.
int32_t GHOST_GetWidthRectangle(GHOST_RectangleHandle rectanglehandle)
GHOST_TSuccess GHOST_SwapWindowBufferRelease(GHOST_WindowHandle windowhandle)
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)
GHOST_TSuccess GHOST_SwapWindowBufferAcquire(GHOST_WindowHandle windowhandle)
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_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_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)
bool GHOST_ValidWindow(GHOST_SystemHandle systemhandle, GHOST_WindowHandle windowhandle)
GHOST_TEventType GHOST_GetEventType(GHOST_EventHandle eventhandle)
GHOST_WindowHandle GHOST_GetEventWindow(GHOST_EventHandle eventhandle)
char * GHOST_GetTitle(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 gpu_settings)
void GHOST_DispatchEvents(GHOST_SystemHandle systemhandle)
GHOST_TimerTaskHandle GHOST_InstallTimer(GHOST_SystemHandle systemhandle, uint64_t delay, uint64_t interval, GHOST_TimerProcPtr timer_proc, GHOST_TUserDataPtr user_data)
GHOST_RectangleHandle GHOST_GetClientBounds(GHOST_WindowHandle windowhandle)
GHOST_TSuccess GHOST_DisposeSystem(GHOST_SystemHandle systemhandle)
@ GHOST_kWindowStateNormal
void * GHOST_TUserDataPtr
Definition GHOST_Types.h:55
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_kKeyH
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned long long int uint64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
nullptr float
uint pos
#define main()
#define printf(...)
#define sin
#define cos
#define sqrt
i
Definition text_draw.cc:230
uint len