我正在为我的项目使用 SharpGL,这将涉及使用 glsl 4.2 版。
在过去的几天里,我尝试了一些示例,但没有一个有效。为 OpenTK 编写的类似代码对我有用,所以我不知道我做错了什么,但在 SharpGL 中,如果我使用着色器和 VBO,则不会将任何内容绘制到屏幕上。要么我失去理智,要么 SharpGL 本身不能正常工作,我对此表示怀疑。
我宁愿将 SharpGL 用于其 WPF 控件。我附上了一个非常简单的示例代码。我将不胜感激有关此事的任何帮助。请注意,稍作改动的相同代码可在 OpenTK 中使用。
主窗口.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;
}
}
}
测试.vert
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
测试片段
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}
主窗口.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>