2015 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2015年6月17日 星期三
WEEK16
#include <GL/glut.h>
float angle=0, oldX=0, oldY=0;
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_LIGHTING);
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, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm();///這是個Arm ver 1 Right Upper Arm
glPushMatrix();
glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(angle, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm(); ///Right Lower Arm
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(-angle, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm();///這是個Arm ver 1 Left Upper Arm
glPushMatrix();
glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(-angle, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
drawArm(); ///Left Lower Arm
glPopMatrix();
glPopMatrix();
drawBody();
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();
}
WEEK15
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL; ///要存檔用的啦!
float oldX=0, oldY=0;
float angle[8]={0};
int now;
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_LIGHTING);
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,0);
drawArm(); ///Right Lower Arm
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
glRotatef(-angle[3], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
glColor3f(0.5,0.5,1);
glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
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(0.5,1,0.5);
drawArm(); ///Left Lower Arm
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{ angle[now] -= y - oldY;
oldY=y; oldX=x;
if(fout==NULL)fout=fopen("a.txt","w+"); ///要存檔用的啦!
for(int i=0;i<8;i++)
{
fprintf(fout,"%.1f\t",angle[i]);
}
fprintf(fout,"\n"); ///要存檔用的啦!
}
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);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
WEEK14
#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); ///step4:把"會對茶把手做旋轉的東西"再移到右上 掛上去
glRotatef(angle,0,0,1); ///step3:把茶壺都對中間旋轉
glTranslatef(0.3,0,0); ///step2:往右移
glutSolidTeapot(0.3); ///step1:這是一個茶壺
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 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()
{
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); ///step4:把"會對茶把手做旋轉的東西"再移到右上 掛上去
glRotatef(angle,0,0,1); ///step3:把茶壺都對中間旋轉
glTranslatef(0.3,0,0); ///step2:往右移*/
drawArm(); ///step1:這是一個茶壺
glPopMatrix();
drawBody();
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 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()
{
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); ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
glRotatef(angle,0,0,1); ///step3:把下面整團都對中間旋轉
glTranslatef(0.25,0,0); ///step2:把他往右移,Arm Joint在中間
drawArm(); ///step1:這是一個Arm
glPushMatrix();
glTranslatef(0.25,0,0); ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
glRotatef(angle,0,0,1); ///step3:把下面整團都對中間旋轉
glTranslatef(0.25,0,0); ///step2:把他往右移,Arm Joint在中間
drawArm(); ///step1:這是一個Arm
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0); ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
glRotatef(-angle,0,0,1); ///step3:把下面整團都對中間旋轉
glTranslatef(-0.25,0,0); ///step2:把他往右移,Arm Joint在中間
drawArm(); ///step1:這是一個Arm
glPushMatrix();
glTranslatef(-0.25,0,0); ///step4:把"會對Arm Joint做旋轉的東西"再移到右上 掛上去
glRotatef(-angle,0,0,1); ///step3:把下面整團都對中間旋轉
glTranslatef(-0.25,0,0); ///step2:把他往右移,Arm Joint在中間
drawArm(); ///step1:這是一個Arm
glPopMatrix();
glPopMatrix();
drawBody();
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();
}
WEEK13
#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)
{
eyeX=x-400;eyeY=300-y;
glutPostRedisplay();
}
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();
}
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("3D");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(motion);
glutTimerFunc(0,timer,0);
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glOrtho(-400,400,-300,300,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
WEEK11
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
if(potState==0)potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
potState=1;
}
else if(state==GLUT_UP)
{
potState=2;
potVX=-(x-350)/3500.0;
potVY=(y-350)/3500.0;
}
}
void motion(int x,int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
if(potState==0)potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
potState=1;
}
else if(state==GLUT_UP)
{
potState=2;
potVX=-(x-350)/3500.0;
potVY=(y-350)/3500.0;
}
}
void motion(int x,int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
訂閱:
文章 (Atom)






