Step01
先至moodle下載freeglut並解壓縮至桌面把lib資料夾內的libfreeglut.a檔案複製貼上並重新命名libglut32.a,開啟codeblocks開新專案,並複製上週程式碼後將方塊程式碼刪除,換成茶壺程式碼利用小球體讓茶壺以手柄為中心旋轉旋轉
程式碼 茶壺程式碼 小球體 以手柄為中心
#include <GL/glut.h>
float angle = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glRotatef(angle, 0,0,1 );
glTranslatef(0.45,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char* argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
Step02
先劃出一個方塊
程式碼 方塊程式碼 旋轉
#include <GL/glut.h>
float angle = 0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3); ///大小
glutSolidCube(0.5); ///正方形
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
///glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char* argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
程式碼 右移
#include <GL/glut.h>
float angle = 0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3); ///大小
glutSolidCube(0.5); ///正方形
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char* argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
連接另一個方塊讓她看起來像手臂
做出另一條手臂 並讓它旋轉另一個方向
程式碼 另一條手臂
#include <GL/glut.h>
float angle = 0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3); ///大小
glutSolidCube(0.5); ///正方形
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(-0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(-0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle++;
}
int main(int argc, char* argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
Step03
讓手臂能夠使用滑鼠來操控上下
程式碼 滑鼠操控 記得註解
#include <GL/glut.h>
float angle = 0;
void myCube()
{
glPushMatrix();
glScalef(1,0.3,0.3); ///大小
glutSolidCube(0.5); ///正方形
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(0.01,30,30);
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(0.25,0,0);
glRotatef(angle, 0,0,1 );
glTranslatef(0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle, 0,0,1 );
glTranslatef(-0.25,0,0);
myCube();
glPushMatrix();
glTranslatef(-0.25,0,0);
glRotatef(-angle, 0,0,1 );
glTranslatef(-0.25,0,0);
myCube();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;
}
void motion(int x, int y)
{
angle = x;///滑鼠motion
}
int main(int argc, char* argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week06");
glutMotionFunc(motion); ///滑鼠motion
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
用老師的網站 https://jsyeh.org/gl/ 練習期中的題目
沒有留言:
張貼留言