-移動、旋轉、縮放-
1.到jsyeh/3dcg10
下載 Window.zip 下載 \windows\Transformation.exe (主角)
data.zip 下載 \windows\data\ (很多模型)
glut32.dll 下載 \windows\glut32.dill
-考試會考-
#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle, 0,0,1);///旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
printf("%.1f\n", angle);
angle++;///每次角度加1度
}
int main(int argc, char*argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04 transformation");
glutIdleFunc(display);///很閒/Idle,去重畫
glutDisplayFunc(display);
glutMainLoop();
}
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle, 0,0,1);///旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
printf("%.1f\n", angle);
angle++;///每次角度加1度
}
int main(int argc, char*argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04 transformation");
glutIdleFunc(display);///很閒/Idle,去重畫
glutDisplayFunc(display);
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle, 0,0,1);///旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
///printf("%.1f\n", angle);angle++;///每次角度加1度
}
void mouse(int button, int state, int x, int y)
{
}
void motion(int x, int y)
{ ///當mouse在拖曳motion時
angle=x;///去改變gangle值
glutPostRedisplay();///請畫面重畫
}
int main(int argc, char*argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04 transformation");
glutMouseFunc(mouse);///mouse
glutMotionFunc(motion);///montion
///glutIdleFunc(display);///很閒/Idle,去重畫
glutDisplayFunc(display);
glutMainLoop();
}
旋轉+移動
#include <stdio.h>
#include <GL/glut.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);///旋轉angle度
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
///printf("%.1f\n", angle);angle++;///每次角度加1度
}
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("Week04 transformation");
glutMouseFunc(mouse);///mouse
glutMotionFunc(motion);///montion
///glutIdleFunc(display);///很閒/Idle,去重畫
glutDisplayFunc(display);
glutMainLoop();
}





沒有留言:
張貼留言