scissor-viewport.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   int i;
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   prog.i = 0;
48}
49
50static void reshape(int width, int height)
51{
52   glViewport(0, 0, 100, 100);
53
54   prog.width = width;
55   prog.height = height;
56
57   glMatrixMode(GL_PROJECTION);
58   glLoadIdentity();
59   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
60   glMatrixMode(GL_MODELVIEW);
61}
62
63static void key(unsigned char key, int x, int y)
64{
65
66   switch (key) {
67   case 27:
68      exit(1);
69   default:
70      glutPostRedisplay();
71      return;
72   }
73}
74
75static void drawQuad(void)
76{
77   glBegin(GL_QUADS);
78   glVertex2d(-1.0, -1.0);
79   glVertex2d( 1.0, -1.0);
80   glVertex2d( 1.0,  1.0);
81   glVertex2d(-1.0,  1.0);
82   glEnd();
83}
84
85static void draw(void)
86{
87   int i;
88
89   glClearColor(0.0, 0.0, 1.0, 0.0);
90   glClear(GL_COLOR_BUFFER_BIT);
91
92   i = prog.i++;
93   if (prog.i >= 3)
94      prog.i = 0;
95
96   glEnable(GL_SCISSOR_TEST);
97
98   {
99      glColor4d(1.0, 0.0, 0.0, 1.0);
100
101      glScissor(i, i, 10 - 2*i, 10 - 2*i);
102      drawQuad();
103   }
104
105   glDisable(GL_SCISSOR_TEST);
106
107   /* glutSwapBuffers(); */
108   glFlush();
109}
110
111int main(int argc, char **argv)
112{
113   GLenum type;
114
115   glutInit(&argc, argv);
116
117   prog.width = 200;
118   prog.height = 200;
119
120   glutInitWindowPosition(100, 0);
121   glutInitWindowSize(prog.width, prog.height);
122
123   /* type = GLUT_RGB | GLUT_DOUBLE; */
124   type = GLUT_RGB | GLUT_SINGLE;
125   glutInitDisplayMode(type);
126
127   if (glutCreateWindow(*argv) == GL_FALSE) {
128      exit(1);
129   }
130
131   init();
132
133   glutReshapeFunc(reshape);
134   glutKeyboardFunc(key);
135   glutDisplayFunc(draw);
136   glutMainLoop();
137   return 0;
138}
139