Search Projects

Tuesday, August 1, 2017

3D Primtives in OpenGL

OpenGl Graphics library or OpenGl toolkit provides different API and methods to draw various 2d and 3d objects. In this post we will able show case you the basic 3d shapes. These 3d primitive can be drawn by calling the predefined methods or function in OpenGL.

What are 3D Primtives in OpenGL?

Like points,lines , traingle and polyogn in 2d we have primitives in 3d as well written in OpenGL itself. The library provide us easy access to draw the 2d Objects with one line of code. We can  use 2d primitives to draw the 3d objects but in OpenGL we have predefined code for 3d objects. We can overrides them as well. These code help quickly draw the 3d objects without writing alots of code for them.

There are many 3D objects defined but we mentioned you the 3d basics ones - Sphere, Cube (cuboid), Cone, Torus,  Icosahedron and Teapot.  These 3d objects can be drawn easily by calling the function from OPenGL graphics library, with some input likes radius, length, height and width etc.


Draw the 3D Primtives


Here we will se how to simply call the function for each 3D objects mentioned in the above para. We will also discuss about the parameter they need. In our case we are using wired 3d Objects and not solid ones.

1. Sphere

 void glutWireSphere(GLdouble radius,GLint slices, GLint stacks);

Radius, not need explanation, while slices are no of subdivisions around z-axis, stacks are no of subdivisions along the z-axis. You may consider the slices and stacks to be like of  longitude and latitudes.

2. Cube


void glutWireCube(GLdouble size);

Cube requied one parameter and that's its size.

3. Cone


void glutWireCone(GLdouble base, GLdouble height, GLint slices,
GLint stacks);

Cone is an objects with triangular face and circular bottom. The base here denotes for radius of the circular base or bottom, height is the height of cone. Similar to Sphere, slices are no of subdivisions around z-axis, stacks are no of subdivisions along the z-axis.

4. Torus


void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius,
GLint nsides, GLint rings);

A torus is an objects that is flat with hole in it's inner part and outer part to be circular. Tours examples are the tubes. The innerRadius and outerRadius represent the inner and outer radii. The nsides represent the no of sides that each radial section contains, while rings represent the no of radial divisions. These two can be understand as sections formed by cutting up to down and left to right respectively, along a fixed axis.

5. Icosahedron

void glutWireIcosahedron(void);

Icosahedron is a solid body with 20 sides. Other solid body are terahedron (4 sides), octahedron(8 sides), dodecahedron(12 sides) etc. These can be also drawn by just replacing name in above like - glutWireTetrahedron.

The GLUT provides functions for these objects with no parameters and with preassigned radii. The preassigned radii of Dodecahedron, Octahedron, Tetrahedron and Icosahedrons are, respectively, sqrt(3), 1, sqrt(3), 1.

One thing you need to do, while rendering set the raddi for them with scaling or translating.

6. Teapot


void glutWireTeapot(GLdouble size);

Here size is relative size of Teapot, which is generated with OpenGL evaluators. Both surface normals and texture coordinates for the teapot are generated.

Below is simple example of drawing these objects with a sample input data.

void drawSphere(void) {
 glutWireSphere(6.0,20,20);
}

void drawCube(void) {
 glutWireCube(6.0);
}

void drawCone(void) {
 glutWireCone(6.0, 8.0, 10, 20);
}

void drawTorus(void) {
 glutWireTorus(1.0, 6.0, 10, 20);
}

void drawIcos(void) {
 glPushMatrix();
 glScalef(6.0,6.0,6.0);
 glutWireIcosahedron();
 glPopMatrix();
}

Now, if you want to draw the solid objects insted of wired just replace wire word to solid like - glutWireCube to glutSolidCube.



You can download the source code for wired 3d primitives and solid 3d primitives.

No comments:

Post a Comment