★自製機器人
第一步:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1,0,0);///紅色
glScalef(0.5,0.2,0.2);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutDisplayFunc(display);
glutMainLoop();
}
第二步:
#include <GL/glut.h>
void mycube()///函式簡化程式
{
glPushMatrix();
glScalef(0.5,0.2,0.2);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///再保護一次
glColor3f(1,0,0);
mycube();///函式簡化程式
glTranslatef(0.4,0.1,0);///改變位置
glColor3f(1,1,0);///黃色
mycube();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutDisplayFunc(display);
glutMainLoop();
}
第三步:
#include <GL/glut.h>
void mycube()///函式簡化程式
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///再保護一次
glRotatef(angle,0,0,1);///轉動
mycube();///函式簡化程式
glPopMatrix();
angle++;///每次角度增加
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);///有空idle時,再重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}
第四步:
#include <GL/glut.h>
void mycube()///函式簡化程式
{
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.5,0.2,0.2);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///再保護一次
glRotatef(angle,0,0,1);///轉動
◎ glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
mycube();///函式簡化程式
glPopMatrix();
angle++;///每次角度增加
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);///有空idle時,再重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}
第五步:
#include <GL/glut.h>
void mycube()///函式簡化程式
{
glPushMatrix();
glColor3f(1,0.3,0.5);
glScalef(0.5,0.2,0.2);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0,0.7,1);
glutSolidCube(0.6);///藍色身體
glPushMatrix();///再保護一次
glTranslatef(0.3,0.3,0);///將整個手臂移動至右上角
glRotatef(angle,0,0,1);///轉動
glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
mycube();///函式簡化程式
glPopMatrix();
angle++;///每次角度增加
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);///有空idle時,再重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}

#include <GL/glut.h>
float angle=0;
float angle2=0;
void bodycube()
{
glPushMatrix();
glColor3f(1,0.7,0.8);///櫻花
glScalef(0.5,0.65,0.65);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void headcube()///頭
{
glPushMatrix();
glColor3f(1,0.7,0.8);///櫻花
glScalef(0.45,0.35,0.35);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void handcube()///手
{
glPushMatrix();
glColor3f(1,0.7,0.8);///櫻花
glScalef(0.4,0.17,0.17);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void joint()///細部位
{
glPushMatrix();
glColor3f(1,1,1);///櫻花
glScalef(0.1,0.4,0.4);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void foot()///腳掌
{
glPushMatrix();
glColor3f(1,0.7,0.8);///櫻花
glScalef(0.3,0.15,0.15);///調大小(要在Push/Pop裡面)
glutSolidCube(1);///正方體
glPopMatrix();
}
void shoulder()///肩膀
{
glPushMatrix();
glColor3f(1,1,1);
glutSolidSphere(0.1,16,16);
glPopMatrix();
}
void eye()///眼睛
{
glPushMatrix();
glColor3f(1,1,1);
glutSolidSphere(0.06,16,16);
glPopMatrix();
}
void eye2()///眼睛
{
glPushMatrix();
glColor3f(0,0,0);
glutSolidSphere(0.04,16,16);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.5,0);
joint();
glPopMatrix();
glPushMatrix();
glTranslatef(0.13,-0.45,0);
joint();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.13,-0.45,0);
joint();
glPopMatrix();
glPushMatrix();
glTranslatef(0.18,-0.6,0);
foot();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.18,-0.6,0);
foot();
glPopMatrix();
glPushMatrix();///右手
glTranslatef(0.29,0.23,0);///將整個手臂移動至右上角
glRotatef(angle,0,0,1);///轉動
glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
handcube();///函式簡化程式
glPushMatrix();///右手臂
glTranslatef(0.18,0,0);///將整個手臂移動至右上角
glRotatef(angle,0,0,1);///轉動
glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
handcube();///函式簡化程式
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.27,0.25,0);
shoulder();
glPopMatrix();
glPushMatrix();///左手
glTranslatef(-0.27,0.23,0);///將整個手臂移動至右上角
glRotatef(-angle,0,0,1);///轉動
glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
handcube();///函式簡化程式
glPushMatrix();///左手臂
glTranslatef(0.18,0,0);///將整個手臂移動至右上角
glRotatef(angle,0,0,1);///轉動
glTranslatef(0.22,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
handcube();///函式簡化程式
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.27,0.25,0);
shoulder();
glPopMatrix();
bodycube();///身體
glPushMatrix();
glTranslatef(0,0.6,0);
headcube();
glPopMatrix();
glPushMatrix();
glTranslatef(0.12,0.65,0);
eye();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.12,0.65,0);
eye();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.12,0.65,0);
glRotatef(angle2,0,0,1);///轉動
glTranslatef(0.01,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
eye2();
glPopMatrix();
glPushMatrix();
glTranslatef(0.12,0.65,0);
glRotatef(-angle2,0,0,1);///轉動
glTranslatef(0.01,0,0);///將要旋轉的中心(機器人的關節)移到畫面中心
eye2();
glPopMatrix();
angle2+=10;
angle++;///每次角度增加
glutSwapBuffers();
}
int main(int argc ,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week09");
glutIdleFunc(display);///有空idle時,再重畫畫面
glutDisplayFunc(display);
glutMainLoop();
}




沒有留言:
張貼留言