#include <cstdlib>
#include <GL/glut.h>
char KEY = 'p';
float WIDTH = 5.0;
void drawPoints()
{
// clear the draw buffer .
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
glColor3ub(127, 127, 127);
glPointSize(WIDTH);
// create a polygon (define the vertices)
glBegin(GL_POINTS); {
glVertex2f(0.5, 0.5);
glVertex2f(-0.6, -0.6);
glVertex2f( 0.0, 0.8);
glVertex2f( 0.6, -0.6);
glVertex2f( -0.5, 0.5);
} glEnd();
// flush the drawing to screen .
glFlush();
}
void drawPolygon()
{
// clear the draw buffer .
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
glColor3ub(227, 227, 227);
glLineWidth(WIDTH);
// create a polygon (define the vertices)
glBegin(GL_POLYGON); {
glVertex2f(0.5, 0.5);
glVertex2f(-0.6, -0.6);
glVertex2f( 0.0, 0.8);
glVertex2f( 0.6, -0.6);
glVertex2f( -0.5, 0.5);
} glEnd();
// flush the drawing to screen .
glFlush();
}
void drawLines()
{
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
glColor3ub(227, 227, 227);
glLineWidth(WIDTH);
glBegin(GL_LINES); {
glVertex2f(0.5, 0.5);
glVertex2f(-0.6, -0.6);
glVertex2f( 0.0, 0.8);
glVertex2f( 0.6, -0.6);
glVertex2f( -0.5, 0.5);
} glEnd();
glFlush();
}
void drawLineStrip()
{
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
glColor3ub(227, 227, 227);
glLineWidth(WIDTH);
glBegin(GL_LINE_STRIP); {
glVertex2f(0.5, 0.5);
glVertex2f(-0.6, -0.6);
glVertex2f( 0.0, 0.8);
glVertex2f( 0.6, -0.6);
glVertex2f( -0.5, 0.5);
} glEnd();
glFlush();
}
void drawLineLoop()
{
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
glColor3ub(227, 227, 227);
glLineWidth(WIDTH);
glBegin(GL_LINE_LOOP); {
glVertex2f(0.5, 0.5);
glVertex2f(-0.6, -0.6);
glVertex2f( 0.0, 0.8);
glVertex2f( 0.6, -0.6);
glVertex2f( -0.5, 0.5);
} glEnd();
glFlush();
}
void draw_shape(){
if (KEY == 'p')
drawPoints();
else if (KEY == 'g')
drawPolygon();
else if (KEY == 'l')
drawLines();
else if (KEY == 's')
drawLineStrip();
else if (KEY == 'o')
drawLineLoop();
glutPostRedisplay();
}
// Keyboard callback function ( called on keyboard event handling )
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
if (key == '1')
WIDTH = 1.0;
else if (key == '5')
WIDTH = 5.0;
KEY = key;
}
// Main execution function
int main(int argc, char *argv[])
{
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(500, 500);
glutInitWindowPosition(200, 0);
glutCreateWindow("OpenGL example"); // Create a window
glutDisplayFunc(draw_shape); // Register display callback
glutKeyboardFunc(keyboard); // Register keyboard callback
// gluOrtho2D(-10, 10, -10, 10);
glutMainLoop(); // Enter main event loop
return (EXIT_SUCCESS);
}
maybe this code will help you with the solution because it is included with it as a help
Just follow the step-by-step instructions
Other files for more help


0 comments