20210315 滑鼠控制&建立網站
一開始先把上次存在Github的程式碼載下來
可參照底下的網址
https://github.com/wiki20010614/2021graphics
把上次的檔案打開後就長這樣
再來要加入滑鼠的功能
執行之後長這樣
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清空
glBegin(GL_TRIANGLES);
glColor3ub(24,94,151); glVertex2f((148-150)/150.0,-(29-150)/150.0);
glColor3ub(24,94,151); glVertex2f((0-150)/150.0,-(300-150)/150.0);
glColor3ub(24,94,151); glVertex2f((300-150)/150.0,-(300-150)/150.0);
glColor3ub(255,255,255); glVertex2f((148-150)/150.0,-(29-150)/150.0);
glColor3ub(255,255,255); glVertex2f((84-150)/150.0,-(146-150)/150.0);
glColor3ub(255,255,255); glVertex2f((213-150)/150.0,-(146-150)/150.0);
glEnd();
glutSwapBuffers();///交換2倍的buffers
}
void mouse(int button,int state,int x,int y)///控制滑鼠
{
printf("button:%d state:%d x:%d y:%d\n",button,state,x,y);///印出滑鼠按鍵 狀態 座標
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);///GULT初始設定
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///顯示模式
glutCreateWindow("富士山");///開視窗
glutDisplayFunc(display);///要顯示的函式
glutMouseFunc(mouse);///使用滑鼠函式
glutMainLoop();///主要的迴圈
}
--------------------------------------------------------------------------------------------------------------------------
再來要用滑鼠來找出圖的座標
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清空
glBegin(GL_TRIANGLES);
glColor3ub(24,94,151); glVertex2f((148-150)/150.0,-(29-150)/150.0);
glColor3ub(24,94,151); glVertex2f((0-150)/150.0,-(300-150)/150.0);
glColor3ub(24,94,151); glVertex2f((300-150)/150.0,-(300-150)/150.0);
glColor3ub(255,255,255); glVertex2f((148-150)/150.0,-(29-150)/150.0);
glColor3ub(255,255,255); glVertex2f((84-150)/150.0,-(146-150)/150.0);
glColor3ub(255,255,255); glVertex2f((213-150)/150.0,-(146-150)/150.0);
glEnd();
glutSwapBuffers();///交換2倍的buffers
}
void mouse(int button,int state,int x,int y)///控制滑鼠
{
if(state==GLUT_DOWN)
{
printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0)\n",x,y);///印出滑鼠座標
}
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);///GULT初始設定
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///顯示模式
glutCreateWindow("富士山");///開視窗
glutDisplayFunc(display);///要顯示的函式
glutMouseFunc(mouse);///使用滑鼠函式
glutMainLoop();///主要的迴圈
}
--------------------------------------------------------------------------------------------------------------------------
接下來要讓圖案移動 先用茶壺來試試看
#include <GL/glut.h>///使用GLUT外掛
#include <stdio.h>
float teaX=0,teaY=0;///茶壺座標
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清空
glPushMatrix();///備份矩陣
glTranslatef(teaX,teaY,0);
glutSolidTeapot(0.5);
glPopMatrix();///還原矩陣
glEnd();
glutSwapBuffers();///交換2倍的buffers
}
void motion(int x,int y)///移動
{
teaX=(x-150)/150.0;///換算座標
teaY=-(y-150)/150.0;
display();///即時更新
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);///GULT初始設定
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///顯示模式
glutCreateWindow("Mouse");///開視窗
glutDisplayFunc(display);///要顯示的函式
glutMotionFunc(motion);///移動的函式
glutMainLoop();///主要的迴圈
}
--------------------------------------------------------------------------------------------------------------------------
最後要來看圖案的轉動
glRotatef(角度,x,y,z)
轉動的方向是以大拇指為軸 其他四指自然握拳的方向
(橘色為旋轉軸 綠色為旋轉方向)
可以依照底下的方式在github建立一個自己的網站
最後附上網址!















沒有留言:
張貼留言