## Monday, April 6, 2015

### OpenGL Geometric Primitives and Menus

In 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 points, lines, polygons, images, and bitmaps are considered to be primitives.

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 = {
{ 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; // 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);
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();
}

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();
}

Point p = { 320, 240 }; // draw_pixel
Point p1 = { 10, 100 }; // draw_line
Point p2 = { 200, 100 }; // --

Point pC = { 320, 240 }; // Circle center point

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

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

case 3: // Circle
break;

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

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

color = colors[choice];
}

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