Week06
step01-1
CodeBlocks-File_New_Project,GLUT專案,檔名為week06-1_TRT_robot2_teapot
利用上周的week05-2_TRT_robot的程式來改,改成製作茶壺
#include <GL/glut.h>
float angle =0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glRotatef(angle,0,0,1);
glTranslatef(0.45,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
增加一橫程式碼把茶壺掛起來
#include <GL/glut.h>
float angle =0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glTranslatef(0.5,0.5,0);//增加一行程式碼
glRotatef(angle,0,0,1);
glTranslatef(0.45,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
step02-1CodeBlocks-File_New_Project,GLUT專案,檔名為week06-2_robot3_hierarchy(有階層的TRT);
做出一個小方塊#include <GL/glut.h>
float angle =0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3);
glutSolidCube(0.5);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
///glTranslatef(0.5,0.5,0);
/// glRotatef(angle,0,0,1);
///glTranslatef(0.45,0,0);
myCube();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
增加兩行,使得會旋轉
#include <GL/glut.h>
float angle =0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3);
glutSolidCube(0.5);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
///glTranslatef(0.5,0.5,0);
glRotatef(angle,0,0,1);///增加兩行
glTranslatef(0.25,0,0);///增加兩行
myCube();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
step02-2
多層TRT
#include <GL/glut.h>
float angle =0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3);
glutSolidCube(0.5);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
step02-3
CodeBlocks-File_New_Project,GLUT專案,檔名為week06-3_robot3_hierarchy
畫出左右兩邊
#include <GL/glut.h>
float angle =0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3);
glutSolidCube(0.5);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();///右手
glTranslatef(0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手
glTranslatef(-0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
step03-1CodeBlocks-File_New_Project,GLUT專案,檔名為week06-4__TRT_robot5_mouse_motion_angle
#include <GL/glut.h>
float angle =0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3);
glutSolidCube(0.5);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();///右手
glTranslatef(0.25,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(-angle,0,0,1);
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glPushMatrix();///左手
glTranslatef(-0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x,int y)
{
angle = x;
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
step03-2
練習期中考
step03-3把大象放進冰箱裡,打開冰箱門,把大象放進去,把門關起來
用mouse來轉動
step04-1上傳github
沒有留言:
張貼留言