2015年5月20日 星期三

Week13 課堂作業, 許志遙

以前在畫茶壺時,大小為0~1之間,並且調整畫面比例會變掉,於是有了以下的方法......


#include <GL/glut.h>

void display(){
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("Camera");
    glutDisplayFunc(display);
    glViewport(0,0,800,600);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-400,400,-300,300,-1000,1000);
    glMatrixMode(GL_MODELVIEW);

    glutMainLoop();
}

但是上訴的方法在手動調整視窗的時候茶壺又變形了,於是又有了下面的方法


#include <GL/glut.h>

void display(){
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}
void reshape(int w, int h){
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("Camera");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
}

加入計時器,自動轉


#include <GL/glut.h>

float eyeX=-100, eyeY=80, eyeZ=-200;
void display(){
    GLfloat pos[] = {0,0,200,0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);     ///打燈光
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eyeZ, 0,0,0, 0,1,0);
        glPushMatrix();
            glutSolidTeapot(100);
        glPopMatrix();
    glPopMatrix();
    glFlush();
}

void motion(int x, int y){                    ///eye-移動
    eyeX=-400; eyeY=300-y;
    glutPostRedisplay();
}

void reshape(int w, int h){
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.001,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void timer(int t){                          ///計時
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300) eyeY+=4;
    glutPostRedisplay();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("Camera");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);

    glutMainLoop();
}




沒有留言:

張貼留言