week13
1.在codeblocks建立一個新專案week13_rect把10行程式碼貼上去,並加上glRectf的函式
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRectf(0.3,0.5,-0.3,-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();
}
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();
}
3.讓手臂旋轉
#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();
}
4.把手臂放好
#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();
}
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.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();
}
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.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();
}
7.把手臂放回身體,並把上手臂旋轉的註解消
#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();
}
8.新增一個新專案week_13_rect_many_TRT增加一個左手,記得負號(鏡射)

#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();
}
沒有留言:
張貼留言