- 作業一 : 用程式做出一個圓
步驟一 : 先打開第一周做過的作業_茶壺。在FB社團的檔案下載。 (freeglut網路上可以免費下載)
步驟二 : 打開freeglut拖曳檔案到桌面,千萬不要直接解壓縮 !
步驟三 : 開啟 CodeBlocks,建立新專案。
步驟四 : 專案類型選擇GLUT Project。
步驟五 : 出現畫面上的視窗,點選 next。
步驟六 : 專案名稱打上「學號」,儲存目的選擇桌面,點選 Next 。
步驟七 : 找出 freeglut下載位置,點選 Next。
步驟八 : 點選 Finish。
步驟九 :將程式輸入到和圖中一樣。
程式 :
#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("04160011 Number 1");
glutDisplayFunc(display);
glutMainLoop();
}
- 作業二 : 用程式改圓的顏色
步驟一 : 先打開剛做過的作業_圓。
步驟二 : 改其中五行程式碼。
程式 :
#include <GL/glut.h>
#include <math.h>
#include <stdio.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);
glColor3f(1,x/300.0,y/300.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160011 Number 1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
#include <math.h>
#include <stdio.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);
glColor3f(1,x/300.0,y/300.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160011 Number 1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
步驟三 : 點圓不同地方會改成不同顏色。
- 作業三 :
步驟一 : 接續上個步驟,改程式碼。
程式 :
#include <GL/glut.h>
#include <math.h>
#include <stdio.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);
}
void motion(int x,int y){
glColor3f(1,x/300.0,y/300.0);
glutPostOverlayRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160011 Number 1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
#include <math.h>
#include <stdio.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);
}
void motion(int x,int y){
glColor3f(1,x/300.0,y/300.0);
glutPostOverlayRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04160011 Number 1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
步驟二 : 按著圓移動滑鼠,可以變色。
- 作業四 : 將圓改成可以張嘴的小精靈
程式 :
#include <GL/glut.h>
#include <math.h>
#include <stdio.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=0 + 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);
}
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("04160011 Number 1");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
步驟二 : 按著滑鼠左鍵拖曳,可改變小精靈嘴巴大小與顏色。















沒有留言:
張貼留言