電腦圖學 第03週 2020-03-18
0. Zuvio複習上週教的內容(點、線、面、色彩)
1. 主題: Mouse函式
1.1. 座標系統 & 小畫家結合
1.2. 利用Mouse來寫程式
2. 主題: Motion函式
3. 主題: 移動 glTranslatef(x,y,z)
4. 作業: 用Mouse來寫程式(描點畫圖)
0. Zuvio複習上週教的內容(點、線、面、色彩)
1. 主題: Mouse函式
1.1. 座標系統 & 小畫家結合
1.2. 利用Mouse來寫程式
2. 主題: Motion函式
3. 主題: 移動 glTranslatef(x,y,z)
4. 作業: 用Mouse來寫程式(描點畫圖)
課堂作業1: Zuvio複習上週(點線面色彩)
this is copy from last week and change the name.
this is new, print mouse's position.
#include <stdio.h>
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
use mouse to move the teapot and i still add the "printf", so it can print the position.
this is how it look like.
we can see the teapot can be moved by the mouse.
#include <stdio.h>
float teapotX=0,teapotY=0;//用變數+1-1表示坐標系
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();//備份矩陣
//glTranslatef(x,y,z);//會移動xyz
glTranslatef(teapotX,teapotY,0);//移動
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=-(y-150)/150.0; //因為Y會反過來所以加-號
printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f((227-150)/150.0,-(132-150)/150.0);//227,132
glVertex2f((151-150)/150.0,-(115-150)/150.0);//151,115
glVertex2f((85-150)/150.0,-(141-150)/150.0);//85,141
glVertex2f((118-150)/150.0,-(188-150)/150.0);//118,188
glVertex2f((180-150)/150.0,-(186-150)/150.0);//180,186
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
use for loop to draw a circle.

glBegin(GL_POLYGON);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( cos(angle), sin(angle));
}
glEnd();

#include <GL/glut.h>
#include <math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
/* glBegin(GL_POLYGON);
glVertex2f((227-150)/150.0,-(132-150)/150.0);//227,132
glVertex2f((151-150)/150.0,-(115-150)/150.0);//151,115
glVertex2f((85-150)/150.0,-(141-150)/150.0);//85,141
glVertex2f((118-150)/150.0,-(188-150)/150.0);//118,188
glVertex2f((180-150)/150.0,-(186-150)/150.0);//180,186*/
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.1*cos(angle)-0.2, 0.3*sin(angle)+0.5);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.1*cos(angle)+0.2, 0.3*sin(angle)+0.5);
}
glEnd();
glBegin(GL_POLYGON);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.8*cos(angle), 0.5*sin(angle)-0.1);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.6*cos(angle), 0.8*sin(angle)-0.9);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.6*cos(angle), 0.8*sin(angle)-0.9);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)+0.5, 0.15*sin(angle)-0.1);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)-0.5, 0.15*sin(angle)-0.16);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)-0.45, 0.15*sin(angle)-0.13);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,1,1);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.26*cos(angle)+0.026, 0.3*sin(angle)-0.15);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,1,1);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.26*cos(angle)+0.026, 0.4*sin(angle)-0.9);
}
glEnd();
glBegin(GL_LINES);
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f((128-150)/150.0,-(128-150)/150.0);//128,128
glVertex2f((108-150)/150.0,-(139-150)/150.0);//108,139
glEnd();
glBegin(GL_LINES);
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f((166-150)/150.0,-(123-150)/150.0);//166,123
glVertex2f((191-150)/150.0,-(139-150)/150.0);//191,139
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((121-150)/150.0,-(157-150)/150.0);//121,157
glVertex2f((38-150)/150.0,-(139-150)/150.0);//38,139
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((118-150)/150.0,-(184-150)/150.0);//118,184
glVertex2f((44-150)/150.0,-(198-150)/150.0);//44,198
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((192-150)/150.0,-(184-150)/150.0);//192,184
glVertex2f((253-150)/150.0,-(202-150)/150.0);//253,202
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((189-150)/150.0,-(155-150)/150.0);//189,155
glVertex2f((267-150)/150.0,-(149-150)/150.0);//267,149
glEnd();
glutSwapBuffers();
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMainLoop();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f((227-150)/150.0,-(132-150)/150.0);//227,132
glVertex2f((151-150)/150.0,-(115-150)/150.0);//151,115
glVertex2f((85-150)/150.0,-(141-150)/150.0);//85,141
glVertex2f((118-150)/150.0,-(188-150)/150.0);//118,188
glVertex2f((180-150)/150.0,-(186-150)/150.0);//180,186
glEnd();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
use for loop to draw a circle.

glBegin(GL_POLYGON);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( cos(angle), sin(angle));
}
glEnd();

#include <GL/glut.h>
#include <math.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
/* glBegin(GL_POLYGON);
glVertex2f((227-150)/150.0,-(132-150)/150.0);//227,132
glVertex2f((151-150)/150.0,-(115-150)/150.0);//151,115
glVertex2f((85-150)/150.0,-(141-150)/150.0);//85,141
glVertex2f((118-150)/150.0,-(188-150)/150.0);//118,188
glVertex2f((180-150)/150.0,-(186-150)/150.0);//180,186*/
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.1*cos(angle)-0.2, 0.3*sin(angle)+0.5);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.1*cos(angle)+0.2, 0.3*sin(angle)+0.5);
}
glEnd();
glBegin(GL_POLYGON);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.8*cos(angle), 0.5*sin(angle)-0.1);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.6*cos(angle), 0.8*sin(angle)-0.9);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,199/255.0,207/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.6*cos(angle), 0.8*sin(angle)-0.9);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)+0.5, 0.15*sin(angle)-0.1);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)-0.5, 0.15*sin(angle)-0.16);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(252/255.0,172/255.0,185/255.0);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.15*cos(angle)-0.45, 0.15*sin(angle)-0.13);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,1,1);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.26*cos(angle)+0.026, 0.3*sin(angle)-0.15);
}
glEnd();
glBegin(GL_POLYGON);
glColor3f(1,1,1);
for(float angle=0; angle<=3.1415926*2; angle+=0.01){
glVertex2f( 0.26*cos(angle)+0.026, 0.4*sin(angle)-0.9);
}
glEnd();
glBegin(GL_LINES);
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f((128-150)/150.0,-(128-150)/150.0);//128,128
glVertex2f((108-150)/150.0,-(139-150)/150.0);//108,139
glEnd();
glBegin(GL_LINES);
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f((166-150)/150.0,-(123-150)/150.0);//166,123
glVertex2f((191-150)/150.0,-(139-150)/150.0);//191,139
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((121-150)/150.0,-(157-150)/150.0);//121,157
glVertex2f((38-150)/150.0,-(139-150)/150.0);//38,139
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((118-150)/150.0,-(184-150)/150.0);//118,184
glVertex2f((44-150)/150.0,-(198-150)/150.0);//44,198
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((192-150)/150.0,-(184-150)/150.0);//192,184
glVertex2f((253-150)/150.0,-(202-150)/150.0);//253,202
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f((189-150)/150.0,-(155-150)/150.0);//189,155
glVertex2f((267-150)/150.0,-(149-150)/150.0);//267,149
glEnd();
glutSwapBuffers();
}
int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week 03");
glutDisplayFunc(display);
glutMainLoop();
}






沒有留言:
張貼留言