作業二
做出可以隨滑鼠點擊地點的茶壺
程式碼
#include <GL/glut.h>
float nowx=0, nowy=0;
void mouse (int button,int state,int x,int y)
{
nowx= (x-150)/150.0;
nowy= -(y-150)/150.0;
}
void display()
{
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslated(nowx,nowy,0);
glColor3f (1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("03160402林品至");
glutDisplayFunc (display);
glutMouseFunc(mouse);
glutMainLoop ();
}
作業三
小圓點隨著滑鼠點擊而移動
程式碼
#include <GL/glut.h>#include <stdio.h>
#include <math.h>
float nowx=0, nowy=0;
void mouse (int button,int state,int x,int y)
{
nowx= (x-150)/150.0;
nowy= -(y-150)/150.0;
printf("glTranslatef(%f,%f,0);\n",nowx,nowy);
}
void display()
{
glClearColor(1,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(nowx,nowy,0);
glColor3f (1,0,0);
///glutSolidTeapot(0.3);
glBegin(GL_POLYGON);
for(float angle=0;angle<3.14159265358979*2;angle+=0.1){
glVertex2f (0.2*cos(angle),0.2*sin(angle));
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("03160402");
glutDisplayFunc (display);
glutMouseFunc(mouse);
glutMainLoop ();
沒有留言:
張貼留言