scissor.c revision 32001f49
1/* 2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc. 3 * Copyright (c) 2009 VMware, Inc. 4 * 5 * Permission to use, copy, modify, distribute, and sell this software and 6 * its documentation for any purpose is hereby granted without fee, provided 7 * that (i) the above copyright notices and this permission notice appear in 8 * all copies of the software and related documentation, and (ii) the name of 9 * Silicon Graphics may not be used in any advertising or 10 * publicity relating to the software without the specific, prior written 11 * permission of Silicon Graphics. 12 * 13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF 14 * ANY KIND, 15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 17 * 18 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR 19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 23 * OF THIS SOFTWARE. 24 */ 25 26#include <stdio.h> 27#include <string.h> 28#include <stdlib.h> 29#include "glut_wrap.h" 30 31struct program 32{ 33 unsigned width; 34 unsigned height; 35 unsigned quads; 36}; 37 38struct program prog; 39 40static void init(void) 41{ 42 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 43 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); 44 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); 45 fflush(stderr); 46} 47 48static void reshape(int width, int height) 49{ 50 51 glViewport(0, 0, (GLint)width, (GLint)height); 52 53 prog.width = width; 54 prog.height = height; 55 56 glMatrixMode(GL_PROJECTION); 57 glLoadIdentity(); 58 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); 59 glMatrixMode(GL_MODELVIEW); 60} 61 62static void key(unsigned char key, int x, int y) 63{ 64 65 switch (key) { 66 case 27: 67 exit(1); 68 default: 69 prog.quads = !prog.quads; 70 glutPostRedisplay(); 71 return; 72 } 73} 74 75static void drawQuad(void) 76{ 77 78 if (prog.quads) { 79 glBegin(GL_QUADS); 80 glVertex2d(-1.0, -1.0); 81 glVertex2d( 1.0, -1.0); 82 glVertex2d( 1.0, 1.0); 83 glVertex2d(-1.0, 1.0); 84 glEnd(); 85 } else { 86 glClear(GL_COLOR_BUFFER_BIT); 87 } 88} 89 90static void draw(void) 91{ 92 glClearColor(0.0, 0.0, 1.0, 0.0); 93 glClear(GL_COLOR_BUFFER_BIT); 94 95 printf("drawing with %s\n", prog.quads ? "quads" : "clears"); 96 97 glEnable(GL_SCISSOR_TEST); 98 99 { 100 glClearColor(1.0, 0.0, 0.0, 1.0); 101 glColor4d(1.0, 0.0, 0.0, 1.0); 102 103 glScissor(1, 1, 10, 10); 104 drawQuad(); 105 glScissor(1, prog.height - 11, 10, 10); 106 drawQuad(); 107 glScissor(prog.width - 11, prog.height - 11, 10, 10); 108 drawQuad(); 109 } 110 111 { 112 glClearColor(0.0, 1.0, 0.0, 1.0); 113 glColor4d(0.0, 1.0, 0.0, 1.0); 114 115 glScissor(12, 1, 10, 10); 116 drawQuad(); 117 glScissor(12, prog.height - 11, 10, 10); 118 drawQuad(); 119 glScissor(prog.width - 22, prog.height - 11, 10, 10); 120 drawQuad(); 121 } 122 123 { 124 glClearColor(1.0, 1.0, 0.0, 1.0); 125 glColor4d(1.0, 1.0, 0.0, 1.0); 126 127 glScissor(1, 12, 10, 10); 128 drawQuad(); 129 glScissor(1, prog.height - 22, 10, 10); 130 drawQuad(); 131 glScissor(prog.width - 11, prog.height - 22, 10, 10); 132 drawQuad(); 133 } 134 135 glDisable(GL_SCISSOR_TEST); 136 137 /* glutSwapBuffers(); */ 138 glFlush(); 139} 140 141int main(int argc, char **argv) 142{ 143 GLenum type; 144 145 glutInit(&argc, argv); 146 147 prog.width = 200; 148 prog.height = 200; 149 150 glutInitWindowPosition(100, 0); 151 glutInitWindowSize(prog.width, prog.height); 152 153 /* type = GLUT_RGB | GLUT_DOUBLE; */ 154 type = GLUT_RGB | GLUT_SINGLE; 155 glutInitDisplayMode(type); 156 157 if (glutCreateWindow(*argv) == GL_FALSE) { 158 exit(1); 159 } 160 161 init(); 162 163 glutReshapeFunc(reshape); 164 glutKeyboardFunc(key); 165 glutDisplayFunc(draw); 166 glutMainLoop(); 167 return 0; 168} 169