Project 1
Project 1
Project 1
h>
#include<string.h>
#include<stdio.h>
#include<GL/glut.h>
///////////////////////////////declarations/////////////////////
int readv,cancelhl=0,okhl=0;
int winwd=1000,winht=700,mainw,subw,saveid;
int current=0;
int curobject;
GLUquadricObj
*point=gluNewQuadric(),*line=gluNewQuadric(),*wire=gluNewQuadric(),*fill=gluNewQuadric();
int cut=0,copied=0,px=0,py=0;
int saved=0;
int first=1;
char filename[20],fname[20],wfname[30];
void activeobjects(int);
void drawcube(int);
void mynew();
#define CUBE 1
#define SPHERE 2
#define CYLINDER 3
#define CONE 4
#define LINE 5
#define RECT 6
//structures
int type;
int state;
int col;//color
int hl;//highlighted
}BUT;
float orgx,orgy,orgz;
float size;
//float radius;
int color;
int type;
int used;//status
}OBJ;
OBJ obar[1000];
OBJ temp;
int OBJECT=1;
BUT col_menu={winwd-75,winht-165,0,70,50,0,0,0,"COLORS",0};
BUT ok_b={80,10,0,40,20,0,0,0,"",0};
BUT cancel_b={130,10,0,40,20,0,0,0,"",0};
ob->orgx=0;
ob->orgy=0;
ob->orgz=-2;
ob->size=0;
ob->color=2;
ob->tx=0;
ob->ty=0;
ob->tz=0;
ob->sx=10;
ob->sy=10;
ob->sz=10;
ob->type=0;
ob->style=0;
ob->used=0;
////////////functions///////////////
int save()
{int i;
FILE * fp;
glutSetWindow(mainw);
glutHideWindow();
if(!saved)
{
//clrscr();
printf("\t\t\tSAVE\n");
printf("\tFile Name:");
scanf("%s",fname);
fp=fopen(fname,"w");
if(fp==NULL)
return -1;
glutShowWindow();
fprintf(fp,"%d",objectcount);
for(i=0;i<objectcount;i++)
if(obar[i].used==1)
fprintf(fp,"%f",obar[i].orgx);
fprintf(fp,"%f",obar[i].orgy);
fprintf(fp,"%f",obar[i].orgz);
fprintf(fp,"%f",obar[i].size);
fprintf(fp,"%d",obar[i].color);
fprintf(fp,"%f",obar[i].tx);
fprintf(fp,"%f",obar[i].ty);
fprintf(fp,"%f",obar[i].tz);
fprintf(fp,"%f",obar[i].sx);
fprintf(fp,"%f",obar[i].sy);
fprintf(fp,"%f",obar[i].sz);
fprintf(fp,"%d",obar[i].type);
fprintf(fp,"%d",obar[i].style);
fprintf(fp,"%d",obar[i].used);
/*if(obar[i].used==1)
fprintf(fp,"%f%f%f%f%d%f%f%f%f%f%f%d%d
%d",obar[i].orgx,obar[i].orgy,obar[i].orgz,obar[i].size,obar[i].color,obar[i].tx,obar[i].ty,obar[i].tz,obar[i].s
x,obar[i].sy,obar[i].sz,obar[i].type,obar[i].style,obar[i].used);*/
saved=1;
fclose(fp);
strcpy(info,"saved");
return 1;
int open()
{int i;
FILE * fp;
glutHideWindow();
printf("\t\t\tOPEN\n");
printf("\tFile Name:");
gets(fname);
fp=fopen(fname,"r");
if(fp==NULL)
return -1;
glutShowWindow();
mynew();
//glutPostRedisplay();
fscanf(fp,"%d",&objectcount);
for(i=0;i<objectcount;i++)
fscanf(fp,"%f",&obar[i].orgx);
fscanf(fp,"%f",&obar[i].orgy);
fscanf(fp,"%f",&obar[i].orgz);
fscanf(fp,"%f",&obar[i].size);
fscanf(fp,"%d",&obar[i].color);
fscanf(fp,"%f",&obar[i].tx);
fscanf(fp,"%f",&obar[i].ty);
fscanf(fp,"%f",&obar[i].tz);
fscanf(fp,"%f",&obar[i].sx);
fscanf(fp,"%f",&obar[i].sy);
fscanf(fp,"%f",&obar[i].sz);
fscanf(fp,"%d",&obar[i].type);
fscanf(fp,"%d",&obar[i].style);
fscanf(fp,"%d",&obar[i].used);
fclose(fp);
strcpy(info,"opened");
return 1;
glutPostRedisplay();
}
void drawText(char *message, float i, float j,float k)
glRasterPos3f(i,j,k);
while (*message)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *message++);
glRasterPos3f(10,10,2);
while (*message)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *message++);
glColor3fv(col[curcol]);
}
////////////// menu functions ///////////////
switch(index)
case 1: mynew();
break;
case 2:if(open())
strcpy(wfname,"3D-GRAPHICS-");
strcat(wfname,fname);
glutSetWindowTitle(wfname);
break;
case 3:
if(save())
{ strcpy(wfname,"3D-GRAPHICS-");
strcat(wfname,fname);
glutSetWindowTitle(wfname);
break;
case 4:exit(0);
break;
switch(index)
case 1:drawing=0;
break;
case 2:transformation=1;
break;
case 3:transformation=2;
break;
case 4:transformation=3;
break;
}
int full_screen=0;
switch(index)
case 1:glutFullScreen();
break;
obar[active].style=index;
obar[current].size=index;
{ drawing=1;
switch(index)
case 1:OBJECT=11;
break;
case 2:OBJECT=12;
break;
case 3:OBJECT=13;
break;
case 4:OBJECT=14;
break;
switch(index)
case 1:cut=1;
temp=obar[active];
break;
case 2:copied=1;
temp=obar[active];
break;
case 3:if(copied)
objectcount+=1;
int i=objectcount-1;
obar[i]=temp;
obar[i].used=1;
obar[i].color=temp.color;
obar[i].orgx=px;
obar[i].orgy=py;
obar[i].orgz=-1000;
obar[i].type=temp.type;
active=i;
else if(cut)
copied=0;
cut=0;
obar[active].orgx=px;
obar[active].orgy=py;
break;
case 4:objectcount--;
active=objectcount-1;
break;
glutPostRedisplay();
}
////////// to draw background
{ glBegin(GL_POLYGON);
glColor3fv(col[c]);
if(c==2)
glColor3f(.9,.9,1);
glVertex3f(x,y,z);
glVertex3f(x,y+height,z);
glVertex3f(x+width,y+height,z);
glVertex3f(x+width,y,z);
glEnd();
void drawhlicon()
if(hlicon==1)
menubackg(0,winht-80,-.5,50,50,2);
else if(hlicon==2)
menubackg(50,winht-80,-.5,50,50,2);
else if(hlicon==3)
menubackg(100,winht-80,-.5,50,50,2);
else if(hlicon==4)
menubackg(150,winht-80,-.5,50,50,2);
else if(hlicon==5)
menubackg(200,winht-80,-.5,50,50,2);
else if(hlicon==6)
menubackg(250,winht-80,-.5,50,50,2);
{ glBegin(GL_POLYGON);
glColor3f(0.6,0.6,1);
glVertex3f(x,y,z);
glVertex3f(x,y+height,z);
glVertex3f(x+width,y+height,z);
glVertex3f(x+width,y,z);
glEnd();
glLineWidth(6.0);
glBegin(GL_LINE_LOOP);
glColor3f(.9,.9,1);
glVertex3f(x,y,z);
glVertex3f(x,y+height,z);
glVertex3f(x+width,y+height,z);
glVertex3f(x+width,y,z);
glEnd();
glLineWidth(1.0);
void chl()
if(chlhl=1)
//chlhl=0;
glColor3f(0,0,1);
glLineWidth(3.0);
glBegin(GL_LINE_LOOP);
glVertex3f(chlx,chly,1);
glVertex3f(chlx,chly+30,1);
glVertex3f(chlx+30,chly+30,1);
glVertex3f(chlx+30,chly,1);
glEnd();
}
/////////////// menu drawing
if(bt->type==0)
if(bt->hl)
glColor3f(0,0,1);
menubackg(bt->x+5,bt->y-5,-0.5,bt->w,bt->h,2);
glColor3fv(col[bt->col]);
drawText(bt->label,(bt->x)+3,(bt->y)+3,0);
if(bt->type==1)
if(bt->hl)
glColor3f(0,0,1);
menubackg(bt->x,bt->y-20,-0.5,bt->w,bt->h,2);
//draw_icon();
}
void colormenu()
{ float y=(winht-590);
for(int i=0;i<=26;i++)
glColor3fv(col[i++]);
glBegin(GL_POLYGON);
glVertex3f(winwd-80,y,-0.5);
glVertex3f(winwd-80,y+30,-0.5);
glVertex3f(winwd-50,y+30,-0.5);
glVertex3f(winwd-50,y,-0.5);
glEnd();
glColor3f(.9,.9,.9);
glBegin(GL_LINE_LOOP);
glVertex3f(winwd-80,y,0);
glVertex3f(winwd-80,y+30,0);
glVertex3f(winwd-50,y+30,0);
glVertex3f(winwd-50,y,0);
glEnd();
glColor3fv(col[i]);
glBegin(GL_POLYGON);
glVertex3f(winwd-50,y,-0.5);
glVertex3f(winwd-50,y+30,-0.5);
glVertex3f(winwd-20,y+30,-0.5);
glVertex3f(winwd-20,y,-0.5);
glEnd();
glColor3f(.9,.9,.9);
glBegin(GL_LINE_LOOP);
glVertex3f(winwd-50,y,0);
glVertex3f(winwd-50,y+30,0);
glVertex3f(winwd-20,y+30,0);
glVertex3f(winwd-20,y,0);
glEnd();
y=y+30;
void drawicons()
glColor3fv(col[0]);
glLineWidth(1.0);
//cube
glPushMatrix();
//glLoadIdentity();
glTranslatef(25,winht-55,30);
glRotatef(25,1,0,0);
glRotatef(15,0,1,0);
glScalef(6,6,6);
drawcube(3);
glPopMatrix();
glPointSize(1.0);
glLineWidth(1.0);
//sphere
glPushMatrix();
//glLoadIdentity();
glTranslatef(75,winht-55,0);
glRotatef(90,0,1,0);
gluSphere(wire,17,20,20);
glPopMatrix();
//cone
glPushMatrix();
//glLoadIdentity();
glTranslatef(125,winht-40,0);
glRotatef(90,1,0,0);
gluCylinder(wire,15,0,30,30,40);
glPopMatrix();
//cylinder
glPushMatrix();
//glLoadIdentity();
glTranslatef(175,winht-40,0);
glRotatef(90,1,0,0);
gluCylinder(wire,13,13,35,28,28);
//gluCylinder(wire,200,200,35,25,20);
//gluCylinder(wire,13,13,35,25,20);
glTranslatef(100,100,0);
//gluCylinder(wire,13,13,35,25,20);
glPopMatrix();
if(drawing==0)
if(transformation==1)
if(key==GLUT_KEY_UP)
obar[active].orgy+=10;
else if(key==GLUT_KEY_RIGHT)
obar[active].orgx+=10;
else if(key==GLUT_KEY_LEFT)
obar[active].orgx-=10;
else if(key==GLUT_KEY_DOWN)
obar[active].orgy-=10;
else if(transformation==2)
if(key==GLUT_KEY_UP)
obar[active].ty+=10;
else if(key==GLUT_KEY_RIGHT)
obar[active].tx+=10;
else if(key==GLUT_KEY_LEFT)
obar[active].tx-=10;
else if(key==GLUT_KEY_DOWN)
obar[active].ty-=10;
else if(transformation==3)
if(key==GLUT_KEY_UP)
obar[active].sy+=1;
else if(key==GLUT_KEY_RIGHT)
obar[active].sx+=1;
else if(key==GLUT_KEY_LEFT)
obar[active].sx-=1;
else if(key==GLUT_KEY_DOWN)
obar[active].sy-=1;
glutPostRedisplay();
if(drawing==0)
if(transformation==1)
printf("Z\n");
if(key=='Z')
obar[active].orgz+=1;
else if(key=='z')
obar[active].orgz-=1;
else if(transformation==2)
if(key=='Z')
obar[active].tz+=1;
else if(key=='z')
obar[active].tz-=1;
else if(transformation==3)
if(key=='Z')
obar[active].sz+=1;
else if(key=='z')
obar[active].sz-=1;
glutPostRedisplay();
int height=glutGet(GLUT_WINDOW_HEIGHT);
int width=glutGet(GLUT_WINDOW_WIDTH);
y=(height-y)*winht/height;
x=(x)*winwd/width;
px=x;
py=y;
file_menu.hl=0;
view_menu.hl=0;
edit_menu.hl=0;
draw_menu.hl=0;
hlicon=0;
chlhl=0;
glutDetachMenu(0);
hlicon=0;
hlicon=1;
hlicon=2;
hlicon=3;
}
else if(x>=150 && x<200)
hlicon=4;
else if(y>=winht-30)
glutAttachMenu(0);
file_menu.hl=1;
glutSetMenu(fileid);
edit_menu.hl=1;
glutSetMenu(editid);
view_menu.hl=1;
glutSetMenu(viewid);
}
draw_menu.hl=1;
glutSetMenu(drawid);
else
glutDetachMenu(0);
int z=y-(winht-590);
z=z/30;
chlhl=1;
chlx=(float)(winwd-80);
chly=((float)z*30+(winht-590));
else
chlx=(float)(winwd-50);
chly=((float)z*30+(winht-590));
}
else if(x<(winwd-100) && x>=0 && y<(winht-80) && y>=50)
if(drawing)
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutSetMenu(styleid);
else
glutAttachMenu(2);
glutSetMenu(rightid);
glutPostRedisplay();
int height=glutGet(GLUT_WINDOW_HEIGHT);
int width=glutGet(GLUT_WINDOW_WIDTH);
float x,y;
y=(height-yy)*winht/height;
x=(xx)*winwd/width;
drawing=1;
OBJECT=1;
strcpy(info,"Cube selected");
OBJECT=2;
strcpy(info,"sphere selected");
OBJECT=3;
strcpy(info,"cone selected");
}
else if(x>=150 && x<200)
OBJECT=4;
strcpy(info,"cylinder selected");
else if(y>=(winht-30))
//select menu
int z=y-(winht-590);
z=z/30;
curcol=2*z;
else
curcol=2*z+1;
}
if(!drawing)
obar[active].color=curcol;
if(drawing)
//draw object();
objectcount+=1;
int i=objectcount-1;;
obar[i].used=1;
obar[i].color=curcol;
obar[i].orgx=x;
obar[i].orgy=y;
obar[i].orgz=-1000;
obar[i].type=OBJECT;
printf("%d ",obar[i].type);
active=i;
current=i;
strcpy(info,"Object drawn");
drawing=0;
}
else
//select object();
// picks(btn,state,xx,height-yy);
if(st==1)
glScalef(1.5,1.5,1.5);
glDrawElements(GL_POINTS,24,GL_UNSIGNED_BYTE,cubeIndices);
else if(st==2)
glutWireCube(3);
else if(st==3)
//glDrawElements(GL_LINE_LOOP,24,GL_UNSIGNED_BYTE,cubeIndices);
glutWireCube(3);
else
glutSolidCube(3);
int m=0;
for(int i=0;i<objectcount;i++)
if(obar[m].used==1)
if(mode == GL_SELECT)
{ glLoadName((GLint)m);
printf("%d ",mode);
i++;
glColor3fv(col[obar[m].color]);
glPushMatrix();
//glLoadIdentity();
glTranslatef(obar[m].orgx,obar[m].orgy,obar[m].orgz);
glRotatef(obar[m].tx,1,0,0);
glRotatef(obar[m].ty,0,1,0);
glRotatef(obar[m].tz,0,0,1);
glScalef(obar[m].sx,obar[m].sy,obar[m].sz);
if(obar[m].type==1)
//glutSolidCube(10);
glScalef(2,2,2);
drawcube(obar[m].style);
else if(obar[m].type==2)
//drawsphere();
if(obar[m].style==1)
gluSphere(point,5,30,30);
else if(obar[m].style==2)
gluSphere(line,5,30,30);
else if(obar[m].style==3)
gluSphere(wire,5,30,30);
else
gluSphere(fill,5,30,30);
else if(obar[m].type==3)
{
glRotatef(90,1,0,0);
if(obar[m].style==1)
gluCylinder(point,5,0,10,50,50);
else if(obar[m].style==2)
gluCylinder(line,5,0,10,50,50);
else if(obar[m].style==3)
gluCylinder(wire,5,0,10,50,50);
else
gluCylinder(fill,5,0,10,50,50);
else if(obar[m].type==4)
glRotatef(90,1,0,0);
if(obar[m].style==1)
gluCylinder(point,3,3,10,50,50);
else if(obar[m].style==2)
gluCylinder(line,3,3,10,50,50);
else if(obar[m].style==3)
gluCylinder(wire,3,3,10,50,50);
else
gluCylinder(fill,3,3,10,50,50);
//else if(obar[m].type==4)
//{
//
//glPushMatrix();
////glLoadIdentity();
////glTranslatef(275,40,0);
////glRotatef(90,1,0,0);
//glScalef(0,0,50);
////gluCylinder(wire,13,13,35,25,20);
//glPopMatrix();
//
// printf("%d %d ",obar[m].type,m);
// if(obar[m].style==1)
// gluDisk(point,0,10,30,30);
// else if(obar[m].style==2)
// gluDisk(line,8,10,50,50);
// else if(obar[m].style==3)
// gluDisk(wire,0,10,50,50);
// else
// gluDisk(fill,0,10,50,50);
//}
else if(obar[m].type==5)
glPushMatrix();
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(5,5,0);
glEnd();
glPopMatrix();
else if(obar[m].type==11)
//drawsphere();
if(obar[m].style==1)
glutWireTeapot(10);
else if(obar[m].style==2)
glutWireTeapot(5);
else if(obar[m].style==3)
glutWireTeapot(10);
else
glutSolidTeapot(10);
else if(obar[m].type==12)
//drawsphere();
//glScalef(5,5,5);
if(obar[m].style==1)
glutWireTorus(1,2,30,30);
else if(obar[m].style==2)
glutWireTorus(1,2,30,30);
else if(obar[m].style==3)
glutWireTorus(1,2,30,30);
else
glutSolidTorus(1,2,30,30);
else if(obar[m].type==13)
//drawsphere();
glScalef(5,5,5);
if(obar[m].style==1)
glutWireIcosahedron();
else if(obar[m].style==2)
glutWireIcosahedron();
else if(obar[m].style==3)
glutWireIcosahedron();
else
glutSolidIcosahedron();
else if(obar[m].type==14)
{
//drawsphere();
glScalef(5,5,5);
if(obar[m].style==1)
glutWireTetrahedron();
else if(obar[m].style==2)
glutWireTetrahedron();
else if(obar[m].style==3)
glutWireTetrahedron();
else
glutSolidTetrahedron();
glPopMatrix();
m++;
i--;
curobject=m;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glutWireSphere(100.0,20,20);
menubackg(0,winht-31,-1,winwd,30-2);//menu bar
menubackg(0,winht-80-1,-1,winwd,50-2);//icon bar
menubackg(winwd-100,0,-1,100,winht-80+4);//color bar
menubackg(0,0,-1,winwd,50);//info bar
drawbutton(&file_menu);
drawbutton(&edit_menu);
drawbutton(&view_menu);
drawbutton(&col_menu);
drawbutton(&draw_menu);
drawinfo(info);
drawhlicon();
colormenu();
activeobjects(GL_RENDER);
chl();
drawicons();
glFlush();
glutSwapBuffers();
}
void OpenGLInit(void)
float i,j,k;
for(i=0;i<=1;i+=0.5)
for(j=0;j<=1;j+=0.5)
for(k=0;k<=1;k+=0.5)
col[m][0]=i;
col[m][1]=j;
col[m][2]=k;
m++;
for(int l=0;l<1000;l++)
objinit(& obar[l]);
glLineWidth(5.0);
glShadeModel( GL_FLAT );
glClearColor(1,1,15, 0.0 );
glClearDepth( 1.0 );
gluQuadricDrawStyle(fill,GLU_FILL);
gluQuadricDrawStyle(wire,GLU_SILHOUETTE);
gluQuadricDrawStyle(line,GLU_LINE);
gluQuadricDrawStyle(point,GLU_POINT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-0,winwd,-.0,winht,-10000,10000);
glMatrixMode( GL_MODELVIEW );
glEnable( GL_DEPTH_TEST );
//gluLookAt(0,0,-1000,0,0,0,0,1,0);
void mynew()
for(int l=0;l<100;l++)
objinit(& obar[l]);
objectcount=0;
active=0;
curcol=0;
drawing=1;
glutPostRedisplay();
}
void mydisp()
glClearColor(.8,.8,1,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(.8,.8,1,1);
glColor3f(1,0,0);
activeobjects(GL_RENDER);
drawText("File Name:",winwd-500,winht-500,-2);
glColor3f(1,1,1);
glBegin(GL_POLYGON);
drawinfo(info);
glVertex3f(winwd-450,winht-500,0);
glVertex3f(winwd-450+100,winht-500,0);
glVertex3f(winwd-450+100,winht-500+35,0);
glVertex3f(winwd-450,winht-500+35,0);
glEnd();
glFlush();
glutSwapBuffers();
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(winwd-400,winht-400,winwd-200,winht-200,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
float aspectRatio;
//h = (h == 0) ? 1 : h;
//w = (w == 0) ? 1 : w;
aspectRatio = (float)w/(float)h;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
if(w<=h)
glOrtho(-0.1,1024,-0.1/aspectRatio,1280/aspectRatio,-10000,10000);
else
glOrtho(-0.1*aspectRatio,1024*aspectRatio,-0.10,1280,-10000,10000);
glMatrixMode( GL_MODELVIEW );
//sglViewport(0,0,winwd,winht);
glutReshapeWindow(winwd,winht);
int main()
glutInitWindowPosition( 0, 0 );
OpenGLInit();
glutDisplayFunc( display );
//glutReshapeFunc(resize);
glutPassiveMotionFunc(pmotion);
glutMouseFunc(mouse);
glutKeyboardFunc(key);
glutSpecialFunc(specialkey);
fileid=glutCreateMenu(filem);
glutAddMenuEntry("NEW",1);
glutAddMenuEntry("OPEN",2);
glutAddMenuEntry("SAVE",3);
glutAddMenuEntry("EXIT",4);
editid=glutCreateMenu(editm);
glutAddMenuEntry("SELECT",1);
glutAddMenuEntry("MOVE",2);
glutAddMenuEntry("ROTATE",3);
glutAddMenuEntry("SCALE",4);
viewid=glutCreateMenu(viewm);
glutAddMenuEntry("FULLSCREEN",1);
styleid=glutCreateMenu(stylem);
glutAddMenuEntry("POINTS",1);
glutAddMenuEntry("LINES",2);
glutAddMenuEntry("WIRE",3);
glutAddMenuEntry("FILL",4);
rightid=glutCreateMenu(rightm);
glutAddMenuEntry("CUT",1);
glutAddMenuEntry("COPY",2);
glutAddMenuEntry("PASTE",3);
glutAddMenuEntry("DELETE",4);
glutAddSubMenu("STYLE",styleid);
drawid=glutCreateMenu(drawm);
glutAddMenuEntry("Teapot",1);
glutAddMenuEntry("Torus",2);
glutAddMenuEntry("IcosaHedron",3);
glutAddMenuEntry("TetraHedron",4);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);
glutMainLoop();