啊 給你看個很酷的東西
你可以點茶壺附近 他都會根據你點的地方給你座標
mouse函式:左右中鍵,DOWN/UP,x座標,y座標
然後就利用這ㄍ
把它可以一直換位子
這樣你點哪邊 茶壺就會跟著可以換位子囉
給你個程式碼
#include <GL/glut.h>
#include <stdio.h>///為了printf()
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)
{/// mouse函式:左右中鍵,DOWN/UP,x座標,y座標
printf("%f %f\n",(x-150)/150.0, (y-150)/150.0);
teapotX= (x-150)/150.0;
teapotY= (y-150)/150.0;
}
int main(int argc, char**argv)
{
glutInit(&argc,argv); //main的參數,於近來
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); //啟動3D的顯示能力,兩倍顯示buffer,有3D
glutCreateWindow("Week03");//建立3D窗子
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop(); //主迴圈停在這,並且不會結束
}
最後你想畫畫ㄇ
教你怎麼畫!
在給你程式碼
#include <GL/glut.h>
#include <stdio.h>float teapotX=0,teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON); // 開始畫圖形
// 程式碼產生的頂點
glVertex2f((83-150)/150.0,-(57-150)/150.0); // 將執行過後的程式碼複製至此,即可畫出圖形
glVertex2f((136-150)/150.0,-(126-150)/150.0);
glVertex2f((192-150)/150.0,-(170-150)/150.0);
glVertex2f((115-150)/150.0,-(246-150)/150.0);
glVertex2f((122-150)/150.0,-(174-150)/150.0);
glVertex2f((66-150)/150.0,-(131-150)/150.0);
glVertex2f((42-150)/150.0,-(261-150)/150.0);
glVertex2f((31-150)/150.0,-(91-150)/150.0);
glVertex2f((19-150)/150.0,-(52-150)/150.0);
glEnd(); // 程式結束
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
if(state==GLUT_DOWN) { // 按下去時產生座標,減一半,除一半.0,y 要加負號
printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y); // 顯示
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week03");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}




沒有留言:
張貼留言