2016年3月14日 星期一

廖婕珊hw4

平移/旋轉/縮放
Translate / Rotate / Scale
glTranslatef(x,y,z);
glRotatef(angle, x,y,z);
glScalef(x, y, z);

mouse的操作

按小視窗又鍵--編輯--標記
可以選取--enter複製

#include <GL/glut.h>
#include <stdio.h>
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);
}
void display()
{
    glClearColor(150/255.0,170/255.0,55/255.0,1); //背景色
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3ub(175,193,81);
    glBegin(GL_POLYGON);
        glVertex2f((140-150)/150.0,-(75-150)/150.0);
        glVertex2f((79-150)/150.0,-(158-150)/150.0);
        glVertex2f((149-150)/150.0,-(223-150)/150.0);
        glVertex2f((226-150)/150.0,-(160-150)/150.0);
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Hello3D");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}

自己做的

老師的截圖


做圓



#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float nowX=0, nowY=0,now=0;
void mouse(int button,int state,int x,int y)
{
    nowX=(x-150)/150.0; nowY=-(y-150)/150.0;
    now+=0.1;
}
void display()
{
    glClearColor(150/255.0,170/255.0,55/255.0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3ub(175,193,81);

    glPushMatrix();
        glBegin(GL_POLYGON);
        for(float angle=0;angle<3.14159265357989*2;angle+=0.1){
            glVertex2f(nowX+0.4*cos(angle),nowY+0.4*sin(angle));
        }
        glEnd();
    glPopMatrix();

    glColor3ub(128,161,31);
    glPushMatrix();
        glBegin(GL_POLYGON);
        glVertex2f(nowX,nowY);
        for(float angle=0;angle<now;angle+=0.1){
            glVertex2f(nowX+0.4*cos(angle),nowY+0.4*sin(angle));
        }
        glEnd();
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Hello3D");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();


}

沒有留言:

張貼留言