Hi all, I am trying to simply put a triangle on the screen using OpenGLView in a Xamarin.Forms shared project. I believe my code is set up right, but no triangle ends up on the screen. Any insight would be much appreciated.
My Application class:
public class App : Application { uint colorRenderBuffer; uint positionSlot; uint colorSlot; OpenGLView view; // cube verticies Vector3[] Verticies = new Vector3[] { new Vector3 (1.0f, -1.0f, 0.0f), new Vector3 (1.0f, 1.0f, 0.0f), new Vector3 (-1.0f, 1.0f, 0.0f) }; /* new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(1.0f, -1.0f, -1.0f), new Vector3(1.0f, 1.0f, -1.0f), new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(-1.0f, -1.0f, -1.0f)};*/ Vector4[] Colors = new Vector4[]{ new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f) }; /* new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)};*/ byte[] Indices = new byte [] { 0, 1, 2 }; /* 2, 3, 0, 4, 6, 5, 4, 7, 6, 2, 7, 3, 7, 6, 2, 0, 4, 1, 4, 1, 5, 6, 2, 1, 1, 6, 5, 0, 3, 7, 0, 7, 4};*/ public App () { // The root page of your application var page = new ContentPage(); view = new OpenGLView (); view.HasRenderLoop = true; SetupRenderBuffers (); SetupFrameBuffer (); CompileShaders (); SetupVBOs (); view.OnDisplay = r => { GL.ClearColor(0.0f,0.0f,1.0f,1.0f); GL.Clear(ClearBufferMask.ColorBufferBit); GL.Viewport(0, 0, (int) view.Width, (int) view.Height); GL.DrawElements(BeginMode.Triangles, Indices.Length, DrawElementsType.UnsignedByte, 0); GL.Finish(); GL.Flush(); //GL.Viewport(0, 0, (int) view.Width, (int) view.Height); }; page.Content = view; MainPage = page; view.Display (); } protected override void OnStart () { // Handle when your app starts } protected override void OnSleep () { // Handle when your app sleeps } protected override void OnResume () { // Handle when your app resumes } void SetupRenderBuffers(){ GL.GenRenderbuffers (1, out colorRenderBuffer); GL.BindBuffer (BufferTarget.ArrayBuffer, colorRenderBuffer); } void SetupFrameBuffer(){ uint frameBuffer; GL.GenFramebuffers (1, out frameBuffer); GL.BindFramebuffer (FramebufferTarget.Framebuffer, frameBuffer); GL.FramebufferRenderbuffer (FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, RenderbufferTarget.Renderbuffer, colorRenderBuffer); } uint CompileShader(string shaderName, ShaderType shaderType){ string prefix; #if __IOS__ prefix = "OpenGLTesting.iOS."; #endif #if __ANDROID__ prefix = "OpenGLTesting.Droid."; #endif var assembly = typeof(App).GetTypeInfo ().Assembly; Stream stream = assembly.GetManifestResourceStream (prefix + shaderName + ".glsl"); string shaderString; using (var reader = new StreamReader (stream)) { shaderString = reader.ReadToEnd (); } Debug.WriteLine (shaderString); uint shaderHandle = (uint)GL.CreateShader (shaderType); GL.ShaderSource ((int)shaderHandle, shaderString); GL.CompileShader (shaderHandle); return shaderHandle; } void CompileShaders(){ uint vertexShader = CompileShader ("SimpleVertex", ShaderType.VertexShader); uint fragmentShader = CompileShader ("SimpleFragment", ShaderType.FragmentShader); uint programHandle = (uint)GL.CreateProgram (); GL.AttachShader (programHandle, vertexShader); GL.AttachShader (programHandle, fragmentShader); GL.LinkProgram (programHandle); GL.UseProgram (programHandle); positionSlot = (uint)GL.GetAttribLocation (programHandle, "Position"); colorSlot = (uint)GL.GetAttribLocation (programHandle, "SourceColor"); GL.EnableVertexAttribArray (positionSlot); GL.EnableVertexAttribArray (colorSlot); } void SetupVBOs(){ uint vertexBuffer; GL.GenBuffers (1, out vertexBuffer); GL.BindBuffer (BufferTarget.ArrayBuffer, vertexBuffer); GL.BufferData (BufferTarget.ArrayBuffer,(IntPtr)(Vector3.SizeInBytes * Verticies.Length), Verticies, BufferUsage.StaticDraw); GL.VertexAttribPointer (positionSlot, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0); uint colorBuffer; GL.GenBuffers (1, out colorBuffer); GL.BindBuffer (BufferTarget.ArrayBuffer, colorBuffer); GL.BufferData (BufferTarget.ArrayBuffer, (IntPtr)(Vector4.SizeInBytes * Colors.Length), Colors, BufferUsage.StaticDraw); GL.VertexAttribPointer (colorSlot, 4, VertexAttribPointerType.Float, false, Vector4.SizeInBytes, 0); uint indexBuffer; GL.GenBuffers (1, out indexBuffer); GL.BindBuffer (BufferTarget.ElementArrayBuffer, indexBuffer); GL.BufferData (BufferTarget.ElementArrayBuffer,(IntPtr)(sizeof(byte) * Indices.Length), Indices, BufferUsage.StaticDraw); } }
And here is my VertexShader
attribute vec4 Position; attribute vec4 SourceColor; varying vec4 DestinationColor; void main(void){ DestinationColor = SourceColor; gl_Position = Position; }
and my FragmentShader
varying lowp vec4 DestinationColor; void main(void){ gl_FragColor = DestinationColor; }