今天早上出門前看了一下氣象新聞
今天白天的紫外線過量欸~
主播說站在太陽下15分鐘就會曬傷欸~
超級誇張的
今天選擇最簡單的茶壺圖形做練習
所以先選擇GLUT
加一個自己的資料夾在桌布存自己的檔案搂~
第一步驟 先畫出兩個茶壺分別在左右邊 做個測試
程式碼:
#include<GL/glut.h>
float x1=-0.4,y1=0,x2=0.4,y2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1,y1,0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2,y2,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("02160446-Robot3D");
glutDisplayFunc(display);
glutMainLoop();
}
第二步驟 加上keyboard可控制x及y軸
程式碼:
#include<GL/glut.h>
float x1=-0.4,y1=0,x2=0.4,y2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1,y1,0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2,y2,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
}
void keyboard(unsigned char key,int x, int y)
{
if(key=='1')y1+=0.1;
if(key=='2')y1-=0.1;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("02160446-Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
第三步驟 加上轉動
這可讓我們清楚了解怎麼製作關節移動及轉動
程式碼:
#include<GL/glut.h>
#include<stdio.h>
float x1=-0.4,y1=0,x2=0,y2=0,angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1,y1,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.25,-0.11,0);///旋轉軸移到中心,這是自己想要的點移到圖的正中心
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2,y2,0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();
}
void keyboard(unsigned char key,int x, int y)
{
if(key=='1')y1+=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("02160446-Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
第四步驟 茶壺接上茶壺並做大小腿轉動
程式碼:
#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(1,0,1);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(x1,y1,0);
glRotatef(angle2,0,0,1);
glTranslatef(-0.25,-0.11,0);///旋轉軸一到中心
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glFlush();
}
void keyboard(unsigned char key,int x, int y)
{
if(key=='1')y1+=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;
if(key=='7')angle2+=10;
if(key=='8')angle2-=10;
printf("%f %f\n",x1,y1);
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("02160446-Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
用上上禮拜的壯漢還做鍵盤控制
這次還可以分別控制各個關節
程式碼:
#include<GL/glut.h>
float angle=0,angle2=0,angle3=0,angle4=0,oldX=0,oldY=0;
void drawBody()
{
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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);
glColor3f(1,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);
glRotatef(-angle3,0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(1,1,1);
drawArm();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle4,0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(0,1,1);
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')angle++;
if(key=='2')angle2++;
if(key=='3')angle3++;
if(key=='4')angle4++;
if(key=='5')angle--;
if(key=='6')angle2--;
if(key=='7')angle3--;
if(key=='8')angle4--;
glutPostRedisplay();
}
void motion(int x,int y)
{
angle-=y-oldY;
oldY=y;oldX=x;
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer (int t)
{
glutTimerFunc(20,timer,t+1);
///angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
現在是鍵盤加上滑鼠控制
鍵盤點"1"的時候轉動滑鼠
就可看到右邊的手臂移動
點"2"可看到右邊的手軸轉動
程式碼:
#include<GL/glut.h>
float oldX=0,oldY=0;//angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;
void drawBody()
{
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')now=1;
if(key=='2')now=2;
if(key=='3')now=3;
if(key=='4')now=4;
glutPostRedisplay();
}
void motion(int x,int y)
{
angle[now]-=y-oldY;
oldY=y;oldX=x;
}
void display()
{
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0.25,0,0);
glColor3f(0,0,1);
drawArm();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle[2],0,0,1);
glColor3f(1,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(1,1,1);
drawArm();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(0,1,1);
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer (int t)
{
glutTimerFunc(20,timer,t+1);
///angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
這接座標會存檔紀錄
#include<GL/glut.h>
#include<stdio.h>///要存檔用的
FILE * fout=NULL;///要存檔用的file for output
float oldX=0,oldY=0;//angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;
void drawBody()
{
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')now=1;
if(key=='2')now=2;
if(key=='3')now=3;
if(key=='4')now=4;
glutPostRedisplay();
}
void motion(int x,int y)
{
angle[now]-=y-oldY;
oldY=y;oldX=x;
for(int i=0;i<8;i++){///要存檔用的
printf("%.1f\t",angle[i]);
}
printf("\n");///要存檔用的
}
void display()
{
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0.25,0,0);
glColor3f(0,0,1);
drawArm();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle[2],0,0,1);
glColor3f(1,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(1,1,1);
drawArm();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(0,1,1);
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer (int t)
{
glutTimerFunc(20,timer,t+1);
///angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
這些座標會存在一個文字檔案裡
#include<GL/glut.h>
#include<stdio.h>///要存檔用的
FILE * fout=NULL;///要存檔用的file for output
float oldX=0,oldY=0;//angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;
void drawBody()
{
glPushMatrix();
glScalef(1,0.5,0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6,0.3,0.3);
glutSolidCube(1);
glPopMatrix();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='1')now=1;
if(key=='2')now=2;
if(key=='3')now=3;
if(key=='4')now=4;
glutPostRedisplay();
}
void motion(int x,int y)
{
angle[now]-=y-oldY;
oldY=y;oldX=x;
if(fout==NULL)fout=fopen("a.txt","w+");///要存檔用的
for(int i=0;i<8;i++){///要存檔用的
fprintf(fout,"%.1f\t",angle[i]);///要存檔用的
}
fprintf(fout,"\n");///要存檔用的
}
void display()
{
GLfloat pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glTranslatef(0.25,0,0);
glColor3f(0,0,1);
drawArm();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle[2],0,0,1);
glColor3f(1,0,1);
glTranslatef(0.25,0,0);
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);
glRotatef(-angle[3],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(1,1,1);
drawArm();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle[4],0,0,1);
glTranslatef(-0.25,0,0);
glColor3f(0,1,1);
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer (int t)
{
glutTimerFunc(20,timer,t+1);
///angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言