progs/redbook: add additional tests for GL 1.4
[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 #define GL_GLEXT_PROTOTYPES
64
65 #include <GL/glut.h>
66 #include <stdlib.h>
67 #include <stdio.h>
68
69 static GLfloat psize = 7.0;
70 static GLfloat pmax[1];
71 static GLfloat constant[3] = {1.0, 0.0, 0.0};
72 static GLfloat linear[3] = {0.0, 0.12, 0.0};
73 static GLfloat quadratic[3] = {0.0, 0.0, 0.01};
74
75 static void init(void)
76 {
77 int i;
78
79 srand (12345);
80
81 glNewList(1, GL_COMPILE);
82 glBegin (GL_POINTS);
83 for (i = 0; i < 250; i++) {
84 glColor3f (1.0, ((rand()/(float) RAND_MAX) * 0.5) + 0.5,
85 rand()/(float) RAND_MAX);
86 /* randomly generated vertices:
87 -5 < x < 5; -5 < y < 5; -5 < z < -45 */
88 glVertex3f ( ((rand()/(float)RAND_MAX) * 10.0) - 5.0,
89 ((rand()/(float)RAND_MAX) * 10.0) - 5.0,
90 ((rand()/(float)RAND_MAX) * 40.0) - 45.0);
91 }
92 glEnd();
93 glEndList();
94
95 glEnable(GL_DEPTH_TEST);
96 glEnable(GL_POINT_SMOOTH);
97 glEnable(GL_BLEND);
98 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 glPointSize(psize);
100 glGetFloatv(GL_POINT_SIZE_MAX_EXT, pmax);
101
102 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, linear);
103 glPointParameterfEXT (GL_POINT_FADE_THRESHOLD_SIZE_EXT, 2.0);
104 }
105
106 static void display(void)
107 {
108 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 glCallList (1);
110 glutSwapBuffers ();
111 }
112
113 static void reshape (int w, int h)
114 {
115 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
116 glMatrixMode (GL_PROJECTION);
117 glLoadIdentity ();
118 gluPerspective (35.0, 1.0, 0.25, 200.0);
119 glMatrixMode (GL_MODELVIEW);
120 glTranslatef (0.0, 0.0, -10.0);
121 }
122
123 static void keyboard(unsigned char key, int x, int y)
124 {
125 switch (key) {
126 case 'b':
127 glMatrixMode (GL_MODELVIEW);
128 glTranslatef (0.0, 0.0, -0.5);
129 glutPostRedisplay();
130 break;
131 case 'c':
132 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, constant);
133 glutPostRedisplay();
134 break;
135 case 'f':
136 glMatrixMode (GL_MODELVIEW);
137 glTranslatef (0.0, 0.0, 0.5);
138 glutPostRedisplay();
139 break;
140 case 'l':
141 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, linear);
142 glutPostRedisplay();
143 break;
144 case 'q':
145 glPointParameterfvEXT (GL_DISTANCE_ATTENUATION_EXT, quadratic);
146 glutPostRedisplay();
147 break;
148 case '+':
149 if (psize < (pmax[0] + 1.0))
150 psize = psize + 1.0;
151 glPointSize (psize);
152 glutPostRedisplay();
153 break;
154 case '-':
155 if (psize >= 2.0)
156 psize = psize - 1.0;
157 glPointSize (psize);
158 glutPostRedisplay();
159 break;
160 case 27:
161 exit(0);
162 break;
163 }
164 }
165
166 int main(int argc, char** argv)
167 {
168 glutInit(&argc, argv);
169 glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
170 glutInitWindowSize (500, 500);
171 glutInitWindowPosition (100, 100);
172 glutCreateWindow (argv[0]);
173 init ();
174 glutDisplayFunc (display);
175 glutReshapeFunc (reshape);
176 glutKeyboardFunc (keyboard);
177 glutMainLoop();
178 return 0;
179 }