week13
step01-1
安裝git
把2023graphicsb 叫下來
cd desktop
git clone https:/github.com/mike7862/2023graphicsb
cd 2023graphicsb
start .
用codeblocks打開 2023graphicsb 裡 Final_Project 的 Final_Project.cbp
更改路徑
變成相對路徑
改成 freeglut\include
改成 freeglut\lib
把freeglut 丟到 Final_Project 專案
把libfreeglut.a 複製step01-2
打開git
把剛才修改的檔案上傳到github
step02-1
打開Maya
擷取模型關節
匯出檔案要選 Export selection
檔案要存objexport檔
最後要放在 Final_Project 裡的 model
step02-2
打開codeblocks
把 glm.h 和 glm.cpp 放入專案
在 codeblocks 裡的專案加入 glm.cpp
程式碼
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * right_up = NULL;
GLMmodel * right_low = 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");
right_up = glmReadOBJ("model/right_up.obj");
right_low = glmReadOBJ("model/right_low.obj");
///glmUnitize(head);
}
glPushMatrix();
glScalef(0.3, 0.3, 0.3);
glmDraw(head, GLM_MATERIAL);
glmDraw(body, GLM_MATERIAL);
glmDraw(right_up, GLM_MATERIAL);
glmDraw(right_low, 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();
}
修改程式碼
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * rightarm = NULL;
GLMmodel * righthand = 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");
rightarm = glmReadOBJ("model/rightarm.obj");
righthand = glmReadOBJ("model/righthand.obj");
///glmUnitize(head);
}
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(rightarm, GLM_MATERIAL);
if (show[3]) glmDraw(righthand, 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
備份到github
git status
git add .
git status
git commit -m "week13"
git push
step03-2
修改程式碼
讓模型能用滑鼠移動
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * rightarm = NULL;
GLMmodel * righthand = 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");
rightarm = glmReadOBJ("model/rightarm.obj");
righthand = glmReadOBJ("model/righthand.obj");
///glmUnitize(head);
}
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(rightarm, GLM_MATERIAL);
if (show[3]) glmDraw(righthand, 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
























沒有留言:
張貼留言