blender/intern/ghost/test/gears/GHOST_C-Test.c

551 lines
15 KiB
C
Raw Normal View History

2002-10-12 11:37:38 +00:00
/**
* ***** BEGIN GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
2010-02-12 13:34:04 +00:00
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
/**
* Copyright (C) 2001 NaN Technologies B.V.
*
* Simple test file for the GHOST library.
* The OpenGL gear code is taken from the Qt sample code which,
* in turn, is probably taken from somewhere as well.
* \author Maarten Gribnau
* \date May 31, 2001
2002-10-12 11:37:38 +00:00
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define FALSE 0
2002-10-12 11:37:38 +00:00
#include "GHOST_C-api.h"
#if defined(WIN32) || defined(__APPLE__)
2012-05-24 13:18:53 +00:00
# ifdef WIN32
# include <windows.h>
# include <GL/gl.h>
# else /* WIN32 */
/* __APPLE__ is defined */
# include <AGL/gl.h>
# endif /* WIN32 */
2002-10-12 11:37:38 +00:00
#else /* defined(WIN32) || defined(__APPLE__) */
2012-05-24 13:18:53 +00:00
# include <GL/gl.h>
2002-10-12 11:37:38 +00:00
#endif /* defined(WIN32) || defined(__APPLE__) */
static void gearsTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time);
int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
2002-10-12 11:37:38 +00:00
static GLfloat fAngle = 0.0;
static int sExitRequested = 0;
static GHOST_SystemHandle shSystem = NULL;
static GHOST_WindowHandle sMainWindow = NULL;
static GHOST_WindowHandle sSecondaryWindow = NULL;
static GHOST_TStandardCursor sCursor = GHOST_kStandardCursorFirstCursor;
static GHOST_WindowHandle sFullScreenWindow = NULL;
static GHOST_TimerTaskHandle sTestTimer;
static GHOST_TimerTaskHandle sGearsTimer;
static void testTimerProc(GHOST_TimerTaskHandle task, GHOST_TUns64 time)
{
printf("timer1, time=%d\n", (int)time);
}
static void gearGL(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
{
GLint i;
GLfloat r0, r1, r2;
GLfloat angle, da;
GLfloat u, v, len;
2002-10-12 11:37:38 +00:00
const double pi = 3.14159264;
r0 = inner_radius;
2012-05-24 13:18:53 +00:00
r1 = (float)(outer_radius - tooth_depth / 2.0);
r2 = (float)(outer_radius + tooth_depth / 2.0);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
da = (float)(2.0 * pi / teeth / 4.0);
2002-10-12 11:37:38 +00:00
glShadeModel(GL_FLAT);
glNormal3f(0.0, 0.0, 1.0);
2002-10-12 11:37:38 +00:00
/* draw front face */
glBegin(GL_QUAD_STRIP);
2012-05-24 13:18:53 +00:00
for (i = 0; i <= teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(width * 0.5));
}
glEnd();
2002-10-12 11:37:38 +00:00
/* draw front sides of teeth */
glBegin(GL_QUADS);
2012-05-24 13:18:53 +00:00
da = (float)(2.0 * pi / teeth / 4.0);
for (i = 0; i < teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
glVertex3f((float)(r2 * cos(angle + 2 * da)), (float)(r2 * sin(angle + 2 * da)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(width * 0.5));
}
glEnd();
2002-10-12 11:37:38 +00:00
glNormal3f(0.0, 0.0, -1.0);
2002-10-12 11:37:38 +00:00
/* draw back face */
glBegin(GL_QUAD_STRIP);
2012-05-24 13:18:53 +00:00
for (i = 0; i <= teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(-width * 0.5));
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
}
glEnd();
2002-10-12 11:37:38 +00:00
/* draw back sides of teeth */
glBegin(GL_QUADS);
2012-05-24 13:18:53 +00:00
da = (float)(2.0 * pi / teeth / 4.0);
for (i = 0; i < teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(-width * 0.5));
glVertex3f((float)(r2 * cos(angle + 2 * da)), (float)(r2 * sin(angle + 2 * da)), (float)(-width * 0.5));
glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
}
glEnd();
2002-10-12 11:37:38 +00:00
/* draw outward faces of teeth */
glBegin(GL_QUAD_STRIP);
2012-05-24 13:18:53 +00:00
for (i = 0; i < teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(angle)), (float)(r1 * sin(angle)), (float)(-width * 0.5));
u = (float)(r2 * cos(angle + da) - r1 * cos(angle));
v = (float)(r2 * sin(angle + da) - r1 * sin(angle));
len = (float)(sqrt(u * u + v * v));
2002-10-12 11:37:38 +00:00
u /= len;
v /= len;
glNormal3f(v, -u, 0.0);
2012-05-24 13:18:53 +00:00
glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(width * 0.5));
glVertex3f((float)(r2 * cos(angle + da)), (float)(r2 * sin(angle + da)), (float)(-width * 0.5));
2002-10-12 11:37:38 +00:00
glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
2012-05-24 13:18:53 +00:00
glVertex3f((float)(r2 * cos(angle + 2 * da)), (float)(r2 * sin(angle + 2 * da)), (float)(width * 0.5));
glVertex3f((float)(r2 * cos(angle + 2 * da)), (float)(r2 * sin(angle + 2 * da)), (float)(-width * 0.5));
u = (float)(r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da));
v = (float)(r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da));
2002-10-12 11:37:38 +00:00
glNormal3f(v, -u, 0.0);
2012-05-24 13:18:53 +00:00
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(angle + 3 * da)), (float)(r1 * sin(angle + 3 * da)), (float)(-width * 0.5));
2002-10-12 11:37:38 +00:00
glNormal3f((float)(cos(angle)), (float)(sin(angle)), 0.0);
}
2012-05-24 13:18:53 +00:00
glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(width * 0.5));
glVertex3f((float)(r1 * cos(0.0)), (float)(r1 * sin(0.0)), (float)(-width * 0.5));
glEnd();
2002-10-12 11:37:38 +00:00
glShadeModel(GL_SMOOTH);
2002-10-12 11:37:38 +00:00
/* draw inside radius cylinder */
glBegin(GL_QUAD_STRIP);
2012-05-24 13:18:53 +00:00
for (i = 0; i <= teeth; i++) {
angle = (float)(i * 2.0 * pi / teeth);
2002-10-12 11:37:38 +00:00
glNormal3f((float)(-cos(angle)), (float)(-sin(angle)), 0.0);
2012-05-24 13:18:53 +00:00
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(-width * 0.5));
glVertex3f((float)(r0 * cos(angle)), (float)(r0 * sin(angle)), (float)(width * 0.5));
}
glEnd();
2002-10-12 11:37:38 +00:00
}
static void drawGearGL(int id)
{
static GLfloat pos[4] = { 5.0f, 5.0f, 10.0f, 1.0f };
static GLfloat ared[4] = { 0.8f, 0.1f, 0.0f, 1.0f };
static GLfloat agreen[4] = { 0.0f, 0.8f, 0.2f, 1.0f };
static GLfloat ablue[4] = { 0.2f, 0.2f, 1.0f, 1.0f };
2002-10-12 11:37:38 +00:00
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
2002-10-12 11:37:38 +00:00
switch (id)
{
2012-05-24 13:18:53 +00:00
case 1:
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ared);
gearGL(1.0f, 4.0f, 1.0f, 20, 0.7f);
break;
case 2:
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, agreen);
gearGL(0.5f, 2.0f, 2.0f, 10, 0.7f);
break;
case 3:
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ablue);
gearGL(1.3f, 2.0f, 0.5f, 10, 0.7f);
break;
default:
break;
2002-10-12 11:37:38 +00:00
}
glEnable(GL_NORMALIZE);
2002-10-12 11:37:38 +00:00
}
static void drawGL(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2002-10-12 11:37:38 +00:00
glPushMatrix();
2002-10-12 11:37:38 +00:00
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
2002-10-12 11:37:38 +00:00
glPushMatrix();
glTranslatef(-3.0, -2.0, 0.0);
glRotatef(fAngle, 0.0, 0.0, 1.0);
drawGearGL(1);
glPopMatrix();
2002-10-12 11:37:38 +00:00
glPushMatrix();
glTranslatef(3.1f, -2.0f, 0.0f);
2012-05-24 13:18:53 +00:00
glRotatef((float)(-2.0 * fAngle - 9.0), 0.0, 0.0, 1.0);
drawGearGL(2);
glPopMatrix();
2002-10-12 11:37:38 +00:00
glPushMatrix();
glTranslatef(-3.1f, 2.2f, -1.8f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
2012-05-24 13:18:53 +00:00
glRotatef((float)(2.0 * fAngle - 2.0), 0.0, 0.0, 1.0);
drawGearGL(3);
glPopMatrix();
2002-10-12 11:37:38 +00:00
glPopMatrix();
2002-10-12 11:37:38 +00:00
}
static void setViewPortGL(GHOST_WindowHandle hWindow)
{
GHOST_RectangleHandle hRect = NULL;
GLfloat w, h;
GHOST_ActivateWindowDrawingContext(hWindow);
hRect = GHOST_GetClientBounds(hWindow);
2002-10-12 11:37:38 +00:00
w = (float)GHOST_GetWidthRectangle(hRect) / (float)GHOST_GetHeightRectangle(hRect);
h = 1.0;
2002-10-12 11:37:38 +00:00
glViewport(0, 0, GHOST_GetWidthRectangle(hRect), GHOST_GetHeightRectangle(hRect));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-w, w, -h, h, 5.0, 60.0);
2002-10-12 11:37:38 +00:00
/* glOrtho(0, bnds.getWidth(), 0, bnds.getHeight(), -10, 10); */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -40.0);
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
glClearColor(.2f, 0.0f, 0.0f, 0.0f);
2002-10-12 11:37:38 +00:00
glClear(GL_COLOR_BUFFER_BIT);
GHOST_DisposeRectangle(hRect);
}
int processEvent(GHOST_EventHandle hEvent, GHOST_TUserDataPtr userData)
{
int handled = 1;
int cursor;
int visibility;
2012-05-24 13:18:53 +00:00
GHOST_TEventKeyData *keyData = NULL;
GHOST_TEventWheelData *wheelData = NULL;
2002-10-12 11:37:38 +00:00
GHOST_DisplaySetting setting;
GHOST_WindowHandle window = GHOST_GetEventWindow(hEvent);
switch (GHOST_GetEventType(hEvent))
{
2012-06-30 22:49:33 +00:00
#if 0
case GHOST_kEventUnknown:
break;
case GHOST_kEventCursorButton:
break;
case GHOST_kEventCursorMove:
break;
#endif
2012-05-24 13:18:53 +00:00
case GHOST_kEventWheel:
{
2012-05-24 13:18:53 +00:00
wheelData = (GHOST_TEventWheelData *)GHOST_GetEventData(hEvent);
if (wheelData->z > 0)
{
view_rotz += 5.f;
}
else {
view_rotz -= 5.f;
}
}
break;
2012-05-24 13:18:53 +00:00
case GHOST_kEventKeyUp:
break;
2002-10-12 11:37:38 +00:00
2012-05-24 13:18:53 +00:00
case GHOST_kEventKeyDown:
2002-10-12 11:37:38 +00:00
{
2012-05-24 13:18:53 +00:00
keyData = (GHOST_TEventKeyData *)GHOST_GetEventData(hEvent);
2002-10-12 11:37:38 +00:00
switch (keyData->key)
{
2012-05-24 13:18:53 +00:00
case GHOST_kKeyC:
2002-10-12 11:37:38 +00:00
{
cursor = sCursor;
cursor++;
if (cursor >= GHOST_kStandardCursorNumCursors)
{
cursor = GHOST_kStandardCursorFirstCursor;
}
sCursor = (GHOST_TStandardCursor)cursor;
GHOST_SetCursorShape(window, sCursor);
}
break;
2012-05-24 13:18:53 +00:00
case GHOST_kKeyF:
if (!GHOST_GetFullScreen(shSystem))
{
/* Begin fullscreen mode */
setting.bpp = 24;
setting.frequency = 85;
setting.xPixels = 640;
setting.yPixels = 480;
/*
2012-06-30 22:49:33 +00:00
* setting.bpp = 16;
* setting.frequency = 75;
* setting.xPixels = 640;
* setting.yPixels = 480;
2012-05-24 13:18:53 +00:00
*/
sFullScreenWindow = GHOST_BeginFullScreen(shSystem, &setting,
FALSE /* stereo flag */);
}
else {
GHOST_EndFullScreen(shSystem);
sFullScreenWindow = 0;
}
break;
case GHOST_kKeyH:
2002-10-12 11:37:38 +00:00
{
visibility = GHOST_GetCursorVisibility(window);
GHOST_SetCursorVisibility(window, !visibility);
}
break;
2012-05-24 13:18:53 +00:00
case GHOST_kKeyQ:
if (GHOST_GetFullScreen(shSystem))
{
GHOST_EndFullScreen(shSystem);
sFullScreenWindow = 0;
}
sExitRequested = 1;
case GHOST_kKeyT:
if (!sTestTimer)
{
sTestTimer = GHOST_InstallTimer(shSystem, 0, 1000, testTimerProc, NULL);
}
else {
GHOST_RemoveTimer(shSystem, sTestTimer);
sTestTimer = 0;
}
break;
case GHOST_kKeyW:
2002-10-12 11:37:38 +00:00
{
if (sMainWindow)
{
char *title = GHOST_GetTitle(sMainWindow);
2012-05-24 13:18:53 +00:00
char *ntitle = malloc(strlen(title) + 2);
2002-10-12 11:37:38 +00:00
sprintf(ntitle, "%s-", title);
GHOST_SetTitle(sMainWindow, ntitle);
free(ntitle);
free(title);
}
}
break;
2012-05-24 13:18:53 +00:00
default:
break;
2002-10-12 11:37:38 +00:00
}
}
break;
2012-05-24 13:18:53 +00:00
case GHOST_kEventWindowClose:
2002-10-12 11:37:38 +00:00
{
GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
if (window2 == sMainWindow)
2002-10-12 11:37:38 +00:00
{
sExitRequested = 1;
}
2012-05-24 13:18:53 +00:00
else {
2002-10-12 11:37:38 +00:00
if (sGearsTimer)
{
GHOST_RemoveTimer(shSystem, sGearsTimer);
sGearsTimer = 0;
}
GHOST_DisposeWindow(shSystem, window2);
2002-10-12 11:37:38 +00:00
}
}
break;
2012-05-24 13:18:53 +00:00
case GHOST_kEventWindowActivate:
handled = 0;
break;
case GHOST_kEventWindowDeactivate:
handled = 0;
break;
case GHOST_kEventWindowUpdate:
2002-10-12 11:37:38 +00:00
{
GHOST_WindowHandle window2 = GHOST_GetEventWindow(hEvent);
if (!GHOST_ValidWindow(shSystem, window2))
2002-10-12 11:37:38 +00:00
break;
setViewPortGL(window2);
drawGL();
GHOST_SwapWindowBuffers(window2);
2002-10-12 11:37:38 +00:00
}
break;
2012-05-24 13:18:53 +00:00
default:
handled = 0;
break;
2002-10-12 11:37:38 +00:00
}
return handled;
}
2012-05-24 13:18:53 +00:00
int main(int argc, char **argv)
2002-10-12 11:37:38 +00:00
{
2012-05-24 13:18:53 +00:00
char *title1 = "gears - main window";
char *title2 = "gears - secondary window";
2002-10-12 11:37:38 +00:00
GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(processEvent, NULL);
/* Create the system */
shSystem = GHOST_CreateSystem();
GHOST_AddEventConsumer(shSystem, consumer);
if (shSystem)
{
/* Create the main window */
sMainWindow = GHOST_CreateWindow(shSystem,
2012-05-24 13:18:53 +00:00
title1,
10,
64,
320,
200,
GHOST_kWindowStateNormal,
GHOST_kDrawingContextTypeOpenGL,
FALSE,
FALSE);
2002-10-12 11:37:38 +00:00
if (!sMainWindow)
{
printf("could not create main window\n");
exit(-1);
}
/* Create a secondary window */
sSecondaryWindow = GHOST_CreateWindow(shSystem,
2012-05-24 13:18:53 +00:00
title2,
340,
64,
320,
200,
GHOST_kWindowStateNormal,
GHOST_kDrawingContextTypeOpenGL,
FALSE,
FALSE);
2002-10-12 11:37:38 +00:00
if (!sSecondaryWindow)
{
printf("could not create secondary window\n");
exit(-1);
}
/* Install a timer to have the gears running */
2012-05-24 13:18:53 +00:00
sGearsTimer = GHOST_InstallTimer(shSystem,
0,
10,
gearsTimerProc,
sMainWindow);
2002-10-12 11:37:38 +00:00
/* Enter main loop */
while (!sExitRequested)
{
if (!GHOST_ProcessEvents(shSystem, 0))
{
#ifdef WIN32
/* If there were no events, be nice to other applications */
Sleep(10);
#endif
}
GHOST_DispatchEvents(shSystem);
}
}
/* Dispose windows */
if (GHOST_ValidWindow(shSystem, sMainWindow))
{
GHOST_DisposeWindow(shSystem, sMainWindow);
}
if (GHOST_ValidWindow(shSystem, sSecondaryWindow))
{
GHOST_DisposeWindow(shSystem, sSecondaryWindow);
}
/* Dispose the system */
GHOST_DisposeSystem(shSystem);
return 0;
}
static void gearsTimerProc(GHOST_TimerTaskHandle hTask, GHOST_TUns64 time)
{
GHOST_WindowHandle hWindow = NULL;
fAngle += 2.0;
view_roty += 1.0;
2002-10-12 11:37:38 +00:00
hWindow = (GHOST_WindowHandle)GHOST_GetTimerTaskUserData(hTask);
if (GHOST_GetFullScreen(shSystem))
{
/* Running full screen */
GHOST_InvalidateWindow(sFullScreenWindow);
}
2012-05-24 13:18:53 +00:00
else {
2002-10-12 11:37:38 +00:00
if (GHOST_ValidWindow(shSystem, hWindow))
{
GHOST_InvalidateWindow(hWindow);
}
}
}