Refactor ImmediateMode rendering logic to use DrawImmediate.
This commit is contained in:
@@ -27,7 +27,6 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
private uint _program_acolor;
|
private uint _program_acolor;
|
||||||
private int _program_transforms;
|
private int _program_transforms;
|
||||||
private int _program_image;
|
private int _program_image;
|
||||||
private int _vao;
|
|
||||||
private GLTexture _white;
|
private GLTexture _white;
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -73,8 +72,6 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, (int)All.One);
|
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, (int)All.One);
|
||||||
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
|
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
|
||||||
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
|
GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
|
||||||
|
|
||||||
GL.GenVertexArray(out _vao);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearColor(Color color)
|
public void ClearColor(Color color)
|
||||||
@@ -97,31 +94,11 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
new ImmediateVertex(new Vector3(a+tangent, depth), Vector2.Zero, color),
|
new ImmediateVertex(new Vector3(a+tangent, depth), Vector2.Zero, color),
|
||||||
];
|
];
|
||||||
|
|
||||||
int buffer = GL.GenBuffer();
|
DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray())
|
||||||
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
|
{
|
||||||
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw);
|
Transforms = Context.ExtensionRequire<IDeviceContextBase>().Transforms,
|
||||||
|
PipelineState = new PipelineState { CullMode = FaceCulling.None },
|
||||||
GL.BindVertexArray(_vao);
|
});
|
||||||
GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_apos);
|
|
||||||
GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_atexcoord);
|
|
||||||
GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_acolor);
|
|
||||||
|
|
||||||
Matrix4x4 view = Context.ExtensionRequire<IDeviceContextBase>().Transforms;
|
|
||||||
|
|
||||||
GL.UseProgram(_program.Handle);
|
|
||||||
|
|
||||||
GL.ActiveTexture(TextureUnit.Texture0);
|
|
||||||
GL.BindTexture(TextureTarget.Texture2D, _white.Handle);
|
|
||||||
|
|
||||||
GL.UniformMatrix4f(_program_transforms, 1, true, ref view);
|
|
||||||
GL.Uniform1i(_program_image, 0);
|
|
||||||
|
|
||||||
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
|
|
||||||
|
|
||||||
GL.DeleteBuffer(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Rectangle(Box2d rectangle, float depth, Vector4 color)
|
public void Rectangle(Box2d rectangle, float depth, Vector4 color)
|
||||||
@@ -136,31 +113,11 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), Vector2.Zero, color),
|
new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), Vector2.Zero, color),
|
||||||
];
|
];
|
||||||
|
|
||||||
int buffer = GL.GenBuffer();
|
DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray())
|
||||||
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
|
{
|
||||||
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw);
|
Transforms = Context.ExtensionRequire<IDeviceContextBase>().Transforms,
|
||||||
|
PipelineState = new PipelineState { CullMode = FaceCulling.None },
|
||||||
GL.BindVertexArray(_vao);
|
});
|
||||||
GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_apos);
|
|
||||||
GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_atexcoord);
|
|
||||||
GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_acolor);
|
|
||||||
|
|
||||||
Matrix4x4 view = Context.ExtensionRequire<IDeviceContextBase>().Transforms;
|
|
||||||
|
|
||||||
GL.UseProgram(_program.Handle);
|
|
||||||
|
|
||||||
GL.ActiveTexture(TextureUnit.Texture0);
|
|
||||||
GL.BindTexture(TextureTarget.Texture2D, _white.Handle);
|
|
||||||
|
|
||||||
GL.UniformMatrix4f(_program_transforms, 1, true, ref view);
|
|
||||||
GL.Uniform1i(_program_image, 0);
|
|
||||||
|
|
||||||
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
|
|
||||||
|
|
||||||
GL.DeleteBuffer(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Rectangle(in RectangleDrawInfo rectangle)
|
public void Rectangle(in RectangleDrawInfo rectangle)
|
||||||
@@ -189,30 +146,12 @@ namespace Dashboard.OpenGL.Drawing
|
|||||||
new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), new Vector2(uv.Min.X, uv.Max.Y), Vector4.One),
|
new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), new Vector2(uv.Min.X, uv.Max.Y), Vector4.One),
|
||||||
];
|
];
|
||||||
|
|
||||||
int buffer = GL.GenBuffer();
|
DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray())
|
||||||
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
|
{
|
||||||
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw);
|
Transforms = Context.ExtensionRequire<IDeviceContextBase>().Transforms,
|
||||||
|
PipelineState = new PipelineState { CullMode = FaceCulling.None },
|
||||||
GL.BindVertexArray(_vao);
|
Texture = texture,
|
||||||
GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset);
|
});
|
||||||
GL.EnableVertexAttribArray(_program_apos);
|
|
||||||
GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_atexcoord);
|
|
||||||
GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset);
|
|
||||||
GL.EnableVertexAttribArray(_program_acolor);
|
|
||||||
Matrix4x4 view = Context.ExtensionRequire<IDeviceContextBase>().Transforms;
|
|
||||||
|
|
||||||
GL.UseProgram(_program.Handle);
|
|
||||||
|
|
||||||
GL.ActiveTexture(TextureUnit.Texture0);
|
|
||||||
GL.BindTexture(TextureTarget.Texture2D, ((GLTexture)texture).Handle);
|
|
||||||
|
|
||||||
GL.UniformMatrix4f(_program_transforms, 1, true, ref view);
|
|
||||||
GL.Uniform1i(_program_image, 0);
|
|
||||||
|
|
||||||
GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
|
|
||||||
|
|
||||||
GL.DeleteBuffer(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IContextBase IContextExtensionBase.Context => Context;
|
IContextBase IContextExtensionBase.Context => Context;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ out vec2 vTexCoords;
|
|||||||
out vec4 vColor;
|
out vec4 vColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 position = vec4(aPos, 1.0) * transforms;
|
vec4 position = transforms * vec4(aPos, 1.0);
|
||||||
gl_Position = position;
|
gl_Position = position;
|
||||||
|
|
||||||
vTexCoords = aTexCoords;
|
vTexCoords = aTexCoords;
|
||||||
|
|||||||
@@ -88,7 +88,26 @@ window.EventRaised += (sender, eventArgs) =>
|
|||||||
if (eventArgs is not PaintEventArgs paint)
|
if (eventArgs is not PaintEventArgs paint)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// window.DeviceContext.Begin();
|
||||||
|
var dcb = window.DeviceContext.ExtensionRequire<IDeviceContextBase>();
|
||||||
|
dcb.ResetTransforms();
|
||||||
|
dcb.ClearDepth();
|
||||||
|
|
||||||
|
imm.Rectangle(
|
||||||
|
new Dashboard.Box2d(
|
||||||
|
new System.Numerics.Vector2(30, 30),
|
||||||
|
new System.Numerics.Vector2(140, 110)),
|
||||||
|
0.2f,
|
||||||
|
new System.Numerics.Vector4(1, 1, 0, 1));
|
||||||
|
imm.Line(
|
||||||
|
new System.Numerics.Vector2(180, 40),
|
||||||
|
new System.Numerics.Vector2(290, 180),
|
||||||
|
6,
|
||||||
|
0.2f,
|
||||||
|
new System.Numerics.Vector4(0, 1, 1, 1));
|
||||||
imm.DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.AsMemory()));
|
imm.DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.AsMemory()));
|
||||||
|
|
||||||
|
// window.DeviceContext.End();
|
||||||
};
|
};
|
||||||
|
|
||||||
app.Run(true, source.Token);
|
app.Run(true, source.Token);
|
||||||
|
|||||||
Reference in New Issue
Block a user