# HG changeset patch # User Dennis C. M. # Date 1686597184 -3600 # Node ID e4003f606e078571eefcbfdb571006b0b87aca16 # Parent ea3c427d922dd92cf4f32b1091c8e24b892a95c7 draw multiple rectangles with loop diff -r ea3c427d922d -r e4003f606e07 34 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/34 Mon Jun 12 20:13:04 2023 +0100 @@ -0,0 +1,55 @@ +#include +#include + + +#define HEIGHT 1080 +#define WIDTH 1920 + + +void setup() { + + // Set background dark + glClearColor(0.0, 0.0, 0.0, 1.0); + + // Set point color and size to 1 pixel + glColor3f(0.0, 1.0, 0.0); + glPointSize(5.0); + + // Matrix projection + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + + // Set the coordinates to be used with the viewport + gluOrtho2D(0, WIDTH, HEIGHT, 0); + + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + + +void display() { + glClear(GL_COLOR_BUFFER_BIT); + glBegin(GL_QUADS); + glVertex2f(100.0f, 100.0f); // Bottom-left vertex of first rectangle + glVertex2f(200.0f, 100.0f); // Bottom-right vertex of first rectangle + glVertex2f(200.0f, 200.0f); // Top-right vertex of first rectangle + glVertex2f(100.0f, 200.0f); // Top-left vertex of first rectangle + glEnd(); + glFlush(); +} + + +int main(int argc, char** argv) { + + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); + glutInitWindowSize(WIDTH, HEIGHT); + glutCreateWindow("OpenGL Window"); + setup(); + glutDisplayFunc(display); + glutMainLoop(); + + return 0; +} diff -r ea3c427d922d -r e4003f606e07 main.c --- a/main.c Mon Jun 12 18:45:30 2023 +0100 +++ b/main.c Mon Jun 12 20:13:04 2023 +0100 @@ -20,18 +20,36 @@ glLoadIdentity(); // Set the coordinates to be used with the viewport - gluOrtho2D(-300, 300, 300, -300); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - + gluOrtho2D(0, WIDTH, HEIGHT, 0); } void display() { glClear(GL_COLOR_BUFFER_BIT); - glRectf(100.0f, 100.0f, 200.0f, 200.0f); - // glVertex2i(1920/2, 1080/2); + glBegin(GL_QUADS); + /* + glVertex2f(100.0, 100.0); // Top left + glVertex2f(150.0, 100.0); // Top right + glVertex2f(150.0, 300.0); // Bottom right + glVertex2f(100.0, 300.0); // Bottom left + + glVertex2f(160.0, 100.0); + glVertex2f(210.0, 100.0); + glVertex2f(210.0, 300.0); + glVertex2d(160.0, 300.0); + */ + + float rect_width = 5.0; + float space = 10.0; + + for (float pos_x = 100.0; pos_x < 500.0; pos_x += 10.0) { + glVertex2f(pos_x, 100.0); + glVertex2f(pos_x + rect_width, 100.0); + glVertex2f(pos_x + rect_width, 300.0); + glVertex2d(pos_x, 300.0); + } + + glEnd(); glFlush(); }