Description:
A polygon
mesh is a collection of vertices, edges and faces that defines the
shape of a polyhedral object
in 3D computer graphics
and solid modeling. The
faces usually consist of triangles (triangle mesh), quadrilaterals, or other simple convex polygons, since this simplifies rendering,
but may also be composed of more general concave polygons, or polygons with
holes. The study of polygon meshes
is a large sub-field of computer graphics and geometric modeling. Different
representations of polygon meshes are used for different applications and
goals. As polygonal meshes are extensively used in computer graphics,
algorithms also exist for collision detection, and rigid-body dynamics of
polygon meshes. The objective of this program is to develop a polygonal mesh
using a set of values.
Program:
#include<stdio.h>
#include<GL/glut.h>
#define dx 15
#define dy 10
GLfloat x[100],y[100];
GLfloat x0=50, y0=50; //initial values for x,y
GLint i,j;
int maxx,maxy;
void display(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for(i=0;i<=maxy;i++)
x[i]=x0+i*dx;
//Compute x[i]
for(j=0;j<=maxx;j++)
y[j]=y0+j*dy;
//compute y[i]
glColor3f(0.0,0.0,1.0);
for(i=0;i<maxy;i++)
for(j=0;j<maxx;j++)
{
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x[i],y[j]);
glVertex2f(x[i],y[j+1]);
glVertex2f(x[i+1],y[j+1]);
glVertex2f(x[i+1],y[j]);
glEnd();
}
glFlush();
}
void main(int argc, char **argv)
{
printf("Enter rows
& Columns\n");
scanf("%d%d",&maxx,&maxy);
glutInit(&argc,argv);
glutInitWindowPosition(10,10);
glutInitWindowSize(500,500);
glutCreateWindow("Rectangular
Mesh");
glutDisplayFunc(display);
glutMainLoop();
}
Output 1:
sahyadrids1@sahyadrids1-Veriton-Series:~/cg_16$ ./mesh
Enter rows & Columns
10 11
Output 2:
sahyadrids1@sahyadrids1-Veriton-Series:~/cg_16$ ./mesh
Enter rows & Columns
25 20