Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 1 | |
| 2 | /* uglpoint.c - WindML/Mesa example program */ |
| 3 | |
| 4 | /* Copyright (C) 2001 by Wind River Systems, Inc */ |
| 5 | |
| 6 | /* |
| 7 | * Mesa 3-D graphics library |
| 8 | * Version: 3.5 |
| 9 | * |
| 10 | * The MIT License |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | * copy of this software and associated documentation files (the "Software"), |
| 13 | * to deal in the Software without restriction, including without limitation |
| 14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 | * and/or sell copies of the Software, and to permit persons to whom the |
| 16 | * Software is furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included |
| 19 | * in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 22 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 24 | * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 | * DEALINGS IN THE SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * Authors: |
| 32 | * Stephane Raimbault <stephane.raimbault@windriver.com> |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | DESCRIPTION |
| 37 | Draw a single point. |
| 38 | */ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <math.h> |
| 42 | |
| 43 | #include <ugl/ugl.h> |
| 44 | #include <ugl/uglevent.h> |
| 45 | #include <ugl/uglinput.h> |
| 46 | |
| 47 | #include <GL/uglmesa.h> |
| 48 | |
| 49 | #define DOUBLE_BUFFER GL_TRUE |
| 50 | |
| 51 | enum { |
| 52 | BLACK = 0, |
| 53 | RED, |
| 54 | GREEN, |
| 55 | BLUE, |
| 56 | WHITE |
| 57 | }; |
| 58 | |
| 59 | UGL_LOCAL GLuint rgb; |
| 60 | UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId; |
| 61 | UGL_LOCAL UGL_EVENT_Q_ID qId; |
| 62 | UGL_LOCAL UGL_MESA_CONTEXT umc; |
| 63 | UGL_LOCAL GLint angleT; |
| 64 | |
| 65 | UGL_LOCAL void initGL (void) |
| 66 | { |
| 67 | /* By passed in RGB mode */ |
| 68 | uglMesaSetColor(BLACK, 0.0, 0.0, 0.0); |
| 69 | uglMesaSetColor(RED, 1.0, 0.0, 0.0); |
| 70 | uglMesaSetColor(GREEN, 0.0, 1.0, 0.0); |
| 71 | uglMesaSetColor(BLUE, 0.0, 0.0, 1.0); |
| 72 | uglMesaSetColor(WHITE, 1.0, 1.0, 1.0); |
| 73 | |
| 74 | glOrtho(0.0, 1.0, 0.0, 1.0, -20.0, 20.0); |
| 75 | |
| 76 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 77 | glClearIndex(BLACK); |
| 78 | } |
| 79 | |
| 80 | UGL_LOCAL void drawGL (void) |
| 81 | { |
| 82 | GLint i; |
| 83 | GLfloat x, y; |
| 84 | |
| 85 | /* Avoid blinking in single buffer */ |
| 86 | |
| 87 | if (DOUBLE_BUFFER) |
| 88 | glClear(GL_COLOR_BUFFER_BIT); |
| 89 | |
| 90 | /* Random points */ |
| 91 | |
| 92 | glBegin(GL_POINTS); |
| 93 | (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED); |
| 94 | |
| 95 | for (i=0; i<150; i++) |
| 96 | { |
| 97 | x = rand() / (RAND_MAX+1.0); |
| 98 | y = rand() / (RAND_MAX+1.0); |
| 99 | glVertex2f(x, y); |
| 100 | } |
| 101 | |
| 102 | (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN); |
| 103 | |
| 104 | for (i=0; i<150; i++) |
| 105 | { |
| 106 | x = (rand() / (RAND_MAX+1.0)); |
| 107 | y = (rand() / (RAND_MAX+1.0)); |
| 108 | glVertex2f(x, y); |
| 109 | } |
| 110 | |
| 111 | (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE); |
| 112 | glVertex2f(0.5,0.5); |
| 113 | |
| 114 | for (i=0; i<150; i++) |
| 115 | { |
| 116 | x = rand() / (RAND_MAX+1.0); |
| 117 | y = rand() / (RAND_MAX+1.0); |
| 118 | glVertex2f(x, y); |
| 119 | } |
| 120 | |
| 121 | glEnd(); |
| 122 | |
| 123 | /* Smooth triangle */ |
| 124 | |
| 125 | glPushMatrix(); |
| 126 | glTranslatef(0.5, 0.5, 0); |
| 127 | glRotatef(angleT, 1.0, -1.0, 0.0); |
| 128 | angleT = angleT++ % 360; |
| 129 | glBegin(GL_TRIANGLES); |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 130 | (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED); |
| 131 | glVertex2f(0.75, 0.25); |
| 132 | (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN); |
| 133 | glVertex2f(0.75, 0.75); |
| 134 | (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE); |
| 135 | glVertex2f(0.25, 0.75); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 136 | glEnd(); |
| 137 | glPopMatrix(); |
| 138 | |
| 139 | /* Flush and swap */ |
| 140 | |
| 141 | glFlush(); |
| 142 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 143 | uglMesaSwapBuffers(); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /************************************************************************ |
| 147 | * |
| 148 | * getEvent |
| 149 | * |
| 150 | * RETURNS: true or false |
| 151 | * |
| 152 | * NOMANUAL |
| 153 | * |
| 154 | */ |
| 155 | |
| 156 | UGL_LOCAL int getEvent(void) |
| 157 | { |
| 158 | UGL_EVENT event; |
| 159 | UGL_STATUS status; |
| 160 | int retVal = 0; |
| 161 | |
| 162 | status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT); |
| 163 | |
| 164 | while (status != UGL_STATUS_Q_EMPTY) |
| 165 | { |
| 166 | UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event; |
| 167 | |
| 168 | if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN) |
| 169 | retVal = 1; |
| 170 | |
| 171 | status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT); |
| 172 | } |
| 173 | |
| 174 | return(retVal); |
| 175 | } |
| 176 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 177 | void windMLPoint (UGL_BOOL windMLMode); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 178 | |
| 179 | void uglpoint (void) |
| 180 | { |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 181 | taskSpawn ("tPoint", 210, VX_FP_TASK, 100000, |
| 182 | (FUNCPTR)windMLPoint, UGL_FALSE,1,2,3,4,5,6,7,8,9); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 185 | void windMLPoint (UGL_BOOL windMLMode) |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 186 | { |
| 187 | GLubyte pPixels[4]; |
| 188 | GLsizei width, height; |
| 189 | UGL_INPUT_DEVICE_ID keyboardDevId; |
| 190 | |
| 191 | angleT = 0; |
| 192 | |
| 193 | uglInitialize(); |
| 194 | |
| 195 | uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId); |
| 196 | |
| 197 | if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, |
| 198 | (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK) |
| 199 | { |
| 200 | qId = uglEventQCreate (eventServiceId, 100); |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | eventServiceId = UGL_NULL; |
| 205 | } |
| 206 | |
| 207 | if (DOUBLE_BUFFER) |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 208 | { |
| 209 | if (windMLMode) |
| 210 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE |
| 211 | | UGL_MESA_WINDML_EXCLUSIVE, NULL); |
| 212 | else |
| 213 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL); |
| 214 | } |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 215 | else |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 216 | { |
| 217 | if (windMLMode) |
| 218 | umc = uglMesaCreateNewContext(UGL_MESA_SINGLE |
| 219 | | UGL_MESA_WINDML_EXCLUSIVE, NULL); |
| 220 | else |
| 221 | umc = uglMesaCreateNewContext(UGL_MESA_SINGLE, NULL); |
| 222 | } |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 223 | |
| 224 | if (umc == NULL) |
| 225 | { |
| 226 | uglDeinitialize(); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | /* Fullscreen */ |
| 231 | |
| 232 | uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH, |
| 233 | UGL_MESA_FULLSCREEN_HEIGHT); |
| 234 | |
| 235 | /* RGB or CI ? */ |
| 236 | |
| 237 | uglMesaGetIntegerv(UGL_MESA_RGB, &rgb); |
| 238 | |
| 239 | initGL(); |
| 240 | |
| 241 | while (!getEvent()) |
| 242 | drawGL(); |
| 243 | |
| 244 | uglMesaGetIntegerv(UGL_MESA_WIDTH, &width); |
| 245 | uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height); |
| 246 | |
| 247 | printf ("glReadPixel return "); |
| 248 | if (rgb) |
| 249 | { |
| 250 | glReadPixels(width/2, height/2, |
| 251 | 1, 1, GL_RGB, |
| 252 | GL_UNSIGNED_BYTE, pPixels); |
| 253 | glFlush(); |
| 254 | printf ("R:%i G:%i B:%i (RGB)", pPixels[0], pPixels[1], pPixels[2]); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | glReadPixels(width/2, height/2, |
| 259 | 1, 1, GL_COLOR_INDEX, |
| 260 | GL_UNSIGNED_BYTE, pPixels); |
| 261 | glFlush(); |
| 262 | if (pPixels[0] == BLUE) |
| 263 | printf ("BLUE (CI)"); |
| 264 | else |
| 265 | printf ("%i (CI))", pPixels[0]); |
| 266 | } |
| 267 | |
| 268 | printf(" for %ix%i\n", width/2, height/2); |
| 269 | |
| 270 | if (eventServiceId != UGL_NULL) |
| 271 | uglEventQDestroy (eventServiceId, qId); |
| 272 | |
| 273 | uglMesaDestroyContext(); |
| 274 | uglDeinitialize(); |
| 275 | |
| 276 | return; |
| 277 | } |