Merge branch '7.8'
[mesa.git] / progs / redbook / pointp.c
1 /*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43 /*
44 * pointp.c
45 * This program demonstrates point parameters and their effect
46 * on point primitives.
47 * 250 points are randomly generated within a 10 by 10 by 40
48 * region, centered at the origin. In some modes (including the
49 * default), points that are closer to the viewer will appear larger.
50 *
51 * Pressing the 'l', 'q', and 'c' keys switch the point
52 * parameters attenuation mode to linear, quadratic, or constant,
53 * respectively.
54 * Pressing the 'f' and 'b' keys move the viewer forward
55 * and backwards. In either linear or quadratic attenuation
56 * mode, the distance from the viewer to the point will change
57 * the size of the point primitive.
58 * Pressing the '+' and '-' keys will change the current point
59 * size. In this program, the point size is bounded, so it
60 * will not get less than 2.0, nor greater than GL_POINT_SIZE_MAX.
61 */
62
63 #include <GL/glew.h>
64 #include <GL/glut.h>
65 #include <stdlib.h>
66 #include <stdio.h>
67
68 static GLfloat psize = 7.0;
69 static GLfloat pmax[1];
70 static GLfloat constant[3] = {1.0, 0.0, 0.0};
71 static GLfloat linear[3] = {0.0, 0.12, 0.0};
72 static GLfloat quadratic[3] = {0.0, 0.0, 0.01};
73
74 static void init(void)
75 {
76 int i;
77
78 srand (12345);
79
80 glNewList(1, GL_COMPILE);
81 glBegin (GL_POINTS);
82 for (i = 0; i < 250; i++) {
83 glColor3f (1.0, ((rand()/(float) RAND_MAX) * 0.5) + 0.5,
84 rand()/(float) RAND_MAX);
85 /* randomly generated vertices:
86 -5 < x < 5; -5 < y < 5; -5 < z < -45 */
87 glVertex3f ( ((rand()/(float)RAND_MAX) * 10.0) - 5.0,
88 ((rand()/(float)RAND_MAX) * 10.0) - 5.0,
89 ((rand()/(float)RAND_MAX) * 40.0) - 45.0);
90 }
91 glEnd();
92 glEndList();
93
94 glEnable(GL_DEPTH_TEST);
95 glEnable(GL_POINT_SMOOTH);
96 glEnable(GL_BLEND);
97 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98 glPointSize(psize);
99 glGetFloatv(GL_POINT_SIZE_MAX_EXT, pmax);
100
101 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, linear);
102 glPointParameterfEXT (GL_POINT_FADE_THRESHOLD_SIZE_EXT, 2.0);
103 }
104
105 static void display(void)
106 {
107 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108 glCallList (1);
109 glutSwapBuffers ();
110 }
111
112 static void reshape (int w, int h)
113 {
114 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
115 glMatrixMode (GL_PROJECTION);
116 glLoadIdentity ();
117 gluPerspective (35.0, 1.0, 0.25, 200.0);
118 glMatrixMode (GL_MODELVIEW);
119 glTranslatef (0.0, 0.0, -10.0);
120 }
121
122 static void keyboard(unsigned char key, int x, int y)
123 {
124 switch (key) {
125 case 'b':
126 glMatrixMode (GL_MODELVIEW);
127 glTranslatef (0.0, 0.0, -0.5);
128 glutPostRedisplay();
129 break;
130 case 'c':
131 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, constant);
132 glutPostRedisplay();
133 break;
134 case 'f':
135 glMatrixMode (GL_MODELVIEW);
136 glTranslatef (0.0, 0.0, 0.5);
137 glutPostRedisplay();
138 break;
139 case 'l':
140 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, linear);
141 glutPostRedisplay();
142 break;
143 case 'q':
144 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, quadratic);
145 glutPostRedisplay();
146 break;
147 case '+':
148 if (psize < (pmax[0] + 1.0))
149 psize = psize + 1.0;
150 glPointSize (psize);
151 glutPostRedisplay();
152 break;
153 case '-':
154 if (psize >= 2.0)
155 psize = psize - 1.0;
156 glPointSize (psize);
157 glutPostRedisplay();
158 break;
159 case 27:
160 exit(0);
161 break;
162 }
163 }
164
165 int main(int argc, char** argv)
166 {
167 glutInit(&argc, argv);
168 glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
169 glutInitWindowSize (500, 500);
170 glutInitWindowPosition (100, 100);
171 glutCreateWindow (argv[0]);
172 glewInit();
173 init ();
174 glutDisplayFunc (display);
175 glutReshapeFunc (reshape);
176 glutKeyboardFunc (keyboard);
177 glutMainLoop();
178 return 0;
179 }