(複習)
開啟一個新的貝殼專案

打上自己的學號
檔案放在桌面上

將freeglut 資料夾拉到桌面

在search directories 的地方做設定
Compiler 選擇freeglut資料夾的include

Linker 選擇freeglut資料夾的lib
在Linker settings
打上這五個咒語:
(1)freeglut
(2)opengl32
(3)glu32
(4)gdi32
(5)winmm
注意!! 要把freeglut.dll這個檔案拉到專案的資料夾裡
不然會執行錯誤!!

(正課)
初步使用"運鏡"
新的程式碼
程式碼如下:
#include <stdio.h>
#include <GL/glut.h>
void display ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glutSolidTeapot(1);
glPopMatrix();
glutSwapBuffers();
}
void motion (int x,int y)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x/80.0, y/200.0,5,
0, 0, 0,
0, 1, 0);
glutPostRedisplay();
}
void resize(int w, int h)
{
glViewport(0, 0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0,(GLdouble) w / h,1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0,5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
}
int main (int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow(":D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutReshapeFunc(resize);
glutMainLoop();
}
按下Build 之後
就會跑出一個可以移動的鏡頭
圖片如下:


2.課堂作業:
讓機器人可以控制他的關節
(用1~8分別控制他的關節和運鏡視角)
程式碼如下:
#include <stdio.h>
#include <GL/glut.h>
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotNow=0, oldX=0, oldY=0;;
FILE *fout=0, *fin=0;
void timer(int t)
{
float alpha=(t%10)/10.0;///!!!!
//pos = newX*alpha+oldX*(1-alpha);
if(t%10==0){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<10;i++){
rotOld[i]=rotNew[i];
fscanf(fin, "%f", &rotNew[i]);
}
}
for(int i=0;i<10;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]*(1-alpha);
}
glutTimerFunc(100, timer, t+1);
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(rot[7]/50.0, rot[8]/200.0, 5,
0, 0, 0,
0, 1, 0);
glPushMatrix();
glScalef(6,6,6);
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='7') rotNow=7;
if(key=='8') rotNow=8;
if(key=='9') rotNow=9;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<10;i++){
fscanf(fin, "%f", &rot[i]);
}
}
if(key=='t'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<10;i++){
fscanf(fin, "%f", &rotNew[i]);
}
glutTimerFunc(100,timer, 0);
glutPostRedisplay();
}
if(key=='s'){///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<10;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
glutPostRedisplay();
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
void resize( int w, int h )
{
glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 65.0, (GLdouble) w / h,1.0, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("3D Interpolate");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutReshapeFunc(resize);
//glutTimerFunc(100, timer, 0);
glutMainLoop();
}
按下build就可以按照自己的想法
去控制機器人的姿勢
還有看機器人的是運鏡視角
圖片如下:



沒有留言:
張貼留言