OpenVR can someone explain me how to draw to vr headset?

Started by menackni, January 31, 2020, 10:16:17

Previous topic - Next topic

menackni

Hello there.

I watch a lot of openvr examples and most of it do VRCompositor_Submit(Eye,Texure, null, Submit).
But when i try do this it do nothing.
Can someone explain me what i do wrong?

Headset Oculus rift dk1.

Code below runs steamvr but dont draw what i want.

package openvr;

import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetWindowPos;
import static org.lwjgl.glfw.GLFW.glfwSetWindowSizeLimits;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glBlendFunc;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glViewport;
import static org.lwjgl.opengl.GL12.GL_TEXTURE_3D;
import java.awt.Dimension;
import java.awt.Toolkit;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.openvr.*;
import org.lwjgl.system.*;
import java.nio.*;
import static org.lwjgl.openvr.VR.*;
import static org.lwjgl.openvr.VRSystem.*;
import static org.lwjgl.system.MemoryStack.*;
import graphics.frameBuffers.frameBuffer;
import voxelRayCast.GSTextures;

public class helpme {

	static long window;
	static Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
	static int w=(int) screenSize.getWidth()/2;
	static int h=(int) screenSize.getHeight()/2;
	
	public static GSTextures tex0=new GSTextures();
	
	public static int VRtoken;
	
    public void run(){//START HERE---START HERE---START HERE---START HERE---START HERE---START HERE---START HERE---START HERE---START HERE---START HERE
    	HelloOpenVR();
		initWindow();
		tex0.initText();
		DrawLoop();
	}
	
    public static void HelloOpenVR(){

        System.err.println("VR_IsRuntimeInstalled()="+VR_IsRuntimeInstalled());
        System.err.println("VR_RuntimePath() ="+VR_RuntimePath());
        System.err.println("VR_IsHmdPresent()="+VR_IsHmdPresent());

        try(MemoryStack stack=stackPush()){
            IntBuffer peError=stack.mallocInt(1);

            VRtoken=VR_InitInternal(peError, VR.EVRApplicationType_VRApplication_Overlay);
            if(peError.get(0)==0){
                OpenVR.create(VRtoken);

                System.err.println("Model Number:"+VRSystem_GetStringTrackedDeviceProperty(
                    k_unTrackedDeviceIndex_Hmd,
                    ETrackedDeviceProperty_Prop_ModelNumber_String,
                    peError
                ));
                System.err.println("Serial Number:"+VRSystem_GetStringTrackedDeviceProperty(
                    k_unTrackedDeviceIndex_Hmd,
                    ETrackedDeviceProperty_Prop_SerialNumber_String,
                    peError
                ));

                IntBuffer www=stack.mallocInt(1);
                IntBuffer hhh=stack.mallocInt(1);
                VRSystem_GetRecommendedRenderTargetSize(www, hhh);
                System.err.println("Recommended width :"+www.get(0));
                System.err.println("Recommended height:"+hhh.get(0));
            }else{
                System.out.println("INIT ERROR SYMBOL:"+VR_GetVRInitErrorAsSymbol(peError.get(0)));
                System.out.println("INIT ERROR  DESCR:"+VR_GetVRInitErrorAsEnglishDescription(peError.get(0)));
            }
        }
    }
	
	public void initWindow(){
		
	    System.out.println("win Init Start.");

	    if(glfwInit()!=true){
	        System.err.println("INIT_GLFW_FAILED");
		System.exit(1);
	    }
	    window=glfwCreateWindow( w, h, "test", 0, 0);
	    glfwSetWindowPos(window, w/2, h/2);
	    glfwSetWindowSizeLimits(window, w, h, w, h);
	    glfwShowWindow(window);
	    glfwMakeContextCurrent(window);
	    GL.createCapabilities();
	    glViewport(0, 0, w, h);
	    glOrtho(0, w, h, 0, -1, 1);
	    glEnable(GL_BLEND);
	    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	    glEnable(GL_TEXTURE_2D);
	    glEnable(GL_TEXTURE_3D);
		
	    System.out.println("win Init End.");
	}
	
	public void Draw(){

	    glEnable(GL_BLEND);
	    GL11.glViewport(0, 0, w, h);
		
	    glColor3f(0,0,1);
		
	    glBegin(GL_QUADS);
        glVertex2f(0, 0);
        glVertex2f(w, 0);
        glVertex2f(w, h);
        glVertex2f(0, h);
        glEnd();
	}
	
	public void FinalDraw(int tex){

	    glEnable(GL_BLEND);
	    GL11.glViewport(0, 0, w, h);
		
	    glBindTexture(GL_TEXTURE_2D, tex);
		
	    glBegin(GL_QUADS);
	    glTexCoord2f(0,1);
        glVertex2f(0, 0);
        glTexCoord2f(1,1);
        glVertex2f(w, 0);
        glTexCoord2f(1,0);
        glVertex2f(w, h);
        glTexCoord2f(0,0);
        glVertex2f(0, h);
        glEnd();
        
            glBindTexture(GL_TEXTURE_2D, 0);
	}
	
    public void DrawLoop(){
    	frameBuffer fb=new frameBuffer(w, h);
    	
		while(!glfwWindowShouldClose(window)){
		    glfwPollEvents();
	            glClearColor(0, 0, 0, 1);
	            glClear(GL_COLOR_BUFFER_BIT);

	            fb.updateTexture(w,h);
	            fb.bindFrameBuffer();
	            Draw();//draw blue screen
	            fb.unbindCurrentFrameBuffer();
	            FinalDraw(fb.getTexture());//draw frame buffer to window
	        
	            Texture t1=Texture.create();
	             t1.set(tex0.noise.get(), VR.ETextureType_TextureType_OpenGL, VR.EColorSpace_ColorSpace_Gamma);
	                VRCompositor.VRCompositor_Submit(VR.EVREye_Eye_Left, t1, null, VR.EVRSubmitFlags_Submit_GlRenderBuffer);
			
			Texture t2=Texture.create();
	                t2.set(fb.getTexture(), VR.ETextureType_TextureType_OpenGL, VR.EColorSpace_ColorSpace_Gamma);
			VRCompositor.VRCompositor_Submit(VR.EVREye_Eye_Right, t2, null, VR.EVRSubmitFlags_Submit_GlRenderBuffer);
			
			glfwSwapBuffers(window);
		}
		VR.VR_ShutdownInternal();
	}
}