劃出兩個茶壺並分別有自己的座標
#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("Robot3D");
glutDisplayFunc(display);
glutMainLoop();
}
用鍵盤1,2操作黃色茶壺的上下
#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("Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2茶壺都可以藉由鍵盤控制上下左右,並讓黃色茶壺可以藉由鍵盤旋轉
#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') x1+=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("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(0,0,1);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(x1,y1,0);
glRotatef(angle2,0,0,1);
glTranslatef(-0.25,-0.11,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') x1+=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("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_LIGHTING);
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(1,0,0); 加入顏色
drawArm(); ///這是右上的手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle2, 0, 0, 1);
glTranslatef(0.25, 0, 0);
glColor3f(0,1,0); 加入顏色
drawArm(); ///這是右下的手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle3, 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(0,0,1); 加入顏色
drawArm(); ///這是左上的手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle4, 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(1,1,0); 加入顏色
drawArm(); ///這是左下的手臂
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
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 keyboard(unsigned char key, int x, int y)
{
if(key=='1')angle++;
if(key=='2')angle2++;
if(key=='3')angle3++;
if(key=='4')angle4++;
glutPostRedisplay();
}
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);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
按鍵盤1 . 2 . 3 . 4分別可以讓紅綠藍黃手臂用滑鼠移動
#include <GL/glut.h>
float oldX=0,oldY=0;
float angle[10]={};
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 display()
{
GLfloat pos[]={0 ,0 , -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
///glEnable(GL_LIGHTING);
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(1,0,0);
drawArm(); ///這是右上的手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(0.25, 0, 0);
glColor3f(0,1,0);
drawArm(); ///這是右下的手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle[3], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(0,0,1);
drawArm(); ///這是左上的手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle[4], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(1,1,0);
drawArm(); ///這是左下的手臂
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x,int y) ///滑鼠互動
{
angle[now] -= 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 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 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);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
檔案存檔
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float oldX=0,oldY=0;
float angle[10]={};
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 display()
{
GLfloat pos[]={0 ,0 , -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
///glEnable(GL_LIGHTING);
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(1,0,0);
drawArm(); ///這是右上的手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(0.25, 0, 0);
glColor3f(0,1,0);
drawArm(); ///這是右下的手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle[3], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(0,0,1);
drawArm(); ///這是左上的手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle[4], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(1,1,0);
drawArm(); ///這是左下的手臂
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
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++)
{
printf("%.1f\t",angle[i]);
}
printf("\n");
}
void mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
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 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);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
freeglut的bin裡面多一個文字檔
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float oldX=0,oldY=0;
float angle[10]={};
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 display()
{
GLfloat pos[]={0 ,0 , -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
///glEnable(GL_LIGHTING);
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(1,0,0);
drawArm(); ///這是右上的手臂
glPushMatrix();
glTranslatef(0.25, 0, 0);
glRotatef(angle[2], 0, 0, 1);
glTranslatef(0.25, 0, 0);
glColor3f(0,1,0);
drawArm(); ///這是右下的手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);
glRotatef(-angle[3], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(0,0,1);
drawArm(); ///這是左上的手臂
glPushMatrix();
glTranslatef(-0.25, 0, 0);
glRotatef(-angle[4], 0, 0, 1);
glTranslatef(-0.25, 0, 0);
glColor3f(1,1,0);
drawArm(); ///這是左下的手臂
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
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 mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
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 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);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言