我正在尝试学习 OpenGL 并想在 SharpGL 中绘制一个三角形。但不幸的是,除了绿屏,我没有得到任何输出。我的代码的哪一部分是错误的?或 shrapGL 无法做到这一点。请不要发送 C++/C tuts,因为已经尝试过(很多),并且在 C# 中有点不同。我还从本教程中复制了我的代码https://learnopengl.com/Getting-started/Hello-Triangle
public OpenGL gl { get; set; }
uint shaderProgram;
uint VBO;
uint VAO;
private void openGLControl_OpenGLInitialized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
gl = openGLControl.OpenGL;
SetShaders();
InitializeTriangle();
}
private void openGLControl_OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
gl.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
gl.UseProgram(shaderProgram);
gl.BindVertexArray(VAO);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
gl.Flush();
}
private void openGLControl_Resized(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
gl.Viewport(0, 0, (int)openGLControl.ActualWidth, (int)openGLControl.ActualHeight);
}
private void InitializeTriangle()
{
float[] vertices ={ -0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f};
// Create VAO
uint[] arrayObjs = new uint[1];
gl.GenVertexArrays(1, arrayObjs);
VAO = arrayObjs[0];
// Create VBO
uint[] bufferObjects = new uint[1];
gl.GenBuffers(1, bufferObjects);
VBO = bufferObjects[0];
// Bind VAO
gl.BindVertexArray(VAO);
// Bind VBO
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO);
gl.BufferData(VBO, vertices, OpenGL.GL_STATIC_DRAW);
// Config VertexAttribPointer and Enable Attrib
gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 3 * sizeof(float), (IntPtr)0);
gl.EnableVertexAttribArray(0);
// Unbind VBO
//gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
// Unbind VAO
gl.BindVertexArray(0);
}
private void SetShaders()
{
// Vertex Shader
string vertexShaderSource = @"#version 330 core
layout (location = 0) in vec3 inPos;
void main()
{
gl_Position = vec4(inPos.x,inPos.y,inPos.z,1.0f);
}";
uint vertexShader = gl.CreateShader(OpenGL.GL_VERTEX_SHADER);
gl.ShaderSource(vertexShader, vertexShaderSource);
gl.CompileShader(vertexShader);
int[] success = new int[1];
gl.GetShader(vertexShader, OpenGL.GL_COMPILE_STATUS, success);
if (success != null && success.Count() > 0 && success[0] == 0)
{
StringBuilder builder = new StringBuilder();
gl.GetShaderInfoLog(vertexShader, 512, IntPtr.Zero, builder);
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(builder.ToString())));
}
// Fragment Shader
string fragmentShaderSource = @"#version 330 core
out vec4 passColor;
void main()
{
passColor = vec4(1.0f,0.5f,0.2f,1.0f);
}";
uint fragmentShader = gl.CreateShader(OpenGL.GL_FRAGMENT_SHADER);
gl.ShaderSource(fragmentShader, fragmentShaderSource);
gl.CompileShader(fragmentShader);
int[] successFragment = new int[1];
gl.GetShader(fragmentShader, OpenGL.GL_COMPILE_STATUS, successFragment);
if (successFragment != null && successFragment.Count() > 0 && successFragment[0] == 0)
{
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Not Success Fragment Shader")));
}
shaderProgram = gl.CreateProgram();
gl.AttachShader(shaderProgram, vertexShader);
gl.AttachShader(shaderProgram, fragmentShader);
gl.LinkProgram(shaderProgram);
gl.UseProgram(shaderProgram);
gl.DetachShader(shaderProgram, vertexShader);
gl.DetachShader(shaderProgram, fragmentShader);
gl.DeleteShader(vertexShader);
gl.DeleteShader(fragmentShader);
}