1.複習上禮拜的TRT程式先貼上10行程式再加入正方形的程式,印出正方形身體。
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glRectf(0.5,0.5,-0.5,-0.5);
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();
return 0;
}
2.加入1個紅色的手臂準備好TRT的程式
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f(1,1,1);
glRectf(0.5,0.5,-0.5,-0.5);
glPushMatrix();
///glTranslatef(x,y,z);
///glRotatef(angle,0,0,1);
///glTranslatef(x2,y2,z2);
glColor3f(1,0,0);
glRectf(0.5,0.5,1,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
3.把紅色手臂旋轉中心放正心,角度設為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.5,0.5,-0.5,-0.5);
glPushMatrix();
///glTranslatef(x,y,z);
glRotatef(angle,0,0,1);
glTranslatef(-0.5,-0.4,0);
glColor3f(1,0,0);
glRectf(0.5,0.5,1,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
4.加入滑鼠的函示紅色手臂會隨者滑鼠點擊來揮手
#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.5,0.5,-0.5,-0.5);
glPushMatrix();
glTranslatef(0.5,0.5,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.5,-0.4,0);
glColor3f(1,0,0);
glRectf(0.5,0.5,1,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
5.把體瘦身然後手臂貼回身體
#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.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.5,-0.4,0);
glColor3f(1,0,0);
glRectf(0.5,0.5,1,0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
6.加上綠色下手臂讓綠手臂旋轉
#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.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
///glRotatef(angle,0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#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.2);
glPushMatrix();
glTranslatef(0.3,0.5,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.3,-0.4,0);
glColor3f(1,0,0);glColor3f(1,0,0);
glRectf(0.3,0.5,0.8,0.3);
glPushMatrix();
glTranslatef(0.8,0.4,0);
glRotatef(angle,0,0,1);
glTranslatef(-0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(0.8,0.5,1.1,0.3);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.5,0);
glRotatef(angle,0,0,1);
glTranslatef(0.3,-0.4,0);
glColor3f(1,0,0);glColor3f(1,0,0);
glRectf(-0.3,0.5,-0.8,0.3);
glPushMatrix();
glTranslatef(-0.8,0.4,0);
glRotatef(angle,0,0,1);
glTranslatef(0.8,-0.4,0);
glColor3f(0,1,0);
glRectf(-0.8,0.5,-1.1,0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutCreateWindow("week13_rect_TRT");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}


沒有留言:
張貼留言