1: #include <GL/glut.h>
2: #include <math.h>
3: int ww = 600, wh = 400;
4: int first = 0;
5: int xi, yi, xf, yf;
6: void putPixel (int x, int y)
7: {
8: glColor3f (0.3, 0.2, 0.0); // activate the pixel by setting the point color to white
9: glBegin (GL_POINTS);
10: glVertex2i (x, y); // set the point
11: glEnd ();
12: glFlush (); // process all openGL routines as quickly as possible
13: }
14: void display()
15: {
16: glClearColor(0.4, 0.7, 0.5, 1.0);
17: glColor3f(0.2, 0.3, 0.3);
18: glClear(GL_COLOR_BUFFER_BIT);
19: glFlush();
20: }
21: void bresenhamAlg (int x0, int y0, int x1, int y1)
22: {
23: int dx = abs (x1 - x0);
24: int dy = abs (y1 - y0);
25: int x, y;
26: if (dx >= dy)
27: {
28: int d = 2*dy-dx;
29: int ds = 2*dy;
30: int dt = 2*(dy-dx);
31: if (x0 < x1)
32: {
33: x = x0;
34: y = y0;
35: }
36: else
37: {
38: x = x1;
39: y = y1;
40: x1 = x0;
41: y1 = y0;
42: }
43: putPixel (x, y);
44: while (x < x1)
45: {
46: if (d < 0)
47: d += ds;
48: else {
49: if (y < y1) {
50: y++;
51: d += dt;
52: }
53: else {
54: y--;
55: d += dt;
56: }
57: }
58: x++;
59: putPixel (x, y);
60: }
61: }
62: else {
63: int d = 2*dx-dy;
64: int ds = 2*dx;
65: int dt = 2*(dx-dy);
66: if (y0 < y1) {
67: x = x0;
68: y = y0;
69: }
70: else {
71: x = x1;
72: y = y1;
73: y1 = y0;
74: x1 = x0;
75: }
76: putPixel (x, y);
77: while (y < y1)
78: {
79: if (d < 0)
80: d += ds;
81: else {
82: if (x > x1){
83: x--;
84: d += dt;
85: } else {
86: x++;
87: d += dt;
88: }
89: }
90: y++;
91: putPixel (x, y);
92: }
93: }
94: }
95: void mouse(int btn, int state, int x, int y)
96: {
97: if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
98: {
99: switch(first)
100: {
101: case 0:
102: xi = x;
103: yi = (wh-y);
104: first = 1;
105: break;
106: case 1:
107: xf = x;
108: yf = (wh-y);
109: bresenhamAlg ( xi, yi, xf, yf);
110: first = 0;
111: break;
112: }
113: }
114: }
115: void myinit()
116: {
117: glViewport(0,0,ww,wh);
118: glMatrixMode(GL_PROJECTION);
119: glLoadIdentity();
120: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
121: glMatrixMode(GL_MODELVIEW);
122: }
123: int main(int argc, char** argv)
124: {
125: glutInit(&argc,argv);
126: glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
127: glutInitWindowSize(ww,wh);
128: glutCreateWindow("Bresenham Line Algorithm");
129: glutDisplayFunc(display);
130: myinit();
131: glutMouseFunc(mouse);
132: glutMainLoop();
133: return 0;
134: }
Thursday, May 15, 2014
Opengl,C++ : Draw Line With Bresenham Line Algorithm
Algorithm to rasterize lines that go from left to right with slope between 0 and 90 degree ,right to left with slope between 0 and 90 degree.
Subscribe to:
Post Comments (Atom)
didnt undrstood the output
ReplyDeletea new window with green screen appears
First you make left mouse click and then make right mouse click, the line will be drawn
Deletethe line is displaying temporarily how to display it permanantly
DeleteThanks @Biruntha
ReplyDelete