To draw A smiley in Graphics with openGL
#include <GL/glut.h> // Include the GLUT library void init() { glClearColor(1.0, 1.0, 1.0, 1.0); // Set the background color to white } void display() { glClear(GL_COLOR_BUFFER_BIT); // Clear the screen // Draw the face glColor3f(1.0, 1.0, 0.0); // Set the color to yellow glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); glVertex2f(0.0, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, 0.0); glEnd(); // Draw the left eye glColor3f(0.0, 0.0, 0.0); // Set the color to black glPointSize(10.0); // Set the point size to 10 pixels glBegin(GL_POINTS); glVertex2f(0.15, 0.25); glEnd(); // Draw the right eye glBegin(GL_POINTS); glVertex2f(0.35, 0.25); glEnd(); // Draw the mouth glColor3f(1.0, 0.0, 0.0); // Set the color to red glLineWidth(2.0); // Set the line width to 2 pixels glBegin(GL_LINES); glVertex2f(0.2, 0.1); glVertex2f(0.3, 0.1); glEnd(); glFlush(); // Flush the graphics pipeline } int main(int argc, char** argv) { glutInit(&argc, argv); // Initialize the GLUT library glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode glutInitWindowSize(500, 500); // Set the window size glutCreateWindow("Smiley Face"); // Create the window glutDisplayFunc(display); // Set the display callback function init(); // Initialize the program glutMainLoop(); // Enter the main loop return 0; }
Output:-
#include <GL/glut.h> // Include the GLUT library void init() { glClearColor(1.0, 1.0, 1.0, 1.0); // Set the background color to white } void display() { glClear(GL_COLOR_BUFFER_BIT); // Clear the screen // Draw the face glColor3f(1.0, 1.0, 0.0); // Set the color to yellow glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); glVertex2f(0.0, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, 0.0); glEnd(); // Draw the left eye glColor3f(0.0, 0.0, 0.0); // Set the color to black glPointSize(10.0); // Set the point size to 10 pixels glBegin(GL_POINTS); glVertex2f(0.15, 0.25); glEnd(); // Draw the right eye glBegin(GL_POINTS); glVertex2f(0.35, 0.25); glEnd(); // Draw the mouth glColor3f(1.0, 0.0, 0.0); // Set the color to red glLineWidth(2.0); // Set the line width to 2 pixels glBegin(GL_LINES); glVertex2f(0.2, 0.1); glVertex2f(0.3, 0.1); glEnd(); glFlush(); // Flush the graphics pipeline } int main(int argc, char** argv) { glutInit(&argc, argv); // Initialize the GLUT library glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode glutInitWindowSize(500, 500); // Set the window size glutCreateWindow("Smiley Face"); // Create the window glutDisplayFunc(display); // Set the display callback function init(); // Initialize the program glutMainLoop(); // Enter the main loop return 0; }
Output:-
This code creates a window using the GLUT library, sets the background color to white, and defines a display function that draws a yellow circle for the face, two black points for the eyes, and a red line for the mouth. The display function also sets the point size and line width to 10 pixels and 2 pixels, respectively.
The main function initializes GLUT, sets the display mode, window size, and window title, and registers the display callback function. The program then enters the main loop, which waits for events and calls the appropriate callback functions.
Overall, this code produces a simple but recognizable smiley face using OpenGL graphics in C.
Comments