2019年5月22日 星期三

瞎78打-機器人

                                       機器人的程式碼

#include <GL/glut.h>
#include "glm.h"
float angle[20];//滑鼠變數用陣列命名
int jointID=1;
GLMmodel* pmodel = NULL;
GLMmodel* rup = NULL;
GLMmodel* rup2 = NULL;
GLMmodel* lup = NULL;
GLMmodel* lup2 = NULL;
GLMmodel* lfoot = NULL;
GLMmodel* lfoot2 = NULL;
GLMmodel* rfoot = NULL;
GLMmodel* rfoot2 = NULL;
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       ///body
       if (!pmodel) {
       pmodel = glmReadOBJ("data/body.obj");
       if (!pmodel) exit(0);
       glmUnitize(pmodel);
       glmFacetNormals(pmodel);
       glmVertexNormals(pmodel, 90.0);
       }

      glPushMatrix();
         glScaled(0.6,0.6,0.6);
         glTranslated(0,0.68,0);
         glRotatef(90,0,1,0);
         glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
      glPopMatrix();
      ///body
       if (!rup) {///read ruphand
       rup = glmReadOBJ("data/ruphand.obj");
       if (!rup) exit(0);
       glmUnitize(rup);
       glmFacetNormals(rup);
       glmVertexNormals(rup, 90.0);
       }
       if (!rup2) { ///read rdownhand
       rup2 = glmReadOBJ("data/test2.obj");
       if (!rup2) exit(0);
       glmUnitize(rup2);
       glmFacetNormals(rup2);
       glmVertexNormals(rup2, 90.0);
       }
       if (!lup) {///read luphand
       lup = glmReadOBJ("data/ruphand.obj");
       if (!lup) exit(0);
       glmUnitize(lup);
       glmFacetNormals(lup);
       glmVertexNormals(lup, 90.0);
       }
       if (!lup2) { ///read rdownhand
       lup2 = glmReadOBJ("data/test1.obj");
       if (!lup2) exit(0);
       glmUnitize(lup2);
       glmFacetNormals(lup2);
       glmVertexNormals(lup2, 90.0);
       }
       if (!rfoot) {///read luphand
       rfoot = glmReadOBJ("data/upfoot.obj");
       if (!rfoot) exit(0);
       glmUnitize(rfoot);
       glmFacetNormals(rfoot);
       glmVertexNormals(rfoot, 90.0);
       }
       if (!lfoot) {///read luphand
       lfoot = glmReadOBJ("data/upfoot.obj");
       if (!lfoot) exit(0);
       glmUnitize(lfoot);
       glmFacetNormals(lfoot);
       glmVertexNormals(lfoot, 90.0);
       }
       if (!rfoot2) {///read luphand
       rfoot2 = glmReadOBJ("data/downfoot.obj");
       if (!rfoot2) exit(0);
       glmUnitize(rfoot2);
       glmFacetNormals(rfoot2);
       glmVertexNormals(rfoot2, 90.0);
       }
       if (!lfoot2) {///read luphand
       lfoot2 = glmReadOBJ("data/downfoot.obj");
       if (!lfoot2) exit(0);
       glmUnitize(lfoot2);
       glmFacetNormals(lfoot2);
       glmVertexNormals(lfoot2, 90.0);
       }

      glPushMatrix();///ruphand
         glScaled(0.7,0.7,0.7);
         glTranslated(0.46,0.42,0);
         glRotatef(-angle[3], 0,0,1);
         glTranslated(0.029,-0.22,0);
         glmDraw(rup, GLM_SMOOTH | GLM_MATERIAL);
           glPushMatrix();///rdownhand
            glScaled(1.6,1.6,1.6);
            glTranslated(0,-0.18,-0.01);
            glRotatef(-angle[4], 0,0,1);
            glTranslated(0.01,-0.14,0);
            glmDraw(rup2, GLM_SMOOTH | GLM_MATERIAL);
           glPopMatrix();
      glPopMatrix();

      glPushMatrix();///luphand
         glScaled(0.7,0.7,0.7);
         glTranslated(-0.5,0.42,0);
         glRotatef(-angle[1], 0,0,1);
         glTranslated(0.029,-0.22,0);
         glmDraw(lup, GLM_SMOOTH | GLM_MATERIAL);
          glPushMatrix();///rdownhand
            glScaled(1.6,1.6,1.6);
            glTranslated(-0.02,-0.18,-0.01);
            glRotatef(-angle[2], 0,0,1);
            glTranslated(0.01,-0.14,0);
            glmDraw(lup2, GLM_SMOOTH | GLM_MATERIAL);
           glPopMatrix();
     glPopMatrix();

     glPushMatrix();///lfoot
         glScaled(1,1,1);
         glTranslated(-0.2,-0.26,-0.2);
         glRotatef(-angle[5], 0,0,1);
         glTranslated(0.006,-0.11,0);
         glmDraw(rfoot, GLM_SMOOTH | GLM_MATERIAL);
            glPushMatrix();///rdownhand
            glScaled(1.6,1.6,1.6);
            glTranslated(-0.00,-0.12,-0.03);
            glRotatef(-angle[7], 0,0,1);
            glTranslated(-0.05,-0.11,0);
            glRotatef(180, 0,1,0);
            glmDraw(rfoot2, GLM_SMOOTH | GLM_MATERIAL);
           glPopMatrix();
     glPopMatrix();

     glPushMatrix();///rfoot
         glScaled(1,1,1);
         glTranslated(0.18,-0.26,-0.1);
         glRotatef(-angle[6], 0,0,1);
         glTranslated(0.006,-0.11,0);
         glmDraw(lfoot, GLM_SMOOTH | GLM_MATERIAL);
            glPushMatrix();///rdownhand
            glScaled(1.6,1.6,1.6);
            glTranslated(0.01,-0.12,-0.03);
            glRotatef(-angle[8], 0,0,1);
            glTranslated(0.039,-0.11,0);
            glmDraw(rfoot2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
     glPopMatrix();


   glutSwapBuffers();
}




    ///glutSwapBuffers();

int oldX,oldY; //滑鼠按下
void mouse(int button,int state,int x,int y)
{
    oldX=x;
    oldY=y;
}
void motion(int x,int y)
{
    angle[jointID]+=x-oldX;
    oldX=x;
    display();
}
void keyboard(unsigned char key,int x,int y) //設定鍵盤按下的變數名稱
{
    if(key=='1') jointID = 1;
    if(key=='2') jointID = 2;
    if(key=='3') jointID = 3;
    if(key=='4') jointID = 4;
    if(key=='5') jointID = 5;
    if(key=='6') jointID = 6;
    if(key=='7') jointID = 7;
    if(key=='8') jointID = 8;


}

int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("A_mon!!");
    glutDisplayFunc(display);
    glClearColor(1,1,1,1);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}

沒有留言:

張貼留言