控制許多不同關節
- 開新空白(貝殼)專案
把Hello world 印在output.txt的程式碼
#include <stdio.h>
FILE *fout=NULL, *fin = NULL;
int main(int argc, char**argv)
{
if(fout==NULL)
fout=fopen("output.txt", "w+");
fprintf(fout, "HelloWorld");
}
*--
執行後會專案資料夾新增出output.txt
--
- 刪掉output.txt
接下來要印出滑鼠按的座標
- 解壓縮freeglut在桌面
- 專案按右鍵選Build option...
在Searth directories標籤下的
- Compiler標籤下加入在freeglut資料夾裡面的include資料夾路徑
- Linker標籤下加入在freeglut資料夾裡面的lib資料夾路徑


- Linker Settings標籤裡加入咒語(有順序)
freeglut
opengl32
gul32
gdi32
winmm
- freeglut.dll移進專案資料夾
全部程式碼
*--
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin = NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3); ///畫茶壺
glutSwapBuffers();
}
void motion(int x, int y) ///將滑鼠拖曳座標印在output.txt
{
if(fout==NULL)
fout=fopen("output.txt", "w+");
fprintf(fout, "%d %d\n", x,y);;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
--*
執行後在3d視窗內任意拖曳滑鼠
關掉視窗後打開output.txt
會看到剛剛的軌跡座標
--
要讓茶壺動
- 開頭宣告int nowX=150, nowY=150;
- 轉換座標(3D視窗大小300x300,正中間是(0,0),x從左邊到右邊-150~150;y從上面到下面150~-150)
glTranslatef((nowX-150)/150.0, -(nowY-150)/150.0,0);
- 新增鍵盤控制
void keyboard(unsigned char key,int x, int y)
{
if(key=='r')
{
if(fin==NULL)
fin=fopen("output.txt", "r"); //讀入剛剛的output.txt
fscanf(fin, "%d %d", &nowX, &nowY);
}
glutPostRedisplay();
}
全部程式碼:
--*
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin = NULL;
int nowX=150, nowY=150;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((nowX-150)/150.0, -(nowY-150)/150.0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
if(fout==NULL)
fout=fopen("output.txt", "w+");
fprintf(fout, "%d %d\n", x,y);;
}
void keyboard(unsigned char key,int x, int y)
{
if(key=='r')
{
if(fin==NULL)
fin=fopen("output.txt", "r");
fscanf(fin, "%d %d", &nowX, &nowY);
}
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
*--
執行後案住R
茶壺就會造著output.txt裡面的座標移動
--
理解老師的程式碼:
---*
#include <GL/glut.h>
#include <stdio.h>
float rot[10]={0};
int rotNow=0, oldX=0, oldY=0;;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();
glutWireCube(0.3);///body
glPushMatrix();
glTranslatef(0,0.2,0);
glutWireCube(0.1);///head
glPopMatrix();
glPushMatrix();
glTranslatef(0.15,0.1,0);
glRotatef(rot[0],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(0.05,0,0);
glRotatef(rot[1],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(0.05,0,0);
glRotatef(rot[2],0,0,1);
glTranslatef(0.05,0,0);
glutWireCube(0.1);///right arm
glPopMatrix();
glPushMatrix();
///glTranslatef(-0.2,0.1,0);
glTranslatef(-0.15,0.1,0);
glRotatef(-rot[4],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///upper arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[5],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///lower arm
glTranslatef(-0.05,0,0);
glRotatef(-rot[6],0,0,1);
glTranslatef(-0.05,0,0);
glutWireCube(0.1);///right hand
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=0, *fin=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') rotNow=0;
if(key=='1') rotNow=1;
if(key=='2') rotNow=2;
if(key=='3') rotNow=3;
if(key=='4') rotNow=4;
if(key=='5') rotNow=5;
if(key=='6') rotNow=6;
if(key=='r'){///按小寫的r 會去讀1行/一組x,y
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<7;i++){
fscanf(fin, "%f", &rot[i]);
}
}
glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
rot[rotNow] += x-oldX;
oldX = x;
glutPostRedisplay();
if(fout==NULL) fout=fopen("motion.txt", "w+");
for(int i=0;i<7;i++){
fprintf(fout, "%3.1f ", rot[i]);
printf("%3.1f ", rot[i]);
}
fprintf(fout, "\n");
printf("\n");
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
if(state==GLUT_DOWN){
oldX=x; oldY=y;
}
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
*---
2.貼圖+背景
3.模型+貼圖
4.檔案













沒有留言:
張貼留言