Wednesday, December 18, 2013

To draw a 2D Gasket using subdivision of line in a triangle

/*
 * gasket.c
 *
 *  Created on: Dec 18, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,50,0.0,50);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
}
void display()
{
    GLfloat vertices[3][2]={{0,0},{25,50},{50,0}};
    int i,j,k;
    int rand();
    GLfloat p[2]={7.5,5.0};
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    for(k=0;k<5000;k++)
    {
        j=rand()%3;
        p[0]=(p[0]+vertices[j][0])/2;
        p[1]=(p[1]+vertices[j][1])/2;
        glVertex2fv(p);
    }
    glEnd();
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("2D Gasket");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
Snapshot:

To draw a red triangle using a display list

/*
 * 4. Give the opengl code segment that generates a display list defining
 *  a red triangle with vertices at (50,50),(150,50) and (100,0)?
 * disp.c
 *
 *  Created on: Dec 18, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
GLuint triangle; //The OpenGL id of the display list
void draw_triangle()
{
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_TRIANGLES);
    glVertex2f(50,50);
    glVertex2f(150,50);
    glVertex2f(100,0);
    glEnd();
}
void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,500,0.0,500);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0,1.0,1.0,1.0);
    triangle = glGenLists(1);
    glNewList(triangle, GL_COMPILE); //Begin the display list
    draw_triangle(); //Add commands for drawing a triangle
                        //to the display list
    glEndList(); //End the display list
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glCallList(triangle);
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("Triangle Display List");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
Snapshot:


Tuesday, December 17, 2013

Opengl code segment to approximate a sphere using subdivision of tetra

/*
write the open gl code segment to approximate a sphere using subdivision?
*/
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<GL/glut.h>

GLfloat v[4][3]={{0.0,0.0,1.0},{0.0,0.94,-0.33},
                     {-0.81,-0.47,-0.33}, {0.81,-0.47,-0.33}};
GLfloat colors[4][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{0.0,0.0,0.0}};
int n;
void triangle(GLfloat *a,GLfloat *b,GLfloat *c)
{
    glColor3fv(colors[0]);
    glVertex3fv(a);
    glColor3fv(colors[1]);
    glVertex3fv(b);
    glColor3fv(colors[2]);
    glVertex3fv(c);
}

void normalize(GLfloat *p)
{
    double d=0.0;
    int i;
    for(i=0;i<3;i++) d+=p[i]*p[i];
    d=sqrt(d);
    if(d>0.0) for(i=0;i<3;i++) p[i]/=d;
}

void divide_tetra(GLfloat *a,GLfloat *b,GLfloat *c,int m)
{
    GLfloat v1[3],v2[3],v3[3];
    int j;
    if(m>0)
    {    /*compute six midpoints*/
        for(j=0;j<3;j++)
            v1[j]=(a[j]+b[j])/2;
        normalize(v1);
        for(j=0;j<3;j++)
            v2[j]=(a[j]+c[j])/2;
        normalize(v2);
        for(j=0;j<3;j++)
            v3[j]=(c[j]+b[j])/2;
        normalize(v3);
        divide_tetra(a,v2,v1,m-1);
        divide_tetra(c,v3,v2,m-1);
        divide_tetra(b,v1,v3,m-1);
        divide_tetra(v1,v2,v3,m-1);
    }
    else
        triangle( a, b, c);      //draw triangle at end of recursion//
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    divide_tetra(v[0],v[1],v[2],n);
    divide_tetra(v[3],v[2],v[1],n);
    divide_tetra(v[0],v[3],v[1],n);
    divide_tetra(v[0],v[2],v[3],n);
    glEnd();
    glFlush();
}

void myReshape(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-2.0,2.0,-2.0,2.0 ,-10.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}

int main(int argc,char **argv)
{
printf("enter the no of division ");
scanf("%d",&n);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("3d gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
return 0;
}

Output:

enter the no of division: 5

Snapshot:

Opengl program to demonstrate hierarchical menus

2. Write an opengl program to demonstrate the hierarchical menus, to draw a rectangle and to increase or decrease the size of the rectangle?
/*
 * menu.c
 *
 *  Created on: Dec 16, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
GLsizei wh=500,ww=500;
GLfloat size=3.0;

void display()
{
glFlush();
}

void myInit()
{
    glViewport(0,0,ww,wh);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(0.0,0.0,0.0,1.0);
    glColor3f(1.0,0.0,0.0);
}

void myReshape(GLsizei w,GLsizei h)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0,0,w,h);
    ww=w;
    wh=h;
}

void drawSquare(int x,int y)
{
    y=wh-y;
    glBegin(GL_POLYGON);
    glVertex2f(x+size,y+size);
    glVertex2f(x-size,y+size);
    glVertex2f(x-size,y-size);
    glVertex2f(x+size,y-size);
    glEnd();
    glFlush();
}

void size_menu(int id)
{
    switch(id)
    {
    case 2: size=size*2;
            break;
    case 3:if(size>1) size=size/2;
    break;
    }
    glutPostRedisplay();
}

void top_menu(int id)
{
    switch(id)
    {
    case 1:exit(0);break;
    }
    glutPostRedisplay();
}

void myMouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    drawSquare(x,y);
    if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
        exit(0);
}

int main(int argc,char **argv)
{int sub_menu;
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(500,500);
    glutCreateWindow("Hierarchical Menus to Draw Squares");
    glutDisplayFunc(display);
    myInit();
    glutMouseFunc(myMouse);
    sub_menu=glutCreateMenu(size_menu);
    glutAddMenuEntry("Increase Square Size",2);
    glutAddMenuEntry("Decrease Square Size",3);
    glutCreateMenu(top_menu);
    glutAddMenuEntry("Quit",1);
    glutAddSubMenu("Resize",sub_menu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutReshapeFunc(myReshape);
    glClear(GL_COLOR_BUFFER_BIT);
    glutMainLoop();
    return 0;
}

Snapshot:

To draw a rotating square

/*
 * square.c
 * The square should stop rotating when middle button is pressed and

start
 * rotating again
 *  when left button is pressed
 *  Created on: Dec 16, 2013
 *      Author: kamath
 */
#include<GL/glut.h>
#include<math.h>
GLfloat theta,thetar;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    thetar=theta/(3.14159/180.0);         //convert theta in degrees to

radians
    glVertex2f(cos(thetar),sin(thetar));
    glVertex2f(-sin(thetar),cos(thetar));
    glVertex2f(-cos(thetar),-sin(thetar));
    glVertex2f(sin(thetar),-cos(thetar));
    glEnd();
    glFlush();
    glutSwapBuffers();
}
void idle()
{
    theta+=2;
    if(theta>=360.0) theta-=360.0;
    glutPostRedisplay();
}

void mouse(int button,int state,int x,int y)      //  change idle

function based on
         //  mouse button pressed
{
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    glutIdleFunc(idle);
    if(button==GLUT_MIDDLE_BUTTON&&state==GLUT_DOWN)
        glutIdleFunc(NULL);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutCreateWindow("Rotating Square");
    glutIdleFunc(idle);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
}

Snapshot: