Graphics Programming with FreeGLUT and a Joystick
Drawing a shape is one thing, but it gets far more fun once you can control it with real hardware. In this post we will use the freeGLUT3 functions of the openGL library, with the help of a joystick, to move an object we created.
Our build system is Ubuntu 17.04 and our g++ editor is Sublime Text 3.
The object we will be moving is the square shown in the figure.
We create our shape with the code below. The key idea here is that we copy the x-axis values we get from the joystick into a static variable named spin, which is what feeds the rotation.
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0); //static spin=0.0
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
spin += donus; //add joystick spin ratio
For those joystick values to mean anything, the joystick has to be registered and referenced within glut.
Glut has developed a function exactly for this: glutJoystickFunc, which takes a function as a parameter. It also takes the number of milliseconds at which it should check the analog data.
glutJoystickFunc(joystick,25);
The joystick function it takes as a parameter is the one we can see here.
Each parameter has its own job: buttonmask is used to detect which buttons on the joystick are pressed, while x, y and z measure the values of the axes on the analog buttons, between -1000 and +1000.
Since we will use this rotation in the rotate function, we take its value mod 360.
void joystick(unsigned int buttonmask, int x, int y, int z)
{
donus = x%360;
glutPostRedisplay();
y_axis = y;
z_axis = z;
cout<<"X EKSENİ "<<x
<<endl<<"Y EKSENİ "<<y
<<endl<<"Z EKSENİ "<<z<<endl;
}
Demo video
sources:
https://www.opengl.org/discussion_boards/showthread.php/132295-JoyStick-implementation-with-GLUT
– Turkish Version –
FreeGLUT ve Joystick ile Grafik Programlama
Bir şekil çizmek bir yana, onu gerçek bir donanımla kontrol edebildiğinizde işler çok daha eğlenceli hale geliyor. Bu yazımızda openGL kütüphanesi olan freeGLUT3 fonksiyonlarını bir joystick yardımı ile kullanarak oluşturduğumuz bir nesneyi hareket ettireceğiz.
Derleme sistemimiz Ubuntu 17.04, g++ editörümüz Sublime Text 3.
Hareket ettireceğimiz nesne şekildeki kare.
Şeklimizi aşağıdaki kodumuzla oluşturuyoruz. Buradaki ana fikir şu: joystickten aldığımız x ekseni değerlerini spin adlı bir statik değişkene aktarıyoruz ve dönüşü besleyen şey de bu.
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0); //static spin=0.0
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
spin += donus; //add joystick spin ratio
O joystick değerlerinin bir anlam ifade etmesi için joysticki tanıtmamız ve bunu glut içerisine referans etmemiz gerekiyor.
Glut bunun için tam da uygun bir fonksiyon geliştirmiş: glutJoystickFunc adlı, parametre olarak bir fonksiyon alan bir fonksiyon. Bu fonksiyon aynı zamanda kaç milisaniyede bir analog veriyi kontrol etmesi gerektiğini de alıyor.
glutJoystickFunc(joystick,25);
Parametre olarak aldığı joystick fonksiyonu ise burada gördüğümüz fonksiyon.
Her parametrenin kendi görevi var: buttonmask joystick üzerindeki hangi butonlara basıldığını anlamak için kullanılıyor, x, y ve z ise analog tuşlardaki eksenlerin -1000 ile +1000 arasındaki değerlerini ölçüyor.
Biz bu dönüşü rotate fonksiyonunda kullanacağımız için mod360 değerini alıyoruz.
void joystick(unsigned int buttonmask, int x, int y, int z)
{
donus = x%360;
glutPostRedisplay();
y_axis = y;
z_axis = z;
cout<<"X EKSENİ "<<x
<<endl<<"Y EKSENİ "<<y
<<endl<<"Z EKSENİ "<<z<<endl;
}
Çalışma videosu
kaynaklar:
https://www.opengl.org/discussion_boards/showthread.php/132295-JoyStick-implementation-with-GLUT

