1/* 2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc. 3 * 4 * Permission to use, copy, modify, distribute, and sell this software and 5 * its documentation for any purpose is hereby granted without fee, provided 6 * that (i) the above copyright notices and this permission notice appear in 7 * all copies of the software and related documentation, and (ii) the name of 8 * Silicon Graphics may not be used in any advertising or 9 * publicity relating to the software without the specific, prior written 10 * permission of Silicon Graphics. 11 * 12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF 13 * ANY KIND, 14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR 18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 22 * OF THIS SOFTWARE. 23 */ 24 25#include <stdio.h> 26#include <string.h> 27#include <stdlib.h> 28#include "glut_wrap.h" 29 30GLenum rgb, doubleBuffer; 31 32#include "tkmap.c" 33 34double plane[4] = { 35 1.0, 0.0, -1.0, 0.0 36}; 37float rotX = 5.0, rotY = -5.0, zTranslate = -65.0; 38float fogDensity = 0.02; 39GLint cubeList = 1; 40 41float scp[18][3] = { 42 { 43 1.000000, 0.000000, 0.000000 44 }, 45 { 46 1.000000, 0.000000, 5.000000 47 }, 48 { 49 0.707107, 0.707107, 0.000000 50 }, 51 { 52 0.707107, 0.707107, 5.000000 53 }, 54 { 55 0.000000, 1.000000, 0.000000 56 }, 57 { 58 0.000000, 1.000000, 5.000000 59 }, 60 { 61 -0.707107, 0.707107, 0.000000 62 }, 63 { 64 -0.707107, 0.707107, 5.000000 65 }, 66 { 67 -1.000000, 0.000000, 0.000000 68 }, 69 { 70 -1.000000, 0.000000, 5.000000 71 }, 72 { 73 -0.707107, -0.707107, 0.000000 74 }, 75 { 76 -0.707107, -0.707107, 5.000000 77 }, 78 { 79 0.000000, -1.000000, 0.000000 80 }, 81 { 82 0.000000, -1.000000, 5.000000 83 }, 84 { 85 0.707107, -0.707107, 0.000000 86 }, 87 { 88 0.707107, -0.707107, 5.000000 89 }, 90 { 91 1.000000, 0.000000, 0.000000 92 }, 93 { 94 1.000000, 0.000000, 5.000000 95 }, 96}; 97 98 99static void Build_lists(void) 100{ 101 102 glNewList(cubeList, GL_COMPILE); 103 glBegin(GL_TRIANGLE_STRIP); 104 glNormal3fv(scp[0]); glVertex3fv(scp[0]); 105 glNormal3fv(scp[0]); glVertex3fv(scp[1]); 106 glNormal3fv(scp[2]); glVertex3fv(scp[2]); 107 glNormal3fv(scp[2]); glVertex3fv(scp[3]); 108 glNormal3fv(scp[4]); glVertex3fv(scp[4]); 109 glNormal3fv(scp[4]); glVertex3fv(scp[5]); 110 glNormal3fv(scp[6]); glVertex3fv(scp[6]); 111 glNormal3fv(scp[6]); glVertex3fv(scp[7]); 112 glNormal3fv(scp[8]); glVertex3fv(scp[8]); 113 glNormal3fv(scp[8]); glVertex3fv(scp[9]); 114 glNormal3fv(scp[10]); glVertex3fv(scp[10]); 115 glNormal3fv(scp[10]); glVertex3fv(scp[11]); 116 glNormal3fv(scp[12]); glVertex3fv(scp[12]); 117 glNormal3fv(scp[12]); glVertex3fv(scp[13]); 118 glNormal3fv(scp[14]); glVertex3fv(scp[14]); 119 glNormal3fv(scp[14]); glVertex3fv(scp[15]); 120 glNormal3fv(scp[16]); glVertex3fv(scp[16]); 121 glNormal3fv(scp[16]); glVertex3fv(scp[17]); 122 glEnd(); 123 glEndList(); 124} 125 126static void Init(void) 127{ 128 static float ambient[] = {0.1, 0.1, 0.1, 1.0}; 129 static float diffuse[] = {1.0, 1.0, 1.0, 1.0}; 130 static float position[] = {90.0, 90.0, 0.0, 0.0}; 131 static float front_mat_shininess[] = {30.0}; 132 static float front_mat_specular[] = {0.0, 0.0, 0.0, 1.0}; 133 static float front_mat_diffuse[] = {0.0, 1.0, 0.0, 1.0}; 134 static float back_mat_shininess[] = {50.0}; 135 static float back_mat_specular[] = {0.0, 0.0, 1.0, 1.0}; 136 static float back_mat_diffuse[] = {1.0, 0.0, 0.0, 1.0}; 137 static float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0}; 138 static float fog_color[] = {0.8, 0.8, 0.8, 1.0}; 139 140 glFrontFace(GL_CW); 141 142 glEnable(GL_DEPTH_TEST); 143 144 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); 145 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); 146 glLightfv(GL_LIGHT0, GL_POSITION, position); 147 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); 148 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); 149 glEnable(GL_LIGHTING); 150 glEnable(GL_LIGHT0); 151 152 glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess); 153 glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular); 154 glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse); 155 glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess); 156 glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular); 157 glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse); 158 159 glEnable(GL_FOG); 160 glFogi(GL_FOG_MODE, GL_EXP); 161 glFogf(GL_FOG_DENSITY, fogDensity); 162 if (rgb) { 163 glFogfv(GL_FOG_COLOR, fog_color); 164 glClearColor(0.8, 0.8, 0.8, 1.0); 165 } else { 166 glFogi(GL_FOG_INDEX, 1<<5); 167 SetFogRamp(5, 3); 168 glClearIndex(128); 169 } 170 171 Build_lists(); 172} 173 174static void Reshape(int width, int height) 175{ 176 177 glViewport(0, 0, (GLint)width, (GLint)height); 178 179 glMatrixMode(GL_PROJECTION); 180 glLoadIdentity(); 181 gluPerspective(45.0, 1.0, 1.0, 200.0); 182 glMatrixMode(GL_MODELVIEW); 183} 184 185static void Key2(int key, int x, int y) 186{ 187 188 switch (key) { 189 case GLUT_KEY_UP: 190 rotX -= 5; 191 break; 192 case GLUT_KEY_DOWN: 193 rotX += 5; 194 break; 195 case GLUT_KEY_LEFT: 196 rotY -= 5; 197 break; 198 case GLUT_KEY_RIGHT: 199 rotY += 5; 200 break; 201 default: 202 return; 203 } 204 205 glutPostRedisplay(); 206} 207 208static void Key(unsigned char key, int x, int y) 209{ 210 211 switch (key) { 212 case 27: 213 exit(1); 214 case 'D': 215 if (rgb) { 216 fogDensity *= 1.10; 217 glFogf(GL_FOG_DENSITY, fogDensity); 218 } 219 break; 220 case 'd': 221 if (rgb) { 222 fogDensity /= 1.10; 223 glFogf(GL_FOG_DENSITY, fogDensity); 224 } 225 break; 226 default: 227 return; 228 } 229 230 glutPostRedisplay(); 231} 232 233static void Draw(void) 234{ 235 236 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 237 238 glPushMatrix(); 239 240 glTranslatef(0, 0, zTranslate); 241 glRotatef(rotY, 0,1,0); 242 glRotatef(rotX, 1,0,0); 243 glScalef(1.0, 1.0, 10.0); 244 245 glCallList(cubeList); 246 247 glPopMatrix(); 248 249 glFlush(); 250 251 if (doubleBuffer) { 252 glutSwapBuffers(); 253 } 254} 255 256static GLenum Args(int argc, char **argv) 257{ 258 GLint i; 259 260 rgb = GL_TRUE; 261 doubleBuffer = GL_TRUE; 262 263 for (i = 1; i < argc; i++) { 264 if (strcmp(argv[i], "-ci") == 0) { 265 rgb = GL_FALSE; 266 } else if (strcmp(argv[i], "-rgb") == 0) { 267 rgb = GL_TRUE; 268 } else if (strcmp(argv[i], "-sb") == 0) { 269 doubleBuffer = GL_FALSE; 270 } else if (strcmp(argv[i], "-db") == 0) { 271 doubleBuffer = GL_TRUE; 272 } else { 273 printf("%s (Bad option).\n", argv[i]); 274 return GL_FALSE; 275 } 276 } 277 return GL_TRUE; 278} 279 280int main(int argc, char **argv) 281{ 282 GLenum type; 283 284 glutInit(&argc, argv); 285 286 if (Args(argc, argv) == GL_FALSE) { 287 exit(1); 288 } 289 290 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300); 291 292 type = GLUT_DEPTH; 293 type |= (rgb) ? GLUT_RGB : GLUT_INDEX; 294 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; 295 glutInitDisplayMode(type); 296 297 if (glutCreateWindow("Fog Test") == GL_FALSE) { 298 exit(1); 299 } 300 301 InitMap(); 302 303 Init(); 304 305 glutReshapeFunc(Reshape); 306 glutKeyboardFunc(Key); 307 glutSpecialFunc(Key2); 308 glutDisplayFunc(Draw); 309 glutMainLoop(); 310 return 0; 311} 312