我想在我的OpenGL GLUT程序中使用鼠標滾輪來放大和縮小場景?我怎麼做?在GLUT中使用鼠標滾輪
28
A
回答
19
請注意,古老的Nate Robin's GLUT庫不支持滾輪。但是,稍後GLUT的實現像FreeGLUT那樣。
在FreeGLUT中使用滾輪非常簡單。方法如下:
聲明一個回調函數,當滾動滾輪時應該調用該函數。這是原型:
void mouseWheel(int, int, int, int);
與(免費)轉運蛋白功能glutMouseWheelFunc()註冊回調。
glutMouseWheelFunc(mouseWheel);
定義回調函數。第二個參數給出了滾動的方向。 +1的值是前向的,-1是向後的。
void mouseWheel(int button, int dir, int x, int y)
{
if (dir > 0)
{
// Zoom in
}
else
{
// Zoom out
}
return;
}
就是這樣!
32
Freeglut的glutMouseWheelFunc回調取決於版本,並在十用標準鼠標功能不可靠,測試按鈕3和4
上glutMouseWheelFunc狀態OpenGlut註釋:
由於缺乏信息關於鼠標,此時不可能在X上正確地實現此操作。使用此功能 限制了您的應用程序的可移植性。 (此功能在 X上工作,只是不可靠。)鼓勵您使用標準的可靠鼠標按鈕報告,而不是輪式事件。
使用標準GLUT鼠標報告:
#include <GL/glut.h>
<snip...>
void mouse(int button, int state, int x, int y)
{
// Wheel reports as button 3(scroll up) and button 4(scroll down)
if ((button == 3) || (button == 4)) // It's a wheel event
{
// Each wheel event reports like a button click, GLUT_DOWN then GLUT_UP
if (state == GLUT_UP) return; // Disregard redundant GLUT_UP events
printf("Scroll %s At %d %d\n", (button == 3) ? "Up" : "Down", x, y);
}else{ // normal button event
printf("Button %s At %d %d\n", (state == GLUT_DOWN) ? "Down" : "Up", x, y);
}
}
<snip...>
glutMouseFunc(mouse);
如前所述的OP,它是 「死簡單」。他錯了。
相關問題
- 1. 使用鼠標滾輪添加滾動
- 2. 鼠標滾輪不在tmux中滾動
- 3. SetKeyDelay鼠標滾輪
- 4. 在銫中禁用鼠標滾輪
- 5. 使用鼠標滾輪和鼠標移動滾動
- 6. Popup - 滾動鼠標滾輪
- 7. 將D3.js從鼠標滾輪變爲控制+鼠標滾輪
- 8. 在C++ builder中使用鼠標滾輪進行TScrollbox滾動
- 9. 如何使用鼠標滾輪在WPF中水平滾動?
- 10. 使用鼠標滾輪縮放圖像。
- 11. 使用鼠標滾輪放大
- 12. 使用鼠標滾輪移動圖像
- 13. 使用鼠標滾輪與jQuery
- 14. GMMap使用鼠標滾輪縮放。
- 15. 如何使用鼠標滾輪
- 16. 放大CTRL +鼠標滾輪在DotNetBrowser中
- 17. LWJGL鼠標滾輪輸入
- 18. jQuery綁定鼠標滾輪
- 19. 鼠標滾輪click&jquery.delegate
- 20. jscrollpane水平鼠標滾輪
- 21. python詛咒鼠標滾輪
- 22. 防反跳鼠標滾輪
- 23. 鼠標滾輪事件
- 24. as3鼠標滾輪反轉
- 25. 鼠標滾輪導航
- 26. Adobe AIR鼠標滾輪
- 27. ContextMenuStrip和鼠標滾輪
- 28. 鼠標滾輪速度
- 29. 使用Qt模仿/僞造鼠標點擊鼠標滾輪
- 30. 滾動面板鼠標滾輪滾動
對我來說很煩,freeGLUT似乎沒有實現glutMouseWheelFunc()回調。相當具有諷刺意味的是,海報抱怨這種發帖方式,但你爲自己發佈的答案是不正確的。 – Rich 2009-01-12 20:57:05
除此之外 - 即使使用`#include`來編譯代碼,glutMouseWheelFunc似乎也不會被調用(就像在Ubuntu 10.04 x86_64上發佈的,它發佈了freeglut 2.6.0)。 解決方法是使用常規的`glutMouseFunc`回調並檢查`按鈕== 3`是否在輪子上,`按鈕== 4'是否輪子不亮。 –
2010-07-14 21:13:09