Eric Anholt | 227af94 | 2008-03-27 11:57:08 -0700 | [diff] [blame^] | 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/glut.h> |
| 30 | |
| 31 | static int win_width, win_height; |
| 32 | |
| 33 | static void |
| 34 | line(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) |
| 35 | { |
| 36 | glBegin(GL_LINES); |
| 37 | glVertex2f(x1, y1); |
| 38 | glVertex2f(x2, y2); |
| 39 | glEnd(); |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | line3(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2) |
| 44 | { |
| 45 | glBegin(GL_LINES); |
| 46 | glVertex3f(x1, y1, z1); |
| 47 | glVertex3f(x2, y2, z2); |
| 48 | glEnd(); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | display(void) |
| 53 | { |
| 54 | glClearColor(0.0, 0.0, 0.0, 0.0); |
| 55 | glClear(GL_COLOR_BUFFER_BIT); |
| 56 | |
| 57 | glColor3f(1.0, 0.0, 0.0); |
| 58 | /* 2 lines clipped along xmin */ |
| 59 | line(-20, win_height / 2 - 20, |
| 60 | 20, win_height / 2 - 20); |
| 61 | line( 20, win_height / 2 + 20, |
| 62 | -20, win_height / 2 + 20); |
| 63 | |
| 64 | glColor3f(0.0, 1.0, 0.0); |
| 65 | /* 2 lines clipped along ymax */ |
| 66 | line(win_width / 2 - 20, win_height + 20, |
| 67 | win_width / 2 - 20, win_height - 20); |
| 68 | line(win_width / 2 + 20, win_height - 20, |
| 69 | win_width / 2 + 20, win_height + 20); |
| 70 | |
| 71 | glColor3f(0.0, 0.0, 1.0); |
| 72 | /* 2 lines clipped along xmax */ |
| 73 | line(win_width - 20, win_height / 2 - 20, |
| 74 | win_width + 20, win_height / 2 - 20); |
| 75 | line(win_width + 20, win_height / 2 + 20, |
| 76 | win_width - 20, win_height / 2 + 20); |
| 77 | |
| 78 | glColor3f(1.0, 1.0, 1.0); |
| 79 | /* 2 lines clipped along ymin */ |
| 80 | line(win_width / 2 - 20, 20, |
| 81 | win_width / 2 - 20, -20); |
| 82 | line(win_width / 2 + 20, -20, |
| 83 | win_width / 2 + 20, 20); |
| 84 | |
| 85 | /* 2 lines clipped along near */ |
| 86 | glColor3f(1.0, 0.0, 1.0); |
| 87 | line3(win_width / 2 - 20 - 20, win_height / 2, 0.5, |
| 88 | win_width / 2 - 20 + 20, win_height / 2, -0.5); |
| 89 | line3(win_width / 2 - 20, win_height / 2 - 20, -0.5, |
| 90 | win_width / 2 - 20, win_height / 2 + 20, 0.5); |
| 91 | |
| 92 | /* 2 lines clipped along far */ |
| 93 | glColor3f(0.0, 1.0, 1.0); |
| 94 | line3(win_width / 2 + 20 - 20, win_height / 2, 1.5, |
| 95 | win_width / 2 + 20 + 20, win_height / 2, 0.5); |
| 96 | line3(win_width / 2 + 20, win_height / 2 - 20, 0.5, |
| 97 | win_width / 2 + 20, win_height / 2 + 20, 1.5); |
| 98 | |
| 99 | /* entirely clipped along near/far */ |
| 100 | glColor3f(.5, .5, .5); |
| 101 | line3(win_width / 2, win_height / 2 - 20, -0.5, |
| 102 | win_width / 2, win_height / 2 + 20, -0.5); |
| 103 | glColor3f(.5, .5, .5); |
| 104 | line3(win_width / 2, win_height / 2 - 20, 1.5, |
| 105 | win_width / 2, win_height / 2 + 20, 1.5); |
| 106 | |
| 107 | glColor3f(1.0, 1.0, 0.0); |
| 108 | /* lines clipped along both x and y limits */ |
| 109 | line(-5, 20, |
| 110 | 20, -5); /* xmin, ymin */ |
| 111 | line(-5, win_height - 20, |
| 112 | 20, win_height + 5); /* xmin, ymax */ |
| 113 | line(win_width - 20, -5, |
| 114 | win_width + 5, 20); /* xmax, ymin */ |
| 115 | line(win_width - 20, win_height + 5, |
| 116 | win_width + 5, win_height - 20); /* xmax, ymax */ |
| 117 | |
| 118 | glutSwapBuffers(); |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | reshape(int width, int height) |
| 123 | { |
| 124 | win_width = width; |
| 125 | win_height = height; |
| 126 | glViewport(0, 0, width, height); |
| 127 | |
| 128 | glMatrixMode(GL_PROJECTION); |
| 129 | glLoadIdentity(); |
| 130 | glOrtho(0, win_width, 0, win_height, 0.0, -1.0); |
| 131 | |
| 132 | glMatrixMode(GL_MODELVIEW); |
| 133 | glLoadIdentity(); |
| 134 | glTranslatef(.25, .25, 0); |
| 135 | } |
| 136 | |
| 137 | static void key( unsigned char key, int x, int y ) |
| 138 | { |
| 139 | (void) x; |
| 140 | (void) y; |
| 141 | |
| 142 | switch (key) { |
| 143 | case 27: /* esc */ |
| 144 | exit(0); |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | glutPostRedisplay(); |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | init(void) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | int |
| 157 | main(int argc, char *argv[]) |
| 158 | { |
| 159 | win_width = 200; |
| 160 | win_height = 200; |
| 161 | |
| 162 | glutInit(&argc, argv); |
| 163 | glutInitWindowPosition(0, 0); |
| 164 | glutInitWindowSize(win_width, win_height); |
| 165 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 166 | glutCreateWindow(argv[0]); |
| 167 | glutReshapeFunc(reshape); |
| 168 | glutKeyboardFunc(key); |
| 169 | glutDisplayFunc(display); |
| 170 | |
| 171 | init(); |
| 172 | |
| 173 | glutMainLoop(); |
| 174 | return 0; |
| 175 | } |