2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月13日 星期五
2014年6月6日 星期五
HW-16 打光
#include <GL/glut.h>
float angleX =0,angle2=0,anglet=0,angleh=0;
float eyeX=0.3,eyeY=0.2,eyeZ=0.4, objX=0,objY=0,objZ=0, upX=0,upY=1,upZ=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle2+=5;
if(key=='4') angle2-=5;
if(key=='5') anglet+=5;
if(key=='6') anglet-=5;
if(key=='7') angleh+=5;
if(key=='8') angleh-=5;
if(key==' '){
eyeX-=0.02; eyeY-=0.02;
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX,eyeY,eyeZ, objX,objY,objZ, upX,upY ,upZ);
glRotatef(angleX, 0,1,0);
glColor3f(0,0,0);
glutSolidCube(0.7);
glPushMatrix();//right shoulder
glTranslatef(0.5,0,0);
glColor3f(0.5,0.5,0.5);
glutSolidSphere(0.17,30,30);
glPushMatrix();//right arm
glRotatef(angle2,0,0,1);
glTranslatef(0.2,0,0);
glScalef(0.3,0.25,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//left shoulder
glTranslatef(-0.5,0,0);
glutSolidSphere(0.17,30,30);
glColor3f(0,0,0);
glPushMatrix();//left arm
glRotatef(angle2,0,0,1);
glTranslatef(-0.2,0,0);
glScalef(0.3,0.25,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//head
glTranslatef(0,0.4,0);
glutSolidCube(0.2);
glColor3f(0,0,0);
glPushMatrix();
glRotatef(angleh,0,1,0);
glTranslatef(0,0.3,0);
glutSolidSphere(0.18,20,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef(0.15,-0.4,0);
glutSolidSphere(0.12,30,30);
glColor3f(0,0,0);
glPushMatrix();
glRotatef(anglet,1,0,0);
glTranslatef(0.018,-0.2,0);
glScalef(1,2,1);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef(-0.15,-0.4,0);
glutSolidSphere(0.12,30,30);
glColor3f(0,0,0);
glPushMatrix();
glRotatef(-anglet,1,0,0);
glTranslatef(-0.018,-0.2,0);
glScalef(1,2,1);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
2014年5月30日 星期五
HW15-內插法的茶壺
#include <GL/glut.h>
float a=0;
float angle=0;
float angle01d=0;
float angleNew=90;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key , int x, int y)
{
if (key=='r')
{
a+=0.1;
angle = angle01d * (1-a) + angleNew *a;
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
----------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float a=0;
float angle[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleold[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};;
float angleNew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(angle[3],angle[4],angle[5]);
glPushMatrix();
glRotatef(angle[0],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,0,0);
glRotatef(angle[1],0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext()
{
a+=0.1;
for(int i=0;i<20;i++) angle[i]= angleold[i] *(1-a) + angleNew[i]*a; //內插公式
}
void timer(int t)
{
glutTimerFunc(10,timer,0);
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key , int x, int y)
{
if (key=='r')
{
readNext();
}
else if (key=='p');
{
glutTimerFunc(0,timer,0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <GL/glut.h>
float a = 0; //內插法比重
//有很多個角度都要做內插法怎麼辦? 利用陣列, 就可以存很多個角度了
float angle[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //用來轉動的角度,由angleOld, angleNew計算出
float angleOld[20] = {0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //舊的角度,用來做內插法
float angleNew[20] = {90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //新的角度,用來做內插法
int angleID = 0;
FILE *fout = NULL, *fin = NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); //1
glRotatef(angle[0], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix(); //2
glTranslatef(0.5, 0,0);
glRotatef(angle[1], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void readNext() //改變角度
{
if (fin == NULL) fin = fopen("this is my file for animation.txt", "r");
a += 0.1; //比重一直增加,角度才會變化
for( int i=0; i<=20; i++){ //全部打出來太遜了, 用for可以直接打20個,比葉問還厲害!
angle[i] = angleOld[i] * (1-a) + angleNew[i] * a;
}
if (a >= 1.0){
a = 0.0;
for (int i=0; i<20; i++){
angleOld[i] = angleNew[i];
fscanf(fin, "%f ", &angleNew[i]);
}
}
}
void timer(int t) //計時器定時抓角度
{
glutTimerFunc(100, timer, 0);
readNext();
glutPostRedisplay();
}
int oldX = 0, oldY = 0;
void mouse (int button, int state, int x, int y)
{
oldX = x;
oldY = y;
}
void motion(int x, int y)
{
angle[angleID] += (x - oldX);
oldX = x;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID = 0;
if(key=='1') angleID = 1;
if(key=='2') angleID = 2;
if(key=='3') angleID = 3;
if(key=='s'){
if(fout==NULL) fout = fopen("this_is_my_file_for_animation.txt","w+");
for(int i=0; i<20; i++){
printf("%f ",angle[i]);
fprintf(fout, "%f ",angle[i]);
}
}
printf("\n");
fprintf(fout, "\n");
if (key == 'r'){
readNext();
}
else if (key== 'p'){
glutTimerFunc(0, timer, 0);
}
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
2014年5月23日 星期五
2014年5月9日 星期五
HW12-木頭人
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle1=0;
void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("Body.jpg");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0); glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1); glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1); glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0); glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle1+=5;
if(key=='4') angle1-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMainLoop();
}
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle1=0;
void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("Body.jpg");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0); glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1); glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1); glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0); glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle1, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle1+=5;
if(key=='4') angle1-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMainLoop();
}
2014年5月2日 星期五
HW11-圓圈茶壺
float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glTranslatef(0.5,0,0);
glutSolidSphere(0.3,30,30);
glTranslatef(-1,0,0);
glutSolidSphere(0.3,30,30);
glTranslatef(0.5,0.5,0);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float angleX =0,angle2=0,angle3=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle2+=5;
if(key=='4') angle2-=5;
if(key=='5') angle3+=5;
if(key=='6') angle3-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glTranslatef(0,0.45,0);
glutWireTeapot(0.3);
glPopMatrix();
glRotatef(angleX, 0,1,0);
glPushMatrix();//右肩
glTranslatef(0.5,0.45,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle2, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(0.3,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.5,0.45,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle2, 0,0,1);
glTranslatef(-0.3,0,0);
glScalef(0.3,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0,0.8,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//左腳
glTranslatef(0.2,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(-angle3, 1,0,0);
glTranslatef(-0.4,-0.2,0);
glScalef(0.2,0.3,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右腳
glTranslatef(-0.2,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle3, 1,0,0);
glTranslatef(0.4,-0.2,0);
glScalef(0.2,0.3,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
訂閱:
文章 (Atom)