2015年6月3日 星期三

Week15_02131735_郭盈萱


#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0,angle2=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///藍色
        glTranslatef(x2, y2, 0);//要掛在哪裡?
        glRotatef(angle, 0,0,1);
        glColor3f(0,0,1);
        glutSolidTeapot(0.2);
    glPushMatrix(); ///綠色
        glTranslatef(x1, y1, 0);//要掛在哪裡?
        glRotatef(angle2, 0,0,1);
        glTranslatef(-0.25, -0.11, 0);
        glColor3f(0,1,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glPopMatrix();
    glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') x1+=0.1;
    if(key=='2') x1-=0.1;
    if(key=='3') y1+=0.1;
    if(key=='4') y1-=0.1;
    if(key=='5') angle+=10;
    if(key=='6') angle-=10;
    printf("%f %f\n", x1, y1);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}






存檔


#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL; // file output

float oldX=0,oldY=0;
float angle[8]={0,0,0,0,0,0,0,0};
int now = 0 ;

void motion (int x,int y)
{
angle[now] -=y -oldY;
oldY=y ; oldX = x ;

if(fout==NULL) fout=fopen("a.text","w+");
for(int i=0;i<8;i++){
    fprintf(fout,"%.1f\t",angle[i]);
}

}
void keyboard(unsigned char key, int x, int y)

{

void drawBody(){
void drawArm(){
void display()

   {
    glPushMatrix();
        glTranslatef(0.5, 0, 0);//要掛在哪裡?
        glRotatef(angle, 0,0,1);
        glTranslatef(0.25,0,0);
        glColor3f(0,0,1); drawArm();
    glPushMatrix();
        glTranslatef(0.25, 0, 0);//要掛在哪裡?
        glRotatef(angle2, 0,0,1);
        glTranslatef(0.25, -0.11, 0);
        glColor3f(0,1,0); drawArm();
    glPopMatrix();
    glPopMatrix();

glPushMatrix();
    glTranslatef(-0.5, 0, 0);//要掛在哪裡?
        glRotatef(-angle3, 0,0,1);
        glTranslatef(-0.25,0 ,0 );
        glColor3f(0.5,0.5,1); drawArm();

        glPushMatrix();
    glTranslatef(-0.25, 0, 0);//要掛在哪裡?
        glRotatef(-angle4, 0,0,1);
        glTranslatef(-0.25,0 ,0 );
        glColor3f(0.5,1,0.5); drawArm();


    glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}


沒有留言:

張貼留言