☆複習上週內容☆
開啟上週檔案
☆本週內容☆
劃出藍色方塊
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float vx[2000],vy[2000];
float angle=0;
int N=0;
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( ); ///備份矩陣
glRotatef(angle,0,0,1); ///旋轉
glScalef(0.5,0.1,0.1);
glColor3f(0.3,0.3,1.0); ///顏色為藍色
glutSolidCube(1); ///方塊
glPopMatrix( ); ///還原矩陣
glutSwapBuffers( );
}
void mouse(int button,int state,int x,int y) ///不做事
{
}
void motion(int x,int y)
{
printf("%d %d\n",x,y);
vx[N]=(x-150)/150.0;
vy[N]=-(y-150)/150.0;
N++;
display( );
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop( );
}
方塊"自行"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( ); ///備份矩陣
glRotatef(angle++,0,0,1); ///角度增加
glutSolidCube(1); ///方塊
glPopMatrix( ); ///還原矩陣
glutSwapBuffers( );
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display); ///Idlec很閒時就重畫面
glutIdleFunc(display); /// 顯示函式
glutMainLoop( );
}
方塊"繞中央"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
glRotatef(angle++,0,0,1); ///角度增加
glTranslatef(-0.25,0,0); ///旋轉中心放在"中央"
glScalef(0.5,0.1,0.1); ///細長的
glutSolidCube(1); ///方塊
glPopMatrix( );
glutSwapBuffers( );
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display); ///Idlec很閒時就重畫面
glutIdleFunc(display); ///顯示函式
glutMainLoop( );
}
方塊"肩上"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void hand( )
{
glPushMatrix( );
glScalef(0.5,0.1,0.1); ///細長的
glutSolidCube(1); ///方塊
glPopMatrix( );
}
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
glTranslatef(-0.25,0,0); ///(3)轉動的手臂掛在肩上
glRotatef(angle++,0,0,1); ///(2)旋轉
glTranslatef(-0.25,0,0); ///(1)旋轉中心放在"中央"
hand( );
glPopMatrix( );
hand( );
glutSwapBuffers( );
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display); ///Idlec很閒時就重畫面
glutIdleFunc(display); ///顯示函式
glutMainLoop( );
}
藍色方塊"按按鍵"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float vx[2000],vy[2000];
float angle=0;
int N=0;
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( ); ///備份矩陣
glRotatef(angle,0,0,1); ///旋轉
glScalef(0.5,0.1,0.1);
glColor3f(0.3,0.3,1.0);
glutSolidCube(1); ///方塊
glPopMatrix( ); ///還原矩陣
glutSwapBuffers( );
}
void keyboard(unsigned char key,int x,int y) ///按下鍵盤
{
angle++; ///角度增加
display( );
}
void mouse(int button,int state,int x,int y) ///不做事
{
}
void motion(int x,int y)
{
printf("%d %d\n",x,y);
vx[N]=(x-150)/150.0;
vy[N]=-(y-150)/150.0;
N++;
display( );
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutKeyboardFunc(keyboard); ///按下鍵盤"旋轉"
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop( );
}
"手肘"與"手臂"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void hand( )
{
glPushMatrix( );
glScalef(0.5,0.1,0.1); ///細長的
glutSolidCube(1); ///方塊
glPopMatrix( );
}
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
glTranslatef(-0.35,0,0); ///(3)轉動的上手臂掛上肩關節
glRotatef(angle,0,0,1); ///(2)旋轉
glTranslatef(-0.35,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///上手臂
glPushMatrix();
glTranslatef(-0.25,0,0); ///(3)轉動的手肘掛在肩上
glRotatef(angle,0,0,1); ///(2)旋轉
glTranslatef(-0.25,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///下手肘
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
angle++; ///角度增加
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display); ///Idlec很閒時就重畫面
glutIdleFunc(display); ///顯示函式
glutMainLoop( );
}
"手肘"與"手臂"旋轉
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void hand( )
{
glPushMatrix( );
glScalef(0.5,0.1,0.1); ///細長的
glutSolidCube(1); ///方塊
glPopMatrix( );
}
void display( )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix( ); ///左半邊
glTranslatef(-0.35,0,0); ///(3)轉動的手臂掛在肩上
glRotatef(angle,0,0,1); ///(2)旋轉
glTranslatef(-0.35,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///上手臂
glPushMatrix( );
glTranslatef(-0.25,0,0); ///(3)轉動的手臂掛在肩上
glRotatef(angle,0,0,1); ///(2)旋轉
glTranslatef(-0.25,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///下手肘
glPopMatrix( );
glPopMatrix( );
glPushMatrix( ); ///右半邊
glTranslatef(0.35,0,0); ///(3)轉動的手臂掛在肩上
glRotatef(-angle,0,0,1); ///(2)旋轉
glTranslatef(0.35,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///上手臂
glPushMatrix( );
glTranslatef(0.25,0,0); ///(3)轉動的手臂掛在肩上
glRotatef(-angle,0,0,1); ///(2)旋轉
glTranslatef(0.25,0,0); ///(1)旋轉中心放在"中央"
hand( ); ///下手肘
glPopMatrix( );
glPopMatrix( );
glutSwapBuffers( );
angle++;
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06 week");
glutDisplayFunc(display); ///Idlec很閒時就重畫面
glutIdleFunc(display); ///顯示函式
glutMainLoop( );
}








沒有留言:
張貼留言