gl draw line rectangle
To draw a line rectangle using OpenGL in C++, you can follow these steps:
Include the necessary header files:
c++ #include <GL/glut.h> #include <GL/gl.h>
Initialize the OpenGL graphics system:
c++ void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 500, 0, 500); }
Define the display function to draw the rectangle:
c++ void display() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glLineWidth(2.0); glBegin(GL_LINE_LOOP); glVertex2i(100, 100); // Top-left corner glVertex2i(400, 100); // Top-right corner glVertex2i(400, 400); // Bottom-right corner glVertex2i(100, 400); // Bottom-left corner glEnd(); glFlush(); }
Define the main function and set the window properties:
c++ int main(int argc, char argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Line Rectangle"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }
Compile and run the program using an appropriate C++ compiler.
These steps will set up an OpenGL window and draw a rectangle with lines using the specified coordinates.