2024年3月4日 星期一

week 03

#mouse滑鼠

1.桌面葉正聖上課用軟體freeglut安裝 拉桌面,再把libfreeglut.a複製成libglut32.a

2.CodeBlocks File-New-Project,GLUT專案,week03_mouse

3.把blog/github的10行程式碼貼進來

4.再三行

#include <stdio.h>為了printf()


void mouse(int button,int state,int x,int y){

     printf("Hello World\n");

}

glutCreateWindow()之後,在 glutDisplayFunc(display);後面加

     glutMouseFunc(mouse);


5.茶壺座標printf("%d %d %d\n",button ,state ,x, y);

button :0左鍵 1:中鍵 2.右鍵




6:移動茶壺

#include <GL/glut.h>

#include <Stdio.h>

float teapotX=0,teapotY=0;

void display()

{

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

glPushMatrix();///被分矩陣

glTranslatef(teapotX,teapotY,0);

glutSolidTeapot(0.3);

glPopMatrix();

glutSwapBuffers();

}


void mouse(int button,int state,int x,int y)

{

  teapotX=(x-150)/150.0;

  teapotY=-(y-150)/150.0;

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);///140

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143

    glutCreateWindow("week03 mouse glTranslatef");///145


     glutDisplayFunc(display);///148

     glutMouseFunc(mouse);///148

     glutMainLoop();///174

}

須知:需要用安培右手定律,其中Z軸是最難移動,
   

*程式碼#include <GL/glut.h>
#include <Stdio.h>
float teapotX=0,teapotY=0;
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();///被分矩陣
glTranslatef(teapotX,teapotY,0);///座標移動
glRotated(angle,0,0,1);///最難的z軸
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}///
int method=1;
int oldx=0,oldy=0;
void mouse(int button,int state,int x,int y)
{
  ///teapotX=(x-150)/150.0;
  ///teapotY=-(y-150)/150.0;
  oldx=x;
  oldy=y;
}
void motion(int x,int y)
{
   if(method==1){
    angle=x;
   }else if(method==2){
      teapotX+=(x-oldx)/150.0;
      teapotY-=(y-oldy)/150.0;
   }
    oldx=x;
    oldy=y;
    display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='c') method=1;///旋轉Rotate
    if(key=='w') method=2;///移動Translate

}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);///140
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143
    glutCreateWindow("week03 mouse glTranslatef");///145

     glutDisplayFunc(display);///148
     glutMouseFunc(mouse);///148
     glutMotionFunc(motion);
     glutKeyboardUpFunc(keyboard);
     glutMainLoop();///174
}
2:旋轉茶壺


#include <GL/glut.h>
#include <Stdio.h>
float teapotX=0,teapotY=0;
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();///被分矩陣
///glTranslatef(teapotX,teapotY,0);吧座標移動
  glRotated(angle,0,0,1);///最難的z軸
  glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}///

void mouse(int button,int state,int x,int y)
{
  teapotX=(x-150)/150.0;
  teapotY=-(y-150)/150.0;
}
void motion(int x,int y)
{
    angle=x;
    display();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);///140
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143
    glutCreateWindow("week03 mouse glTranslatef");///145

     glutDisplayFunc(display);///148
     glutMouseFunc(mouse);///148
     glutMotionFunc(motion);
     glutMainLoop();///174
}
3:

沒有留言:

張貼留言