2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
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.
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
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
36 #define PI 3.141592654
40 #define RD 0xA4,0x00,0x00,0xFF
41 #define WT 0xFF,0xFF,0xFF,0xFF
42 #define brickImageWidth 16
43 #define brickImageHeight 16
48 GLenum rgb
, doubleBuffer
;
65 GLenum doDither
= GL_TRUE
;
66 GLenum shade
= GL_TRUE
;
67 GLenum texture
= GL_TRUE
;
69 float xRotation
= 30.0, yRotation
= 30.0, zRotation
= 0.0;
70 GLint radius1
, radius2
;
71 GLdouble angle1
, angle2
;
74 GLint orientation
= GLU_OUTSIDE
;
76 GLUquadricObj
*quadObj
;
78 GLubyte brickImage
[4*brickImageWidth
*brickImageHeight
] = {
79 RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
,
80 RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
,
81 RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
,
82 RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
,
83 WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
,
84 RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
,
85 RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
,
86 RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
,
87 RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
,
88 WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
,
89 RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
,
90 RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
,
91 RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
,
92 RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
,
93 WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
, WT
,
94 RD
, RD
, RD
, RD
, WT
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, RD
, WT
, RD
96 char *texFileName
= 0;
99 static void CALLBACK
ErrorHandler(GLenum which
)
102 fprintf(stderr
, "Quad Error: %s\n", (char *) gluErrorString(which
));
105 typedef void (GLAPIENTRY
*callback_t
)();
107 static void Init(void)
109 static GLint colorIndexes
[3] = {0, 200, 255};
110 static float ambient
[] = {0.1, 0.1, 0.1, 1.0};
111 static float diffuse
[] = {0.5, 1.0, 1.0, 1.0};
112 static float position
[] = {90.0, 90.0, 150.0, 0.0};
113 static float front_mat_shininess
[] = {30.0};
114 static float front_mat_specular
[] = {0.2, 0.2, 0.2, 1.0};
115 static float front_mat_diffuse
[] = {0.5, 0.28, 0.38, 1.0};
116 static float back_mat_shininess
[] = {50.0};
117 static float back_mat_specular
[] = {0.5, 0.5, 0.2, 1.0};
118 static float back_mat_diffuse
[] = {1.0, 1.0, 0.2, 1.0};
119 static float lmodel_ambient
[] = {1.0, 1.0, 1.0, 1.0};
120 static float lmodel_twoside
[] = {GL_TRUE
};
121 static float decal
[] = {GL_DECAL
};
122 static float repeat
[] = {GL_REPEAT
};
123 static float nearest
[] = {GL_NEAREST
};
124 static PPMImage
*image
;
129 glClearColor(0.0, 0.0, 0.0, 0.0);
131 glEnable(GL_DEPTH_TEST
);
133 glLightfv(GL_LIGHT0
, GL_AMBIENT
, ambient
);
134 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, diffuse
);
135 glLightfv(GL_LIGHT0
, GL_POSITION
, position
);
136 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, lmodel_ambient
);
137 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE
, lmodel_twoside
);
138 glEnable(GL_LIGHTING
);
141 glMaterialfv(GL_FRONT
, GL_SHININESS
, front_mat_shininess
);
142 glMaterialfv(GL_FRONT
, GL_SPECULAR
, front_mat_specular
);
143 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, front_mat_diffuse
);
144 glMaterialfv(GL_BACK
, GL_SHININESS
, back_mat_shininess
);
145 glMaterialfv(GL_BACK
, GL_SPECULAR
, back_mat_specular
);
146 glMaterialfv(GL_BACK
, GL_DIFFUSE
, back_mat_diffuse
);
148 glMaterialiv( GL_FRONT_AND_BACK
, GL_COLOR_INDEXES
, colorIndexes
);
151 glTexEnvfv(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, decal
);
152 glTexParameterfv(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, repeat
);
153 glTexParameterfv(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, repeat
);
154 glTexParameterfv(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, nearest
);
155 glTexParameterfv(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, nearest
);
157 image
= LoadPPM(texFileName
);
158 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
159 gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, image
->sizeX
, image
->sizeY
,
160 GL_RGB
, GL_UNSIGNED_BYTE
, image
->data
);
162 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
163 glTexImage2D(GL_TEXTURE_2D
, 0, 4, brickImageWidth
, brickImageHeight
,
164 0, GL_RGBA
, GL_UNSIGNED_BYTE
, (GLvoid
*)brickImage
);
167 quadObj
= gluNewQuadric();
168 gluQuadricCallback(quadObj
, GLU_ERROR
, (callback_t
) ErrorHandler
);
179 static void Reshape(int width
, int height
)
182 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
184 glMatrixMode(GL_PROJECTION
);
186 glFrustum(-1, 1, -1, 1, 1, 10);
187 gluLookAt(2, 2, 2, 0, 0, 0, 0, 0, 1);
188 glMatrixMode(GL_MODELVIEW
);
191 static void Key2(int key
, int x
, int y
)
214 static void Key(unsigned char key
, int x
, int y
)
229 gluQuadricDrawStyle(quadObj
, GLU_FILL
);
232 gluQuadricDrawStyle(quadObj
, GLU_POINT
);
235 gluQuadricDrawStyle(quadObj
, GLU_LINE
);
238 gluQuadricDrawStyle(quadObj
, GLU_SILHOUETTE
);
244 glShadeModel(GL_SMOOTH
);
245 gluQuadricNormals(quadObj
, GLU_SMOOTH
);
247 glShadeModel(GL_FLAT
);
248 gluQuadricNormals(quadObj
, GLU_FLAT
);
267 switch(orientation
) {
269 orientation
= GLU_INSIDE
;
273 orientation
= GLU_OUTSIDE
;
276 gluQuadricOrientation(quadObj
, orientation
);
280 whichQuadric
= (whichQuadric
+ 1) % 4;
321 gluQuadricTexture(quadObj
, GL_TRUE
);
322 glEnable(GL_TEXTURE_2D
);
324 gluQuadricTexture(quadObj
, GL_FALSE
);
325 glDisable(GL_TEXTURE_2D
);
330 glDisable(GL_CULL_FACE
);
333 glEnable(GL_CULL_FACE
);
334 glCullFace(GL_FRONT
);
337 glEnable(GL_CULL_FACE
);
349 doDither
= !doDither
;
350 (doDither
) ? glEnable(GL_DITHER
) : glDisable(GL_DITHER
);
359 static void Draw(void)
363 glRotatef(xRotation
, 1, 0, 0);
364 glRotatef(yRotation
, 0, 1, 0);
365 glRotatef(zRotation
, 0, 0, 1);
367 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
369 glColor3f(1.0, 1.0, 1.0);
370 switch (whichQuadric
) {
372 glTranslatef(0, 0, -height
/20.0);
373 gluCylinder(quadObj
, radius1
/10.0, radius2
/10.0, height
/10.0,
377 gluSphere(quadObj
, radius1
/10.0, slices
, stacks
);
380 gluPartialDisk(quadObj
, radius2
/10.0, radius1
/10.0, slices
,
381 stacks
, angle1
, angle2
);
384 gluDisk(quadObj
, radius2
/10.0, radius1
/10.0, slices
, stacks
);
395 static GLenum
Args(int argc
, char **argv
)
400 doubleBuffer
= GL_FALSE
;
402 for (i
= 1; i
< argc
; i
++) {
403 if (strcmp(argv
[i
], "-ci") == 0) {
405 } else if (strcmp(argv
[i
], "-rgb") == 0) {
407 } else if (strcmp(argv
[i
], "-sb") == 0) {
408 doubleBuffer
= GL_FALSE
;
409 } else if (strcmp(argv
[i
], "-db") == 0) {
410 doubleBuffer
= GL_TRUE
;
411 } else if (strcmp(argv
[i
], "-f") == 0) {
412 if (i
+1 >= argc
|| argv
[i
+1][0] == '-') {
413 printf("-f (No file name).\n");
416 texFileName
= argv
[++i
];
419 printf("%s (Bad option).\n", argv
[i
]);
426 int main(int argc
, char **argv
)
430 glutInit(&argc
, argv
);
432 if (Args(argc
, argv
) == GL_FALSE
) {
436 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300);
439 type
|= (rgb
) ? GLUT_RGB
: GLUT_INDEX
;
440 type
|= (doubleBuffer
) ? GLUT_DOUBLE
: GLUT_SINGLE
;
441 glutInitDisplayMode(type
);
443 if (glutCreateWindow("Quad Test") == GL_FALSE
) {
451 glutReshapeFunc(Reshape
);
452 glutKeyboardFunc(Key
);
453 glutSpecialFunc(Key2
);
454 glutDisplayFunc(Draw
);