WEEK13
Step01-1
用CodeBlock開啟上周的Final_Project.cbp
右鍵Final_project > Properties
Search directories>Compiler把裡面的路徑改成freeglut/include
Search directories>Linker把裡面的路徑改成freeglut/include
把freeglut的檔案放進去Final_Project
Step01-2
開啟2023graphicsb檔案裏面的.gitinore用notepad++
每一行的最前面,如果是#代表註解。
Step02-1
打開MAYA
匯入模型
把模型的身體分成許多部份
有手,身體,頭,腳.etc
在Final_Project裡面建資料夾
把剛剛分好的身體部位檔案丟進去Final_ProjectStep02-1
把之前week08-1的glm.cpp和glm.h檔案丟進去Final_Project裡面
Step02-2
再把CodeBlock裡面的Final_Project>Add files >把glm.cpp加進去
打上以下程式碼:
#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;
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();
}
這樣就把身體和右手做出來了,順便把整個大小縮小了
Step03-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();
}
這樣就可以指定想要顯示的部位了😉
現在可以先上傳github備份
Step03-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();
}
最後上傳GITHUB✌
沒有留言:
張貼留言