In OpenGL programming we have so many function, methods included in the open source libraries. Most basic thing we need to learn in OpenGL...

In

Another major use in OpenGL are the colors, they are used in so many ways and are also very important in

Use of the Points in the OpenGL Porgram is very important and it's base for all heavy programming. After the points we have Lines, this OpenGL Geometric Primitive use to draw many 2d and 3d shapes like triangle, rectangles, squares and other polygons with the use of proper vertices coordinates. We have many Polygon functions also defined in OpenGL graphics API. Only the circle which we need to draw. In today's Program we talk about of these OpenGL Geometric Primitive.

Learn How to Draw a Circle in OpenGL Programming.

The following program will demonstrate the use of

**OpenGL programming**we have so many function, methods included in the open source libraries. Most basic thing we need to learn in OpenGL are the**Geometric Primitives**. In the OpenGL*are considered to be primitives.*__points, lines, polygons, images, and bitmaps__Another major use in OpenGL are the colors, they are used in so many ways and are also very important in

*OpenGL Programming*. We render the object with color using glcolor2f or glcolor3f etc as required in the program.Use of the Points in the OpenGL Porgram is very important and it's base for all heavy programming. After the points we have Lines, this OpenGL Geometric Primitive use to draw many 2d and 3d shapes like triangle, rectangles, squares and other polygons with the use of proper vertices coordinates. We have many Polygon functions also defined in OpenGL graphics API. Only the circle which we need to draw. In today's Program we talk about of these OpenGL Geometric Primitive.

Learn How to Draw a Circle in OpenGL Programming.

The following program will demonstrate the use of

**OpenGL Geometric Primitives and Menus**. This program is submitted by +venkatesh bandaru. It good to see he share his knowledge with us. I hope you like the program which will do the following -- Allow user to choose different sets of color
- Let Choose the Primitive like pixels, lines, circle and eclipse.
- With the combined selection of Color and Primitive objects, it get drawn on the screen.
- The program used the Concept of Menu and Sub menu for selection, which is done with right click of mouse.

Following the source code -

*#include <iostream>*

*#include <glut.h>**#include <math.h>*

*struct Point {**GLint x;**GLint y;**};*

*struct GLColor {**GLfloat red;**GLfloat green;**GLfloat blue;**};*

*GLColor colors[6] = {**{ 0.0f, 0.0f, 0.0f }, // Black**{ 1.0f, 0.0f, 0.0f }, // Red**{ 0.0f, 1.0f, 0.0f }, // Green**{ 0.0f, 0.0f, 1.0f }, // Blue**{ 1.0f, 1.0f, 0.0f }, // Yellow**{ 1.0f, 0.0f, 1.0f } // Purple**};*

*GLColor color = colors[0]; // Default: Black*

*void init() {**glClearColor(1.0f, 1.0f, 1.0f, 0);**glColor3f(color.red, color.green, color.blue);**glPointSize(1.0f);**glMatrixMode(GL_PROJECTION);**glLoadIdentity();**gluOrtho2D(0.0, 640.0, 0.0, 480.0);**}*

*void display(void) {*

*}*

*void draw_pixel(Point p) {**glClear(GL_COLOR_BUFFER_BIT);**glColor3f(color.red, color.green, color.blue);**glBegin(GL_POINTS);**glVertex2i(p.x, p.y);**glEnd();**glFlush();**}*

*void draw_dda(Point p1, Point p2) {**GLfloat dx = p2.x - p1.x;**GLfloat dy = p2.y - p1.y;*

*GLfloat x1 = p1.x;**GLfloat y1 = p1.y;*

*GLfloat step = 0;*

*if (abs(dx) > abs(dy)) {**step = abs(dx);**}**else {**step = abs(dy);**}*

*GLfloat xInc = dx / step;**GLfloat yInc = dy / step;*

*glClear(GL_COLOR_BUFFER_BIT);**glColor3f(color.red, color.green, color.blue);**glBegin(GL_POINTS);*

*for (float i = 1; i <= step; i++) {**glVertex2i(x1, y1);**x1 += xInc;**y1 += yInc;**}**glEnd();**glFlush();*

*}*

*void draw_circle(Point pC, GLfloat radius) {**GLfloat step = 1 / radius;**GLfloat x, y;*

*glClear(GL_COLOR_BUFFER_BIT);**glColor3f(color.red, color.green, color.blue);**glBegin(GL_POINTS);*

*for (GLfloat theta = 0; theta <= 360; theta += step) {**x = pC.x + (radius * cos(theta));**y = pC.y + (radius * sin(theta));**glVertex2i(x, y);**}**glEnd();**glFlush();**}*

*void draw_ellipse(Point pC, GLfloat radiusY, GLfloat radiusX) {**GLfloat step = 1 / radiusX;**GLfloat x, y;*

*glClear(GL_COLOR_BUFFER_BIT);**glColor3f(color.red, color.green, color.blue);**glBegin(GL_POINTS);**for (GLfloat theta = 0; theta <= 360; theta += step) {**x = pC.x + (radiusX * cos(theta));**y = pC.y + (radiusY * sin(theta));**glVertex2i(x, y);**}**glEnd();**glFlush();**}*

*void mainMenuHandler(int choice) {**Point p = { 320, 240 }; // draw_pixel**Point p1 = { 10, 100 }; // draw_line**Point p2 = { 200, 100 }; // --*

*Point pC = { 320, 240 }; // Circle center point**GLfloat radius = 200; // Circle radius*

*switch (choice) {**case 1: // Pixel**draw_pixel(p);**break;*

*case 2: // Line**draw_dda(p1, p2);**break;*

*case 3: // Circle**draw_circle(pC, radius);**break;*

*case 4: // Ellipse**draw_ellipse(pC, 60.0f, 200.0f);**break;*

*case 5: // Exit**exit(0);**break;**}**}*

*void subMenuHandler(int choice) {**color = colors[choice];**}*

*int main(int argc, char **argv) {**glutInit(&argc, argv);**glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);**glutInitWindowPosition(200, 200);**glutInitWindowSize(640, 480);**glutCreateWindow("OpenGL Circle and Menus");**glutDisplayFunc(display);**init();*

*int subMenu = glutCreateMenu(subMenuHandler);**glutAddMenuEntry("Default", 0);**glutAddMenuEntry("Red", 1);**glutAddMenuEntry("Green", 2);**glutAddMenuEntry("Blue", 3);**glutAddMenuEntry("Yellow", 4);**glutAddMenuEntry("Purple", 5);*

*glutCreateMenu(mainMenuHandler);**glutAddSubMenu("Change Color", subMenu);**glutAddMenuEntry("Pixel", 1);**glutAddMenuEntry("Line", 2);**glutAddMenuEntry("Circle", 3);**glutAddMenuEntry("Ellipse", 4);**glutAddMenuEntry("Exit", 5);*

*glutAttachMenu(GLUT_RIGHT_BUTTON);**glutMainLoop();*

*return 0;**}**I hope you liked this simple OpenGL Programming Concept. Do share your view with comments.*