Last active
April 3, 2023 00:49
-
-
Save nowke/965fed0d5191bf373f1262be584207bb to your computer and use it in GitHub Desktop.
Bresenham Line Drawing - OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gl/glut.h> | |
#include <stdio.h> | |
int x1, y1, x2, y2; | |
void myInit() { | |
glClear(GL_COLOR_BUFFER_BIT); | |
glClearColor(0.0, 0.0, 0.0, 1.0); | |
glMatrixMode(GL_PROJECTION); | |
gluOrtho2D(0, 500, 0, 500); | |
} | |
void draw_pixel(int x, int y) { | |
glBegin(GL_POINTS); | |
glVertex2i(x, y); | |
glEnd(); | |
} | |
void draw_line(int x1, int x2, int y1, int y2) { | |
int dx, dy, i, e; | |
int incx, incy, inc1, inc2; | |
int x,y; | |
dx = x2-x1; | |
dy = y2-y1; | |
if (dx < 0) dx = -dx; | |
if (dy < 0) dy = -dy; | |
incx = 1; | |
if (x2 < x1) incx = -1; | |
incy = 1; | |
if (y2 < y1) incy = -1; | |
x = x1; y = y1; | |
if (dx > dy) { | |
draw_pixel(x, y); | |
e = 2 * dy-dx; | |
inc1 = 2*(dy-dx); | |
inc2 = 2*dy; | |
for (i=0; i<dx; i++) { | |
if (e >= 0) { | |
y += incy; | |
e += inc1; | |
} | |
else | |
e += inc2; | |
x += incx; | |
draw_pixel(x, y); | |
} | |
} else { | |
draw_pixel(x, y); | |
e = 2*dx-dy; | |
inc1 = 2*(dx-dy); | |
inc2 = 2*dx; | |
for (i=0; i<dy; i++) { | |
if (e >= 0) { | |
x += incx; | |
e += inc1; | |
} | |
else | |
e += inc2; | |
y += incy; | |
draw_pixel(x, y); | |
} | |
} | |
} | |
void myDisplay() { | |
draw_line(x1, x2, y1, y2); | |
glFlush(); | |
} | |
void main(int argc, char **argv) { | |
printf( "Enter (x1, y1, x2, y2)\n"); | |
scanf("%d %d %d %d", &x1, &y1, &x2, &y2); | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(0, 0); | |
glutCreateWindow("Bresenham's Line Drawing"); | |
myInit(); | |
glutDisplayFunc(myDisplay); | |
glutMainLoop(); | |
} |
thanks for that
its not working in codeblocks. its says stopped working
you code doesn't return anything...it will work just by either altering to this..
main(int argc, char **argv) {/
Can you try this - https://github.com/nowke/cg_lab/tree/master/1_bresenham_line ?
how to use glfw+glad to do it??
I want to plot a pixel after 4 sec. How can I do this ?
Using a sleep command
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it is not working in codeblocks what to do