WEEK13
Work01
第一步:開啟新專案,選擇Console application
選擇C++
第二步:當專案新增後,先於左方的總檔案按右鍵選擇Build Options
選擇Link settings,將
freeglut
opengl32
glu32
gdi32
winmm
加入於Link libraries
選擇Search directories中的Compiler
將freeglut中的include資料夾位置加入
選擇Search directories中的Linker
將freeglut中的lib資料夾位置加入
//------------------------------------------------------程式碼-------------------------------------------------
#include <GL/glut.h>
void display()
{
glPushMatrix();
glutSolidTeapot(0.3);
glTranslatef(0.5, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------
當程式進行執行與偵錯時,會發現freeglut.dll無法偵測到,因此下方偵錯區發現程式執行位置,將freeglut.dll放入執行資料夾中
//------------------------------------------------------程式碼-------------------------------------------------
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清理視窗畫面
glPushMatrix();
glutSolidTeapot(0.3);//建立茶壺1
glRotatef(rotX,0,0,1);//讓茶壺2可以對茶壺1進行公轉
glTranslatef(0.5, 0, 0);//讓茶壺2移動
glRotatef(rotX,0,0,1);讓茶壺2可以進行自轉
glutSolidTeapot(0.3);//建立茶壺2
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX = x;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------
製作可自轉和公轉的茶壺
Work02-關節轉動
利用旋轉與移動進行類似關節移動的動作
//------------------------------------------------------程式碼-------------------------------------------------
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.5, 0, 0);//移動茶壺1的中心點座標
glutSolidTeapot(0.2);//建立茶壺1
glTranslatef(0.3, 0.05, 0);//移動茶壺2的中心點座標
glRotatef(rotX,0,0,1);//以茶壺2的圖片中心做旋轉定點
glTranslatef(0.28, 0, 0);//移動茶壺2的中心點座標
glutSolidTeapot(0.2);//建立茶壺2
glTranslatef(0.3, 0.05, 0);//移動茶壺3的中心點座標
glRotatef(rotX,0,0,1);//以茶壺3的圖片中心做旋轉定點
glTranslatef(0.28, 0, 0);移動茶壺3的中心點座標
glutSolidTeapot(0.2);//建立茶壺3
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX = x;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------
Work03
簡單版本人體關節動作
//------------------------------------------------------程式碼-------------------------------------------------
#include <GL/glut.h>
float rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0, 0.1, 0);
glutWireCube(0.2);
glTranslatef(0, -0.2, 0);
glutWireCube(0.2);
glPushMatrix();
glTranslatef(0, 0.35, 0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(0.075, 0.2, 0);
glRotatef(rotX,0,0,1);
glTranslatef(0.075, 0, 0);
glutWireCube(0.1);
glTranslatef(0.05, 0, 0);
glRotatef(rotX,0,0,1);
glTranslatef(0.05, 0, 0);
glutWireCube(0.1);
glTranslatef(0.05, 0, 0);
glRotatef(rotX,0,0,1);
glTranslatef(0.05, 0, 0);
glutWireCube(0.1);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.075, 0.2, 0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.075, 0, 0);
glutWireCube(0.1);
glTranslatef(-0.05, 0, 0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.05, 0, 0);
glutWireCube(0.1);
glTranslatef(-0.05, 0, 0);
glRotatef(-rotX,0,0,1);
glTranslatef(-0.05, 0, 0);
glutWireCube(0.1);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX = x;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Robot");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------













沒有留言:
張貼留言