1/*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include <stdlib.h>
29#include <GL/glew.h>
30#include "glut_wrap.h"
31
32static int win_width, win_height;
33
34static void
35line(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
36{
37	glBegin(GL_LINES);
38	glVertex2f(x1, y1);
39	glVertex2f(x2, y2);
40	glEnd();
41}
42
43static void
44line3(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
45{
46	glBegin(GL_LINES);
47	glVertex3f(x1, y1, z1);
48	glVertex3f(x2, y2, z2);
49	glEnd();
50}
51
52static void
53display(void)
54{
55	glClearColor(0.0, 0.0, 0.0, 0.0);
56	glClear(GL_COLOR_BUFFER_BIT);
57
58	glColor3f(1.0, 0.0, 0.0);
59	/* 2 lines clipped along xmin */
60	line(-20, win_height / 2 - 20,
61	      20, win_height / 2 - 20);
62	line( 20, win_height / 2 + 20,
63	     -20, win_height / 2 + 20);
64
65	glColor3f(0.0, 1.0, 0.0);
66	/* 2 lines clipped along ymax */
67	line(win_width / 2 - 20, win_height + 20,
68	     win_width / 2 - 20, win_height - 20);
69	line(win_width / 2 + 20, win_height - 20,
70	     win_width / 2 + 20, win_height + 20);
71
72	glColor3f(0.0, 0.0, 1.0);
73	/* 2 lines clipped along xmax */
74	line(win_width - 20, win_height / 2 - 20,
75	     win_width + 20, win_height / 2 - 20);
76	line(win_width + 20, win_height / 2 + 20,
77	     win_width - 20, win_height / 2 + 20);
78
79	glColor3f(1.0, 1.0, 1.0);
80	/* 2 lines clipped along ymin */
81	line(win_width / 2 - 20,  20,
82	     win_width / 2 - 20, -20);
83	line(win_width / 2 + 20, -20,
84	     win_width / 2 + 20,  20);
85
86	/* 2 lines clipped along near */
87	glColor3f(1.0, 0.0, 1.0);
88	line3(win_width / 2 - 20 - 20, win_height / 2,       0.5,
89	      win_width / 2 - 20 + 20, win_height / 2,      -0.5);
90	line3(win_width / 2 - 20,      win_height / 2 - 20, -0.5,
91	      win_width / 2 - 20,      win_height / 2 + 20,  0.5);
92
93	/* 2 lines clipped along far */
94	glColor3f(0.0, 1.0, 1.0);
95	line3(win_width / 2 + 20 - 20, win_height / 2,      1.5,
96	      win_width / 2 + 20 + 20, win_height / 2,      0.5);
97	line3(win_width / 2 + 20,      win_height / 2 - 20, 0.5,
98	      win_width / 2 + 20,      win_height / 2 + 20, 1.5);
99
100	/* entirely clipped along near/far */
101	glColor3f(.5, .5, .5);
102	line3(win_width / 2, win_height / 2 - 20, -0.5,
103	      win_width / 2, win_height / 2 + 20, -0.5);
104	glColor3f(.5, .5, .5);
105	line3(win_width / 2, win_height / 2 - 20, 1.5,
106	      win_width / 2, win_height / 2 + 20, 1.5);
107
108	glColor3f(1.0, 1.0, 0.0);
109	/* lines clipped along both x and y limits */
110	line(-5, 20,
111	     20, -5); /* xmin, ymin */
112	line(-5, win_height - 20,
113	     20, win_height + 5); /* xmin, ymax */
114	line(win_width - 20, -5,
115	     win_width + 5,  20); /* xmax, ymin */
116	line(win_width - 20, win_height + 5,
117	     win_width + 5,  win_height - 20); /* xmax, ymax */
118
119	glutSwapBuffers();
120}
121
122static void
123reshape(int width, int height)
124{
125	win_width = width;
126	win_height = height;
127	glViewport(0, 0, width, height);
128
129	glMatrixMode(GL_PROJECTION);
130	glLoadIdentity();
131	glOrtho(0, win_width, 0, win_height, 0.0, -1.0);
132
133	glMatrixMode(GL_MODELVIEW);
134	glLoadIdentity();
135	glTranslatef(.25, .25, 0);
136}
137
138static void key( unsigned char key, int x, int y )
139{
140	(void) x;
141	(void) y;
142
143	switch (key) {
144	case 27: /* esc */
145		exit(0);
146		break;
147	}
148
149	glutPostRedisplay();
150}
151
152static void
153init(void)
154{
155}
156
157int
158main(int argc, char *argv[])
159{
160	win_width = 200;
161	win_height = 200;
162
163	glutInit(&argc, argv);
164	glutInitWindowPosition(0, 0);
165	glutInitWindowSize(win_width, win_height);
166	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
167	glutCreateWindow(argv[0]);
168        glewInit();
169	glutReshapeFunc(reshape);
170	glutKeyboardFunc(key);
171	glutDisplayFunc(display);
172
173	init();
174
175	glutMainLoop();
176	return 0;
177}
178