Gilad
Gilad

Reputation: 353

SharpGL: Simple shading and vbo not working

I am using SharpGL for my project, which will involve using glsl version 4.2.

I have tried a few examples over the past few days, none have been working. Similar code written for OpenTK have worked for me, so I have no idea what I am doing wrong, but in SharpGL nothing is drawn to the screen if I am using shaders and VBO. Either I am losing my mind or something isn't working correctly with SharpGL itself, which I doubt.

I would rather use SharpGL for its WPF control. I am attaching a very simple example code. I would appreciate any help on the matter. Note that the same code with minor changes works in OpenTK.

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

using System.Runtime.InteropServices;
using System.IO;

using SharpGL;
using SharpGL.SceneGraph;

namespace SharpGLTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        OpenGL gl;
        uint theProgram, positionBufferObject;
        string strVertexShader = "test.vert";
        string strFragmentShader = "test.frag";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            gl = args.OpenGL;

            InitializeVertexBuffer();
            InitializeProgram();
        }

        private uint CreateProgram(List<uint> shaderList)
        {
            uint program = gl.CreateProgram();

            foreach (uint shader in shaderList)
                gl.AttachShader(program, shader);

            gl.LinkProgram(program);

            ProgramErrorInfo(program);

            foreach (uint shader in shaderList)
                gl.DetachShader(program, shader);

            return program;
        }

        private uint CreateShader(uint eShaderType, string strShaderFile)
        {
            uint shader = gl.CreateShader(eShaderType);
            string[] strFileData = { File.ReadAllText(strShaderFile) };

            gl.ShaderSource(shader, strFileData);

            gl.CompileShader(shader);

            ShaderErrorInfo(shader);

            return shader;
        }

        private void InitializeProgram()
        {
            List<uint> shaderList = new List<uint>();

            shaderList.Add(CreateShader(OpenGL.GL_VERTEX_SHADER, strVertexShader));
            shaderList.Add(CreateShader(OpenGL.GL_FRAGMENT_SHADER, strFragmentShader));

            theProgram = CreateProgram(shaderList);

            foreach (uint shader in shaderList)
                gl.DeleteShader(shader);

        }

        private void InitializeVertexBuffer()
        {
            float[] vertexPositions = {
                0.75f, 0.75f, 0.0f, 1.0f,
                0.75f, -0.75f, 0.0f, 1.0f,
                -0.75f, -0.75f, 0.0f, 1.0f,
            };
            GCHandle handle = GCHandle.Alloc(vertexPositions, GCHandleType.Pinned);
            IntPtr vertexPtr = handle.AddrOfPinnedObject();
            var size = Marshal.SizeOf(typeof(float)) * vertexPositions.Length;

            uint[] pbo = new uint[1];
            gl.GenBuffers(1, pbo);
            positionBufferObject = pbo[0];

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBufferObject);
            gl.BufferData(OpenGL.GL_ARRAY_BUFFER, size, vertexPtr, OpenGL.GL_STATIC_DRAW);
            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);

            handle.Free();
        }

        private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);

            gl.UseProgram(theProgram);

            gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBufferObject);
            gl.EnableVertexAttribArray(0);
            gl.VertexAttribPointer(0, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);

            gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);

            gl.DisableVertexAttribArray(0);
            gl.UseProgram(0);

            gl.Flush();
        }

        private void OpenGLControl_Resized(object sender, OpenGLEventArgs args)
        {
            gl.Viewport(0, 0, gl.RenderContextProvider.Width, gl.RenderContextProvider.Height);
        }

        private bool ShaderErrorInfo(uint shaderId)
        {
            StringBuilder builder = new StringBuilder(2048);
            gl.GetShaderInfoLog(shaderId, 2048, IntPtr.Zero, builder);
            string res = builder.ToString();
            if (!res.Equals(""))
            {
                System.Console.WriteLine(res);
                return false;
            }

            return true;
        }

        private bool ProgramErrorInfo(uint programId)
        {
            StringBuilder builder = new StringBuilder(2048);
            gl.GetProgramInfoLog(programId, 2048, IntPtr.Zero, builder);
            string res = builder.ToString();
            if (!res.Equals(""))
            {
                System.Console.WriteLine(res);
                return false;
            }

            return true;
        }

    }
}

test.vert

#version 330

layout(location = 0) in vec4 position;
void main()
{
    gl_Position = position;
}

test.frag

#version 330

out vec4 outputColor;
void main()
{
   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}

MainWindow.xaml

<Window x:Class="SharpGLTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF">
    <Grid>
        <sharpGL:OpenGLControl x:Name="OpenGLControl"
            OpenGLInitialized="OpenGLControl_OpenGLInitialized"
            OpenGLDraw="OpenGLControl_OpenGLDraw" 
            RenderContextType="FBO"
            Resized="OpenGLControl_Resized"
                               />
    </Grid>
</Window>

Upvotes: 1

Views: 4288

Answers (2)

Nicolas David
Nicolas David

Reputation: 179

I know this question is rather old, however, I was googling a way to use gl.GetShaderInfoLog when I got first here, and then to the Shader.cs of SharpGL's source code.

In SharpGl there is indeed no glGetShaderiv/glGetProgramiv. Yet, this is not the info log that gives us the compile status, this is obtained like so (extract from Shader.cs):

public bool GetCompileStatus(OpenGL gl)
    {
        int[] parameters = new int[] { 0 };
        gl.GetShader(shaderObject, OpenGL.GL_COMPILE_STATUS, parameters);
        return parameters[0] == OpenGL.GL_TRUE;
    }

In my case, this showed me an error, while your way didn't raise a thing.

In the end, Tim was on the right track about compiling error. Now, do YOU really have a compiling error ? I don't know, but to answer this, properly checking a compiling error is crucial.

Upvotes: 1

Tim
Tim

Reputation: 35943

Not sure if sharpGL does error checking, but if not you should probably have some glGetError somewhere to test.

I'm not exactly sure what your problem could be, but I wanted to point out that I don't think checking the length of the infolog is a sufficient way to test if an operation was successful.

To test for error in shader compile/link, you should use glGetShaderiv/glGetProgramiv with the target GL_COMPILE_STATUS or GL_LINK_STATUS. The result of this boolean determines if the compile/link was successful.

Using infolog length will probably be correct most of the time, but it's not guaranteed. I've seen bugs in some platforms where a link can fail and have no error log, and I also know that it's possible for a compile/link to succeed and still print a message.

Upvotes: 0

Related Questions