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  *  multisamp.c
45  *  This program draws shows how to use multisampling to
46  *  draw anti-aliased geometric primitives.  The same
47  *  display list, a pinwheel of triangles and lines of
48  *  varying widths, is rendered twice.  Multisampling is
49  *  enabled when the left side is drawn.  Multisampling is
50  *  disabled when the right side is drawn.
51  *
52  *  Pressing the 'b' key toggles drawing of the checkerboard
53  *  background.  Antialiasing is sometimes easier to see
54  *  when objects are rendered over a contrasting background.
55  */
56 #include <GL/glew.h>
57 #include "glut_wrap.h"
58 #include <stdlib.h>
59 #include <stdio.h>
60 
61 static int bgtoggle = 1;
62 
63 /*
64  *  Print out state values related to multisampling.
65  *  Create display list with "pinwheel" of lines and
66  *  triangles.
67  */
init(void)68 static void init(void)
69 {
70    static GLint buf[1], sbuf[1];
71    int i, j;
72 
73    glClearColor(0.0, 0.0, 0.0, 0.0);
74    glGetIntegerv (GL_SAMPLE_BUFFERS_ARB, buf);
75    printf ("number of sample buffers is %d\n", buf[0]);
76    glGetIntegerv (GL_SAMPLES_ARB, sbuf);
77    printf ("number of samples is %d\n", sbuf[0]);
78 
79    glNewList (1, GL_COMPILE);
80    for (i = 0; i < 19; i++) {
81       glPushMatrix();
82       glRotatef(360.0*(float)i/19.0, 0.0, 0.0, 1.0);
83       glColor3f (1.0, 1.0, 1.0);
84       glLineWidth((i%3)+1.0);
85       glBegin (GL_LINES);
86          glVertex2f (0.25, 0.05);
87          glVertex2f (0.9, 0.2);
88       glEnd ();
89       glColor3f (0.0, 1.0, 1.0);
90       glBegin (GL_TRIANGLES);
91          glVertex2f (0.25, 0.0);
92          glVertex2f (0.9, 0.0);
93          glVertex2f (0.875, 0.10);
94       glEnd ();
95       glPopMatrix();
96    }
97    glEndList ();
98 
99    glNewList (2, GL_COMPILE);
100    glColor3f (1.0, 0.5, 0.0);
101    glBegin (GL_QUADS);
102    for (i = 0; i < 16; i++) {
103       for (j = 0; j < 16; j++) {
104          if (((i + j) % 2) == 0) {
105             glVertex2f (-2.0 + (i * 0.25), -2.0 + (j * 0.25));
106             glVertex2f (-2.0 + (i * 0.25), -1.75 + (j * 0.25));
107             glVertex2f (-1.75 + (i * 0.25), -1.75 + (j * 0.25));
108             glVertex2f (-1.75 + (i * 0.25), -2.0 + (j * 0.25));
109          }
110       }
111    }
112    glEnd ();
113    glEndList ();
114 }
115 
116 /*  Draw two sets of primitives, so that you can
117  *  compare the user of multisampling against its absence.
118  *
119  *  This code enables antialiasing and draws one display list
120  *  and disables and draws the other display list
121  */
display(void)122 static void display(void)
123 {
124    glClear(GL_COLOR_BUFFER_BIT);
125 
126    if (bgtoggle)
127       glCallList (2);
128 
129    glEnable (GL_MULTISAMPLE_ARB);
130    glPushMatrix();
131    glTranslatef (-1.0, 0.0, 0.0);
132    glCallList (1);
133    glPopMatrix();
134 
135    glDisable (GL_MULTISAMPLE_ARB);
136    glPushMatrix();
137    glTranslatef (1.0, 0.0, 0.0);
138    glCallList (1);
139    glPopMatrix();
140    glutSwapBuffers();
141 }
142 
reshape(int w,int h)143 static void reshape(int w, int h)
144 {
145    glViewport(0, 0, w, h);
146    glMatrixMode(GL_PROJECTION);
147    glLoadIdentity();
148    if (w <= (2 * h))
149       gluOrtho2D (-2.0, 2.0,
150          -2.0*(GLfloat)h/(GLfloat)w, 2.0*(GLfloat)h/(GLfloat)w);
151    else
152       gluOrtho2D (-2.0*(GLfloat)w/(GLfloat)h,
153          2.0*(GLfloat)w/(GLfloat)h, -2.0, 2.0);
154    glMatrixMode(GL_MODELVIEW);
155    glLoadIdentity();
156 }
157 
keyboard(unsigned char key,int x,int y)158 static void keyboard(unsigned char key, int x, int y)
159 {
160    switch (key) {
161       case 'b':
162       case 'B':
163          bgtoggle = !bgtoggle;
164          glutPostRedisplay();
165          break;
166       case 27:  /*  Escape Key  */
167          exit(0);
168       default:
169          break;
170     }
171 }
172 
173 /*  Main Loop
174  *  Open window with initial window size, title bar,
175  *  RGBA display mode, and handle input events.
176  */
main(int argc,char ** argv)177 int main(int argc, char** argv)
178 {
179    glutInit(&argc, argv);
180    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE);
181    glutInitWindowSize (600, 300);
182    glutCreateWindow (argv[0]);
183    init();
184    glutReshapeFunc (reshape);
185    glutKeyboardFunc (keyboard);
186    glutDisplayFunc (display);
187    glutMainLoop();
188    return 0;
189 }
190