WEEK15
到 https://jsyeh.org/3dcg10/ 之後下載 data.zip和windows.zip和win32.zip
之後把projection.exe打開
glPerspective視野大小、長寬比、最近距離、最遠距離
eye視野方向
center中心點位置
up手的方向、頭的方向
開一個新專案
改這兩行,轉換投影矩陣方法
gluPerspective方法
#include <GL/glut.h>void display(){glClear(GL_COLOR_BUFFER_BIT);glutSolidTeapot(0.3);glutSwapBuffers();}
////step1 調整茶壺的觀看位置
void reshape(int w, int h){glViewport(0, 0, w, h);///視窗裡,會看到的2D範圍float ar = w/(float) h;///aspect ratio長寬比glMatrixMode(GL_PROJECTION);///先切換到project矩陣glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)gluPerspective(60, ar, 0.01, 1000);glMatrixMode(GL_MODELVIEW);///做好後馬上放切回model viewglLoadIdentity();gluLookAt(0,0,1, 0,0,0, 0,1,0);///在0,0,1 看茶壺0,0,0, up是0,1,0glutPostRedisplay();}
///step2 增加可以移動觀看的motion函式,詪step1很相像
float eyeX=0, eyeY=0;void motion(int x, int y){eyeX= (x-150.0)/150.0;eyeY = (150.0-y)/150.0;glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(eyeX, eyeY, 1, 0,0,0, 0,1,0);glutPostRedisplay();}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);glutCreateWindow("week15");glutMotionFunc(motion);glutDisplayFunc(display);glutReshapeFunc(reshape);///老師把名子改成reshape;glutMainLoop();}
之後我們要把關節存成陣列,然後用存檔讀黨的方式把動作存下來做成動畫
///week12-5_TRT_keyboard_mouse 要用 keyboard mouse 來操控
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h" ///week13 step02-1
GLMmodel * head = NULL; ///week13 step02-1
GLMmodel * body = NULL; ///week13 step02-1
GLMmodel * uparmR = NULL; ///week13 step02-1
GLMmodel * lowarmR = NULL; ///week13 step02-1
int show[4] = {1, 1, 1, 1};/// week14_step03-1 用 show[i] 來決定要不要顯示
int ID = 3;///0:頭 1身體 2上手臂 3下手臂 ///week14_step03-1
void keyboard(unsigned char key, int x, int y) {/// week13 step03-1
if(key=='0') ID = 0; ///week14_step02-2
if(key=='1') ID = 1; ///week14_step02-2
if(key=='2') ID = 2; ///week14_step02-2
if(key=='3') ID = 3; ///week14_ste if (ID==0) glColor3f(1,0,0); ///選定的,變紅色
else glColor3f(1,1,1);
if (show[0]) glmDraw(head, GLM_MATERIAL);
if (ID==1) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[1]) glmDraw(body, GLM_MATERIAL);
glPushMatrix(); ///屁股
glTranslatef(-0.233333, 0.413333, 0);
glRotatef( angle[2] , 0 , 0 , 1);
glTranslatef(0.233333, -0.413333, 0);
///glTranslatef(teapotX,teapotY,0);
if (ID==2) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[2]) glmDraw(ass, GLM_MATERIAL);
glPopMatrix();
if (ID==3) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[3]) glmDraw(uparmR, GLM_MATERIAL);
glPushMatrix(); ///下手臂
glTranslatef(-1.933333, 0.140000, 0);
glRotatef( angle[4] , 0 , 0 , 1);
glTranslatef(1.933333, -0.140000, 0);
///glTranslatef(teapotX,teapotY,0);
if (ID==4) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[4]) glmDraw(armR, GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot(0.02);
glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int y)
{
teapotX += (x - oldX)/150.0;
teapotY -= (y - oldY)/150.0;
angle[ID] += ( x - oldX );
oldX = x;
oldY = y;
///angle = x;
printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
teapotX = (x-150)/150.0;
teapotY = (150-y)/150.0;
if(fout==NULL) fout = fopen("file4.txt","w");
fprintf(fout, "%f %f\n",teapotX , teapotY);
}
display();
}
//void keyboard(unsigned char key,int x,int y)
//{
// if (fin==NULL)
// {
// fclose(fout);
// fin = fopen("file4.txt","r");
// }
// fscanf(fin,"%f %f",&teapotX , &teapotY);
// display();
//}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}p02-2
///if(key=='0') show[0] = !show[0];/// week13 step03-1
///if(key=='1') show[1] = !show[1];/// week13 step03-1
///if(key=='2') show[2] = !show[2];/// week13 step03-1
///if(key=='3') show[3] = !show[3];/// week13 step03-1
glutPostRedisplay();
} ///原來的keyboard先註解、不要用
FILE * fout = NULL; ///step02-2 一開始,檔案沒有開, NULL
FILE * fin = NULL; ///step02-2 要讀檔用的指標, 一開始也是 NULL
float teapotX=0, teapotY=0; ///幫我們看移動值
///float angle=0, angle2=0, angle3=0;///step03-2 擺動作
float angle[20] = {};
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.2, 0.2, 0.2); ///week13_step02-2
if(body==NULL){///week13 step02-1
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");///week13 step02-1
uparmR = glmReadOBJ("model/uparmR.obj");///Week13 step03-1
lowarmR = glmReadOBJ("model/lowarmR.obj");///Week13 step03-1
///glmUnitize(body); ///week13 step02-1 這行之後會改
}
if (ID==0) glColor3f(1,0,0); ///選定的,變紅色
else glColor3f(1,1,1);
if (show[0]) glmDraw(head, GLM_MATERIAL);
if (ID==1) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[1]) glmDraw(body, GLM_MATERIAL);
glPushMatrix(); ///屁股
glTranslatef(-0.233333, 0.413333, 0);
glRotatef( angle[2] , 0 , 0 , 1);
glTranslatef(0.233333, -0.413333, 0);
///glTranslatef(teapotX,teapotY,0);
if (ID==2) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
///if (show[2]) glmDraw(ass, GLM_MATERIAL);
glPopMatrix();
if (ID==3) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
if (show[3]) glmDraw(uparmR, GLM_MATERIAL);
glPushMatrix(); ///下手臂
glTranslatef(-1.933333, 0.140000, 0);
glRotatef( angle[4] , 0 , 0 , 1);
glTranslatef(1.933333, -0.140000, 0);
///glTranslatef(teapotX,teapotY,0);
if (ID==4) glColor3f(1,0,0);///選定的,變紅色
else glColor3f(1,1,1);
///if (show[4]) glmDraw(armR, GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot(0.02);
glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int y)
{
teapotX += (x - oldX)/150.0;
teapotY -= (y - oldY)/150.0;
angle[ID] += ( x - oldX );
oldX = x;
oldY = y;
///angle = x;
printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
teapotX = (x-150)/150.0;
teapotY = (150-y)/150.0;
if(fout==NULL) fout = fopen("file4.txt","w");
fprintf(fout, "%f %f\n",teapotX , teapotY);
}
display();
}
//void keyboard(unsigned char key,int x,int y)
//{
// if (fin==NULL)
// {
// fclose(fout);
// fin = fopen("file4.txt","r");
// }
// fscanf(fin,"%f %f",&teapotX , &teapotY);
// display();
//}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}





沒有留言:
張貼留言