我目前正在为嵌入式系统开发带有OpenGL(LWJGL)的3d查看器。
无需赘述,有一个用Java / Swing编写的应用程序,当前已在Java + Swing上完全实现了UI和逻辑。但是我们决定,除了拥有2d自上而下的图片之外,拥有3d视图也很酷,这就是我要来的地方。
我会说我必须使用GLES 3.0或Opengl 2.1,我更喜欢GLES,因为与2.1相比,它具有更多功能
我还有一个使用GLFW窗口系统(LWJGL的默认设置)实现的独立应用程序,该应用程序可以在设备上正常运行-它不会滞后,并且可以提供不错的FPS。同样,在GLFW窗口中运行时。
但是现在我需要最好将其附加到JFrame,这就是我的问题所在。基本上,我需要将3D视图渲染为背景,然后在其顶部显示Swing按钮/面板和其他窗口(例如,带有选项菜单)。
我已经实现了一个基本的算法,即简单地读取FrameBuffer并将其绘制在画布上作为光栅图像。但这很慢。我得到的像是10 FPS。
private void render()
{
logic.Render(window); //GLFW window
window.update();
ByteBuffer nativeBuffer = BufferUtils.createByteBuffer(GlobalSettings.WINDOW_WIDTH*GlobalSettings.WINDOW_HEIGHT*4);
BufferedImage image = new BufferedImage(GlobalSettings.WINDOW_WIDTH,GlobalSettings.WINDOW_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);
GLES30.glReadPixels(0, 0, GlobalSettings.WINDOW_WIDTH,GlobalSettings.WINDOW_HEIGHT, GLES20.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, nativeBuffer);
byte[] imgData = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
nativeBuffer.get(imgData);
Graphics g = canvas.getGraphics();
g.drawImage(image, 0,0, GlobalSettings.WINDOW_WIDTH,GlobalSettings.WINDOW_HEIGHT, null);
}
我尝试过的另一件事是使用该库https://github.com/LWJGLX/lwjgl3-awt,似乎很多建议。问题是,它使用了OpenGL 3.1的某些功能,并且使其在2.1上下文下完全可以正常工作。但是对于GLES-我根本无法使它工作,而且显然不能让它为2.1创建上下文,然后进一步使用GLES功能,因此它基本上破坏了我的整个渲染代码。
也许我只是做的不对,但是无论如何-我无法使其适合我。
这就是我所站的地方。我以为我会在这里寻求帮助。在我看来,这时似乎必须完全为OpenGL / GLFW编写整个接口,而完全放弃Swing。这根本不理想-我什至想知道我们是否有这样的时间。
如果可以通过AWT使我的工作正常运行,请给我指明正确的方向。
最佳答案
我将自己发布答案。据我所知,它在Linux桌面和嵌入式系统上都可以很好地工作。
这个想法是采用EGL上下文和GLES,并将AWT画布表面设置为该上下文的直接目标。您无需读取缓冲区,然后将其绘制在画布上。
以下是我的Window类初始化。
private Canvas canvas;
private JAWTDrawingSurface ds;
public long display;
private long eglDisplay;
public long drawable;
private long surface;
public static final JAWT awt;
static {
awt = JAWT.calloc();
awt.version(JAWT_VERSION_1_4);
if (!JAWT_GetAWT(awt))
throw new AssertionError("GetAWT failed");
}
public void lock() throws AWTException {
int lock = JAWT_DrawingSurface_Lock(ds, ds.Lock());
if ((lock & JAWT_LOCK_ERROR) != 0)
throw new AWTException("JAWT_DrawingSurface_Lock() failed");
}
public void unlock() throws AWTException {
JAWT_DrawingSurface_Unlock(ds, ds.Unlock());
}
public void Init2()
{
frame = new JFrame("AWT test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(width, height));
canvas = new Canvas();
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
int error;
System.out.println("Window init2() started");
this.ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
//JAWTDrawingSurface ds = JAWT_GetDrawingSurface(canvas, awt.GetDrawingSurface());
try
{
lock();
try
{
JAWTDrawingSurfaceInfo dsi = JAWT_DrawingSurface_GetDrawingSurfaceInfo(ds, ds.GetDrawingSurfaceInfo());
JAWTX11DrawingSurfaceInfo dsiWin = JAWTX11DrawingSurfaceInfo.create(dsi.platformInfo());
int depth = dsiWin.depth();
this.display = dsiWin.display();
this.drawable = dsiWin.drawable();
System.out.printf("EGL Display %d, drawable: \n", display, drawable);
eglDisplay = eglGetDisplay(display);
EGLCapabilities egl;
try (MemoryStack stack = stackPush()) {
IntBuffer major = stack.mallocInt(1);
IntBuffer minor = stack.mallocInt(1);
if (!eglInitialize(eglDisplay, major, minor)) {
throw new IllegalStateException(String.format("Failed to initialize EGL [0x%X]", eglGetError()));
}
egl = EGL.createDisplayCapabilities(eglDisplay, major.get(0), minor.get(0));
}
System.out.println("EGL caps created");
IntBuffer attrib_list = BufferUtils.createIntBuffer(18);
attrib_list.put(EGL_CONFORMANT).put(EGL_OPENGL_ES2_BIT);
//attrib_list.put(EGL_ALPHA_MASK_SIZE).put(4);
//attrib_list.put(EGL_ALPHA_SIZE).put(4);
//attrib_list.put(EGL_RED_SIZE).put(5);
//attrib_list.put(EGL_GREEN_SIZE).put(6);
//attrib_list.put(EGL_BLUE_SIZE).put(5);
//attrib_list.put(EGL_DEPTH_SIZE).put(4);
//attrib_list.put(EGL_SURFACE_TYPE).put(EGL_WINDOW_BIT);
attrib_list.put(EGL_NONE);
attrib_list.flip();
PointerBuffer fbConfigs = BufferUtils.createPointerBuffer(1);
IntBuffer numConfigs = BufferUtils.createIntBuffer(1);
boolean test2 = eglChooseConfig(eglDisplay, attrib_list, fbConfigs,numConfigs);
if (fbConfigs == null || fbConfigs.capacity() == 0) {
// No framebuffer configurations supported!
System.out.println("No supported framebuffer configurations found");
}
long test = numConfigs.get(0);
IntBuffer context_attrib_list = BufferUtils.createIntBuffer(18);
context_attrib_list.put(EGL_CONTEXT_MAJOR_VERSION).put(3);
context_attrib_list.put(EGL_CONTEXT_MINOR_VERSION).put(0);
context_attrib_list.put(EGL_NONE);
context_attrib_list.flip();
long context = eglCreateContext(eglDisplay,fbConfigs.get(0),EGL_NO_CONTEXT,context_attrib_list);
error = eglGetError();
surface = eglCreateWindowSurface(eglDisplay,fbConfigs.get(0),drawable,(int[])null);
error = eglGetError();
eglMakeCurrent(eglDisplay,surface,surface,context);
error = eglGetError();
GLESCapabilities gles = GLES.createCapabilities();
System.out.println("CLES caps created");
}
finally
{
unlock();
}
}
catch (Exception e)
{
System.out.println("JAWT Failed");
}
// Render with OpenGL ES
glClearColor(0f,0f,0f,1f);
glfwSwapInterval(vSync);
glEnable(GL_DEPTH_TEST);
int[] range = new int[2];
int[] precision = new int[2];
glGetShaderPrecisionFormat(GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, range, precision);
System.out.println("Window context Initialized");
}
请注意,属性列表可以是您想要的任何内容,也可以什么都不是。我实际上没有遇到GLES问题,但是当我没有将3.0指定为上下文的版本兼容性时-由于某种原因,它试图使用OpenGl ES-CL 1.1并失败了。无论如何,只需指定次要和主要上下文版本即可对其进行修复。
另请注意,这仅是初始化。 JFrame和Canvas在其他位置创建,然后Canvas传递给Window类构造函数。
另一个重要的事情是lock()/ unlock()函数。您将收到许多XCB错误,如果没有这些错误,则会崩溃-因为其他线程在绘制时会尝试更新显示,从而导致冲突,并且XCB将会崩溃。
另外,在交换缓冲区(即实际绘制帧缓冲区时)时需要锁定和解锁。
public void update()
{
try
{
lock();
eglSwapBuffers(eglDisplay, surface);
unlock();
}
catch (Exception e)
{
System.out.println("Swap buffers failed");
}
}
不要介意缺少我当前的异常处理-一旦找到解决方案,我就在编辑答案,以免我的先前版本使人们感到困惑。
我还要感谢lwjgl-awt项目,因为它给了我一些想法。它不支持EGL,因此我不得不对其进行一些修改,但是我从那里开始参与其中,因此在应有的信誉中获得了信誉。
https://github.com/LWJGLX/lwjgl3-awt
为了比较起见,这里是GLFW版本。基本上,这是同一个类的初始化,但是它只是做其他事情。但是,这里的窗口是直接在方法内部创建的。
public void Init()
{
System.out.println("Window init() started");
GLFWErrorCallback.createPrint().set();
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize glfw");
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
// GLFW setup for EGL & OpenGL ES
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
if (windowHandle == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
System.out.printf("Window handle created %d\n", windowHandle);
SetCallbacks();
// EGL capabilities
displayHandle = glfwGetEGLDisplay();
System.out.printf("EGL DisplayHandle %d\n", displayHandle);
try (MemoryStack stack = stackPush()) {
IntBuffer major = stack.mallocInt(1);
IntBuffer minor = stack.mallocInt(1);
if (!eglInitialize(displayHandle, major, minor)) {
throw new IllegalStateException(String.format("Failed to initialize EGL [0x%X]", eglGetError()));
}
EGLCapabilities egl = EGL.createDisplayCapabilities(displayHandle, major.get(0), minor.get(0));
}
System.out.println("EGL caps created");
// OpenGL ES capabilities
glfwMakeContextCurrent(windowHandle);
System.out.printf("Current context: %d.%d rev %d, Client_Context: %d\n",glfwGetWindowAttrib(windowHandle,GLFW_CONTEXT_VERSION_MAJOR),
glfwGetWindowAttrib(windowHandle,GLFW_CONTEXT_VERSION_MINOR), glfwGetWindowAttrib(windowHandle,GLFW_CONTEXT_REVISION),
glfwGetWindowAttrib(windowHandle,GLFW_CLIENT_API));
GLESCapabilities gles = GLES.createCapabilities();
System.out.println("CLES caps created");
// Render with OpenGL ES
//glfwShowWindow(windowHandle);
glClearColor(0f,0f,0f,1f);
glfwSwapInterval(vSync);
glEnable(GL_DEPTH_TEST);
int[] range = new int[2];
int[] precision = new int[2];
glGetShaderPrecisionFormat(GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, range, precision);
System.out.printf("Current context: %d.%d\n",glfwGetWindowAttrib(windowHandle,GLFW_CONTEXT_VERSION_MAJOR),
glfwGetWindowAttrib(windowHandle,GLFW_CONTEXT_VERSION_MINOR));
}