OpenTK 不渲染我的三角形的颜色
OpenTK doesn't render the color of my triangle
我正在学习对游戏引擎进行编程,这就是我遵循教程的原因,通过该教程,我已经走到这一步了,尽管我的代码与他们的代码相同(他们的代码在视频中有效)但它不起作用它的意思是。无论如何,三角形保持黑色。没有任何错误。
主程序脚本:
using System;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.Common;
using System.Drawing;
using OpenTK.Graphics.OpenGL4;
using System.IO;
namespace Game_Engine
{
public static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
GameWindowSettings gws = GameWindowSettings.Default;
NativeWindowSettings nws = NativeWindowSettings.Default;
gws.IsMultiThreaded = false;
gws.RenderFrequency = 60;
gws.UpdateFrequency = 60;
nws.APIVersion = Version.Parse("4.1.0");
nws.AutoLoadBindings = true;
nws.Size = new Vector2i(1280, 720);
nws.Title = "Horizon";
GameWindow window = new GameWindow(gws, nws);
window.UpdateFrame += (FrameEventArgs args) => {
};
ShaderProgram shaderProgram = new ShaderProgram(){id = 0};
window.Load += () =>
{
Console.WriteLine("Hello");
ShaderProgram shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
};
window.RenderFrame += (FrameEventArgs args) =>
{
GL.UseProgram( shaderProgram.id );
GL.ClearColor(1.0f, 0.0f, 0.0f, 0.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
float[] verts = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f };
float[] color = { 1f, 0, 0, 0, 1f ,0 ,0, 0, 1f };
int vao = GL.GenVertexArray();
int vertices = GL.GenBuffer();
int colors = GL.GenBuffer();
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertices);
GL.BufferData( BufferTarget.ArrayBuffer, verts.Length * sizeof(float), verts, BufferUsageHint.StaticCopy);
GL.EnableVertexAttribArray( 0 );
GL.VertexAttribPointer( 0, 3, VertexAttribPointerType.Float, false, 0, 0 );
GL.BindBuffer(BufferTarget.ArrayBuffer, colors);
GL.BufferData(BufferTarget.ArrayBuffer, color.Length * sizeof(float), color, BufferUsageHint.StaticCopy);
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
GL.DeleteBuffer(vertices);
GL.DeleteBuffer(colors);
GL.DeleteVertexArray( vao );
window.SwapBuffers();
};
window.Run();
}
private static Shader LoadShader(string shaderLocation, ShaderType type)
{
int shaderId = GL.CreateShader( type );
GL.ShaderSource( shaderId, File.ReadAllText( shaderLocation ) );
GL.CompileShader( shaderId );
string infoLog = GL.GetShaderInfoLog( shaderId );
if (!string.IsNullOrEmpty(infoLog))
{
throw new Exception(infoLog);
}
return new Shader() { id = shaderId };
}
private static ShaderProgram LoadShaderProgram( string vertextShaderLocation, string fragmentShaderLocation)
{
int shaderProgramId = GL.CreateProgram();
Shader vertextShader = LoadShader(vertextShaderLocation, ShaderType.VertexShader);
Shader fragmentShader = LoadShader(fragmentShaderLocation, ShaderType.FragmentShader);
GL.AttachShader(shaderProgramId, vertextShader.id);
GL.AttachShader(shaderProgramId, fragmentShader.id);
GL.LinkProgram(shaderProgramId);
GL.DetachShader(shaderProgramId, vertextShader.id);
GL.DetachShader(shaderProgramId, fragmentShader.id);
GL.DeleteShader(vertextShader.id);
GL.DeleteShader(fragmentShader.id);
string infoLog = GL.GetProgramInfoLog(shaderProgramId);
if (!string.IsNullOrEmpty(infoLog))
{
throw new Exception(infoLog);
}
return new ShaderProgram() { id = shaderProgramId };
}
public struct Shader
{
public int id;
}
public struct ShaderProgram
{
public int id;
}
}
}
片段着色器(在 glsl 中):
#version 400
in vec3 color_in;
out vec4 color_out;
void main(){
color_out = vec4(color_in.r, color_in.g, color_in.b, 1);
}
顶点着色器(在 glsl 中):
#version 330
layout(location = 0) in vec3 vPosition;
layout(location = 1) in vec3 vColors;
out vec3 color_in;
void main() {
color_in = vColors;
gl_Position = vec4( vPosition, 1.0 );
}
我已经尝试了我对 OpenTK 非常有限的知识所能做的一切,但没有任何改变。我已经在网上搜索并找到答案他们仍然没有帮助
您实际上将着色器程序分配给事件回调函数范围内的局部变量。您需要将其分配给 Main
:
范围内的变量
ShaderProgram shaderProgram = new ShaderProgram() { id = 0 };
window.Load += () =>
{
Console.WriteLine("Hello");
// ShaderProgram shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
};
我正在学习对游戏引擎进行编程,这就是我遵循教程的原因,通过该教程,我已经走到这一步了,尽管我的代码与他们的代码相同(他们的代码在视频中有效)但它不起作用它的意思是。无论如何,三角形保持黑色。没有任何错误。
主程序脚本:
using System;
using OpenTK.Mathematics;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.Common;
using System.Drawing;
using OpenTK.Graphics.OpenGL4;
using System.IO;
namespace Game_Engine
{
public static class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
GameWindowSettings gws = GameWindowSettings.Default;
NativeWindowSettings nws = NativeWindowSettings.Default;
gws.IsMultiThreaded = false;
gws.RenderFrequency = 60;
gws.UpdateFrequency = 60;
nws.APIVersion = Version.Parse("4.1.0");
nws.AutoLoadBindings = true;
nws.Size = new Vector2i(1280, 720);
nws.Title = "Horizon";
GameWindow window = new GameWindow(gws, nws);
window.UpdateFrame += (FrameEventArgs args) => {
};
ShaderProgram shaderProgram = new ShaderProgram(){id = 0};
window.Load += () =>
{
Console.WriteLine("Hello");
ShaderProgram shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
};
window.RenderFrame += (FrameEventArgs args) =>
{
GL.UseProgram( shaderProgram.id );
GL.ClearColor(1.0f, 0.0f, 0.0f, 0.0f);
GL.Clear(ClearBufferMask.ColorBufferBit);
float[] verts = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f };
float[] color = { 1f, 0, 0, 0, 1f ,0 ,0, 0, 1f };
int vao = GL.GenVertexArray();
int vertices = GL.GenBuffer();
int colors = GL.GenBuffer();
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertices);
GL.BufferData( BufferTarget.ArrayBuffer, verts.Length * sizeof(float), verts, BufferUsageHint.StaticCopy);
GL.EnableVertexAttribArray( 0 );
GL.VertexAttribPointer( 0, 3, VertexAttribPointerType.Float, false, 0, 0 );
GL.BindBuffer(BufferTarget.ArrayBuffer, colors);
GL.BufferData(BufferTarget.ArrayBuffer, color.Length * sizeof(float), color, BufferUsageHint.StaticCopy);
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
GL.DeleteBuffer(vertices);
GL.DeleteBuffer(colors);
GL.DeleteVertexArray( vao );
window.SwapBuffers();
};
window.Run();
}
private static Shader LoadShader(string shaderLocation, ShaderType type)
{
int shaderId = GL.CreateShader( type );
GL.ShaderSource( shaderId, File.ReadAllText( shaderLocation ) );
GL.CompileShader( shaderId );
string infoLog = GL.GetShaderInfoLog( shaderId );
if (!string.IsNullOrEmpty(infoLog))
{
throw new Exception(infoLog);
}
return new Shader() { id = shaderId };
}
private static ShaderProgram LoadShaderProgram( string vertextShaderLocation, string fragmentShaderLocation)
{
int shaderProgramId = GL.CreateProgram();
Shader vertextShader = LoadShader(vertextShaderLocation, ShaderType.VertexShader);
Shader fragmentShader = LoadShader(fragmentShaderLocation, ShaderType.FragmentShader);
GL.AttachShader(shaderProgramId, vertextShader.id);
GL.AttachShader(shaderProgramId, fragmentShader.id);
GL.LinkProgram(shaderProgramId);
GL.DetachShader(shaderProgramId, vertextShader.id);
GL.DetachShader(shaderProgramId, fragmentShader.id);
GL.DeleteShader(vertextShader.id);
GL.DeleteShader(fragmentShader.id);
string infoLog = GL.GetProgramInfoLog(shaderProgramId);
if (!string.IsNullOrEmpty(infoLog))
{
throw new Exception(infoLog);
}
return new ShaderProgram() { id = shaderProgramId };
}
public struct Shader
{
public int id;
}
public struct ShaderProgram
{
public int id;
}
}
}
片段着色器(在 glsl 中):
#version 400
in vec3 color_in;
out vec4 color_out;
void main(){
color_out = vec4(color_in.r, color_in.g, color_in.b, 1);
}
顶点着色器(在 glsl 中):
#version 330
layout(location = 0) in vec3 vPosition;
layout(location = 1) in vec3 vColors;
out vec3 color_in;
void main() {
color_in = vColors;
gl_Position = vec4( vPosition, 1.0 );
}
我已经尝试了我对 OpenTK 非常有限的知识所能做的一切,但没有任何改变。我已经在网上搜索并找到答案他们仍然没有帮助
您实际上将着色器程序分配给事件回调函数范围内的局部变量。您需要将其分配给 Main
:
ShaderProgram shaderProgram = new ShaderProgram() { id = 0 };
window.Load += () =>
{
Console.WriteLine("Hello");
// ShaderProgram shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
shaderProgram = LoadShaderProgram("../../../../vertex_shader.glsl", "../../../../fragment_shader.glsl");
};