Week15
Step01
開啟連結 https://jsyeh.org/3dcg10/
下載 windows \ Transformation.exe
下載 \ windows \ data \模型檔
Step02
打開Projection.exe檔
Step03
glPerspective( ) 裡的參數 英文都是縮寫
gluLookAt( )
從哪裡看 eye看哪裡 center食指向上 指到哪拍哪 up
fovy : field of view ( Y方向 ) : 視野的角度
fovy的角度越大,東西就會越小,最大到179度
aspect : aspect ratio : 長寬比
zNear zFar : 遠近
Step04
程式碼:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0 , 0 , w , h); ///視窗裡會看到2D視窗
float ar = w/(float) h; ///apsect ratio長寬比
glMatrixMode(GL_PROJECTION); ///先切換到 Project 矩陣
glLoadIdentity(); ///矩陣清空,成為單位矩陣(最一開始的矩陣)
gluPerspective(60 , ar , 0.01 , 1000); ///現在一片空白,因為我們在茶壺裡
glMatrixMode(GL_MODELVIEW); ///做好後,馬上切回 model view 矩陣
glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)
gluLookAt(0,0,1, 0,0,0, 0,1,0);
///在0,0,1 看著茶壺 0,0,0, up是0,1,0
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutDisplayFunc(display);
glutReshapeFunc(reshape); ///老師把名字變reshape
glutMainLoop();
}
Step05 用 motion()函式,移動滑鼠
程式碼:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0 , 0 , w , h); ///視窗裡會看到2D視窗
float ar = w/(float) h; ///apsect ratio長寬比
glMatrixMode(GL_PROJECTION); ///先切換到 Project 矩陣
glLoadIdentity(); ///矩陣清空,成為單位矩陣(最一開始的矩陣)
gluPerspective(60 , ar , 0.01 , 1000); ///現在一片空白,因為我們在茶壺裡
glMatrixMode(GL_MODELVIEW); ///做好後,馬上切回 model view 矩陣
glLoadIdentity();///矩陣清空,成為單位矩陣(最一開始的矩陣)
gluLookAt(0,0,1, 0,0,0, 0,1,0);
///在0,0,1 看著茶壺 0,0,0, up是0,1,0
glutPostRedisplay();
}
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();
}
Step06 開啟FinalProject檔案
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {1,1,1,1,1}; ///show[1]來決定要不要顯示
int ID=4; ///0是頭 1是身體 2是屁股 3是上手臂 4是下手臂
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') ID=0;
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
glutPostRedisplay();
}
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0 , teapotY=0;
float angle[20] = {};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.2,0.2,0.2);
if(body==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
ass = glmReadOBJ("model/ass.obj");
uparmR = glmReadOBJ("model/uparmR.obj");
armR = glmReadOBJ("model/armR.obj");
}
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();
}
Step07 讀入檔案motion.txt
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {1,1,1,1,1}; ///show[1]來決定要不要顯示
int ID=4; ///0是頭 1是身體 2是屁股 3是上手臂 4是下手臂
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0 , teapotY=0;
float angle[20] = {};
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') ID=0;
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='s'){
if(fout==NULL) fout = fopen("motion.txt", "w");
for(int i=0 ; i<20 ; i++)
{
fprintf(fout, "%.2f ", angle[i] );
}
fprintf(fout, "\n");
}
else if (key=='c'){
if(fin!=NULL) fclose(fin);
fin = NULL;
if(fout!=NULL) fclose(fout);
fout = NULL;
}
else if (key=='r'){
if(fin == NULL) fin = fopen("motion.txt", "r");
for(int i=0 ; i<20 ; i++)
{
fscanf(fin, "%f", &angle[i] );
}
glutPostRedisplay();
}
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.2,0.2,0.2);
if(body==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
ass = glmReadOBJ("model/ass.obj");
uparmR = glmReadOBJ("model/uparmR.obj");
armR = glmReadOBJ("model/armR.obj");
}
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();
}
step08
上傳檔案git指令
1. git指令
2. cd desktop
3. git clone https://github.com/chiche/2023graphicsa
4. cd 2023graphicsa
5. start .
6. git add .
7. git status
8. git config --global user.email "liao2013823@gmail.com"
9. git config --global user.name "chiche0306"
10. git commit -m "week13"
11. git push


沒有留言:
張貼留言