الأحد، 13 ديسمبر 2015

SubMenue - sphere .. cone and colour



#include <stdio.h>
#include <math.h>
#include <iostream>
#include <glut.h>
using namespace std;

GLfloat angle = 0.0; //angle for cube1
GLfloat tangle = 0.0; //angle for cube2
char c='s';
float r=1.0,g=0.0,b=0.0;
//-------------------------------------------------------------------
void cube (void) {
glPushMatrix(); //set where to start the current object transformations
glTranslatef(0.5, 1, 0); //move cube1 to the right
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube1 to red
//glutWireCube(0.5);
glutWireCone(0.5, 0.8, 16, 16);
glPopMatrix(); //end the current object transformations
}

void cube2 (void) {
glPushMatrix(); //set where to start the current object
glTranslatef(1.5, 1, 0); //move cube2 to the left
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube2 to green
//glutWireTeapot(0.5);
glutWireTeapot(0.5);
glPopMatrix(); //end the current object transformations
}
void cube3 (void) {
glPushMatrix(); //set where to start the current object
glTranslatef(2.5, 1, 0); //move cube2 to the left
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(r, g, b); //change cube2 to green
glutWireSphere(0.5,16,16);

glPopMatrix(); //end the current object transformations
}
//-------------------------------------------------------------------


//-------------------------------------------------------------------
void myDisplay(void)
{
 glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

if (c=='c')
cube();
if (c=='t')
cube2();
if(c=='s')
cube3();
glFlush();
glutSwapBuffers();
angle+= 0.5;
tangle+= 1.0;

}

void exitmenu(int m)
{
if (m == 1) exit(0);  // exit the program if exit is selected


}
void shapemenu(int s)
{

if (s==1)
c='c';
if(s==2)
c='t';
if(s==3)
c='s';
}
void colormenu(int c)
{

if (c==1)
{r=1.0;g=0.0; b=0.0;}
if(c==2)
{r=0.0;g=1.0; b=0.0;}
if(c==3)
{r=0.0;g=0.0; b=1.0;}
}
void myMouse(int button, int state, int x, int y)
{
  if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(myDisplay); // starts animation
  else if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    glutIdleFunc(NULL); // stops animation
}
void set_up_menu(void)
{
int coloridm, shapeidm;
coloridm=glutCreateMenu(colormenu);
glutAddMenuEntry("RED",1);
glutAddMenuEntry("GREEN",2);
glutAddMenuEntry("BLUE",3);
shapeidm=glutCreateMenu(shapemenu);
    glutAddMenuEntry("Cone",1);
glutAddMenuEntry("Teapot",2);
 glutAddMenuEntry("Sphere",3);

 glutCreateMenu(exitmenu);

 glutAddSubMenu("3D Shape", shapeidm);
 glutAddSubMenu("Color", coloridm);
glutAddMenuEntry("Exit",1);  //this is not a submenu -> calls callback


 glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void myInit(void)
{
  glClearColor(0.0, 0.0, 0.0, 0.0); // set the background to black
  glColor3f(1.0f, 1.0f, 1.0f);      // set the drawing color to white
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0, 3.0, 0.0, 3.0);
}

//-------------------------------------------------------------------
void main( int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
  glutInitWindowSize(550,450);
  glutInitWindowPosition(0,0);
  glutCreateWindow("One Triangle");
  glutMouseFunc(myMouse);
  glutDisplayFunc(myDisplay);
  //glutKeyboardFunc(myKeyboard);
  //glutIdleFunc(myDisplay);

  myInit();
  set_up_menu();
  glutMainLoop();
}

ليست هناك تعليقات:

إرسال تعليق