筆記
Step1-1 glRectf (x1 , y1 , x2 , y2) 方塊
1. File-New-Project , GLUT專案 , week13_rect_TRT
2. 貼上GLUT 10行程式,不用茶壺
3.把手臂接到身體
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);//白色的
glRectf(0.3,0.5,-0.3,-0.5);//四邊形
glPushMatrix();
///glTranslatef(x,y,z);(3)把手臂掛回身體
///glRotatef(angle,0,0,1);(2)旋轉 對Z軸轉
///glTranslatef(x2,y2,z2);(1)把手臂的旋轉中心,放中心
glColor3f(1,0,0);//紅色的
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
//glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
Step1-2 再把紅色的手臂加上去
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
///glTranslatef(); 把手臂放回身體
///glRotatef(); 旋轉
///glTranslatef(); 手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
Step1-3 先把旋轉中心放在正中心
---> 把 (0.3 0.4) 移到 (0 0)
glTranslatef (-0.3, -0.4, 0);
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
///glTranslatef(); ///把手臂放回身體
///glRotatef(); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
Step1-4 把手臂旋轉45度
#include <GL/glut.h>
float angle=45;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
///glTranslatef(); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
Step1-5 把手臂放回身體正確位置
#include <GL/glut.h>
float angle=45;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutDisplayFunc(display);
glutMainLoop();
}
Step2-1 讓手臂能隨著滑鼠移動去旋轉
#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step2-2 要開新的專案 week13_rect_TRT_TRT 做出更多的關節
1. File-New-Project , GLUT專案 , week_rect_TRT_TRT
2.把前面的程式拿來用
3.要新增的地方=>第二個關節
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
////glTranslatef(x,y,z); ///把手臂放回身體
///glRotatef(angle,0,0,1); ///旋轉
///glTranslatef(x2,y2,z2); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step2-3 調整手臂的中心位置並旋轉
(把上手臂的旋轉註解掉,因會很難看出下手臂在旋轉的樣子)
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
//glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
////glTranslatef(x,y,z); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step2-4 把手臂放回身體,並把上手臂旋轉的註解消掉
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step2-5 新增一個新專案week_13_rect_many_TRT
1.增加一個左手,記得負號(鏡射)
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void motion(int x,int y){
angle += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0); ///把手臂放回身體
glRotatef(angle,0,0,1); ///旋轉
glTranslatef(0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(-0.7,0.5,-1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step3-1 讓所有關節都有角度,大概20個角度
1.設float angle[20] ,把下面angle改成 angle[0]、angle[1]......
2.再設定一個int angleID=0;
angle[angleID]--->當angleID=0時,angle[0] 以此類推
3.建立一個void keyboard函式
4.在main函式裡面新增 glutKeyboardFunc(keyboard)
--->才能用鍵盤操作關節
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void mouse(int Button, int state, int x, int y){
oldX= x;
}
void keyboard (unsigned char key,int x,int y){
if(key=='0') angleID=0;
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
}
void motion(int x,int y){
angle[angleID] += (x-oldX);
oldX = x;
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);///白色
glRectf(0.3,0.5,-0.3,-0.5); ///身體
glPushMatrix();
glTranslatef(0.3,0.4,0); ///把手臂放回身體
glRotatef(angle[0],0,0,1); ///旋轉
glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(0.3,0.5,0.7,0.3);
glPushMatrix();
glTranslatef(0.7,0.4,0); ///把手臂放回身體
glRotatef(angle[1],0,0,1); ///旋轉
glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(0.7,0.5,1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0); ///把手臂放回身體
glRotatef(angle[2],0,0,1); ///旋轉
glTranslatef(0.3,-0.4,0); ///手臂的旋轉中心
glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.7,0.3);
glPushMatrix();
glTranslatef(-0.7,0.4,0); ///把手臂放回身體
glRotatef(angle[3],0,0,1); ///旋轉
glTranslatef(0.7,-0.4,0); ///手臂的旋轉中心
glColor3f(0,1,0);
glRectf(-0.7,0.5,-1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);
glutCreateWindow("week13 rect TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言