Mercurial > public > algo-animator
annotate main.c @ 3:e4003f606e07
draw multiple rectangles with loop
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Mon, 12 Jun 2023 20:13:04 +0100 |
parents | ea3c427d922d |
children | 035d3880da04 |
rev | line source |
---|---|
0 | 1 #include <stdio.h> |
2 #include <GL/glut.h> | |
3 | |
4 | |
5 #define HEIGHT 1080 | |
6 #define WIDTH 1920 | |
7 | |
8 | |
9 void setup() { | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
10 |
0 | 11 // Set background dark |
12 glClearColor(0.0, 0.0, 0.0, 1.0); | |
13 | |
14 // Set point color and size to 1 pixel | |
15 glColor3f(0.0, 1.0, 0.0); | |
16 glPointSize(5.0); | |
17 | |
18 // Matrix projection and reset with identity | |
19 glMatrixMode(GL_PROJECTION); | |
20 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
21 |
0 | 22 // Set the coordinates to be used with the viewport |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
23 gluOrtho2D(0, WIDTH, HEIGHT, 0); |
0 | 24 } |
25 | |
26 | |
27 void display() { | |
28 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
29 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
30 /* |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
31 glVertex2f(100.0, 100.0); // Top left |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
32 glVertex2f(150.0, 100.0); // Top right |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
33 glVertex2f(150.0, 300.0); // Bottom right |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
34 glVertex2f(100.0, 300.0); // Bottom left |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
35 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
36 glVertex2f(160.0, 100.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
37 glVertex2f(210.0, 100.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
38 glVertex2f(210.0, 300.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
39 glVertex2d(160.0, 300.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
40 */ |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
41 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
42 float rect_width = 5.0; |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
43 float space = 10.0; |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
44 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
45 for (float pos_x = 100.0; pos_x < 500.0; pos_x += 10.0) { |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
46 glVertex2f(pos_x, 100.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
47 glVertex2f(pos_x + rect_width, 100.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
48 glVertex2f(pos_x + rect_width, 300.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
49 glVertex2d(pos_x, 300.0); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
50 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
51 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
52 glEnd(); |
0 | 53 glFlush(); |
54 } | |
55 | |
56 | |
57 int main(int argc, char** argv) { | |
58 glutInit(&argc, argv); | |
59 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
60 glutInitWindowSize(WIDTH, HEIGHT); | |
61 glutCreateWindow("OpenGL Window"); | |
62 setup(); | |
63 glutDisplayFunc(display); | |
64 glutMainLoop(); | |
65 | |
66 return 0; | |
67 } |