Eackz's picture

Please help, I cannot render a triangle in opengl 3.0 with GlControl

Hello, I've been working with OpenTK for trying to build my own Level Editor in Windows Forms with OpenGL 3.0, And so far I Have managed to get the Custom GLControl to change colors, but no matter what I do, I cannot get a triangle to Render. I swear I'm doing everything right, I checked for GL Errors, and the shaders didn't return any error messages. I downloaded the latest version of OpenTK, and even that didn't help :<

Here is my GLControl Code:

namespace WindowsFormsApplication1
{
    class OpenGL3 : OpenTK.GLControl
    {
        public OpenGL3()
            : base(new GraphicsMode(32, 24, 8, 4), 3, 0, GraphicsContextFlags.ForwardCompatible)
        {
            this.InitializeComponent();
        }
 
        private void InitializeComponent()
        {
            /*this.SuspendLayout();
            // 
            // OpenGL3
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.Name = "OpenGL3";
            this.ResumeLayout(false);*/
 
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (started)
            {
                base.OnPaint(e);
                rGraphicsEngine.BeginRender();
                MakeCurrent();
            }
        }
        protected override void OnResize(EventArgs e)
        {
            if (started)
            {
                if (ClientSize.Height == 0)
                {
                    ClientSize = new System.Drawing.Size(ClientSize.Width, 1);
                    GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
                }
                Invalidate(true);
            }
        }
        public bool started;
        public GraphicsEngine rGraphicsEngine;
 
    }
}
 
And Here is my Drawing Code:
 
namespace WindowsFormsApplication1
{
    struct VertexColor
    {
        public float PositionX;
        public float PositionY;
        public float PositionZ;
    };
    struct VertexTextured
    {
        public float PositionX;
        public float PositionY;
        public float PositionZ;
        public float U;
        public float V;
    };
    struct VertexLighted
    {
        public float PositionX;
        public float PositionY;
        public float PositionZ;
        public float U;
        public float V;
        public float NormalX;
        public float NormalY;
        public float NormalZ;
    };
    class GraphicsEngine
    {
        public GraphicsEngine()
        {
        }
        public void Initialize()
        {
            textureShader = new Shader();
            GL.ClearColor(Color.Blue);
            //GL.Enable(EnableCap.DepthTest);
            FBOPositionData[0] = new Vector3(-20.5f, -20.5f, 5.0f);
            FBOPositionData[1] = new Vector3(20.5f, -20.5f, 5.0f);
            FBOPositionData[2] = new Vector3(20.5f, 20.5f, 5.0f);
            FBOPositionData[3] = new Vector3(-20.5f, -20.5f, 5.0f);
            FBOPositionData[4] = new Vector3(20.5f, 20.5f, 5.0f);
            FBOPositionData[5] = new Vector3(-20.5f, 20.5f, 5.0f);
            XZCamera = new Camera();
            XZCamera.viewMatrix = Matrix4.LookAt(new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1));
            XZCamera.projectionMatrix = Matrix4.CreateOrthographic(1024, 1024, 1, 1000);
 
            GL.GenBuffers(1, out backBufferVertexBuffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, backBufferVertexBuffer);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, new IntPtr(FBOPositionData.Length * Vector3.SizeInBytes), FBOPositionData, BufferUsageHint.StaticDraw);
            //GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
 
            GL.GenVertexArrays(1, out backBufferVertexArray);
            GL.BindVertexArray(backBufferVertexArray);
 
            GL.BindBuffer(BufferTarget.ArrayBuffer, backBufferVertexBuffer);
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
 
            SetUpShaders();
            SetUpFBO();
        }
        public void SetUpShaders()
        {
            CompileShaders(vertexTextureShaderText, pixelTextureShaderText, textureShader);
        }
        public void CompileShaders(string vShaderString, string pShaderString, Shader shader)
        {
            shader.vertexShader = GL.CreateShader(ShaderType.VertexShader);
            shader.pixelShader = GL.CreateShader(ShaderType.FragmentShader);
            GL.ShaderSource(shader.vertexShader, vShaderString);
            GL.ShaderSource(shader.pixelShader, pixelTextureShaderText);
            GL.CompileShader(shader.vertexShader);
            GL.CompileShader(shader.pixelShader);
            MessageBox.Show(GL.GetShaderInfoLog(shader.vertexShader));
            MessageBox.Show(GL.GetShaderInfoLog(shader.pixelShader));
            shader.ShaderProgram1 = GL.CreateProgram();
            GL.AttachShader(shader.ShaderProgram1, shader.vertexShader);
            GL.AttachShader(shader.ShaderProgram1, shader.pixelShader);
            GL.LinkProgram(shader.ShaderProgram1);
            GL.UseProgram(shader.ShaderProgram1);
            GL.BindAttribLocation(textureShader.ShaderProgram1, 0, "in_Position");
        }
        public void RenderObjects()
        {
        }
        public void BeginRender()
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
            RenderFBO();
            EndRender();
            //GL.DisableVertexAttribArray(0);
        }
        public void RenderShader()
        {
        }
        public void EndRender()
        {
            gLControlReference.SwapBuffers();
            ec = GL.GetError();
            if (ec != 0)
            {
                MessageBox.Show(ec.ToString());
            }
        }
        public void SetUpFBO()
        {
        }
        public void RenderFBO()
        {
            GL.UseProgram(textureShader.ShaderProgram1);
            //GL.EnableClientState(ArrayCap.VertexArray);
            GL.BindVertexArray(backBufferVertexArray);
            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, backBufferVertexBuffer);
            GL.Viewport(0, 0, gLControlReference.Width, gLControlReference.Height);
            GL.DrawArrays(BeginMode.Triangles, 0, 6);
        }
        ErrorCode ec;
        Vector3[] FBOPositionData = new Vector3[6];
        public OpenGL3 gLControlReference;
        public Camera XZCamera;
        public Camera XYCamera;
        public Camera YZCamera;
        public Camera XYZCamera;
 
        public Shader textureShader;
        public int backBufferVertexArray;
        public int backBufferVertexBuffer;
        public string vertexTextureShaderText = @"
#version 330
 
in vec3 in_Position;
 
void main(void)
{
    gl_Position = vec4(in_Position, 1.0);
}";
 
        public string pixelTextureShaderText = @"
#version 330
 
out vec4 color;
 
void main(void)
{
    color.r = 1.0;
    color.g = 1.0;
    color.b = 0.0;
    color.a = 1.0;
}";
    }
}

Also, Sorry for the lack of code tags, The spam filter didn't like them :/


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

In a right-handed coordinate system, -z points towards the screen. I believe the issue is that your triangle is lying behind the viewer.

Try changing FBOPositionData to use negative z values:
FBOPositionData[0] = new Vector3(-20.5f, -20.5f, 5.0f);
to
FBOPositionData[0] = new Vector3(-20.5f, -20.5f, -5.0f);

One more thing is that GL.BindAttribLocation must be called before GL.LinkProgram, otherwise it will have no effect. This doesn't change anything in this specific case (you only have a single attribute), but it's something to keep in mind in the future.

Eackz's picture

Hmm, Strange. I did what you said, And I got something to render, but it's kinda weird. It only Renders works if you set only some of the z values to negative. I get only a section of the quad to render. Like, I have an L Shape instead of the whole quad.

Eackz's picture

Nvm, I found the solution, aparently I had to set all the depth values to 0.0f, since I had not set the z-buffer length. I also wasn't using any Orthographic View Matrix, so I couldn't set the z-buffer values.