progs/gallium: Add trivial gallium demos
[mesa.git] / progs / trivial / tri-query.c
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 <GL/glew.h>
29 #include <GL/glut.h>
30
31
32
33 #define CI_OFFSET_1 16
34 #define CI_OFFSET_2 32
35
36 static GLuint OccQuery;
37
38 GLenum doubleBuffer;
39
40 static void Init(void)
41 {
42 if (!glutExtensionSupported("GL_ARB_occlusion_query")) {
43 fprintf(stderr, "Sorry, this program requires GL_ARB_occlusion_query\n");
44 exit(1);
45 }
46
47 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
48 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
49 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
50 fflush(stderr);
51
52 glClearColor(0.0, 0.0, 1.0, 0.0);
53
54 glGenQueriesARB(1, &OccQuery);
55 }
56
57 static void Reshape(int width, int height)
58 {
59
60 glViewport(0, 0, (GLint)width, (GLint)height);
61
62 glMatrixMode(GL_PROJECTION);
63 glLoadIdentity();
64 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
65 glMatrixMode(GL_MODELVIEW);
66 }
67
68 static void Key(unsigned char key, int x, int y)
69 {
70
71 switch (key) {
72 case 27:
73 exit(1);
74 default:
75 break;
76 }
77
78 glutPostRedisplay();
79 }
80
81 static void Draw(void)
82 {
83 GLuint passed;
84 GLint ready;
85
86 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
87
88 glEnable(GL_DEPTH_TEST);
89
90 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
91
92 glBegin(GL_TRIANGLES);
93 glColor3f(0,0,.7);
94 glVertex3f( 0.9, -0.9, -30.0);
95 glColor3f(.8,0,0);
96 glVertex3f( 0.9, 0.9, -30.0);
97 glColor3f(0,.9,0);
98 glVertex3f(-0.9, 0.0, -30.0);
99 glEnd();
100
101 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
102
103 do {
104 /* do useful work here, if any */
105 glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
106 } while (!ready);
107 glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
108
109 fprintf(stderr, " %d Fragments Visible\n", passed);
110
111 glFlush();
112
113 if (doubleBuffer) {
114 glutSwapBuffers();
115 }
116 }
117
118 static GLenum Args(int argc, char **argv)
119 {
120 GLint i;
121
122 doubleBuffer = GL_FALSE;
123
124 for (i = 1; i < argc; i++) {
125 if (strcmp(argv[i], "-sb") == 0) {
126 doubleBuffer = GL_FALSE;
127 } else if (strcmp(argv[i], "-db") == 0) {
128 doubleBuffer = GL_TRUE;
129 } else {
130 fprintf(stderr, "%s (Bad option).\n", argv[i]);
131 return GL_FALSE;
132 }
133 }
134 return GL_TRUE;
135 }
136
137 int main(int argc, char **argv)
138 {
139 GLenum type;
140
141 glutInit(&argc, argv);
142
143 if (Args(argc, argv) == GL_FALSE) {
144 exit(1);
145 }
146
147 glutInitWindowPosition(100, 0); glutInitWindowSize( 250, 250);
148
149 type = GLUT_RGB | GLUT_DEPTH;
150 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
151 glutInitDisplayMode(type);
152
153 if (glutCreateWindow(*argv) == GL_FALSE) {
154 exit(1);
155 }
156
157 glewInit();
158
159 Init();
160
161 glutReshapeFunc(Reshape);
162 glutKeyboardFunc(Key);
163 glutDisplayFunc(Draw);
164 glutMainLoop();
165 return 0;
166 }