private void RenderPreview(IntPtr data, uint cx, uint cy) { libobs.obs_video_info ovi = Obs.GetVideoInfo(); int newW = (int)cx; int newH = (int)cy; uint baseWidth = ovi.base_width; uint baseHeight = ovi.base_height; float previewAspect = (float)cx / cy; float baseAspect = (float)baseWidth / baseHeight; //calculate new width and height for source to make it fit inside the preview area if (previewAspect > baseAspect) { newW = (int)(cy * baseAspect); } else { newH = (int)(cx / baseAspect); } int centerX = ((int)cx - newW) / 2; int centerY = ((int)cy - newH) / 2; previewScale = (float)newW / baseWidth; GS.ViewportPush(); GS.ProjectionPush(); //setup orthographic projection of the whole scene to be presented on viewport GS.Ortho(0.0f, baseWidth, 0.0f, baseHeight, -100.0f, 100.0f); GS.SetViewport(centerX, centerY, newW, newH); //draw scene background ClearBackground(baseWidth, baseHeight); //render all visible sources Obs.RenderMainView(); //calculate bottom-right corner on scene space int right = (int)cx - centerX; int bottom = (int)cy - centerY; //ortho for the outer area which would normally not appear on scene GS.Ortho(-centerX, right, -centerY, bottom, -100.0f, 100.0f); GS.ResetViewport(); //render editing overlays RenderSceneEditing(data); GS.ProjectionPop(); GS.ViewportPop(); GS.LoadVertexBuffer(null); }
private Vector2 GetPositionInScene(Vector2 position) { libobs.obs_video_info ovi = Obs.GetVideoInfo(); int baseWidth = (int)ovi.base_width; int baseHeight = (int)ovi.base_height; int scaledWidth = (int)(Width / previewScale); int scaledHeight = (int)(Height / previewScale); int left = (scaledWidth - baseWidth) / 2; int top = (scaledHeight - baseHeight) / 2; return(new Vector2((int)((position.x / previewScale) - left), (int)((position.y / previewScale) - top))); }