/// <summary> /// Start your app. /// </summary> /// <param name="width">Stage width</param> /// <param name="height">Stage height</param> /// <param name="viewportHeight"></param> /// <param name="viewportWidth"></param> /// <param name="rootType">The root class of your app</param> /// <exception cref="InvalidOperationException">When rootType is null or this function is called twice</exception> /// <exception cref="NotSupportedException">When the OpenGL framebuffer creation fails.</exception> /// <exception cref="ArgumentException">When width or height are less than 32.</exception> public static void Start(uint width, uint height, uint viewportWidth, uint viewportHeight, Type rootType) { Debug.WriteLine("Sparrow starting"); if (width < 32 || height < 32 || viewportWidth < 32 || viewportHeight < 32) { throw new ArgumentException($"Invalid dimensions: {width}x{height}"); } var ver = Gl.CurrentVersion; if (ver.Api == "gl") { if (ver.Major < 4) { throw new NotSupportedException("You need at least OpenGL 4.0 to run Sparrow!"); } } else { if (ver.Major < 3) { throw new NotSupportedException("You need at least OpenGL ES 3.0 to run Sparrow!"); } IsRunningOpenGLES = true; } Gl.Disable(EnableCap.CullFace); Gl.Disable(EnableCap.Dither); Gl.Enable(EnableCap.DepthTest); Gl.DepthFunc(DepthFunction.Always); BlendMode.Get(BlendMode.NORMAL).Activate(); FramebufferStatus status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != FramebufferStatus.FramebufferComplete) { throw new NotSupportedException("GL Framebuffer creation error. Status: " + status); } _viewPort = Rectangle.Create(0, 0, viewportWidth, viewportHeight); _previousViewPort = Rectangle.Create(); GPUInfo.PrintGPUInfo(); // Desktop GL core profile needs a VAO for vertex attrib pointers to work. uint vao = Gl.GenVertexArray(); Gl.BindVertexArray(vao); if (rootType == null) { throw new InvalidOperationException("Root cannot be null!"); } if (Root != null) { throw new InvalidOperationException("App already initialized!"); } _painter = new Painter(width, height); Stage = new Stage(width, height); DefaultJuggler = new Juggler(); UpdateViewPort(true); Root = (DisplayObject)Activator.CreateInstance(rootType); Stage.AddChild(Root); _frameId = 1; // starts with 1, so things on the first frame are cached }
public ScreenController(IntPtr window) { this.window = window; var glcontext = SDL.SDL_GL_CreateContext(window); SDL.SDL_GL_SetSwapInterval(1); shader = new ShaderProgram( File.ReadAllText(Assets.LoadPath("outputvert.glsl")), File.ReadAllText(Assets.LoadPath("outputfrag.glsl")) ); framebuffer = Gl.GenFramebuffer(); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer); texture = Gl.GenTexture(); Gl.UseProgram(shader); Gl.BindTexture(TextureTarget.Texture2D, texture); Gl.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, screenWidth, screenHeight, 0, PixelFormat.Rgb, PixelType.UnsignedByte, IntPtr.Zero); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, TextureParameter.Nearest); Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, TextureParameter.Nearest); var renderBuffer = Gl.GenRenderbuffer(); Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderBuffer); Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent16, screenWidth, screenHeight); Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, renderBuffer); Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, texture, 0); Gl.DrawBuffer(DrawBufferMode.ColorAttachment0); var error = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); switch (error) { case FramebufferErrorCode.FramebufferComplete: break; default: Console.WriteLine($"fbo error: {error}"); break; } vertices = new VBO <Vector3>( new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0), new Vector3(-1, 1, 0), } ); uvs = new VBO <Vector2>( new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), } ); triangles = new VBO <uint>( new uint[] { 0, 1, 2, 0, 2, 3 }, BufferTarget.ElementArrayBuffer ); drawScreen = new DrawRenderer( new ShaderProgram( File.ReadAllText(Assets.LoadPath("screenvert.glsl")), File.ReadAllText(Assets.LoadPath("screenfrag.glsl")) ) ); }