code:
build and run:#include<GL/glut.h>void display(){glutSolidTeapot(0.3);glutSwapBuffers();}int main(int argc, char *argv[]){glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("我的魔法會把你撕成碎片");glutDisplayFunc(display);glutMainLoop();}
任務2:mouse函式
code中加入回調函式mouse,glutMouseFunc調用mouse。
執行:
任務3:通過滑鼠改顏色
code:在mouse函式中加入兩行:
glColor3f(mouse/4.0, x/255.0, y/255.0);glutPostRedisplay();
執行:
通過點擊滑鼠/滑動滾輪和移動滑鼠來控制茶壺顏色變化。
任務4:拖拽動作
glutMotionFunc(motion): glutMotionFunc調用回調函式motion
執行:void motion(int x, int y){glColor3f(1, x/255.0, y/255.0);glutPostRedisplay();printf("%d %d\n", x, y);}
任務5:畫圓
code:在display中定義畫圓的程式
執行:glBegin(GL_POLYGON);for(float angle=0;angle<3.1415926*2;angle+=0.01)glVertex2f(cos(angle), sin(angle));glEnd();
橢圓:
glVertex2f(0.7*cos(angle), sin(angle));
張開嘴巴的小精靈:
code:
void display(){//glutSolidTeapot(0.3);glBegin(GL_POLYGON);for(float angle=0;angle<3.1415926*2;angle+=0.01)glVertex2f(cos(angle), sin(angle));glEnd();glColor3f(0,0,0);glBegin(GL_POLYGON);for(float angle=3.1415926 - 0.5;angle<3.1415926+0.5;angle+=0.01)glVertex2f(cos(angle), sin(angle));glVertex2f(0.0, 0.0);glEnd();glutSwapBuffers();}
沒有留言:
張貼留言