forked from sprintr/opengl-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenGL-Square.cpp
77 lines (63 loc) · 1.25 KB
/
OpenGL-Square.cpp
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
#include <gl/glut.h>
#include <math.h>
struct Point {
GLint x;
GLint y;
};
void draw_dda(Point p1, Point p2) {
GLfloat dx = p2.x - p1.x;
GLfloat dy = p2.y - p1.y;
GLfloat x1 = p1.x;
GLfloat y1 = p1.y;
GLfloat step = 0;
if(abs(dx) > abs(dy)) {
step = abs(dx);
} else {
step = abs(dy);
}
GLfloat xInc = dx/step;
GLfloat yInc = dy/step;
for(float i = 1; i <= step; i++) {
glVertex2i(x1, y1);
x1 += xInc;
y1 += yInc;
}
}
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
void draw_square(Point a, GLint length) {
Point b = {a.x + length, a.y},
c = {b.x, b.y+length},
d = {c.x-length, c.y};
draw_dda(a, b);
draw_dda(b, c);
draw_dda(c, d);
draw_dda(d, a);
}
void display(void) {
Point p1 = {100, 100};
GLint length = 100; // size of square
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
draw_square(p1, length);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(200, 200);
glutCreateWindow("Open GL");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}