顯示具有 01160926_魏全嶸 標籤的文章。 顯示所有文章
顯示具有 01160926_魏全嶸 標籤的文章。 顯示所有文章

2014年6月6日 星期五

Week16 防破圖擴範圍

#include <GL/glut.h>
float eyeX=0.3, eyeY=0.2, eyeZ=0.3, objX=0, objY=0, objZ=0, upX=0, upY=1, upZ=0;
float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 if(key=='3') angle2+=5;
 if(key=='4') angle2-=5;
 if(key=='5') angle3+=5,angle4-=5;
 if(key=='6') angle3-=5,angle4+=5;
 if(key=='7') angle5+=5;
 if(key=='8') angle5-=5;
 if(key==' ')
  {
eyeX-=0.02,eyeY-=0.02;
}else if(key=='0'){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);//欲擴充之範圍以防止破圖
glMatrixMode(GL_MODELVIEW);
}
 glutPostRedisplay();//請電腦重新畫一次畫面
}

Week16 旋轉運鏡

#include <GL/glut.h>
float eyeX=0.3, eyeY=0.2, eyeZ=0.3, objX=0, objY=0, objZ=0, upX=0, upY=1, upZ=0;
float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 if(key=='3') angle2+=5;
 if(key=='4') angle2-=5;
 if(key=='5') angle3+=5,angle4-=5;
 if(key=='6') angle3-=5,angle4+=5;
 if(key=='7') angle5+=5;
 if(key=='8') angle5-=5;
 if(key==' ')
  {
eyeX-=0.02,eyeY-=0.02; //欲旋轉ˋ之角度
}
 glutPostRedisplay();//請電腦重新畫一次畫面
}

Week16 一行運鏡


float eyeX=0.3, eyeY=0.2, eyeZ=0.3, objX=0, objY=0, objZ=0, upX=0, upY=1, upZ=0;
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);

2014年5月30日 星期五

自動播放

                                                  #include <GL/glut.h>
float a = 0;  
float angle[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
float angleOld[20] = {0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
float angleNew[20] = {90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();
      glRotatef(angle[0], 0,0,1);
      glutSolidTeapot(0.3);
     glPopMatrix();
     glPushMatrix();
      glTranslatef(0.5, 0,0);
  glRotatef(angle[1], 0,0,1);
  glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}
void readNext()
{
 a += 0.3; 
 for( int i=0; i<=20; i++){ 
  angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
 }
}
void timer(int t)
{
 glutTimerFunc(100, timer, 0);
 readNext();
 glutPostRedisplay();

void keyboard(unsigned char key, int x, int y)
{
 if (key == 'r'){
  readNext();
 }
 else if (key== 'p'){
  glutTimerFunc(0, timer, 0);
 }
 glutPostRedisplay();
}
int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week15");
 
 glutKeyboardFunc(keyboard);
 glutDisplayFunc(display);
 glutMainLoop();
}


旋轉茶壺 內插

#include<GL/glut.h>
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='s'){
}else if(key=='r'){
a+=0.1;
angle[0]=angleOld[0]*(1-a)+angleNew[0]*a;
glutPostRedisplay();
}else if(key=='p'){
}
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}

2014年5月23日 星期五

2014年5月2日 星期五

機器人膝蓋

#include <GL/glut.h>

float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 if(key=='3') angle2+=5;
 if(key=='4') angle2-=5;
 if(key=='5') angle3+=5,angle4-=5;
 if(key=='6') angle3-=5,angle4+=5;
 if(key=='7') angle5+=5;
 if(key=='8') angle5-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 

 
  glPushMatrix();//右肩
  glTranslatef(0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//右手臂
glRotatef(angle4,1,0,0);
glTranslatef(0,-0.3,0);
glScalef(0.2,0.8,0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
 
    glPushMatrix();//左肩
  glTranslatef(-0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//左手臂
glRotatef(angle3,1,0,0);
glTranslatef(0,-0.3,0);
glScalef(0.2,0.8, 0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
 
    glPushMatrix();//頭
  glTranslatef(0,0.4,0);
  glutSolidSphere(0.2,30,30);
    glPopMatrix();
   
    glPushMatrix();//屁屁
  glTranslatef(0,-0.2,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//左大腿
  glRotatef(angle3,1,0,0);
glTranslatef(0.1,-0.2,0);
glScalef(0.2,0.6,0.2);
glutSolidCube(0.5);
glPushMatrix();//左小腿
glRotatef(angle4,1,0,0);
glTranslatef(0,-0.6,0);
glScalef(1,1.2,1);
glutSolidCube(0.5);
glPopMatrix();

glPopMatrix();

 
  glPushMatrix();//右大腿
  glRotatef(angle4,1,0,0);
glTranslatef(-0.1,-0.2,0);
glScalef(0.2,0.6,0.2);
glutSolidCube(0.5);

glPushMatrix();
glTranslatef(0,0.,0);
glRotatef(angle3,1,0,0);
glTranslatef(0,-0.6,0);
glPushMatrix();//右小腿
glScalef(1,1.2,1);
glutSolidCube(0.5);
glPopMatrix();
glPopMatrix();
glPopMatrix();
    glPopMatrix();
   



    glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

機器人手臂

#include <GL/glut.h>

float angleX =0,angle2=0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 if(key=='3') angle2+=5;
 if(key=='4') angle2-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 

 
  glPushMatrix();//右肩 
  glTranslatef(0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//右手臂 
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
   
    glPushMatrix();//左肩 
  glTranslatef(-0.4,0,0);
  glutSolidSphere(0.2,30,30);
  glPushMatrix();//左手臂 
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
  glPopMatrix();
    glPopMatrix();
   
    glPushMatrix();
  glTranslatef(0,0.4,0);
  glutSolidSphere(0.2,30,30);
    glPopMatrix();
 
   
 


 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

加上階層式管理位置控制較簡單明確

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 
 
  glPushMatrix();
  glTranslatef(0.5,0,0);
  glutSolidSphere(0.3,30,30);
   glPopMatrix();
   
   glPushMatrix();
  glTranslatef(-0.5,0,0);
  glutSolidSphere(0.3,30,30);
   glPopMatrix();
   
   glPushMatrix();
  glTranslatef(0,0.5,0);
  glutSolidSphere(0.3,30,30);
   glPopMatrix();
   
   glPushMatrix();
  glTranslatef(0,-0.5,0);
  glutSolidSphere(0.3,30,30);
   glPopMatrix();
   
    glPushMatrix();
  glTranslatef(0.5,-0.5,0);
  glutSolidSphere(0.3,30,30);
   glPopMatrix();


 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

旋轉茶壺加上下左右圓球

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 
  glTranslatef(0.5,0,0);
  glutSolidSphere(0.3,30,30);
  glTranslatef(-1,0,0);
  glutSolidSphere(0.3,30,30);
  glTranslatef(0.5,0.5,0);
  glutSolidSphere(0.3,30,30);
  glTranslatef(0,-1,0);
  glutSolidSphere(0.3,30,30);
 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

旋轉茶壺家左右的圓球

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
 if(key=='1') angleX+=5;
 if(key=='2') angleX-=5;
 glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
 glEnable(GL_LIGHT0);
 glEnable(GL_NORMALIZE);
 glEnable(GL_COLOR_MATERIAL);
 glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX, 0,1,0);
  glutSolidTeapot(0.3);
 
  glTranslatef(0.5,0,0);
  glutSolidSphere(0.3,30,30);
  glTranslatef(-1,0,0);
  glutSolidSphere(0.3,30,30);
 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
 glutCreateWindow("week11");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();
 glutMainLoop();
}

week11可旋轉茶壺

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
}

void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myLight();
glutMainLoop();
}

2014年4月25日 星期五

鍵盤控制旋轉



#include <GL/glut.h>
float angleX=0;
void keyboard(unsigned char key, int x, int y)
{
     if(key=='a') angleX+=5;
     if(key=='b') angleX-=5;
     glutPostRedisplay();
}
void display ()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();
        glRotatef(angleX, 0,1,0);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}

int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
   
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
   
    glutMainLoop();
}