Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 3.5 |
| 4 | * |
| 5 | * The MIT License |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * Linux Magazine July 2001 |
| 27 | * Conversion to UGL/Mesa from GLUT by Stephane Raimbault, 2001 |
| 28 | */ |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <math.h> |
| 33 | |
| 34 | #include <ugl/ugl.h> |
| 35 | #include <ugl/uglevent.h> |
| 36 | #include <ugl/uglinput.h> |
| 37 | #include <ugl/uglucode.h> |
| 38 | |
| 39 | #include <GL/uglmesa.h> |
| 40 | #include <GL/glu.h> |
| 41 | |
| 42 | /* Need GLUT_SHAPES */ |
| 43 | |
| 44 | #include <GL/uglglutshapes.h> |
| 45 | |
| 46 | #ifndef PI |
| 47 | #define PI 3.14159265 |
| 48 | #endif |
| 49 | |
| 50 | UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId; |
| 51 | UGL_LOCAL UGL_EVENT_Q_ID qId; |
| 52 | UGL_LOCAL UGL_MESA_CONTEXT umc; |
| 53 | UGL_LOCAL volatile UGL_BOOL stopWex; |
| 54 | |
| 55 | UGL_LOCAL GLint angle; |
| 56 | UGL_LOCAL GLfloat Sin[360], Cos[360]; |
| 57 | UGL_LOCAL GLfloat L0pos[]={0.0, 2.0, -1.0}; |
| 58 | UGL_LOCAL GLfloat L0dif[]={0.3, 0.3, 0.8}; |
| 59 | UGL_LOCAL GLfloat L1pos[]={2.0, 2.0, 2.0}; |
| 60 | UGL_LOCAL GLfloat L1dif[]={0.5, 0.5, 0.5}; |
| 61 | UGL_LOCAL GLfloat Mspec[3]; |
| 62 | UGL_LOCAL GLfloat Mshiny; |
| 63 | UGL_LOCAL GLuint theTeapot; |
| 64 | |
| 65 | UGL_LOCAL void calcTableCosSin() |
| 66 | { |
| 67 | int i; |
| 68 | for(i=0;i<360;i++) { |
| 69 | Cos[i] = cos(((float)i)/180.0*PI); |
| 70 | Sin[i] = sin(((float)i)/180.0*PI); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | UGL_LOCAL void initGL(void) |
| 75 | { |
| 76 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 77 | glColor3f(1.0, 0.0, 0.0); |
| 78 | glEnable(GL_DEPTH_TEST); |
| 79 | |
| 80 | glShadeModel(GL_SMOOTH); |
| 81 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); |
| 82 | glEnable(GL_LIGHTING); |
| 83 | glEnable(GL_LIGHT0); |
| 84 | glEnable(GL_LIGHT1); |
| 85 | glLightfv(GL_LIGHT0, GL_DIFFUSE, L0dif); |
| 86 | glLightfv(GL_LIGHT0, GL_SPECULAR, L0dif); |
| 87 | glLightfv(GL_LIGHT1, GL_DIFFUSE, L1dif); |
| 88 | glLightfv(GL_LIGHT1, GL_SPECULAR, L1dif); |
| 89 | |
| 90 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec); |
| 91 | glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny); |
| 92 | |
| 93 | glMatrixMode(GL_PROJECTION); |
| 94 | glLoadIdentity(); |
| 95 | gluPerspective(45.0, 1.0, 0.1, 10.0); |
| 96 | glMatrixMode(GL_MODELVIEW); |
| 97 | |
| 98 | theTeapot = glGenLists(1); |
| 99 | glNewList(theTeapot, GL_COMPILE); |
| 100 | glutSolidTeapot(1.0); |
| 101 | glEndList(); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | UGL_LOCAL void drawGL() |
| 106 | { |
| 107 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 108 | |
| 109 | glLoadIdentity(); |
| 110 | |
| 111 | gluLookAt(4.5*Cos[angle], 2.0,4.5*Sin[angle],0.0,0.0,0.0,0.0, |
| 112 | 1.0,0.0); |
| 113 | glLightfv(GL_LIGHT0, GL_POSITION, L0pos); |
| 114 | glLightfv(GL_LIGHT1, GL_POSITION, L1pos); |
| 115 | |
| 116 | glCallList(theTeapot); |
| 117 | |
| 118 | glFlush(); |
| 119 | |
| 120 | uglMesaSwapBuffers(); |
| 121 | } |
| 122 | |
| 123 | UGL_LOCAL void echoUse(void) |
| 124 | { |
| 125 | printf("tTeapot keys:\n"); |
| 126 | printf(" Left Counter clockwise rotation (y-axis)\n"); |
| 127 | printf(" Right Clockwise rotation (y-axis)\n"); |
| 128 | printf(" j Enable/disable Light0\n"); |
| 129 | printf(" k Enable/disable Light1\n"); |
| 130 | printf(" m Add specular\n"); |
| 131 | printf(" l Remove specular\n"); |
| 132 | printf(" o Add shininess\n"); |
| 133 | printf(" p Remove shininess\n"); |
| 134 | printf(" ESC Exit\n"); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | UGL_LOCAL void readKey (UGL_WCHAR key) |
| 139 | { |
| 140 | switch(key) |
| 141 | { |
| 142 | case UGL_UNI_RIGHT_ARROW: |
| 143 | angle +=2; |
| 144 | if (angle>= 360) |
| 145 | angle-=360; |
| 146 | break; |
| 147 | case UGL_UNI_LEFT_ARROW: |
| 148 | angle -=2; |
| 149 | if (angle<0) |
| 150 | angle+=360; |
| 151 | break; |
| 152 | case 'j': |
| 153 | glIsEnabled(GL_LIGHT0) ? |
| 154 | glDisable(GL_LIGHT0) : glEnable(GL_LIGHT0); |
| 155 | break; |
| 156 | case 'k': |
| 157 | glIsEnabled(GL_LIGHT1) ? |
| 158 | glDisable(GL_LIGHT1) : glEnable(GL_LIGHT1); |
| 159 | break; |
| 160 | case 'm': |
| 161 | Mspec[0]+=0.1; |
| 162 | if(Mspec[0]>1) |
| 163 | Mspec[0]=1; |
| 164 | Mspec[1]+=0.1; |
| 165 | if(Mspec[1]>1) |
| 166 | Mspec[1]=1; |
| 167 | Mspec[2]+=0.1; |
| 168 | if(Mspec[2]>1) |
| 169 | Mspec[2]=1; |
| 170 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec); |
| 171 | break; |
| 172 | case 'l': |
| 173 | Mspec[0]-=0.1; |
| 174 | if(Mspec[0]>1) |
| 175 | Mspec[0]=1; |
| 176 | Mspec[1]-=0.1; |
| 177 | if(Mspec[1]>1) |
| 178 | Mspec[1]=1; |
| 179 | Mspec[2]-=0.1; |
| 180 | if(Mspec[2]>1) |
| 181 | Mspec[2]=1; |
| 182 | glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Mspec); |
| 183 | break; |
| 184 | case 'o': |
| 185 | Mshiny -= 1; |
| 186 | if (Mshiny<0) |
| 187 | Mshiny=0; |
| 188 | glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny); |
| 189 | break; |
| 190 | case 'p': |
| 191 | Mshiny += 1; |
| 192 | if (Mshiny>128) |
| 193 | Mshiny=128; |
| 194 | glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, Mshiny); |
| 195 | break; |
| 196 | case UGL_UNI_ESCAPE: |
| 197 | stopWex = UGL_TRUE; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | UGL_LOCAL void loopEvent(void) |
| 203 | { |
| 204 | UGL_EVENT event; |
| 205 | UGL_INPUT_EVENT * pInputEvent; |
| 206 | |
| 207 | UGL_FOREVER |
| 208 | { |
| 209 | if (uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT) |
| 210 | != UGL_STATUS_Q_EMPTY) |
| 211 | { |
| 212 | pInputEvent = (UGL_INPUT_EVENT *)&event; |
| 213 | |
| 214 | if (pInputEvent->header.type == UGL_EVENT_TYPE_KEYBOARD && |
| 215 | pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN) |
| 216 | readKey(pInputEvent->type.keyboard.key); |
| 217 | } |
| 218 | |
| 219 | drawGL(); |
| 220 | if (stopWex) |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 225 | void windMLTeapot (UGL_BOOL windMLMode); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 226 | |
| 227 | void uglteapot (void) |
| 228 | { |
| 229 | taskSpawn ("tTeapot", 210, VX_FP_TASK, 100000, (FUNCPTR)windMLTeapot, |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 230 | UGL_FALSE,1,2,3,4,5,6,7,8,9); |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 233 | void windMLTeapot (UGL_BOOL windMLMode) |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 234 | { |
| 235 | UGL_INPUT_DEVICE_ID keyboardDevId; |
| 236 | GLsizei displayWidth, displayHeight; |
| 237 | GLsizei x, y, w, h; |
| 238 | |
| 239 | angle = 45; |
| 240 | Mspec[0] = 0.5; |
| 241 | Mspec[1] = 0.5; |
| 242 | Mspec[2] = 0.5; |
| 243 | Mshiny = 50; |
| 244 | |
| 245 | uglInitialize (); |
| 246 | |
| 247 | uglDriverFind (UGL_KEYBOARD_TYPE, 0, |
| 248 | (UGL_UINT32 *)&keyboardDevId); |
| 249 | |
| 250 | uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0, (UGL_UINT32 *)&eventServiceId); |
| 251 | |
| 252 | qId = uglEventQCreate (eventServiceId, 100); |
| 253 | |
| 254 | /* Double buffering */ |
Brian Paul | 3069330 | 2001-09-10 19:21:13 +0000 | [diff] [blame] | 255 | if (windMLMode) |
| 256 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE |
| 257 | | UGL_MESA_WINDML_EXCLUSIVE, NULL); |
| 258 | else |
| 259 | umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL); |
| 260 | |
Brian Paul | 976c26c | 2001-08-20 16:07:10 +0000 | [diff] [blame] | 261 | if (umc == NULL) |
| 262 | { |
| 263 | uglDeinitialize (); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | uglMesaMakeCurrentContext (umc, 0, 0, 1, 1); |
| 268 | |
| 269 | uglMesaGetIntegerv(UGL_MESA_DISPLAY_WIDTH, &displayWidth); |
| 270 | uglMesaGetIntegerv(UGL_MESA_DISPLAY_HEIGHT, &displayHeight); |
| 271 | |
| 272 | h = (displayHeight*2)/3; |
| 273 | w = h; |
| 274 | x = (displayWidth-w)/2; |
| 275 | y = (displayHeight-h)/2; |
| 276 | |
| 277 | uglMesaMoveToWindow(x, y); |
| 278 | uglMesaResizeToWindow(w, h); |
| 279 | |
| 280 | calcTableCosSin(); |
| 281 | |
| 282 | initGL (); |
| 283 | |
| 284 | echoUse(); |
| 285 | |
| 286 | stopWex = UGL_FALSE; |
| 287 | loopEvent(); |
| 288 | |
| 289 | uglEventQDestroy (eventServiceId, qId); |
| 290 | |
| 291 | uglMesaDestroyContext(); |
| 292 | uglDeinitialize (); |
| 293 | |
| 294 | return; |
| 295 | } |