顯示具有 01163611_譚崇彣 標籤的文章。 顯示所有文章
顯示具有 01163611_譚崇彣 標籤的文章。 顯示所有文章

2015年6月28日 星期日

20150610_譚崇彣_HW14

複習-基本TRT

#include <GL/glut.h>

float angle1=0;
void display(){
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///Part 0,
        glColor3f(0, 0, 1);
        glutSolidTeapot(0.3);///body base 先做基本的身體, 再藏起來

        glPushMatrix();
            glTranslatef(0.5, 0.15, 0);
            glRotatef(angle1, 0,0,1);
            glTranslatef(0.3, 0, 0);

            glScalef(1, 0.3, 0.3);
            glColor3f(1, 0, 0);
            glutSolidSphere(0.3, 30, 30);
            glPopMatrix();
        glPopMatrix();

    glFlush();
}

void motion(int x, int y){
    angle1=x;
    glutPostRedisplay();
}

int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

20150527_譚崇彣_HW12

#include <GL/glut.h>

float angle=0;
void display(){
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0.6,0.2,0);     ///4.下面全部,都會對壺柄做旋轉
        glRotatef(angle,0,0,1);     ///3.下面全部,都對中間做選轉
        glTranslatef(0.3,0,0);      ///2.茶壺右移,柄在中間
        glutSolidTeapot(0.3);        ///1.做出茶壺
    glPopMatrix();
    glutSolidTeapot(0.3);
    glFlush();
}

void timer(int t){
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}

int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);

    glutMainLoop();
}


帶入函式

#include <GL/glut.h>

float angle=0;

void disBody(){
    glPushMatrix();
        glScalef(1,0.5,0.5);
        glutSolidCube(1);
    glPopMatrix();
}

void disArm(){
    glPushMatrix();
        glScalef(0.6,0.3,0.3);
        glutSolidCube(1);
    glPopMatrix();
}

void display(){
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0.5,0.25,0);     ///4.下面全部,都會對壺柄做旋轉
        glRotatef(angle,0,0,1);     ///3.下面全部,都對中間做選轉
        glTranslatef(0.3,0,0);      ///2.茶壺右移,柄在中間
        disArm();                   ///1.做出茶壺
    glPopMatrix();
    disBody();
    glFlush();
}

void timer(int t){
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}

int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);

    glutMainLoop();
}


帶入手腕

#include <GL/glut.h>

float angle=0, oldX=0, oldY=0;
void disBody(){
    glPushMatrix();
        glScalef(1,0.5,0.5);
        glutSolidCube(1);
    glPopMatrix();
}

void disArm(){
    glPushMatrix();
        glScalef(0.6,0.3,0.3);
        glutSolidCube(1);
    glPopMatrix();
}

void display(){
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///右手
    glPushMatrix();
        glTranslatef(0.5,0,0);     ///4.下面全部,都會對arm做旋轉
        glRotatef(angle,0,0,1);     ///3.下面全部,都對中間做選轉
        glTranslatef(0.25,0,0);      ///2.arm右移,柄在中間
        disArm();                   ///1.做出arm
        glPushMatrix();
            glTranslatef(0.25,0,0);     ///4.下面全部,都會對arm做旋轉
            glRotatef(angle,0,0,1);     ///3.下面全部,都對中間做選轉
            glTranslatef(0.25,0,0);      ///2.arm右移,柄在中間
            disArm();                   ///1.做出arm
        glPopMatrix();
    glPopMatrix();
    ///左手
    glPushMatrix();
        glTranslatef(-0.5,0,0);     ///4.下面全部,都會對arm做旋轉
        glRotatef(-angle,0,0,1);     ///3.下面全部,都對中間做選轉
        glTranslatef(-0.25,0,0);      ///2.arm右移,柄在中間
        disArm();                   ///1.做出arm
        glPushMatrix();
            glTranslatef(-0.25,0,0);     ///4.下面全部,都會對arm做旋轉
            glRotatef(-angle,0,0,1);     ///3.下面全部,都對中間做選轉
            glTranslatef(-0.25,0,0);      ///2.arm右移,柄在中間
            disArm();                   ///1.做出arm
        glPopMatrix();
    glPopMatrix();
    disBody();
    glFlush();
}

void motion(int x, int y){
    angle-=y-oldY;
    oldY=y; oldX=x;
}

void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) {
        oldX=x; oldY=y;
    }
}

void timer(int t){
    glutTimerFunc(20,timer,t+1);
    //angle++;
    glutPostRedisplay();
}

int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

20150520_譚崇彣_HW11

#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();
}

2015年6月11日 星期四

20150513_譚崇彣_HW10

#include "glm.h"            ///引入glm.h和glm.c(要copy到目錄)
GLMmodel * pmodel = NULL;

void display(){
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glFlush();
}

int main(int argc, char**argv){
    pmodel=glmReadOBJ("porsche.obj");
    glmUnitize(pmodel);
    glmFacetNormals(pmodel);
    glmVertexNormals(pmodel, 90.0);

    glutInit(&argc, argv);
    glutCreateWindow("carrrrrrrrr!!");
    glutDisplayFunc(display);
    glutMainLoop();
}


打燈

#include "glm.h"
GLMmodel * pmodel = NULL;

void display(){
    GLfloat pos[] = {0.0, 0.0, -1.0, 0.0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glFlush();
}

int main(int argc, char**argv){
    pmodel=glmReadOBJ("porsche.obj");
    glmUnitize(pmodel);
    glmFacetNormals(pmodel);
    glmVertexNormals(pmodel, 90.0);

    glutInit(&argc, argv);
    glutCreateWindow("carrrrrrrrr!!");
    glutDisplayFunc(display);
    glutMainLoop();
}

2015年6月3日 星期三

20150603_譚崇彣_HW13






#include <GL/glut.h>
#include <stdio.h>
FILE * fout = NULL;

float oldX=0, oldY=0;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;
void motion(int x, int y)
{   angle[now] -= y - oldY;
    oldY=y; oldX=x;
    for(int i=0;i<10;i++){
        printf("%.1f\t",angle[i]);
    }
    printf("\n");
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') now=1;
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    glutPostRedisplay();
}
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    { ///Lighting
    glColor3ub(166,132,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle[1],0,0,1);///整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        glColor3f(0,0,1);
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle[2],0,0,1);//把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            glColor3f(0,1,1);
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(-angle[3],0,0,1);//把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        glColor3f(1,0,1);
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(-angle[4],0,0,1);//把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            glColor3f(1,0,0);
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}

void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}