line-smooth.c revision 32001f49
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 <math.h> 26#include <stdio.h> 27#include <string.h> 28#include <stdlib.h> 29#include <GL/glew.h> 30#include "glut_wrap.h" 31 32 33#define CI_OFFSET_1 16 34#define CI_OFFSET_2 32 35 36 37GLenum doubleBuffer; 38GLboolean smooth = GL_TRUE; 39GLboolean flat = GL_TRUE; 40GLfloat width = 1.0; 41 42 43static void Init(void) 44{ 45 float range[2], aarange[2]; 46 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 47 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); 48 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); 49 fflush(stderr); 50 glGetFloatv(GL_LINE_WIDTH_RANGE, aarange); 51 glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, range); 52 printf("Non-AA line width range: %f .. %f\n", range[0], range[1]); 53 printf("AA line width range: %f .. %f\n", aarange[0], aarange[1]); 54 fflush(stdout); 55} 56 57 58static void Reshape(int width, int height) 59{ 60 glViewport(0, 0, (GLint)width, (GLint)height); 61 glMatrixMode(GL_PROJECTION); 62 glLoadIdentity(); 63 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); 64 glMatrixMode(GL_MODELVIEW); 65 glLoadIdentity(); 66 glTranslatef(0, 0, -30); 67} 68 69static void Key(unsigned char key, int x, int y) 70{ 71 switch (key) { 72 case 'f': 73 flat = !flat; 74 break; 75 case 'w': 76 width -= 0.5; 77 if (width < 0.5) 78 width = 0.5; 79 break; 80 case 'W': 81 width += 0.5; 82 break; 83 case 's': 84 smooth = !smooth; 85 break; 86 case 27: 87 exit(1); 88 default: 89 return; 90 } 91 printf("LineWidth: %g FlatShade: %d\n", width, flat); 92 glutPostRedisplay(); 93} 94 95 96static void Draw(void) 97{ 98 float a; 99 100 glClear(GL_COLOR_BUFFER_BIT); 101 102 glLineWidth(width); 103 104 if (smooth) { 105 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 106 glEnable(GL_BLEND); 107 glEnable(GL_LINE_SMOOTH); 108 } 109 110 glShadeModel(flat ? GL_FLAT : GL_SMOOTH); 111 112 glBegin(GL_LINES); 113 for (a = 0; a < 3.14159; a += 0.2) { 114 float x = .9 * cos(a); 115 float y = .9 * sin(a); 116 117 glColor3f(1, 0, 1); 118 glVertex2f(-x, -y); 119 glColor3f(1, 1, 1); 120 glVertex2f( x, y); 121 } 122 glEnd(); 123 124 glDisable(GL_LINE_SMOOTH); 125 glDisable(GL_BLEND); 126 127 glFlush(); 128 129 if (doubleBuffer) { 130 glutSwapBuffers(); 131 } 132} 133 134static GLenum Args(int argc, char **argv) 135{ 136 GLint i; 137 138 doubleBuffer = GL_FALSE; 139 140 for (i = 1; i < argc; i++) { 141 if (strcmp(argv[i], "-sb") == 0) { 142 doubleBuffer = GL_FALSE; 143 } else if (strcmp(argv[i], "-db") == 0) { 144 doubleBuffer = GL_TRUE; 145 } else { 146 fprintf(stderr, "%s (Bad option).\n", argv[i]); 147 return GL_FALSE; 148 } 149 } 150 return GL_TRUE; 151} 152 153 154int main(int argc, char **argv) 155{ 156 GLenum type; 157 158 glutInit(&argc, argv); 159 160 if (Args(argc, argv) == GL_FALSE) { 161 exit(1); 162 } 163 164 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250); 165 166 type = GLUT_RGB; 167 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; 168 glutInitDisplayMode(type); 169 170 if (glutCreateWindow(argv[0]) == GL_FALSE) { 171 exit(1); 172 } 173 174 Init(); 175 176 glutReshapeFunc(Reshape); 177 glutKeyboardFunc(Key); 178 glutDisplayFunc(Draw); 179 glutMainLoop(); 180 return 0; 181} 182