2.打開GLUT專案 嘗試使用程式碼將茶壺往x方向移動0.5(Translate)
程式碼:
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5,0,0);///most important
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04162443 TRS");
glutDisplayFunc(display);
glutMainLoop();
}
3.加入motion讓茶壺可以用滑鼠移動位置
程式碼:
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(mouseX,mouseY,0);///most important
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX = (x-150)/150.0;
mouseY = -(y-150)/150.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04162443 TRS");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
4.複習小精靈
程式碼:
#include <GL/glut.h>
#include<math.h>
float mouth=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///把畫面清空才能在重畫時沒有殘影
glColor3f(1,1,0);///顏色
glBegin(GL_TRIANGLE_FAN);///開始畫
glVertex2f(0,0);///圓心
for(float angle=0+mouth;angle<=3.1415926*2-mouth;angle+=0.01){
glVertex2f( cos(angle), sin(angle) );
}
glEnd();///結束畫
glutSwapBuffers();
}
void motion(int x,int y)
{
mouth = x/300.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04162443 TRS");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
程式碼:
#include <GL/glut.h>
#include<math.h>
float mouth=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///把畫面清空才能在重畫時沒有殘影
glColor3f(1,1,0);///顏色
glBegin(GL_TRIANGLE_FAN);///開始畫
glVertex2f(0,0);///圓心
for(float angle=0+mouth;angle<=3.1415926*2-mouth;angle+=0.01){
glVertex2f( cos(angle), sin(angle) );
}
glEnd();///結束畫
glutSwapBuffers();
}
void motion(int x,int y)
{
mouth = x/300.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("04162443 TRS");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}




沒有留言:
張貼留言