简体   繁体   English

从Java中的GLFW获取鼠标坐标的问题(可能是字节缓冲区问题)

[英]problems with getting the mouse coordinates from GLFW in Java (potentially a bytebuffer issue)

I recently made the switch from LWJGL 2 to LWJGL 3, and after a few hours of gaping at the documentation and assembling a program to use it, I had this code. 我最近进行了从LWJGL 2到LWJGL 3的切换,在花了几个小时研究文档并组装一个程序来使用它之后,我有了这段代码。 note that the code in the methods are all static, and Eclipse is giving me no issues with the code related to this. 请注意,这些方法中的代码都是静态的,并且Eclipse没有给我与此相关的代码问题。 Also, note that changing it from allocateDirect to allocate had no effect. 另外,请注意,将其从allocateDirect更改为allocate无效。

  //At the beginning of the class declaration: public static ByteBuffer mouseXb=ByteBuffer.allocateDirect(8), mouseYb=ByteBuffer.allocateDirect(8); public static double mouseX=0,mouseY=0; //Then later, in another method in the same class glfwPollEvents(); glfwGetCursorPos(window, mouseXb, mouseYb); mouseX=mouseXb.getDouble(); mouseY=mouseYb.getDouble(); System.out.println(mouseX+", "+mouseY); mouseXb.flip(); mouseYb.flip(); 

Oddly, though, I get values like: (Also note that they only changed when the mouse moved around in the window, and never when outside of it, nor when the mouse was not moving) 不过,奇怪的是,我得到的值如下:(还要注意,它们仅在鼠标在窗口中四处移动时才会更改,而在窗口外或鼠标不动时则不会更改)

 2.0857E-317, 2.604651E-317 3.121831E-317, 2.604651E-317 5.1940924E-317, 2.604651E-317 7.2664804E-317, 2.604651E-317 6.7490474E-317, 2.0865855E-317 4.6771653E-317, 7.785178E-317 5.19561E-317, 5.7129166E-317 

Okay, I fixed the issue, but I'll leave this post here in case anyone else has the same problem. 好的,我已解决了该问题,但是如果其他人也有同样的问题,我将在此处保留此帖子。 It turns out GLFW uses little endian and Bytebuffers are big endian by default. 事实证明,默认情况下GLFW使用Little Endian,而Bytebuffers是big endian。

You can solve the issue by adding this (if you were using my code): mouseYb.order(ByteOrder.LITTLE_ENDIAN); mouseXb.order(ByteOrder.LITTLE_ENDIAN); 您可以通过添加以下内容(如果使用的是我的代码)来解决此问题: mouseYb.order(ByteOrder.LITTLE_ENDIAN); mouseXb.order(ByteOrder.LITTLE_ENDIAN); mouseYb.order(ByteOrder.LITTLE_ENDIAN); mouseXb.order(ByteOrder.LITTLE_ENDIAN);

I'm still quite new to LWJGL 3 myself, and had some trouble with the ByteBuffers. 我本人对LWJGL 3还是很陌生,并且在ByteBuffers方面遇到了一些麻烦。 After a bit of looking around I found this page: LWJGL - Bindings FAQ . 经过一番环顾,我发现了以下页面: LWJGL-Bindings FAQ

In the org.lwjgl package there's a class called BufferUtils, which has a static function to generate a ByteBuffer. 在org.lwjgl包中,有一个称为BufferUtils的类,该类具有用于生成ByteBuffer的静态函数。 From the source code I noticed they called the order method too, just like with your fix, albeit with a different parameter: 从源代码中,我注意到它们也调用了order方法,就像您的修复程序一样,尽管具有不同的参数:

public static ByteBuffer createByteBuffer(int capacity)
{
    return BUFFER_ALLOCATOR.malloc(capacity).order(ByteOrder.nativeOrder());
}

The FAQ recommends using this option. FAQ建议使用此选项。 Apparently your code's working fine now, but I just thought you should know in case you run into any problems in the future. 显然您的代码现在可以正常工作,但我只是想您应该知道,以防将来遇到任何问题。

Also, thanks for your code snippet, I didn't know how to work the ByteBuffers, and now I do! 另外,感谢您的代码片段,我不知道如何使用ByteBuffer,现在可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM