(1)GOTO--- jsyeh.org/3dcg10 載 5/18 PDF*2
課堂作業二:Camera運鏡--
開啟貝殼專案--加入include & lib
加入五個 freeglut , opengl32, glu32 ,gdi32 ,winmm
程式碼--
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.5);
glPopMatrix();
glutSwapBuffers();
}
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("camera");
glutDisplayFunc(display);
//glutMotionFunc(motion);
glutReshapeFunc(resize);
glutMainLoop();
}
調整camera
程式碼--
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.9);
glPopMatrix();
glutSwapBuffers();
}
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);
}
void motion(int x, int y)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x/100.0,y/100.0,5.0,
0.0,0.0,0.0,
0.0,1.0,0.0);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("camera");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutReshapeFunc(resize);
glutMainLoop();
}
課堂作業三:
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotID=1, oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(rot[7]/100.0,rot[8]/100.0,5.0,
0.0,0.0,0.0,
0.0,1.0,0.0);
glPushMatrix();
glScalef(6,6,6);
glutWireCube(0.3);
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[3],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x;
}
}void motion(int x, int y)
{
rot[rotID] += (x-oldX);
oldX=x;
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);
}
void timer(int t){
float alpha=(t%10)/10.0;
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]);
printf( "%f ", rotNew[i]);
}
fprintf(fin, "\n");
printf("\n");
}
for(int i=0;i<10;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]+(1-alpha);
}
glutTimerFunc(50, timer, t+1);
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') rotID=1;
if(key=='2') rotID=2;
if(key=='3') rotID=3;
if(key=='4') rotID=4;
if(key=='5') rotID=5;
if(key=='6') rotID=6;
if(key=='7') rotID=7;
if(key=='8') rotID=8;
if(key=='9') rotID=9;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rot[i]);
printf( "%f ", rot[i]);
}
fprintf(fin, "\n");
printf("\n");
}
if(key=='s'){ ///可以記錄頭尾所在的位置
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++){
fprintf( fout, "%.1f ", rot[i]);
printf( "%.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
if(key=='t'){ ///直接播放出動畫,透過內插法
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rotNew[i]);
printf( "%f ", rotNew[i]);
}
fprintf(fin, "\n");
printf("\n");
glutTimerFunc(100, timer, 0);
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutReshapeFunc(resize);
glutMainLoop();
}
#include <GL/glut.h>
FILE *fout=NULL, *fin=NULL;
float rot[10]={0}, rotOld[10]={0}, rotNew[10]={0};
int rotID=1, oldX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(rot[7]/100.0,rot[8]/100.0,5.0,
0.0,0.0,0.0,
0.0,1.0,0.0);
glPushMatrix();
glScalef(6,6,6);
glutWireCube(0.3);
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glTranslatef(0.05,0,0);
glRotatef(rot[3],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){
oldX=x;
}
}void motion(int x, int y)
{
rot[rotID] += (x-oldX);
oldX=x;
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);
}
void timer(int t){
float alpha=(t%10)/10.0;
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]);
printf( "%f ", rotNew[i]);
}
fprintf(fin, "\n");
printf("\n");
}
for(int i=0;i<10;i++){
rot[i]=rotNew[i]*alpha+rotOld[i]+(1-alpha);
}
glutTimerFunc(50, timer, t+1);
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') rotID=1;
if(key=='2') rotID=2;
if(key=='3') rotID=3;
if(key=='4') rotID=4;
if(key=='5') rotID=5;
if(key=='6') rotID=6;
if(key=='7') rotID=7;
if(key=='8') rotID=8;
if(key=='9') rotID=9;
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rot[i]);
printf( "%f ", rot[i]);
}
fprintf(fin, "\n");
printf("\n");
}
if(key=='s'){ ///可以記錄頭尾所在的位置
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<10;i++){
fprintf( fout, "%.1f ", rot[i]);
printf( "%.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
if(key=='t'){ ///直接播放出動畫,透過內插法
if(fin==NULL) fin=fopen("motion.txt","r");
for(int i=0;i<10;i++){
fscanf( fin, "%f ", &rotNew[i]);
printf( "%f ", rotNew[i]);
}
fprintf(fin, "\n");
printf("\n");
glutTimerFunc(100, timer, 0);
}
glutPostRedisplay();///請電腦貼個公告,有空就重新display
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutReshapeFunc(resize);
glutMainLoop();
}



沒有留言:
張貼留言