Week 4
HW1互評同學GLUT作業圖,挑選出3個自認為最好作品上傳至Moodle。
HW2利用三角函數畫出正圓形
glVertex2f(cos(angle),sin(angle));
#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();
}
int main(int argc, char*argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT shape");//視窗名稱
glutDisplayFunc(display);
glutMainLoop();
}
Hw3 加上Mouse函式
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
static void display(void)
{
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);
glColor3f(1,x/300.0,y/300.0);//x,y表示滑鼠作標,除以圓大小300*300
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMouseFunc(mouse);//呼叫mouse函式
glutMainLoop();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
static void display(void)
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01){
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void motion(int x,int y)
{
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();//Post個便利貼,告知電腦Re-display
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMotionFunc(motion);//滑鼠拖曳變色函式
glutMainLoop();
return EXIT_SUCCESS;
}
Hw4嘴巴會動的小精靈
加入Motion函式 拖曳(Drag)使圖形動起來
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
float mouth=0;
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//做清除
glBegin(GL_TRIANGLE_FAN);//三角扇形
glVertex2f(0,0);
for(float angle=0 +mouth;angle<=3.1415926*2 - mouth;angle+=3.1415926/100){//
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
void motion(int x,int y)//MOUSE Dragging!!滑鼠拖曳
{
mouth = x/300.0;
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();//Post個便利貼,告知電腦Re-display
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160373 Taiwan Number One");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
沒有留言:
張貼留言