顯示具有 01161185_吳佩芯 標籤的文章。 顯示所有文章
顯示具有 01161185_吳佩芯 標籤的文章。 顯示所有文章

2014年5月30日 星期五

week15課堂作業

內插法的使用

#include <GL/glut.h>
float a=0; //a is the alpha weight內差的比重
float angle=0;
float angleOld=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 =angleOld*(1-a)+angleNew*a;//內差法
}
glutPostRedisplay();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");

glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}

=========加入timer來控制茶壺轉動=========
#include <GL/glut.h>
float a=0; //a is the alpha weight內差的比重
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();
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);//0的話就是立即開始播放,若為2000的話就是2秒後開始播放
}
glutPostRedisplay();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");

glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}

2014年5月23日 星期五

sing a song + save file


#include <windows.h>
#include <mmsystem.h> //Multi Media system

int main()
{
PlaySound("people.wav",NULL,SND_SYNC);//SND_SYNC 會等同步,SND_ASYNC 不等同步
}
=========以下為播mp3音檔的程式==========
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h> //Multi Media system
#include "CMP3_MCI.h"
CMP3_MCI myMP3;

int main()
{
//PlaySound("people.wav",NULL,SND_SYNC);//SND_SYNC 會等同步,SND_ASYNC 不等同步,這是播放wav檔的程式
myMP3.Load("cry.mp3");
myMP3.Play();
printf("please input n:\n");
int n;
scanf("%d",&n);
}
==========下為加入讀檔的動畫==========
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>

//float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++)
{//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
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[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix(); //技巧2:小手
glTranslatef(0, -0.25, 0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.25, 0); //改變重心
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix(); //技巧2:小手
glTranslatef(0, -0.25, 0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(0, -0.25, 0); //改變重心
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void timer (int t)
{
glutTimerFunc(1000,timer,0);
if(fin==NULL) fin = fopen("hae.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f %f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
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=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;

if(key=='s') //先設定每個關節動作的位置
{
if(fout==NULL) fout =fopen("hae.txt","w+");
fprintf(fout,"%f %f %f %f %f %f %f %f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
printf("%f %f %f %f %f %f %f %f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}

if(key=='r') //讀取每個關節位置
{
if(fin==NULL) fin = fopen("hae.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f %f\n",angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='p')
{
glutTimerFunc(1000,timer,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0,oldY=0;
void motion(int x,int y)
{
angle[angleID]+=x-oldX;
oldX=x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}

2014年5月16日 星期五

week13作業

*神奇密碼*-lcv -lcxcore -lhighgui


#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
//float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
    for(int i=0; i<4; i++)
{//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
   img[i] = cvLoadImage( filename[i] );
   cvCvtColor(img[i], img[i], CV_BGR2RGB);
   glGenTextures(1, &id[i]);
   glBindTexture(GL_TEXTURE_2D, id[i]);//head
   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[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix(); //技巧2:小手
glTranslatef(0, -0.25, 0);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.25, 0); //改變重心
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix(); //技巧2:小手
glTranslatef(0, -0.25, 0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(0, -0.25, 0); //改變重心
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();  
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle[6]), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//right foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
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=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0,oldY=0;
void motion(int x,int y)
{
angle[angleID]+=x-oldX;
oldX=x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}

2014年5月9日 星期五

week12

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void texture()
{
    GLuint id;
    IplImage * img = cvLoadImage("body.png");
    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 display()
{
    glEnable(GL_DEPTH_TEST);//深度測試
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清畫面
    glPushMatrix();
    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();
    glPopMatrix();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleX+=5;
    if(key=='2') angleX-=5;
    glutPostRedisplay();//請電腦重新畫一次畫面
}

int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11");
   
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    texture();
    glutMainLoop();
}

2014年5月2日 星期五

week11上課題目


#include <GL/glut.h>

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, 4.0f, -6.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);//旋轉 
glutWireTeapot(0.3);//茶壺 
glTranslatef( 0.5, 0, 0);
glutSolidSphere( 0.3, 30, 30);//右邊圓 
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);//旋轉
glTranslatef(-0.5, 0, 0);
glutSolidSphere( 0.3, 30, 30);//左邊圓 
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);//旋轉
glTranslatef( 0, 0.35, 0);
glutSolidSphere( 0.2, 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, 3.0f, -6.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);//茶壺 
glPushMatrix();//右邊圓(關節)
glTranslatef( 0.4, 0, 0);
glutSolidSphere( 0.2, 30, 30); 
glPushMatrix(); //右邊手臂 
glRotatef(angle2, 0,0,1);//旋轉
glTranslatef( 0.2, 0, 0);//移動手臂位置 (x,y,z) 
glScalef(0.4,0.2,0.2);//放大倍率 (x,y,z)
glutSolidCube(1);//大小為1倍 
glPopMatrix();
glPopMatrix();
glPushMatrix(); //左邊圓(關節) 
glTranslatef(-0.4, 0, 0); 
glutSolidSphere( 0.2, 30, 30);
glPushMatrix(); //左邊手臂 
glRotatef(angle2, 0,0,1);//旋轉
glTranslatef( -0.2, 0, 0);//移動手臂位置 (x,y,z) 
glScalef(0.4,0.2,0.2);//放大倍率 (x,y,z)
glutSolidCube(1);//大小為1倍 
glPopMatrix();
glPopMatrix();
glPushMatrix();//中間身體
glTranslatef( 0, 0.35, 0);
glutSolidSphere( 0.2, 30, 30);
glPopMatrix();
glPushMatrix(); // 右大腿 
glTranslatef(0.05, -0.1, 0);//移動右腿位置 (x,y,z)
glRotatef(angle3, 1, 0, 0);//有旋轉 
glTranslatef( 0.1, -0.3, 0); //移動旋轉位置 
glPushMatrix();
glScalef(0.2,0.4,0.2);//放大倍率 (x,y,z)
glutSolidCube(1);//大小為1倍 
glPopMatrix();
glPopMatrix();
glPushMatrix(); // 左大腿 
glTranslatef(-0.25, -0.1, 0);//移動左腿位置 (x,y,z)
glRotatef(angle3, 1, 0, 0);//有旋轉 
glTranslatef( 0.1, -0.3, 0); //移動旋轉位置 
glPushMatrix();
glScalef(0.2,0.4,0.2);//放大倍率 (x,y,z)
glutSolidCube(1);//大小為1倍 
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
 
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
 
myLight();
glutMainLoop();
}

2014年4月25日 星期五

week10 按鍵,滑鼠,移動旋轉縮放

#include <GL/glut.h>
float angleX=0;
void keyboard(unsigned char key,int x, int y)
{
if(key=='a') angleX+=3; //每按一下,增加3度
if(key=='b') angleX-=3;
glutPostRedisplay();//改變角度,重新re-display顯示
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3); //畫出茶壺
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

glutMainLoop();
}
#include <stdio.h>
printf("%c %d %d\n",key,x,y);

增加了打光的部分程式碼
#include <GL/glut.h>
#include <stdio.h>

float angleX=0;
void keyboard(unsigned char key,int x, int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();//改變角度,重新re-display顯示
}

void myLight()
{
const GLfloat light_position[] = { 2.0f, 2.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);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();//畫出來

glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

float angleY=0, startY=0;
float angleX=0, startX=0;
float m[16]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
void mouse(int button, int state, int x, int y)
{
printf("%d %d %d %d\n",button,state,x,y);
startX=x; 
startY=y;
}
void motion(int x,int y)
{
printf("%d %d\n", x, y);
angleX = x - startX; startX = x; //再把+=改成=,因為下面已經轉動累積起來了 
angleY = y - startY; startY = y;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 2.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();
float dist= sqrt(angleX*angleX+angleY*angleY);
glRotatef(dist,-angleY,-angleX,0);
glMultMatrixf(m);
glGetFloatv(GL_MODELVIEW_MATRIX,m);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();//畫出來 
glutMainLoop();
}

2014年4月11日 星期五

多拉A夢

#include <GL/glut.h>
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);//要3D的深度測試
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空畫面 
    glPushMatrix();
    glRotatef(angle,0,1,0);//對整個多拉A夢做旋轉 
   
    glPushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭


glColor3f(1,1,1);//白色
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉
glPopMatrix();

glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);//身體 
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}
void idle()
{
angle++;
glutPostRedisplay(); //要去Re-Display重新顯示 
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week08");
glutDisplayFunc(display);
glutIdleFunc(idle);//執行某個函式 

glutMainLoop();
}

加入打光片段程式碼
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
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_AMBIENT,  light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);



#include <GL/glut.h>
float angle=0;
void eye()
{
glPushMatrix();
glColor3f(1,1,1);
glutSolidSphere(0.1,20,20);//先畫白色的眼球 
glTranslatef(0,0,0.1);// 動一點點試試看
  glColor3f(0,0,0);//再畫瞳孔(黑)
glutSolidSphere(0.02,10,10);
glPopMatrix();  
}
void head()
{
glPushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭

glColor3f(1,1,1);
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉
glPopMatrix();
glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);身體
glPopMatrix();

void display()
{
glEnable(GL_DEPTH_TEST);//要3D的深度測試
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空畫面 
    glPushMatrix();
    glRotatef(angle,0,1,0);//對整個多拉A夢做旋轉 
    head();
    glTranslatef(0,0,0.6);//往外慢慢移
    eye();
    /*ushMatrix();
glTranslatef(0,0.3,0);//往上移動
glColor3f(0,0,1);
glutSolidSphere(0.3,30,30);//頭

glColor3f(1,1,1);//白色
glTranslatef(0,-0.1,0.1);
glutSolidSphere(0.25,30,30);//臉*/
glPopMatrix();

glPushMatrix();
glColor3f(1,1,1);
//glutSolidSphere(0.2,30,30);//身體 
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}
void idle()
{
angle++;
glutPostRedisplay(); //要去Re-Display重新顯示 
}
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
int main()
{
glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
glutCreateWindow("week08");
glutDisplayFunc(display);
glutIdleFunc(idle);//執行某個函式 

glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glutMainLoop();

}