2020年3月17日 星期二

week03想睡嘞

week3
上週教的!!!
程式碼:
#include <GL/glut.h>///為了 glutXXXX()還有glXXXXX
#include <stdio.h>///TODO: 為了printf()
void display()
{
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();

}
void mouse(int button,int state,int x,int y)///TODO:
{///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);///display的函式
    glutMouseFunc(mouse);///TODO:mouse的函式
    glutMainLoop();

}
*滑鼠點一下和放開的座標
程式碼:
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0, teapotY=0; ///這裡是-1.0~ =1.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;
    ///                0....150...300 
   ///減150     -150...0.....150
  ///除150      -1.......0.......1

}
int main(int argc,  char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}
*滑鼠點到哪裡,茶壺的位置就變到哪,原理是清除畫面,重新畫一個
 

程式碼:

#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON); // 開始畫圖形
// 程式碼產生的頂點
glVertex2f((83-150)/150.0,-(57-150)/150.0); // 將執行過後的程式碼複製至此,即可畫出圖形
glVertex2f((136-150)/150.0,-(126-150)/150.0);
glVertex2f((192-150)/150.0,-(170-150)/150.0);
glVertex2f((115-150)/150.0,-(246-150)/150.0);
glVertex2f((122-150)/150.0,-(174-150)/150.0);
glVertex2f((66-150)/150.0,-(131-150)/150.0);
glVertex2f((42-150)/150.0,-(261-150)/150.0);
glVertex2f((31-150)/150.0,-(91-150)/150.0);
glVertex2f((19-150)/150.0,-(52-150)/150.0);
glEnd(); // 程式結束

glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
if(state==GLUT_DOWN) { // 按下去時產生座標,減一半,除一半.0,y 要加負號
printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y); // 顯示
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();

}



我的作業
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float teapotX=0,teapotY=0;
void display()
{
    glClearColor(137/255.0,32/255.5,154/255.0,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
    glBegin(GL_POLYGON);///開始畫
    glColor3f((250/255.0),(223/255.0),(91/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.5*cos(angle),0.5*sin(angle));
            }
    glEnd();///結束畫

    glBegin(GL_POLYGON);///開始畫
    glColor3f((228/255.0),(55/255.0),(56/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.3*cos(angle),0.4*sin(angle));
            }
    glEnd();///結束畫
    glBegin(GL_POLYGON);///開始畫
    glColor3f((250/255.0),(223/255.0),(91/255.0));
        for(float angle=0 ; angle<=3.1415926 * 1 ;angle+=0.01)
            {
                glVertex2f(0.3*cos(angle),0.4*sin(angle));
            }
    glEnd();///結束畫
    glBegin(GL_POLYGON);///開始畫
    glColor3f((50/255.0),(67/255.0),(160/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.05*cos(angle)-0.2,0.05*sin(angle)+0.15);
            }
    glEnd();///結束畫
    glBegin(GL_POLYGON);///開始畫
    glColor3f((50/255.0),(67/255.0),(160/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.05*cos(angle)+0.2,0.05*sin(angle)+0.15);
            }
    glEnd();///結束畫
    glBegin(GL_POLYGON);///開始畫
    glColor3f((255/255.0),(255/255.0),(255/255.0));
        glVertex2f( (105-150)/150.0, -(150-150)/150.0);
        glVertex2f( (195-150)/150.0, -(150-150)/150.0);
        glVertex2f( (195-150)/150.0, -(163-150)/150.0);
        glVertex2f( (105-150)/150.0, -(163-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);
    }
}
int main(int argc,char ** argv)
{
    glutInit( &argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();

}


史努比
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float teapotX=0,teapotY=0;
void display()
{
    glClearColor(251/255.0,184/255.5,31/255.0,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
    glBegin(GL_POLYGON);///開始畫
     glColor3f((255/255.0),(255/255.0),(255/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.7*cos(angle),0.45*sin(angle));
            }
    glEnd();///結束畫

    glBegin(GL_POLYGON);///開始畫
     glColor3f((255/255.0),(255/255.0),(255/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.42*cos(angle)+0.25,0.35*sin(angle)+0.35);
            }
             glVertex2f( (182-150)/150.0, -(181-150)/150.0);
 glVertex2f( (117-150)/150.0, -(87-150)/150.0);
 glVertex2f( (121-150)/150.0, -(83-150)/150.0);
 glVertex2f( (125-150)/150.0, -(80-150)/150.0);
 glVertex2f( (128-150)/150.0, -(77-150)/150.0);
 glVertex2f( (132-150)/150.0, -(74-150)/150.0);
 glVertex2f( (137-150)/150.0, -(71-150)/150.0);
 glVertex2f( (137-150)/150.0, -(66-150)/150.0);
 glVertex2f( (133-150)/150.0, -(69-150)/150.0);
 glVertex2f( (131-150)/150.0, -(74-150)/150.0);
 glVertex2f( (129-150)/150.0, -(76-150)/150.0);
 glVertex2f( (125-150)/150.0, -(80-150)/150.0);
 glVertex2f( (122-150)/150.0, -(84-150)/150.0);
 glVertex2f( (120-150)/150.0, -(87-150)/150.0);
 glVertex2f( (238-150)/150.0, -(69-150)/150.0);
 glVertex2f( (243-150)/150.0, -(78-150)/150.0);
 glVertex2f( (247-150)/150.0, -(85-150)/150.0);
 glVertex2f( (250-150)/150.0, -(91-150)/150.0);
 glVertex2f( (253-150)/150.0, -(98-150)/150.0);
 glVertex2f( (237-150)/150.0, -(69-150)/150.0);
 glVertex2f( (241-150)/150.0, -(79-150)/150.0);
 glVertex2f( (245-150)/150.0, -(84-150)/150.0);
 glVertex2f( (249-150)/150.0, -(90-150)/150.0);
 glVertex2f( (253-150)/150.0, -(95-150)/150.0);
    glEnd();///結束畫

      glBegin(GL_POLYGON);///開始畫
     glColor3f((255/255.0),(255/255.0),(255/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.35*cos(angle)+0.5,0.45*sin(angle));
            }
    glEnd();///結束畫


    glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.12*cos(angle)-0.2,0.08*sin(angle));
            }
    glEnd();///結束畫

    glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.15*cos(angle)+0.58,0.27*sin(angle)-0.05);
            }
    glEnd();///結束畫

    glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.05*cos(angle)+0.2,0.09*sin(angle)+0.2);
            }
    glEnd();///結束畫

    glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
        for(float angle=0 ; angle<=3.1415926 * 2 ;angle+=0.01)
            {
                glVertex2f(0.05*cos(angle)-0.07,0.09*sin(angle)+0.2);
            }
    glEnd();///結束畫

     glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
 glVertex2f( (147-150)/150.0, -(190-150)/150.0);
 glVertex2f( (154-150)/150.0, -(192-150)/150.0);
 glVertex2f( (162-150)/150.0, -(191-150)/150.0);
 glVertex2f( (170-150)/150.0, -(190-150)/150.0);
 glVertex2f( (176-150)/150.0, -(186-150)/150.0);
 glVertex2f( (181-150)/150.0, -(183-150)/150.0);
 glVertex2f( (147-150)/150.0, -(189-150)/150.0);
 glVertex2f( (151-150)/150.0, -(189-150)/150.0);
 glVertex2f( (157-150)/150.0, -(189-150)/150.0);
 glVertex2f( (165-150)/150.0, -(187-150)/150.0);
 glVertex2f( (173-150)/150.0, -(184-150)/150.0);
 glVertex2f( (179-150)/150.0, -(182-150)/150.0);
 glVertex2f( (183-150)/150.0, -(180-150)/150.0);

   glVertex2f( (173-150)/150.0, -(185-150)/150.0);
 glVertex2f( (176-150)/150.0, -(182-150)/150.0);
 glVertex2f( (178-150)/150.0, -(181-150)/150.0);
 glVertex2f( (179-150)/150.0, -(182-150)/150.0);
 glVertex2f( (179-150)/150.0, -(184-150)/150.0);
 glVertex2f( (175-150)/150.0, -(186-150)/150.0);
 glVertex2f( (173-150)/150.0, -(186-150)/150.0);

  glVertex2f( (179-150)/150.0, -(183-150)/150.0);
 glVertex2f( (181-150)/150.0, -(180-150)/150.0);

  glVertex2f( (150-150)/150.0, -(188-150)/150.0);
 glVertex2f( (155-150)/150.0, -(191-150)/150.0);
 glVertex2f( (164-150)/150.0, -(192-150)/150.0);
 glVertex2f( (170-150)/150.0, -(190-150)/150.0);
 glVertex2f( (177-150)/150.0, -(185-150)/150.0);
 glVertex2f( (182-150)/150.0, -(180-150)/150.0);
   glEnd();///結束畫

   glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
 glVertex2f( (118-150)/150.0, -(78-150)/150.0);
 glVertex2f( (123-150)/150.0, -(76-150)/150.0);
 glVertex2f( (129-150)/150.0, -(77-150)/150.0);
 glVertex2f( (137-150)/150.0, -(76-150)/150.0);
 glVertex2f( (143-150)/150.0, -(76-150)/150.0);
 glVertex2f( (146-150)/150.0, -(76-150)/150.0);
  glVertex2f( (120-150)/150.0, -(76-150)/150.0);
 glVertex2f( (124-150)/150.0, -(76-150)/150.0);
 glVertex2f( (126-150)/150.0, -(76-150)/150.0);
 glVertex2f( (131-150)/150.0, -(76-150)/150.0);
 glVertex2f( (132-150)/150.0, -(76-150)/150.0);

  glVertex2f( (118-150)/150.0, -(76-150)/150.0);
 glVertex2f( (126-150)/150.0, -(75-150)/150.0);
 glVertex2f( (131-150)/150.0, -(75-150)/150.0);
 glVertex2f( (137-150)/150.0, -(76-150)/150.0);
 glVertex2f( (142-150)/150.0, -(76-150)/150.0);

glEnd();///結束畫

   glBegin(GL_POLYGON);///開始畫
     glColor3f((0/255.0),(0/255.0),(0/255.0));
 glVertex2f( (164-150)/150.0, -(76-150)/150.0);
 glVertex2f( (174-150)/150.0, -(76-150)/150.0);
 glVertex2f( (180-150)/150.0, -(75-150)/150.0);
 glVertex2f( (183-150)/150.0, -(75-150)/150.0);
 glVertex2f( (186-150)/150.0, -(76-150)/150.0);
 glVertex2f( (191-150)/150.0, -(75-150)/150.0);

  glVertex2f( (171-150)/150.0, -(77-150)/150.0);
 glVertex2f( (174-150)/150.0, -(75-150)/150.0);
 glVertex2f( (181-150)/150.0, -(73-150)/150.0);
 glVertex2f( (184-150)/150.0, -(73-150)/150.0);
 glVertex2f( (188-150)/150.0, -(74-150)/150.0);
 glVertex2f( (193-150)/150.0, -(76-150)/150.0);

  glVertex2f( (169-150)/150.0, -(74-150)/150.0);
 glVertex2f( (173-150)/150.0, -(74-150)/150.0);
 glVertex2f( (179-150)/150.0, -(73-150)/150.0);
 glVertex2f( (189-150)/150.0, -(74-150)/150.0);
 glVertex2f( (193-150)/150.0, -(76-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);
    }
}
int main(int argc,char ** argv)
{
    glutInit( &argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();

}



沒有留言:

張貼留言