Step1
搜尋網址: @jsyeh.org/3dcg10/
利用此網站下載 data(模型),win32(主角),glut32.dll檔
利用模組來複習跟溫習程式
Step2
複習程式
glTranslatef(x,y,z);///移動
glRotatef(angle*角度*,x,y,z);///轉動
glScalef(x,y,z);///縮放
glBegin(GL_POLYGON);///開始畫
glColor3f(r,g,b);///顏色
glVertex3f(x,y,z);///頂點
glEnd();///結束畫 與glBegin()對應
Step3
利用茶壺旋轉複習
***程式碼***
#include <GL/glut.h>///開啟GL外掛
#include <stdio.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清理畫面
glPushMatrix();
glRotatef(angle,0,0,1);///轉動
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();///秀出東西
printf("%.1f\n",angle);
angle++;
}
int main(int argc, char*argv[])///主函式
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05160400Week4");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}
Step4
利用茶壺手動點擊旋轉
***程式碼***
#include <GL/glut.h>///開啟GL外掛
#include <stdio.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清理畫面
glPushMatrix();
glRotatef(angle,0,0,1);///轉動
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();///秀出東西
}
void mouse(int button, int state, int x, int y)
{
}
void motion(int x, int y)
{
angle=x;///改變angle值
glutPostRedisplay();///重畫
}
int main(int argc, char*argv[])///主函式
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05160400Week4");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step5
任意移動
***程式碼***
#include <GL/glut.h>///開啟GL外掛
#include <stdio.h>
float angle=0,teapotX=0,teapotY=0;
int oldX=0, oldY=0, nowRotate=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清理畫面
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glRotatef(angle,0,0,1);///轉動
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();///秀出東西
}
void mouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;
oldX=x; oldY=y;
}
void motion(int x, int y)
{
if(nowRotate==1) angle +=(x-oldX);
else
{
teapotX +=(x-oldX)/150.0;
teapotY +=(oldY-y)/150.0;
}
oldX=x; oldY=y;
glutPostRedisplay();
}
int main(int argc, char*argv[])///主函式
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05160400Week4");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言