This project is read-only.

Simple shading and vbo not working. Please help.

Nov 22, 2012 at 3:17 PM

Hi. 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. 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.

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>

Please help. Please.

Aug 4, 2014 at 4:13 PM
        var GL_INFO_LOG_LENGTH = 35716U;
        var k = new int[1];
        this.openGl.GetShader(shader.Value, GL_INFO_LOG_LENGTH, k);
        if (k[0] == -1)
        {
            return string.Empty;
        }

        if (k[0] == 0)
        {
            return string.Empty;
        }

        var result = new StringBuilder();
        result.Capacity = k[0];
        unsafe
        {
            fixed (void* p = k)
            {
                this.openGl.GetShaderInfoLog(shader.Value, k[0], new IntPtr(p), result);
            }
        }

        return result.ToString();