2016年3月15日 星期二

Week 04 魏珩

作業一:

作業互評
1.


2.


3.


4.


5.



作業二:

輸入網址:jsyeh.org/3dcg10
下載data、windows、glut32.dll

解壓縮,data、glut32.dll放到windows裡面,點選Transformation

打開執行

一開始畫面


glTranslatef(左右,上下,前後)




glRotatef(旋轉,X,Y,Z),右手定理



glScalef調大小(X,Y,Z)





作業三:
使茶壺按一下滑鼠,往左、右、上、下移動.

程式碼:
#include <GL/glut.h>

void display()
{
    glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glTranslatef(0, -0.1, 0);///按一下往下0.1
    glColor3f(1,0,0);
    glutSolidTeapot(0.3);


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

    glutDisplayFunc(display);


    glutMainLoop();
}





使滑鼠點一下,茶壺移到滑鼠所在位置

程式碼:
#include <GL/glut.h>
float nowX=0, nowY=0;
void mouse(int button, int state, int x, int y)
{
    nowX = (x-150)/150.0; nowY=-(y-150)/150.0;
}
void display()
{
    glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();///備份Matrix
        glTranslatef(nowX, nowY, 0);///按一下往下0.1
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原Matrix

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

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}






畫圓

程式碼:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float nowX=0, nowY=0;
void mouse(int button, int state, int x, int y)
{
    nowX = (x-150)/150.0; nowY=-(y-150)/150.0;
    printf("glTranslatef(%f,%f,0);\n", nowX, nowY);
}
void display()
{
    glClearColor(0,0.5,0.3,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(nowX, nowY, 0);
        glColor3f(1,0,0);

        glBegin(GL_POLYGON);
            for(float angle=0; angle<3.14159265358979*2; angle+=0.1)
            {
                glVertex2f( 0.05*cos(angle), 0.05*sin(angle));
            }
        glEnd();
    glPopMatrix();

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

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}

一樣可以用滑鼠來控制原點。





可以印出圓點的位置

沒有留言:

張貼留言