Week13
Week13-1.1
Step 1:先安裝Git,然後開啟Git bash
Step 3:輸入 git clone https://github.com/Fafa01/2023graphicsb,把自己github資料夾下載下來
Step 6:開啟上周的Final_Project.cbp
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(state==GLUT_DOWN){
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);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
Step 7:對project按右鍵,並點選Properities
Step 8:點選 Project's build options...
Step 9:在 Search directories中,把Compiler路徑改成 freeglut/include
Step 11:解壓縮freeglut,並把freeglut資料夾丟進Final_Project資料夾中
Step 12:把gitignore.txt用Notepad++開啟
提示:
每行的最前面,如果以 # 開頭,則代表註解
把剛剛的檔案利用下列指令來上傳
(要打在cmd或是git cmd上喔)
git add . 加入檔案
git status 確認檔案
git config --global user.email"你的github email"
git config --global user.name"你的github ID"
git commit -m"欲輸入內容"
git push 上傳
Week13-2
Step 1:打開Maya
Step 2:import模型
Step 3:把模型的身體各部位分別存檔(要存成obj檔喔),並在Final_Project資料夾中,
創建model資料夾,並存入
Step 4:把之前week08的glm.cpp和glm.h檔案丟進去Final_Project資料夾中
(如果沒有glm.cpp和glm.h,檔案在 https://jsyeh.org/3dcg10/ 的sorce中)
glm.cpp是glm.c把副檔名改成cpp
Step 5:在CodeBlock中,開啟上周的Final_project,並按下Final_project-Add files把glm.cpp加入進去
Step 6:打上下列程式碼
程式碼
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * body =NULL;
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(head==NULL){
head = glmReadOBJ("model/head.obj");
glmUnitize(head);
}
glmDraw(head,GLM_MATERIAL);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(state==GLUT_DOWN){
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);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * body =NULL;
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(head==NULL){
head = glmReadOBJ("model/head.obj");
glmUnitize(head);
}
glmDraw(head,GLM_MATERIAL);
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(state==GLUT_DOWN){
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);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
Step 7:執行程式,剛剛模型的頭就會出現了
Week 13-2.2
Step 1:更改程式碼為下列
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * body =NULL;
GLMmodel * Ruparm =NULL;
GLMmodel * Rhand =NULL;
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(head==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
Ruparm = glmReadOBJ("model/Ruparm.obj");
Rhand = glmReadOBJ("model/Rhand.obj");
}
glPushMatrix();
glScalef(0.3,0.3,0.3);
glmDraw(head,GLM_MATERIAL);
glmDraw(body,GLM_MATERIAL);
glmDraw(Ruparm,GLM_MATERIAL);
glmDraw(Rhand,GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(state==GLUT_DOWN){
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("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
這樣 剛剛的模型的半邊身體就會出現了
Week 13-3.1
若把程式碼改成下列,則可選擇要顯示哪個部位
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * body =NULL;
GLMmodel * Ruparm =NULL;
GLMmodel * Rhand =NULL;
int show[4] ={1,0,0,0};
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') show[0]=!show[0];
if(key=='1') show[1]=!show[1];
if(key=='2') show[2]=!show[2];
if(key=='3') show[3]=!show[3];
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(head==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
Ruparm = glmReadOBJ("model/Ruparm.obj");
Rhand = glmReadOBJ("model/Rhand.obj");
}
glPushMatrix();
glScalef(0.3,0.3,0.3);
if(show[0]) glmDraw(head,GLM_MATERIAL);
if(show[1]) glmDraw(body,GLM_MATERIAL);
if(show[2]) glmDraw(Ruparm,GLM_MATERIAL);
if(show[3]) glmDraw(Rhand,GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(state==GLUT_DOWN){
if(fout==NULL)
fout=fopen("file4.txt","w");
fprintf(fout,"%f %f\n",teapotX,teapotY);
}
display();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
Week 13-3.2
把程式碼改成下列,則可任意移動頭的位置
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * body =NULL;
GLMmodel * Ruparm =NULL;
GLMmodel * Rhand =NULL;
int show[4] ={1,0,0,0};
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') show[0]=!show[0];
if(key=='1') show[1]=!show[1];
if(key=='2') show[2]=!show[2];
if(key=='3') show[3]=!show[3];
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
if(head==NULL){
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj");
Ruparm = glmReadOBJ("model/Ruparm.obj");
Rhand = glmReadOBJ("model/Rhand.obj");
}
glPushMatrix();
glScalef(0.3,0.3,0.3);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
if(show[0]) glmDraw(head,GLM_MATERIAL);
glPopMatrix();
if(show[1]) glmDraw(body,GLM_MATERIAL);
if(show[2]) glmDraw(Ruparm,GLM_MATERIAL);
if(show[3]) glmDraw(Rhand,GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){
oldX =x ;
oldY = y;
}
}
void motion(int x,int y)
{
teapotX+=(x-oldX)/150.0;
teapotY-=(y-oldY)/150.0;
oldX = x;
oldY = y;
glutPostRedisplay();
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week13");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言