2020年5月19日 星期二

week12_obj_arm

#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
float angle1=-30;
float angle2=120;
float angle3=-150;
float angle4=40;
float angle5=0;
float angle6=0;
char name[40];
GLMmodel* up[4];
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 timer(int t)
{
    glutTimerFunc(1,timer,t+1);
    glutPostRedisplay();
}
void load()
{
    if (!up[0])
    {
        up[0] = glmReadOBJ("data/arm/floor.obj");//抓取檔案
        if (!up[0]) printf("no find");
        glmUnitize(up[0]);

    }
    if (!up[1])
    {
        up[1] = glmReadOBJ("data/arm/arm.obj");
        if (!up[1]) printf("no find");
        glmUnitize(up[1]);

    }
    if (!up[2])
    {
        up[2] = glmReadOBJ("data/arm/axis.obj");
        if (!up[2]) printf("no find");
        glmUnitize(up[2]);

    }
       if (!up[3])
    {
        up[3] = glmReadOBJ("data/arm/ro.obj");
        if (!up[3]) printf("no find");
        glmUnitize(up[3]);

    }
}
void arm()
{
    glPushMatrix();
        glScaled(0.25,0.25,0.25);
        glmDraw(up[1], GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    load();

    glPushMatrix();
    glRotated(-25,1,0,0);
    glScaled(0.6,0.6,0.6);
    glRotated(angle5+angle6,0,1,0);
    glTranslated(0,-0.8,0);
    glmDraw(up[0], GLM_SMOOTH | GLM_MATERIAL);
    glTranslated(0,0.08,0);
    glRotated(0,0,0,1);
    glTranslated(0,0.17,0);
    arm();
    glTranslated(0,0.21,0);
    glRotated(-angle1,0,0,1);
    glTranslated(0,0.17,0);
    arm();
    glTranslated(0,0.21,0);
    glRotated(-angle2*2,0,0,1);
    glTranslated(0,0.17,0);
    arm();
    glTranslated(0,0.3,0);
    glRotated(angle3,0,0,1);
    glTranslated(-0.19,0,0);
    glmDraw(up[2], GLM_SMOOTH | GLM_MATERIAL);
    glTranslated(-0.3,-0.001*angle4-0.08,0);
    glmDraw(up[3], GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case '`':
        case '\b':
            exit(0);
            break;
        case ',':
            angle5++;angle5++;
            break;
        case '.':
            angle5--;angle5--;
            break;
        case 'y':
            if(angle4<72)
            angle4+=2;
            break;
        case 'h':
            if(angle4>-10)
            angle4-=2;
            break;
        case 'u':
            if(angle1<-16) angle1++;
            if(angle2>106) angle2--;
            if(angle3>-164) angle3--;
            break;
        case 'j':
            if(angle1>-90)   angle1--;
            if(angle2<180) angle2++;
            if(angle3<-90) angle3++;
            break;
    }
    printf("1_%g 2_%g 3_%g\n",angle1,angle2,angle3);
    glutPostRedisplay();
}
void mouseFunc(int button,int state,int x, int y)
{
    printf("%d %d %d %d \n",button,state,x,y);
    if(button==3) angle6-=2;
    if(button==4) angle6+=2;
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(600,600);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Steam");
    glutDisplayFunc(display);
    glutTimerFunc(1,timer,0);
    glClearColor(0,1,1,1);
    glutKeyboardFunc(key);
    glutMouseFunc(mouseFunc);
    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();

}

沒有留言:

張貼留言