示例#1
0
			void initEyeTarget(EyeType eye)
			{
				EyeTarget e = new EyeTarget();
				e.eye = eye;
				e.fbo = GL.GenFramebuffer();
				e.depthTexture = GL.GenRenderbuffer();
				e.desc = OvrDLL.ovr_GetRenderDesc(session, eye, eye == EyeType.Left ? hmdDesc.LeftDefaultEyeFov : hmdDesc.RightDefaultEyeFov);
				e.renderTargetSize = OvrDLL.ovr_GetFovTextureSize(session, EyeType.Left, hmdDesc.LeftDefaultEyeFov, 1.0f);

				e.proj = OvrDLL.ovrMatrix4f_Projection(e.desc.Fov, 0.1f, 1000.0f, ProjectionModifier.ClipRangeOpenGL);

				//create the texture swap chain
				TextureSwapChainDesc swapDesc = new TextureSwapChainDesc()
				{
					Type = TextureType.Texture2D,
					ArraySize = 1,
					Format = TextureFormat.R8G8B8A8_UNORM_SRGB,
					Width = e.renderTargetSize.Width,
					Height = e.renderTargetSize.Height,
					MipLevels = 1,
					SampleCount = 1,
					StaticImage = 0
				};

				result = OvrDLL.ovr_CreateTextureSwapChainGL(session, ref swapDesc, ref e.swapChain);
				if (result < 0)
				{
					Console.WriteLine("Error creating swap chain");
					OvrDLL.ovr_GetLastErrorInfo(ref error);
					Console.WriteLine("Last Error Info: {0}-{1}", error.result, error.ErrorString);
				}

				int swapChainLength = 0;
				OvrDLL.ovr_GetTextureSwapChainLength(session, e.swapChain, ref swapChainLength);
				Console.WriteLine("Swapchain length: {0}", swapChainLength);

				for(int i = 0; i< swapChainLength; i++)
				{
					UInt32 texId = 0;
					OvrDLL.ovr_GetTextureSwapChainBufferGL(session, e.swapChain, i, ref texId);
					GL.BindTexture(TextureTarget.Texture2D, texId);
					GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
					GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
					GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
					GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

				}

				int currentIndex = 0;
				OvrDLL.ovr_GetTextureSwapChainCurrentIndex(session, e.swapChain, ref currentIndex);
				Console.WriteLine("Swapchain current index: {0}", currentIndex);

				UInt32 chainTexId = 0;
				OvrDLL.ovr_GetTextureSwapChainBufferGL(session, e.swapChain, currentIndex, ref chainTexId);

				GL.BindFramebuffer(FramebufferTarget.Framebuffer, e.fbo);
				GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, e.depthTexture);
				GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent32f, e.renderTargetSize.Width, e.renderTargetSize.Height);
				GL.FramebufferRenderbuffer(FramebufferTarget.DrawFramebuffer, FramebufferAttachment.Depth, RenderbufferTarget.Renderbuffer, e.depthTexture);
				
				GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, chainTexId, 0);
				DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[1] { DrawBuffersEnum.ColorAttachment0 };
				GL.DrawBuffers(1, drawBuffers);

				//check frame buffer completeness
				FramebufferErrorCode err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
				if(err != FramebufferErrorCode.FramebufferComplete)
				{
					Console.WriteLine("Error in frame buffer: {0}", err);
				}

				eyes[(int)eye] = e;

				GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
			}