#include <GL/glut.h>
#include <stdio.h>
void display()
{
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y){
mouse 的函式(左0中1右2,下0/上1,x座標,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("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);mouse 的函式
glutMainLoop();
float teapotX=0, teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
清背景
glPushMatrix();
備份矩陣
glTranslatef(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
備份矩陣
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y){
printf("%f %f\n",(x-150)/150.0,(y-150)/150.0);
teapotX= (x-150)/150.0;
teapotY=-(y-150)/150.0;
讓x:0~300變成-1~+1
讓y:0~300變成-1~+1
如
0~150~300
減150 -150~0~150
除150.0 -1~0~1
利用滑鼠來寫程式
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
畫出多邊形
glVertex2f( (127-150)/150.0,-(44-150)/150.0);
glVertex2f( (13-150)/150.0,-(226-150)/150.0);
glVertex2f( (266-150)/150.0,-(239-150)/150.0);
glVertex2f( (151-150)/150.0,-(283-150)/150.0);
glVertex2f( (36-150)/150.0,-(39-150)/150.0);
glVertex2f( (258-150)/150.0,-(38-150)/150.0);
glEnd();
glutSwapBuffers();
}
void mouse(int button, int state, int x,int y){
if(state==GLUT_DOWN){
當滑鼠按下時印出下面的東西
printf(" glVertex2f( (%d-150)/150.0),-(%d-150)/150.0);\n",x,y);
}
}
這樣就可以少打很多程式碼
加入顏色
glClearColor(16/255.0,99/255.0,152/255.0,1);
glColor3f(211/255.0,123/255.0,55/255.0);
畫出圓形
void display()
{
glClearColor(211/255.0,123/255.0,55/255.0,1);
背景顏色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(180/255.0,77/255.0,169/255.0);
圓形的顏色
for(float angle=0;angle<=2*3.1415;angle+=0.1){
glVertex2f(0.5*cos(angle),0.5*sin(angle));
}
畫圓
glEnd();
glutSwapBuffers();
}
小狗的程式碼
#include <GL/glut.h>
#include <math.h>
void display()
{
glClearColor(255/255.0,255/255.0,255/255.0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);///The black frame of head
glColor3f(0/255.0,0/255.0,0/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.1){
glVertex2f(0.85*cos(angle),0.85*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);///Head
glColor3f(229/255.0,175/255.0,76/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.1){
glVertex2f(0.8*cos(angle),0.8*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);///Nose
glColor3f(0/255.0,0/255.0,0/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.1){
glVertex2f(0.15*cos(angle),0.15*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);///The line between nose and mouth
glVertex2f((155-150)/150.0,-(200-150)/150.0);
glVertex2f((155-150)/150.0,-(150-150)/150.0);
glVertex2f((145-150)/150.0,-(200-150)/150.0);
glVertex2f((145-150)/150.0,-(150-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///The black frame of mouth
glVertex2f( (95-150)/150.0,-(194-150)/150.0);
glVertex2f( (191-150)/150.0,-(192-150)/150.0);
glVertex2f( (194-150)/150.0,-(203-150)/150.0);
glVertex2f( (189-150)/150.0,-(212-150)/150.0);
glVertex2f( (182-150)/150.0,-(227-150)/150.0);
glVertex2f( (178-150)/150.0,-(234-150)/150.0);
glVertex2f( (171-150)/150.0,-(239-150)/150.0);
glVertex2f( (159-150)/150.0,-(242-150)/150.0);
glVertex2f( (151-150)/150.0,-(243-150)/150.0);
glVertex2f( (143-150)/150.0,-(245-150)/150.0);
glVertex2f( (127-150)/150.0,-(240-150)/150.0);
glVertex2f( (122-150)/150.0,-(236-150)/150.0);
glVertex2f( (116-150)/150.0,-(232-150)/150.0);
glVertex2f( (110-150)/150.0,-(227-150)/150.0);
glVertex2f( (105-150)/150.0,-(224-150)/150.0);
glVertex2f( (98-150)/150.0,-(217-150)/150.0);
glVertex2f( (94-150)/150.0,-(211-150)/150.0);
glVertex2f( (92-150)/150.0,-(205-150)/150.0);
glVertex2f( (89-150)/150.0,-(201-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///Mouth
glColor3f(254/255.0,145/255.0,145/255.0);
glVertex2f( (111-150)/150.0,-(198-150)/150.0);
glVertex2f( (188-150)/150.0,-(199-150)/150.0);
glVertex2f( (185-150)/150.0,-(211-150)/150.0);
glVertex2f( (182-150)/150.0,-(220-150)/150.0);
glVertex2f( (176-150)/150.0,-(227-150)/150.0);
glVertex2f( (169-150)/150.0,-(233-150)/150.0);
glVertex2f( (156-150)/150.0,-(239-150)/150.0);
glVertex2f( (148-150)/150.0,-(239-150)/150.0);
glVertex2f( (141-150)/150.0,-(239-150)/150.0);
glVertex2f( (136-150)/150.0,-(237-150)/150.0);
glVertex2f( (128-150)/150.0,-(234-150)/150.0);
glVertex2f( (120-150)/150.0,-(227-150)/150.0);
glVertex2f( (114-150)/150.0,-(222-150)/150.0);
glVertex2f( (110-150)/150.0,-(220-150)/150.0);
glVertex2f( (105-150)/150.0,-(217-150)/150.0);
glVertex2f( (97-150)/150.0,-(200-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///The Black frame of the left ear
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f( (48-150)/150.0,-(75-150)/150.0);
glVertex2f( (67-150)/150.0,-(0-150)/150.0);
glVertex2f( (100-150)/150.0,-(40-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///Left ear
glColor3f(229/255.0,175/255.0,76/255.0);
glVertex2f( (58-150)/150.0,-(56-150)/150.0);
glVertex2f( (69-150)/150.0,-(14-150)/150.0);
glVertex2f( (84-150)/150.0,-(36-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///The black frame of the right ear
glColor3f(0/255.0,0/255.0,0/255.0);
glVertex2f( (204-150)/150.0,-(36-150)/150.0);
glVertex2f( (257-150)/150.0,-(4-150)/150.0);
glVertex2f( (246-150)/150.0,-(66-150)/150.0);
glEnd();
glBegin(GL_POLYGON);///Right ear
glColor3f(229/255.0,175/255.0,76/255.0);
glVertex2f( (213-150)/150.0,-(35-150)/150.0);
glVertex2f( (247-150)/150.0,-(14-150)/150.0);
glVertex2f( (238-150)/150.0,-(53-150)/150.0);
glEnd();
glTranslatef(-0.45,0.2,0);///Left eye(White)
glBegin(GL_POLYGON);
glColor3f(255/255.0,255/255.0,255/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.1*cos(angle),0.1*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);///Left eye(Black)
glColor3f(0/255.0,0/255.0,0/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.06*cos(angle),0.06*sin(angle));
}
glEnd();
glTranslatef(0.9,0,0);///Right eye(White)
glBegin(GL_POLYGON);
glColor3f(255/255.0,255/255.0,255/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.1*cos(angle),0.1*sin(angle));
}
glEnd();
glBegin(GL_POLYGON);///Right eye(Black)
glColor3f(0/255.0,0/255.0,0/255.0);
for(float angle=0;angle<=2*3.1415;angle+=0.01){
glVertex2f(0.06*cos(angle),0.06*sin(angle));
}
glEnd();
glutSwapBuffers();
}
int main(int argc,char ** argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("This is a \"DOGGGGGGG\"");
glutDisplayFunc(display);
glutMainLoop();
}





沒有留言:
張貼留言