
以上是期中考會考的東西,

照著老師說的按了- 使點變少
--------------------------------------------------------------------------------------------------------------------------
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();
angle++;
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week 04 transformation");
glutIdleFunc(display);
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);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
}
void motion(int x,int y)
{
angle=x;
glutPostRedisplay();
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week 04 transformation");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
float angle=0, teapotX=0 , teapotY=0; 加上移動的座標
int oldX=0, oldY=0, nowRotate=0; translate,1;rotate
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();
}
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; 按下去的時候 把oldx oldy記起來
}
void motion(int x,int y)
{
if(nowRotate==1) angle += (x-oldX);
else{
teapotX += (x-oldX)/150.0; 去改變angle值 加上改變量
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("Week 04 transformation");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言