Last active
January 12, 2024 00:53
-
-
Save souravrane/9bac9ad9165ddc3fd463f890953b6ef1 to your computer and use it in GitHub Desktop.
OpenGL - Scanline filling algorithm
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<stdio.h> | |
#include<GL/glut.h> | |
#include<math.h> | |
int le[500], re[500], flag=0 ,m; | |
void init() | |
{ | |
gluOrtho2D(0, 500, 0, 500); | |
} | |
void edge(int x0, int y0, int x1, int y1) | |
{ | |
if (y1<y0) | |
{ | |
int tmp; | |
tmp = y1; | |
y1 = y0; | |
y0 = tmp; | |
tmp = x1; | |
x1 = x0; | |
x0 = tmp; | |
} | |
int x = x0; | |
m = (y1 - y0) / (x1 - x0); | |
for (int i = y0; i<y1; i++) | |
{ | |
if (x<le[i]) | |
le[i] = x; | |
if (x>re[i]) | |
re[i] = x; | |
x += (1 / m); | |
} | |
} | |
void display() | |
{ | |
glClearColor(1, 1, 1, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glColor3f(0, 0, 1); | |
glBegin(GL_LINE_LOOP); | |
glVertex2f(200, 100); | |
glVertex2f(100, 200); | |
glVertex2f(200, 300); | |
glVertex2f(300, 200); | |
glEnd(); | |
for (int i = 0; i<500; i++) | |
{ | |
le[i] = 500; | |
re[i] = 0; | |
} | |
edge(200, 100, 100, 200); | |
edge(100, 200, 200, 300); | |
edge(200, 300, 300, 200); | |
edge(300, 200, 200, 100); | |
if (flag == 1) | |
{ | |
for (int i = 0; i < 500; i++) | |
{ | |
if (le[i] < re[i]) | |
{ | |
for (int j = le[i]; j < re[i]; j++) | |
{ | |
glColor3f(1, 0, 0); | |
glBegin(GL_POINTS); | |
glVertex2f(j, i); | |
glEnd(); | |
} | |
} | |
} | |
} | |
glFlush(); | |
} | |
void ScanMenu(int id) { | |
if (id == 1) | |
flag = 1; | |
else if (id == 2) | |
flag = 0; | |
else | |
exit(0); | |
glutPostRedisplay(); | |
} | |
void main(int argc, char **argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitWindowPosition(100, 100); | |
glutInitWindowSize(500, 500); | |
glutCreateWindow("scan line"); | |
init(); | |
glutDisplayFunc(display); | |
glutCreateMenu(ScanMenu); | |
glutAddMenuEntry("scanfill", 1); | |
glutAddMenuEntry("clear", 2); | |
glutAddMenuEntry("exit", 3); | |
glutAttachMenu(GLUT_RIGHT_BUTTON); | |
glutMainLoop(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment