opentk没有正确设置三角形的颜色



我使用的是opentk,使用的是c#,我想用统一的颜色来设置对象的颜色,但是三角形的颜色总是黑色

using System;
using OpenTK;
using OpenTK.Mathematics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
namespace open
{
class Game : GameWindow
{
int VBO, VAO;
int ShaderProgram;
public Color4 TriColor { get; set; }
public void tricolor(Color4 val)
{
int loc = GL.GetUniformLocation(VBO, "input_color");
if (loc == -1)
throw new Exception("cannot find location of uniform");
GL.Uniform4(loc, val.R, val.G, val.B, val.A);
Console.WriteLine("edit color");
//GL.GetUniform()
}
float[] positions = new float[]
{
0.0f,  0.5f, 0f,
0.5f, -0.5f, 0f,
-0.5f, -0.5f, 0f
};
public Game() : base(GameWindowSettings.Default, NativeWindowSettings.Default)
{
Console.WriteLine("program trying to start");
}
protected override void OnResize(ResizeEventArgs e)
{
GL.Viewport(0, 0, e.Width, e.Height);
base.OnResize(e);
}
protected override void OnLoad()
{
VBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * positions.Length, positions, BufferUsageHint.DynamicDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
VAO = GL.GenVertexArray();
GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.BindVertexArray(0);

string vertixShaderCode =
load_shaders.loadShaderFile(@"C:UsersAhmedsourcereposopenopenvertex shader.shader");

string FragmentShaderCode = 
load_shaders.loadShaderFile(@"C:UsersAhmedsourcereposopenopenfragment shader.shader");
int VertexShaderHandel = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(VertexShaderHandel, vertixShaderCode);
GL.CompileShader(VertexShaderHandel);
string Vshaderinfo = GL.GetShaderInfoLog(VertexShaderHandel);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Vshaderinfo);
Console.ForegroundColor = ConsoleColor.White;
int FragmentShaderHandel = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(FragmentShaderHandel, FragmentShaderCode);
GL.CompileShader(FragmentShaderHandel);
string Fshaderinfo = GL.GetShaderInfoLog(FragmentShaderHandel);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Fshaderinfo);
Console.ForegroundColor = ConsoleColor.White;
ShaderProgram = GL.CreateProgram();
GL.AttachShader(ShaderProgram, VertexShaderHandel);
GL.AttachShader(ShaderProgram, FragmentShaderHandel);
GL.LinkProgram(ShaderProgram);
GL.DetachShader(ShaderProgram, VertexShaderHandel);
GL.DetachShader(ShaderProgram, FragmentShaderHandel);
GL.DeleteShader(VertexShaderHandel);
GL.DeleteShader(FragmentShaderHandel);
tricolor(new Color4(0f, 0f, 1f, 1f));
base.OnLoad();
}
protected override void OnUnload()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DeleteBuffer(VBO);

GL.UseProgram(0);
GL.DeleteProgram(ShaderProgram); ;
base.OnUnload();
}
protected override void OnRenderFrame(FrameEventArgs args)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.ClearColor(new Color4(0.2f,0.5f,0.7f,1f));
GL.UseProgram(ShaderProgram);
GL.BindVertexArray(VAO);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
Context.SwapBuffers();
base.OnRenderFrame(args);
}
}
}

这里是顶点着色器源代码

#version 330 core
layout(location = 0) in vec3 VPos;
uniform vec4 input_color;
out vec4 a_color;
void main()
{
gl_Position = vec4(VPos, 1.0);
a_color = input_color;
}

和这里的片段着色器源代码

#version 330 core
out vec4 final_color;
in vec4 a_color;
void main() {
final_color = a_color;
}

我试图通过使用GL.Uniform4(loc, val.R, val.G, val.B, val.A)来改变制服;而不是GL.Uniform4(loc, val);但是同样的结果出现在

GL.Uniform*设置当前安装程序的默认统一块中的变量值。因此,在设置uinform:

之前,您需要使用GL.UseProgram安装程序。
GL.UseProgram(ShaderProgram); 
tricolor(new Color4(0f, 0f, 1f, 1f));

或者,您可以使用glUniform*的直接状态访问版本,即glProgramUniform*:

GL.ProgramUniform4(ShaderProgram, loc, val.R, val.G, val.B, val.A);

相关内容

  • 没有找到相关文章

最新更新