2015 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2015年6月18日 星期四
2015年6月10日 星期三
2015/6/10week16 課堂作業 黃志楷
目標:複習移動,旋轉,縮放
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0,0,1);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5,0.15,0);
glRotatef(angle,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)
{
angle=x;
glutPostRedisplay();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutCreateWindow("02160421");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
目標:寫檔讀檔
#include <GL/glut.h>
#include <stdio.h>
float oldX=0, oldY=0;
float angle[10];
int now=0;
int i;
FILE* f=NULL;
FILE* f1=NULL;
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
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
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
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
drawArm(); ///Left Lower Arm
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{ angle[now] -= y - oldY;
oldY=y; oldX=x;
if(f==NULL)f=fopen("02160421.txt","w+");
for(i=0;i<4;i++)
{
fprintf(f,"%f\t",angle[i]);
}
fprintf(f,"\n");
}
void timerplay(int t)
{
glutTimerFunc(500,timerplay,t+1);
if(f1==NULL)f1=fopen("02160421.txt","r");
for(i=0;i<4;i++)
{
fscanf(f1,"%f",&angle[i]);
}
glutPostRedisplay();
}
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;
if(key=='p')glutTimerFunc(10,timerplay,0);
}
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);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
2015年6月3日 星期三
2015/6/3week15 課堂作業 黃志楷
目標:茶壺在特定位置旋轉
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x1, y1, 0);//要掛在哪裡?
glRotatef(angle, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslatef(x2, y2, 0);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)//可以進行監控
{
if(key=='1') x1+=0.1;
if(key=='2') x1-=0.1;
if(key=='3') y1+=0.1;
if(key=='4') y1-=0.1;
if(key=='5') angle+=10;
if(key=='6') angle-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
目標:一帶一控制(大包小)
#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0,angle2=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(x2, y2, 0);
glRotatef(angle, 0,0,1);
glColor3f(1,0,0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(x1, y1, 0);//要掛在哪裡?
glRotatef(angle2, 0,0,1);
glTranslatef(-0.25, -0.11, 0);
glColor3f(1,1,0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') x1+=0.1;
if(key=='2') x1-=0.1;
if(key=='3') y1+=0.1;
if(key=='4') y1-=0.1;
if(key=='5') angle+=10;
if(key=='6') angle-=10;
if(key=='7')angle2+=10;
if(key=='8')angle2-=10;
printf("%f %f\n", x1, y1);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("Robot3D");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
目標:自由控制關節
#include <GL/glut.h>
float angle=0, oldX=0, oldY=0,angle2=0,angle3=0,angle4=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(angle2, 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(-angle3, 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(-angle4, 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 keyboard(unsigned char key,int x,int y)
{
if(key=='1')angle++;
if(key=='2')angle2++;
if(key=='3')angle3++;
if(key=='4')angle4++;
if(key=='5')angle--;
if(key=='6')angle2--;
if(key=='7')angle3--;
if(key=='8')angle4--;
}
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);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
目標:用陣列來控制關節
#include <GL/glut.h>
float oldX=0, oldY=0;
float angle[10];
int now=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[1], 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[2], 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[3], 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[4], 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[now] -= y - oldY;
oldY=y; oldX=x;
}
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;
}
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);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
目標:將數值匯出檔案
#include <GL/glut.h>
#include <stdio.h>
float oldX=0, oldY=0;
float angle[10];
int now=0;
int i;
FILE* f=NULL;
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
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
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
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
drawArm(); ///Left Lower Arm
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{ angle[now] -= y - oldY;
oldY=y; oldX=x;
if(f==NULL)f=fopen("02160421.txt","w+");
for(i=0;i<4;i++)
{
fprintf(f,"%.1f\t",angle[i]);
}
fprintf(f,"\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;
}
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);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
}
2015年5月27日 星期三
2015/5/27week14 課堂作業 黃志楷
目標:茶壺旋轉並移動旋轉軸
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);///移動位置
glRotatef(angle,0,0,1);///旋轉
glTranslatef(0.3,0,0);///移動旋轉軸
glutSolidTeapot(0.3);///畫出茶壺
glPopMatrix();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutCreateWindow("02160421");
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);///移動位置
glRotatef(angle,0,0,1);///旋轉
glTranslatef(0.3,0,0);///移動旋轉軸
drawarm();
glPopMatrix();
drawbody();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20,timer,t+1);
angle++;
glutPostRedisplay();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutCreateWindow("02160421");
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);///移動位置
glRotatef(angle,0,0,1);///旋轉
glTranslatef(0.25,0,0);///移動旋轉軸
drawarm();
glPushMatrix();
glTranslatef(0.25,0,0);///移動位置
glRotatef(angle,0,0,1);///旋轉
glTranslatef(0.25,0,0);///移動旋轉軸
drawarm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5,0,0);///移動位置
glRotatef(-angle,0,0,1);///旋轉
glTranslatef(-0.25,0,0);///移動旋轉軸
drawarm();
glPushMatrix();
glTranslatef(-0.25,0,0);///移動位置
glRotatef(-angle,0,0,1);///旋轉
glTranslatef(-0.25,0,0);///移動旋轉軸
drawarm();
glPopMatrix();
glPopMatrix();
drawbody();
glFlush();
}
void timer(int t)
{
glutTimerFunc(20,timer,t+1);
//angle++;
glutPostRedisplay();
}
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;}
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutCreateWindow("02160421");
glutDisplayFunc(display);
glutTimerFunc(20,timer,0);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
2015年5月20日 星期三
2015/5/20week13 課堂作業 黃志楷
今日目標:投影矩陣,攝影機運鏡
目標:垂直投影
#include<GL/glut.h>
void display()
{
glPushMatrix();
glutSolidTeapot(100);
glPopMatrix();
glFlush();
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitWindowSize(800,600);
glutCreateWindow("02160421");
glutDisplayFunc(display);
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glOrtho(-400,400,-300,300,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
目標:拖動視窗大小,物件不會變動大小
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(400,300);
glutCreateWindow("02160421");
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)
{
eyex=x-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,10000);
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("02160421");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(motion);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
2015年5月13日 星期三
2015/5/13 week12 課堂作業 黃志楷
目標:讀入3D模型
#include "glm.h"///宣告glm.h
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("02160421");
glutDisplayFunc(display);
glutMainLoop();
}
目標:加入光線
#include "glm.h"///宣告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("02160421");
glutDisplayFunc(display);
glutMainLoop();
}
2015年5月6日 星期三
2015/5/6 week11 課堂作業 黃志楷
目標:球體等速度旋轉
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
float angle=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("1.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
void timer(int t)
{
glutTimerFunc(20,timer,0);///重複呼叫時間
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(0,timer,0);///呼叫時間
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
目標:位置會規律移動
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include<math.h>
float angle=0,potx=0,poty=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glTranslatef(potx,poty,0);
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("1.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
void timer(int t)
{
glutTimerFunc(10,timer,0);///重複呼叫時間
potx=cos(angle/180.0*3.14);
poty=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(0,timer,0);///呼叫時間
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
目標:位置會上下移動
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include<math.h>
float angle=0,potx=0,poty=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glTranslatef(potx,poty,0);
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("1.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
void timer(int t)
{
glutTimerFunc(10,timer,0);///重複呼叫時間
//potx=cos(angle/180.0*3.14);
poty=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(10,timer,0);///呼叫時間
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
目標:拉動目標並射出
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include<math.h>
int potq=0;
float angle=0,potx=0,poty=0,potvx=0,potvy=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glTranslatef(potx,poty,0);
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("1.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
void timer(int t)
{
glutTimerFunc(10,timer,t+1);///重複呼叫時間
angle+=1;
if(potq==0){poty=0.5*sin(angle/180.0*3.14);}
if(potq==2){potx+=potvx; poty+=potvy;}
glutPostRedisplay();
}
void mouse(int bu,int st,int x,int y)
{
if(st==GLUT_DOWN){potq=1;}
else if(st==GLUT_UP){potq=2;potvx=-(x-350)/3500.0;potvy=-(y-350)/3500.0;}
}
void motion(int x,int y)
{
if(potq==1){potx=-(x-350)/350.0;poty=-(y-350)/350.0;}
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///顯示
glutTimerFunc(10,timer,0);///呼叫時間
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
訂閱:
文章 (Atom)



















