#第04週上課重點
1. 作業互評、評分
2. Mouse互動
3. 實作:glutMouseFunc( )
glutMotionFunc( )
4. 圓、sin( )、cos( )、Shape
5. 作業:Blog、Moodle
--------------------------------------------------------------------------------------------------------------------------
◎滑鼠點擊改變圓的顏色:
#include <GL/glut.h>
#include <math.h>
void display ( )
{
glBegin (GL_POLYGON);
for (float angle = 0 ; angle <= 3.1415926 *2 ; angle += 0.01)
glVertex2f ( cos(angle) , sin( angle ) );
glEnd ( ) ;
glutSwapBuffers ( ) ;
}
void mouse(int button , int state , int x , int y) ///滑鼠點擊改變顏色
{
printf ("%d %d %d %d\n" , button , state , x , y ) ; ///Now print the values
glColor3f ( 1 , x/300.0 , y/300.0 ) ; ///Now change color to any color
}
/*void motion( int x , int y ) ///滑鼠拖曳改變顏色
{
glColor3f ( 1 , x/300.0 , y/300.0 ) ;
glutPostRedisplay( ) ;
}*/
int main( int argc, char*argv[ ] )
{
glutInit(&argc , argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
glutCreateWindow("04160683 Number 1") ;
glutDisplayFunc(display) ;
glutMouseFunc(mouse) ; ///mouse函數
///glutMotionFunc(motion) ; ///motion函數
glutMainLoop( ) ;
}
執行截圖 :
◎滑鼠拖曳改變圓的顏色:
#include <GL/glut.h>
#include <math.h>
void display ( )
{
glBegin (GL_POLYGON);
for (float angle = 0 ; angle <= 3.1415926 *2 ; angle += 0.01)
glVertex2f ( cos(angle) , sin( angle ) );
glEnd ( ) ;
glutSwapBuffers ( ) ;
}
/*void mouse(int button , int state , int x , int y) ///滑鼠點擊改變顏色
{
printf ("%d %d %d %d\n" , button , state , x , y ) ; ///Now print the values
glColor3f ( 1 , x/300.0 , y/300.0 ) ; ///Now change color to any color
}*/
void motion( int x , int y ) ///滑鼠拖曳改變顏色
{
glColor3f ( 1 , x/300.0 , y/300.0 ) ;
glutPostRedisplay( ) ; ///貼便利貼告訴電腦執行"Re-Display"動作
}
int main( int argc, char*argv[ ] )
{
glutInit(&argc , argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
glutCreateWindow("04160683 Number 1") ;
glutDisplayFunc(display) ;
///glutMouseFunc(mouse) ; ///mouse函數
glutMotionFunc(motion) ; ///motion函數
glutMainLoop( ) ;
}
執行截圖 :
◎滑鼠拖曳使嘴巴開合:
#include <GL/glut.h>
#include <math.h>
float mouth = 0 ; ///代表嘴巴的角度
void display ( )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin ( GL_TRIANGLE_FAN ) ;
glVertex2f ( 0 , 0 ) ;
for (float angle = mouth ; angle <= 3.1415926 *2 - mouth; angle += 3.1415926/100 )
glVertex2f ( cos(angle) , sin( angle ) ) ;
glEnd ( ) ;
glutSwapBuffers ( ) ;
}
/*void mouse(int button , int state , int x , int y) ///滑鼠點擊改變顏色
{
printf ("%d %d %d %d\n" , button , state , x , y ) ; ///Now print the values
glColor3f ( 1 , x/300.0 , y/300.0 ) ; ///Now change color to any color
}*/
void motion( int x , int y ) ///滑鼠拖曳改變顏色
{
mouth = x/300.0;
glColor3f ( 1 , x/300.0 , y/300.0 ) ;
glutPostRedisplay( ) ;
}
int main( int argc, char*argv[ ] )
{
glutInit(&argc , argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
glutCreateWindow("04160683 Number 1") ;
glutDisplayFunc(display) ;
///glutMouseFunc(mouse) ; ///mouse函數
glutMotionFunc(motion) ; ///motion函數
glutMainLoop( ) ;
}
執行截圖 :
沒有留言:
張貼留言