Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Bouncing ball demo. |
| 3 | * |
| 4 | * This program is in the public domain |
| 5 | * |
| 6 | * Brian Paul |
| 7 | * |
| 8 | * Conversion to GLUT by Mark J. Kilgard |
| 9 | * |
| 10 | * Conversion to UGL/Mesa by Stephane Raimbault |
| 11 | */ |
| 12 | |
| 13 | #include <math.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <ugl/ugl.h> |
| 18 | #include <ugl/uglevent.h> |
| 19 | #include <ugl/uglinput.h> |
| 20 | |
| 21 | #include <GL/uglmesa.h> |
| 22 | |
| 23 | #define COS(X) cos( (X) * 3.14159/180.0 ) |
| 24 | #define SIN(X) sin( (X) * 3.14159/180.0 ) |
| 25 | |
| 26 | #define RED 1 |
| 27 | #define WHITE 2 |
| 28 | #define CYAN 3 |
| 29 | |
| 30 | UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId; |
| 31 | UGL_LOCAL UGL_EVENT_Q_ID qId; |
| 32 | UGL_LOCAL UGL_MESA_CONTEXT umc; |
| 33 | |
| 34 | UGL_LOCAL GLuint Ball; |
| 35 | UGL_LOCAL GLfloat Zrot, Zstep; |
| 36 | UGL_LOCAL GLfloat Xpos, Ypos; |
| 37 | UGL_LOCAL GLfloat Xvel, Yvel; |
| 38 | UGL_LOCAL GLfloat Xmin, Xmax; |
| 39 | UGL_LOCAL GLfloat Ymin; |
| 40 | /* UGL_LOCAL GLfloat Ymax = 4.0; */ |
| 41 | UGL_LOCAL GLfloat G; |
| 42 | |
| 43 | UGL_LOCAL GLuint make_ball(void) |
| 44 | { |
| 45 | GLuint list; |
| 46 | GLfloat a, b; |
| 47 | GLfloat da = 18.0, db = 18.0; |
| 48 | GLfloat radius = 1.0; |
| 49 | GLuint color; |
| 50 | GLfloat x, y, z; |
| 51 | |
| 52 | list = glGenLists(1); |
| 53 | |
| 54 | glNewList(list, GL_COMPILE); |
| 55 | |
| 56 | color = 0; |
| 57 | for (a = -90.0; a + da <= 90.0; a += da) |
| 58 | { |
| 59 | glBegin(GL_QUAD_STRIP); |
| 60 | for (b = 0.0; b <= 360.0; b += db) |
| 61 | { |
| 62 | if (color) |
| 63 | { |
| 64 | glIndexi(RED); |
| 65 | glColor3f(1, 0, 0); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | glIndexi(WHITE); |
| 70 | glColor3f(1, 1, 1); |
| 71 | } |
| 72 | |
| 73 | x = radius * COS(b) * COS(a); |
| 74 | y = radius * SIN(b) * COS(a); |
| 75 | z = radius * SIN(a); |
| 76 | glVertex3f(x, y, z); |
| 77 | |
| 78 | x = radius * COS(b) * COS(a + da); |
| 79 | y = radius * SIN(b) * COS(a + da); |
| 80 | z = radius * SIN(a + da); |
| 81 | glVertex3f(x, y, z); |
| 82 | |
| 83 | color = 1 - color; |
| 84 | } |
| 85 | glEnd(); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | glEndList(); |
| 90 | |
| 91 | return list; |
| 92 | } |
| 93 | |
| 94 | UGL_LOCAL void initGL(GLsizei width, GLsizei height) |
| 95 | { |
| 96 | float aspect = (float) width / (float) height; |
| 97 | glViewport(0, 0, (GLint) width, (GLint) height); |
| 98 | |
| 99 | uglMesaSetColor(RED, 1.0, 0.0, 0.0); |
| 100 | uglMesaSetColor(WHITE, 1.0, 1.0, 1.0); |
| 101 | uglMesaSetColor(CYAN, 0.0, 1.0, 1.0); |
| 102 | |
| 103 | glMatrixMode(GL_PROJECTION); |
| 104 | glLoadIdentity(); |
| 105 | glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0); |
| 106 | glMatrixMode(GL_MODELVIEW); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | UGL_LOCAL void drawGL(void) |
| 111 | { |
| 112 | GLint i; |
| 113 | static float vel0 = -100.0; |
| 114 | |
| 115 | glClear(GL_COLOR_BUFFER_BIT); |
| 116 | |
| 117 | glIndexi(CYAN); |
| 118 | glColor3f(0, 1, 1); |
| 119 | glBegin(GL_LINES); |
| 120 | for (i = -5; i <= 5; i++) |
| 121 | { |
| 122 | glVertex2i(i, -5); |
| 123 | glVertex2i(i, 5); |
| 124 | } |
| 125 | for (i = -5; i <= 5; i++) |
| 126 | { |
| 127 | glVertex2i(-5, i); |
| 128 | glVertex2i(5, i); |
| 129 | } |
| 130 | for (i = -5; i <= 5; i++) |
| 131 | { |
| 132 | glVertex2i(i, -5); |
| 133 | glVertex2f(i * 1.15, -5.9); |
| 134 | } |
| 135 | glVertex2f(-5.3, -5.35); |
| 136 | glVertex2f(5.3, -5.35); |
| 137 | glVertex2f(-5.75, -5.9); |
| 138 | glVertex2f(5.75, -5.9); |
| 139 | glEnd(); |
| 140 | |
| 141 | glPushMatrix(); |
| 142 | glTranslatef(Xpos, Ypos, 0.0); |
| 143 | glScalef(2.0, 2.0, 2.0); |
| 144 | glRotatef(8.0, 0.0, 0.0, 1.0); |
| 145 | glRotatef(90.0, 1.0, 0.0, 0.0); |
| 146 | glRotatef(Zrot, 0.0, 0.0, 1.0); |
| 147 | |
| 148 | glCallList(Ball); |
| 149 | |
| 150 | glPopMatrix(); |
| 151 | |
| 152 | glFlush(); |
| 153 | |
| 154 | uglMesaSwapBuffers(); |
| 155 | |
| 156 | Zrot += Zstep; |
| 157 | |
| 158 | Xpos += Xvel; |
| 159 | if (Xpos >= Xmax) |
| 160 | { |
| 161 | Xpos = Xmax; |
| 162 | Xvel = -Xvel; |
| 163 | Zstep = -Zstep; |
| 164 | } |
| 165 | if (Xpos <= Xmin) |
| 166 | { |
| 167 | Xpos = Xmin; |
| 168 | Xvel = -Xvel; |
| 169 | Zstep = -Zstep; |
| 170 | } |
| 171 | Ypos += Yvel; |
| 172 | Yvel += G; |
| 173 | if (Ypos < Ymin) |
| 174 | { |
| 175 | Ypos = Ymin; |
| 176 | if (vel0 == -100.0) |
| 177 | vel0 = fabs(Yvel); |
| 178 | Yvel = vel0; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | UGL_LOCAL int getEvent(void) |
| 183 | { |
| 184 | UGL_EVENT event; |
| 185 | UGL_STATUS status; |
| 186 | int retVal = 0; |
| 187 | |
| 188 | status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT); |
| 189 | |
| 190 | while (status != UGL_STATUS_Q_EMPTY) |
| 191 | { |
| 192 | UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event; |
| 193 | |
| 194 | if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN) |
| 195 | retVal = 1; |
| 196 | |
| 197 | status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT); |
| 198 | } |
| 199 | |
| 200 | return(retVal); |
| 201 | } |
| 202 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 203 | void windMLBounce (UGL_BOOL windMLMode); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 204 | |
| 205 | void uglbounce (void) |
| 206 | { |
| 207 | taskSpawn("tBounce", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLBounce, |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 208 | UGL_FALSE,1,2,3,4,5,6,7,8,9); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 211 | void windMLBounce(UGL_BOOL windMLMode) |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 212 | { |
| 213 | GLsizei width, height; |
| 214 | UGL_INPUT_DEVICE_ID keyboardDevId; |
| 215 | |
| 216 | Zrot = 0.0; |
| 217 | Zstep = 6.0; |
| 218 | Xpos = 0.0; |
| 219 | Ypos = 1.0; |
| 220 | Xvel = 0.2; |
| 221 | Yvel = 0.0; |
| 222 | Xmin = -4.0; |
| 223 | Xmax = 4.0; |
| 224 | Ymin = -3.8; |
| 225 | G = -0.1; |
| 226 | |
| 227 | uglInitialize(); |
| 228 | |
| 229 | uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId); |
| 230 | |
| 231 | uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId); |
| 232 | |
| 233 | qId = uglEventQCreate (eventServiceId, 100); |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 234 | |
| 235 | if (windMLMode) |
| 236 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE |
| 237 | | UGL_MESA_WINDML_EXCLUSIVE, NULL); |
| 238 | else |
| 239 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 240 | |
| 241 | if (umc == NULL) |
| 242 | { |
| 243 | uglDeinitialize(); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | /* Fullscreen */ |
| 248 | |
| 249 | uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH, |
| 250 | UGL_MESA_FULLSCREEN_HEIGHT); |
| 251 | |
| 252 | Ball = make_ball(); |
| 253 | glCullFace(GL_BACK); |
| 254 | glEnable(GL_CULL_FACE); |
| 255 | glDisable(GL_DITHER); |
| 256 | glShadeModel(GL_FLAT); |
| 257 | |
| 258 | uglMesaGetIntegerv(UGL_MESA_WIDTH, &width); |
| 259 | uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height); |
| 260 | |
| 261 | initGL(width, height); |
| 262 | |
| 263 | while(!getEvent()) |
| 264 | drawGL(); |
| 265 | |
| 266 | uglEventQDestroy (eventServiceId, qId); |
| 267 | |
| 268 | uglMesaDestroyContext(); |
| 269 | uglDeinitialize (); |
| 270 | |
| 271 | return; |
| 272 | } |