embedding javaFX inside opengl

this code allows you to use Javafx as your GUI in OpenGL. Includes an example using LWJGL3 and GLFW

http://pastebin.java-gaming.org/e0c5c939d2a19
http://pastebin.java-gaming.org/a067532503d19
http://pastebin.java-gaming.org/c5c3d1a093f15

This looks interesting, not very familiar with JavaFX yet but would you mind explaining briefly the technique you are using here to embed JavaFX inside OpenGL? are you simply rendering the javafx gui to a javafx image and then copying that over to an opengl texture? or some other method?

JavaFX has components for embedding itself into Swing and SWT. What i’m doing is loading the image into a ByteBuffer and then using that to update a texture (you could subclass texupdater and use a PBO with a mapped bytebuffer too). then in my demo, I’m using GLFW to get input from the mouse and keyboard to feed to Javafx.

[quote=“deepthought,post:3,topic:54914”]
The org.lwjgl.util.stream package in LWJGL-FX contains a few implementations and supports buffering.

How are you getting mouse and keyboard events into javafx?

I’m not. It is a proof of concept demo, from 3 years ago using LWJGL 2. It features both rendering JavaFX content in an OpenGL scene and rendering the OpenGL scene as a JavaFX node. It isn’t something I’d use in a real application, so never bothered with event translation.

any ideas how i could draw a cursor in the texture for if the window hides the system cursor or if i’m doing an in-game screen like in doom3?

Have you tried GLFW cursor functions? I don’t see why you would need to draw it in the texture.

that works for GLFW, but i want the capability so i can do those in-game terminals like in doom3.

I’m assuming you’re projecting the JavaFX texture on a 3D surface then. In the same way, you could project a custom cursor texture, after rendering the JavaFX texture.

texture’s going to be UV mapped. I could just use java to composite the cursor on top before i update the texture. Or maybe do it inside my shader.

i need a bit of help with the compositing.
This is what I’m using now.


public boolean getPixels(IntBuffer dest, int width, int height)
    {
    	if(scenePeer!=null)
    	{
    	 boolean success=scenePeer.getPixels(dest, width, height);
    	 
    	
    	
    	if(success)
    	{
    		
    	
    		if(drawcursor&&currentframe!=null)
    		{
    		
    			cursorlock.lock();
    			System.out.println("draw cursor");
    			//NOTE composite the cursor
    			
    		com.sun.prism.Image img =(com.sun.prism.Image) currentframe.getPlatformImage();
    	
    			
    			
    			for(int x=0;x<currentframe.getWidth();x++)
    			{
    				for(int y=0;y<currentframe.getHeight();y++)
    				{
    					
    					int framex=(int) (mousex-currentframe.getHotspotX()+x);
    					int framey=(int) (mousey-currentframe.getHotspotY()+y);
    					
    					
    					if(framex>=0&&framex<=width&&framey>=0&&framey<=height)
    					{
    						
    						int index=framex+(framey*width);
    						
    						int destcolor=dest.get(index);
    						
    						int destb=destcolor&0xff;
    						int destg=(destcolor>>8)&0xff;
    						int destr= (destcolor>>16)&0xff;
    						int desta=(destcolor>>24)&0xff;
    						
    						
    						int srccolor=img.getArgb(x, y);
    						
    						int srcb=srccolor&0xff;
    						int srcg=(srccolor>>8)&0xff;
    						int srcr= (srccolor>>16)&0xff;
    						int srca=(srccolor>>24)&0xff;
    						
    						
    						
    						int enda=srca+desta*(1-srca);
    						
    						int endr=0,endb=0,endg=0;
    						
    						
    						System.out.println(enda);
    						if(enda!=0)
    						{
    						 endb=(srcb*srca+destb*desta*(1-srca))/enda;
    						 endg=(srcg*srca+destg*desta*(1-srca))/enda;
    						 endr=(srcr*srca+destr*desta*(1-srca))/enda;
    						}
    						
    						int endcolor=0;
    						endcolor|= endb;
    						endcolor|= endg<<8;
    						endcolor|=endr<<16;
    						endcolor|=enda<<24;
    						dest.put(index, endcolor);
    						
    						
    					}
    					
    					
    				}

    			}
    			cursorlock.unlock();
    			
    			
    		
    		}
    		
    		return true;
    	}

    	}
    	return false;
    }

It’s getting some strange results. for some odd reason it puts the cursor over the text field but under the button. if i switch the source and destination, it renders under the text field but over the button. why?

never mind. got it working

  int AlphaBlend( int bg,  int src)
   {
    	
    	int bA=src&0xff;
    	int gA=(src>>8)&0xff;
    	int rA=(src>>16)&0xff;
    	int aA=(src>>24)&0xff;
    	
    	int bB=bg&0xff;
    	int gB=(bg>>8)&0xff;
    	int rB=(bg>>16)&0xff;
    	int aB=(bg>>24)&0xff;
    	
    	
    	int rOut = (rA * aA / 255) + (rB * aB * (255 - aA) / (255*255));
    			int gOut = (gA * aA / 255) + (gB * aB * (255 - aA) / (255*255));
    			int bOut = (bA * aA / 255) + (bB * aB * (255 - aA) / (255*255));
    			int aOut = aA + (aB * (255 - aA) / 255);
    			
    			
    			return (aOut<<24)|(rOut<<16)|(gOut<<8)|(bOut);
   }

I’ve updated the pastebin code for cursor support.