In this article, we will discuss how to create a front-facing view of a hut in OpenGL using two basic shapes i.e. triangle and rectangle.
Approach: Follow the steps below to solve the problem:
- Initialize the toolkit using the function glutInit (&argc, argv).
- Set the display mode and specify the color scheme using the function glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB).
- Specify the window size using the function glutInitWindowSize(1200, 740).
- Set the starting position for the window using the function glutInitWindowPosition (0, 0).
- Initialize the window and set the title using the function glutCreateWindow("Basic hut like structure").
- Initialize the myInit() function and perform the following steps:
- Set the background color to orange using the function glClearColor(1.0, 0.5, 0.0, 1.0).
- Specify the display area using the function gluOrtho2D(0.0, 400.0, 0.0, 400.0).
- Initialize the myDisplay() function and perform the following steps:
- Clear the screen using the function glClear(GL_COLOR_BUFFER_BIT).
- The rectangular part of the hut can be drawn using the function glPointSize(4.0).
- Set the drawing color to glColor3f(0.5f, 0.5f, 0.5f).
- Create the 2 windows, the door, the top triangle, and the main rectangle of the house using the polygon command and setting their vertices by using the functions:
- glBegin(GL_POLYGON);
- glVertex2i ( x, y );
- Color the polygons in the above steps using the function glColor3f(R, G, B).
Below is the implementation of the above approach:
Output:
👁 Image