https://www.youtube.com/watch?v=sbu1JZY77AI
//對照版
https://www.youtube.com/watch?v=oZ45lns7eoQ
//無對照
2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月13日 星期五
2014年6月6日 星期五
運鏡的路
真的搞不太懂eye,obj,up怎麼調, 感覺很不規律@@
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float eyeX=-0.3, eyeY=1.2, eyeZ=0.4, objX=-0.2, objY=-0.5, objZ=-0.2, UpX=0, UpY=1, UpZ=0;
//運鏡初始點
float angleX =0,angleA=0,angleY=0;
float thesize=0.2;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5,angleX+=5,angleY=(1);
if(key=='2') angleA-=5,angleX-=5,angleY=0;
if(key==' '){
objX+=0.02;
}
if(key=='0'){ //timer開始
glutTimerFunc(0, timerRotateShow,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
void timerRotateShow(int t)
{
if(t%10==0)glClearColor((rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0);//背景rand變色
glutTimerFunc(30, timerRotateShow,t+1);
eyeX=cos(t/100.0*3.1415);//螺旋旋轉
eyeZ=sin(t/100.0*3.1415);//螺旋旋轉
eyeY-=0.005;
thesize+=0.01;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.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();
gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, UpX, UpY, UpZ);
glScalef(thesize,thesize,thesize);
glPushMatrix();//Eyes
glRotatef(angleX, 0,0.1,0);
glColor3f(0,0,0);
glTranslatef(0.1,0.3,-0.25);
glutSolidSphere(0.02,10,10);
glPushMatrix();
glTranslatef(-0.2,0,0);
glutSolidSphere(0.02,10,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();//身體
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();// 右大腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//右小腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.68, 0);
glRotatef(angleA, -0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();// 左大腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左小腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.68, 0);
glRotatef(angleA, 0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左肩
glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//左手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//右手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);//初始運鏡值
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
float eyeX=-0.3, eyeY=1.2, eyeZ=0.4, objX=-0.2, objY=-0.5, objZ=-0.2, UpX=0, UpY=1, UpZ=0;
//運鏡初始點
float angleX =0,angleA=0,angleY=0;
float thesize=0.2;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5,angleX+=5,angleY=(1);
if(key=='2') angleA-=5,angleX-=5,angleY=0;
if(key==' '){
objX+=0.02;
}
if(key=='0'){ //timer開始
glutTimerFunc(0, timerRotateShow,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
void timerRotateShow(int t)
{
if(t%10==0)glClearColor((rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0);//背景rand變色
glutTimerFunc(30, timerRotateShow,t+1);
eyeX=cos(t/100.0*3.1415);//螺旋旋轉
eyeZ=sin(t/100.0*3.1415);//螺旋旋轉
eyeY-=0.005;
thesize+=0.01;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.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();
gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, UpX, UpY, UpZ);
glScalef(thesize,thesize,thesize);
glPushMatrix();//Eyes
glRotatef(angleX, 0,0.1,0);
glColor3f(0,0,0);
glTranslatef(0.1,0.3,-0.25);
glutSolidSphere(0.02,10,10);
glPushMatrix();
glTranslatef(-0.2,0,0);
glutSolidSphere(0.02,10,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();//身體
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();// 右大腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//右小腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.68, 0);
glRotatef(angleA, -0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();// 左大腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左小腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.68, 0);
glRotatef(angleA, 0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左肩
glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//左手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//右手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glMatrixMode(GL_PROJECTION);//初始運鏡值
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
}
2014年5月30日 星期五
機器的路3(最終章)
經歷過各種進化難題,我們可愛的小機器人..的夥伴茶壺君終於可以順順的動了!
為了慶祝這一刻以及迎接最終試煉的到來,讓我們收下這長長的程式碼吧!
機器人可以和茶壺君通過最終試煉嗎 讓我們繼續看下去
#include <stdio.h>
#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 angleNew[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[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;
//宣告讀檔囉 (在readnext裡面使用)
void readNext()
{
if(fin==NULL) fin=fopen("Position","r");//讀檔
a+=0.1;
for(int i=0;i<20;i++) 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;//mouse X , mouse Y
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'){/*save*/
if(fout==NULL) fout=fopen("Position","w+");
for(int i=0;i<20;i++){
printf( "%f",angle[i]);//寫在小黑
fprintf(fout,"%f",angle[i]);//寫在NOTE
}
printf( "\n");//同上
fprintf(fout,"\n");
}
if(key=='r'){ readNext(); }//鍵盤移動用
if(key=='p'){ glutTimerFunc(0,timer,0); }//滑鼠移動動畫用
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
為了慶祝這一刻以及迎接最終試煉的到來,讓我們收下這長長的程式碼吧!
機器人可以和茶壺君通過最終試煉嗎 讓我們繼續看下去
#include <stdio.h>
#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 angleNew[20]={0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleOld[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;
//宣告讀檔囉 (在readnext裡面使用)
void readNext()
{
if(fin==NULL) fin=fopen("Position","r");//讀檔
a+=0.1;
for(int i=0;i<20;i++) 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;//mouse X , mouse Y
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'){/*save*/
if(fout==NULL) fout=fopen("Position","w+");
for(int i=0;i<20;i++){
printf( "%f",angle[i]);//寫在小黑
fprintf(fout,"%f",angle[i]);//寫在NOTE
}
printf( "\n");//同上
fprintf(fout,"\n");
}
if(key=='r'){ readNext(); }//鍵盤移動用
if(key=='p'){ glutTimerFunc(0,timer,0); }//滑鼠移動動畫用
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2014年5月23日 星期五
機器的路2
上週大受好評! 立即開拍機器的路2
想要個有聲又有影又會自己動的機器人嗎?
沒錯!只要動動手指頭,免錢又帥氣機器人讓你帶回家!
程式碼下收
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>//Multi Media System
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];
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++){
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()
{
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();
glTranslatef(0,0.35,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[3], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(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);
glutSolidSphere(0.12,30,30);
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(angle[6], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glutSolidSphere(0.12,30,30);
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)//timer是你機器人最好的夥伴
{
glutTimerFunc(1000,timer,0);
if(fin==NULL)fin=fopen("animation.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')//按下一次s 就可以記錄你帥氣機器人的英姿
{
if(fout==NULL)fout = fopen("animation.txt","w+");
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]);
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]);
}
if(key=='r')//按下r (請記得先儲存後再按) 就可以再看一次你家帥氣機器人的英姿
{
if(fin==NULL)fin=fopen("animation.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')//加上play更帥氣,不用瘋狂按 每秒自己就會跑
{
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()
{
myMP3.Load("Life.mp3");
myMP3.Play();
//PlaySound("ccheer.wav",NULL,SND_ASYNC);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
想要個有聲又有影又會自己動的機器人嗎?
沒錯!只要動動手指頭,免錢又帥氣機器人讓你帶回家!
程式碼下收
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>//Multi Media System
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];
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++){
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()
{
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();
glTranslatef(0,0.35,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[3], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(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);
glutSolidSphere(0.12,30,30);
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(angle[6], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glutSolidSphere(0.12,30,30);
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)//timer是你機器人最好的夥伴
{
glutTimerFunc(1000,timer,0);
if(fin==NULL)fin=fopen("animation.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')//按下一次s 就可以記錄你帥氣機器人的英姿
{
if(fout==NULL)fout = fopen("animation.txt","w+");
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]);
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]);
}
if(key=='r')//按下r (請記得先儲存後再按) 就可以再看一次你家帥氣機器人的英姿
{
if(fin==NULL)fin=fopen("animation.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')//加上play更帥氣,不用瘋狂按 每秒自己就會跑
{
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()
{
myMP3.Load("Life.mp3");
myMP3.Play();
//PlaySound("ccheer.wav",NULL,SND_ASYNC);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2014年5月16日 星期五
01160581 機器的路
大學生做出來的超非凡機器人
不管是來個滑板滑壘
還是想來學個忍術
非凡機器人讓你十個願望一次滿足:3
這個價值非凡的機器人怎麼製作?
很簡單動動你的手指鍵盤滑鼠按幾下就可以了。
而且 完全免費!
接下來 齁康報給你知,程式碼下收
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
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();
glTranslatef(0,0.35,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
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();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[3], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(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);
glutSolidSphere(0.12,30,30);
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(angle[6], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glutSolidSphere(0.12,30,30);
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);
myTexture();
glutMouseFunc(mouse);
glutMotionFunc(motion);
//以上不要犯蠢,上面寫了一堆結果忘記呼叫,讓你哭天搶地機器人也回不來了
glutMainLoop();
}
最後特別大感謝!
葉正聖老師友情提供程式碼
2014年5月2日 星期五
帥氣茶壺機器人
#include <GL/glut.h>
float angleX =0,angleA=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5;
if(key=='2') angleA-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.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);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();//身體
//glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();// 右大腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//右小腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.6, 0);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();// 左大腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左小腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.6, 0);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左肩
//glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//左手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
//glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//右手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
float angleX =0,angleA=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5;
if(key=='2') angleA-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.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);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();//身體
//glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();// 右大腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//右小腿
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.6, 0);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();// 左大腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左小腿
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.6, 0);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();//左肩
//glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//左手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
//glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();//右手臂
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
水藍色真的很美。
2014年4月25日 星期五
旋轉角度01160581
#include<GL/glut.h>
#include<stdio.h>
float angleX=0,startX=0;
void keyboard(unsigned char key, int x, int y)
{
printf("%c %d %d\n",key,x,y);//KEY鍵 滑鼠的x,y座標
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();//改變角度後,重新re-display
}
void mouse(int button, int state, int x, int y)
{
printf("%d %d %d %d\n",button, state, x, y);
startX=x;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
//angleX= x - startX;
//angleY= y - startY;這兩行如果滑鼠放掉又會變成圓點開始。
angleX += x - startX; startX=x;
angleY += y - startY; startY=y;//這樣才可以累加
glutPostRedisplay();
}
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();
}
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);
glutInitWindowSize(600,600);//修改視窗大小
glutCreateWindow("week10");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
#include<stdio.h>
float angleX=0,startX=0;
void keyboard(unsigned char key, int x, int y)
{
printf("%c %d %d\n",key,x,y);//KEY鍵 滑鼠的x,y座標
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();//改變角度後,重新re-display
}
void mouse(int button, int state, int x, int y)
{
printf("%d %d %d %d\n",button, state, x, y);
startX=x;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
//angleX= x - startX;
//angleY= y - startY;這兩行如果滑鼠放掉又會變成圓點開始。
angleX += x - startX; startX=x;
angleY += y - startY; startY=y;//這樣才可以累加
glutPostRedisplay();
}
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();
}
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);
glutInitWindowSize(600,600);//修改視窗大小
glutCreateWindow("week10");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
2014年4月11日 星期五
01160581 Week08 脫窗之多拉B夢
#include <GL/glut.h>
//#include "glm.h"
//GLMmodle *modle;
float angle=0;
void eye()
{
glPushMatrix();
glTranslatef(-0.1, 0.4, 0.2);
glColor3f(1, 1, 1);
glutSolidSphere(0.1, 20, 20);
glColor3f(0,0,0);
glTranslatef(-0.02, 0.05 , 0.07);
glutSolidSphere(0.03, 10, 10);
glPopMatrix();
glPushMatrix();
glTranslatef(0.1, 0.4, 0.2);
glColor3f(1, 1, 1);
glutSolidSphere(0.1, 20, 20);
glColor3f(0,0,0);
glTranslatef(-0.02, 0.05 , 0.07);
glutSolidSphere(0.03, 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.05 , 0.1);
glutSolidSphere(0.24, 30, 30);
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 1 ,0);
head();
eye();
glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
glutTimerFunc(20,timer,1);
angle++;
glutPostRedisplay();
}
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 };//打光//-5.0f把光打到前面
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week08");
glutDisplayFunc(display);
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);//打光
// model=glmReadOBJ("檔名");
// glmUnitize(model);
glutTimerFunc(2000,timer,1);
glutMainLoop();
}
成功做出有點脫窗的多拉A夢! 可是我怎麼覺得他比較像青蛙
加上TIMER會比較跩
2014年3月28日 星期五
看了會吐的貼圖語法
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
void display()
{
glEnable(GL_DEPTH_TEST);//深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160581");
glutDisplayFunc(display);
//準備貼圖
glEnable(GL_TEXTURE_2D);
IplImage* img = cvLoadImage("earth.jpg");
cvCvtColor(img,img,CV_BGR2RGB);
GLuint id;
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);
glutMainLoop();
// cvNamedWindow("week06");
// cvShowImage("week06",img);
// cvWaitKey(0);
}
#include <opencv/cv.h>
#include <GL/glut.h>
void display()
{
glEnable(GL_DEPTH_TEST);//深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160581");
glutDisplayFunc(display);
//準備貼圖
glEnable(GL_TEXTURE_2D);
IplImage* img = cvLoadImage("earth.jpg");
cvCvtColor(img,img,CV_BGR2RGB);
GLuint id;
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);
glutMainLoop();
// cvNamedWindow("week06");
// cvShowImage("week06",img);
// cvWaitKey(0);
}
有了這個,貼圖輕鬆打,看了不會吐!
地球轉轉
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
GLUquadric * quad = NULL;
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);//深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glRotatef(90,1,0,0);
gluQuadricTexture(quad,true);
gluSphere(quad,1,30,30);
glPopMatrix();
glutSwapBuffers();
}
void idle()
{
angle+=1;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160581");
glutDisplayFunc(display);
glutIdleFunc(idle);
quad=gluNewQuadric();
//準備貼圖
glEnable(GL_TEXTURE_2D);
IplImage* img = cvLoadImage("earth.jpg");
cvCvtColor(img,img,CV_BGR2RGB);
GLuint id;
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);
glutMainLoop();
// cvNamedWindow("week06");
// cvShowImage("week06",img);
// cvWaitKey(0);
}
2014年3月21日 星期五
WEEK05
#include "glm.h"
GLMmodel* pmodel = NULL;
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
pmodel = glmReadOBJ("data/porsche.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
(要跑車出來要加這些)
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
(要打光要加這些)
GLMmodel* pmodel = NULL;
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
pmodel = glmReadOBJ("data/porsche.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
(要跑車出來要加這些)
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float pos[]={0,0,-1,0};
glLightfv(GL_LIGHT0,GL_POSITION,pos);
(要打光要加這些)
2014年3月7日 星期五
2014年2月21日 星期五
訂閱:
文章 (Atom)