1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include <GL/glut.h> #include <iostream>
#define INSIDE 0 #define LEFT 1 #define RIGHT 2 #define BOTTOM 4 #define TOP 8
struct ClipWindow { float xmin, xmax, ymin, ymax; };
ClipWindow rect; int x_0, y_0, x_1, y_1;
int computeCode(float x, float y, ClipWindow rect) { int code = INSIDE; if (x < rect.xmin) code |= LEFT; else if (x > rect.xmax) code |= RIGHT; if (y < rect.ymin) code |= BOTTOM; else if (y > rect.ymax) code |= TOP; return code; }
void cohenSutherlandClip(float& x_0, float& y_0, float& x_1, float& y_1, ClipWindow rect) { int code1 = computeCode(x_0, y_0, rect); int code2 = computeCode(x_1, y_1, rect); bool accept = false;
while (true) { if (!(code1 | code2)) { accept = true; break; } else if (code1 & code2) { break; } else { int code_out; float x, y;
if (code1 != 0) code_out = code1; else code_out = code2;
if (code_out & TOP) { x = x_0 + (x_1 - x_0) * (rect.ymax - y_0) / (y_1 - y_0); y = rect.ymax; } else if (code_out & BOTTOM) { x = x_0 + (x_1 - x_0) * (rect.ymin - y_0) / (y_1 - y_0); y = rect.ymin; } else if (code_out & RIGHT) { y = y_0 + (y_1 - y_0) * (rect.xmax - x_0) / (x_1 - x_0); x = rect.xmax; } else if (code_out & LEFT) { y = y_0 + (y_1 - y_0) * (rect.xmin - x_0) / (x_1 - x_0); x = rect.xmin; }
if (code_out == code1) { x_0 = x; y_0 = y; code1 = computeCode(x_0, y_0, rect); } else { x_1 = x; y_1 = y; code2 = computeCode(x_1, y_1, rect); } } }
if (accept) { glBegin(GL_LINES); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(x_0, y_0); glVertex2f(x_1, y_1); glEnd(); } }
void myDisplay() { glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_LINE_LOOP); glVertex2f(rect.xmin, rect.ymin); glVertex2f(rect.xmax, rect.ymin); glVertex2f(rect.xmax, rect.ymax); glVertex2f(rect.xmin, rect.ymax); glEnd();
int orig_x_0 = 50, orig_y_0 = 350; int orig_x_1 = 550, orig_y_1 = 50;
glBegin(GL_LINES); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(orig_x_0, orig_y_0); glVertex2f(orig_x_1, orig_y_1); glEnd();
float cx_0 = orig_x_0, cy_0 = orig_y_0; float cx_1 = orig_x_1, cy_1 = orig_y_1; glLineWidth(3.0); cohenSutherlandClip(cx_0, cy_0, cx_1, cy_1, rect); glLineWidth(1.0);
glFlush(); }
void Init() { glShadeModel(GL_FLAT);
rect.xmin = 100; rect.xmax = 400; rect.ymin = 100; rect.ymax = 300; }
void myReshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); glMatrixMode(GL_MODELVIEW); }
int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(600, 400); glutCreateWindow("实验2_03毕爽爽 Cohen-Sutherland 算法");
Init();
glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape);
glutMainLoop(); return 0; }
|