Another restructure with code updates.

Updated RectTextrue.cs, VertexInformation.cs, and IDrawable.cs to properly pass indice information.
This commit is contained in:
2020-02-20 18:07:17 -05:00
parent 1a9ff8a26f
commit 519d31f2e1
38 changed files with 62 additions and 10 deletions

View File

@@ -0,0 +1,12 @@
using OpenTK;
namespace RecrownedGTK.Graphics.Render {
public interface IDrawable
{
/// <summary>
/// Fills the parameters with the information needed to draw this texture.
/// </summary>
/// <param name="vertices">The vertices in pairs of 3 normalized in the order x, y, and z.</param>
/// <param name="textureData">The texture data to be used. Can be null.</param>
void Draw(out VertexInformation[] vertices, out uint[] indices, out TextureData textureData);
}
}

View File

@@ -0,0 +1,70 @@
using RecrownedGTK.Types;
using OpenTK.Graphics;
namespace RecrownedGTK.Graphics.Render {
public class RectTexture : Rectangle, IDrawable {
private uint[] indices;
private VertexInformation[] vertices;
private TextureData textureData;
public Color4 Color {
get {
return vertices[0].color;
}
set {
for (int vert = 0; vert < vertices.Length; vert++) {
vertices[vert].color = value;
}
}
}
public override float Width {
get {
return vertices[1].coords.X - vertices[0].coords.X;
}
set {
vertices[1].coords.X = vertices[0].coords.X + value;
vertices[2].coords.X = vertices[3].coords.X + value;
}
}
public override float Height {
set {
vertices[3].coords.Y = vertices[0].coords.Y + value;
vertices[2].coords.Y = vertices[1].coords.Y + value;
}
get {
return vertices[3].coords.Y - vertices[0].coords.Y;
}
}
public override float X {
set {
float width = Width;
vertices[0].coords.X = value;
Width = width;
}
get {
return vertices[0].coords.X;
}
}
public override float Y {
set {
float height = Height;
vertices[0].coords.Y = value;
Height = height;
}
get {
return vertices[0].coords.Y;
}
}
public RectTexture(TextureData textureData) {
this.textureData = textureData;
vertices = new VertexInformation[4];
indices = new uint[] {0, 1, 3,
1, 2, 3};
}
public void Draw(out VertexInformation[] vertices, out uint[] indices, out TextureData textureData) {
indices = this.indices;
vertices = this.vertices;
textureData = this.textureData;
}
}
}

View File

@@ -0,0 +1,89 @@
using System;
using System.IO;
using System.Text;
using OpenTK.Graphics.OpenGL;
namespace RecrownedGTK.Graphics.Render.Shaders {
public class Shader : IDisposable {
int handle;
public bool IsDisposed {
get;
private set;
}
public Shader(string vertexPath, string fragmentPath) {
IsDisposed = false;
int vertShader = 0;
int fragShader = 0;
string vertShaderSource;
string fragShaderSource;
using (StreamReader stream = new StreamReader(vertexPath, Encoding.UTF8)) {
vertShaderSource = stream.ReadToEnd();
}
using (StreamReader stream = new StreamReader(fragmentPath, Encoding.UTF8)) {
fragShaderSource = stream.ReadToEnd();
}
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, vertShaderSource);
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, fragShaderSource);
string log;
GL.CompileShader(vertShader);
if ((log = GL.GetShaderInfoLog(vertShader)) == "") {
throw new ArgumentException("Error while compiling vertex shader: " + log, "vertexPath");
}
GL.CompileShader(fragShader);
if ((log = GL.GetShaderInfoLog(fragShader)) == "") {
throw new ArgumentException("Error while compiling fragment shader: " + log, "fragmentPath");
}
//Creates the shader program.
handle = GL.CreateProgram();
//Attaches shader to program and links it.
GL.AttachShader(handle, vertShader);
GL.AttachShader(handle, fragShader);
GL.LinkProgram(handle);
GL.DetachShader(handle, vertShader);
GL.DetachShader(handle, fragShader);
GL.DeleteShader(vertShader);
GL.DeleteShader(fragShader);
}
public void Use() {
GL.UseProgram(handle);
}
public int GetAttribLocation(string attributeName) {
return GL.GetAttribLocation(handle, attributeName);
}
protected virtual void Dispose(bool disposing) {
if (IsDisposed) {
return;
}
if (disposing) {
}
GL.DeleteProgram(handle);
IsDisposed = true;
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
~Shader() {
Dispose(false);
}
public static Shader CreateBasicShader() {
Shader shader = new Shader("Graphics/Render/Shader/default.vert", "Graphics/Render/Shader/default.frag");
return shader;
}
}
}

View File

@@ -0,0 +1,10 @@
out vec4 outputColor;
in vec2 texCoord;
in vec4 color;
uniform sampler2D texture0;
void main()
{
outputColor = texture(texture0, texCoord) * color;
}

View File

@@ -0,0 +1,15 @@
#version 330 core
in vec3 aPosition;
in vec2 aTexCoord;
in vec4 aColor;
uniform mat4 transform;
out vec2 texCoord;
out vec4 color;
void main()
{
texCoord = aTexCoord;
color = aColor;
gl_Position = vec4(aPosition, 1.0f) * transform;
}

View File

@@ -0,0 +1,34 @@
using System;
namespace RecrownedGTK.Graphics.Render {
public class TextureBatch : IDisposable {
private bool disposed;
private bool begun;
public TextureBatch() {
//TODO: Finish this class.
}
public void Begin() {
if (begun) throw new InvalidOperationException("This TextureBatch has already been started.");
begun = true;
}
public void End() {
if (!begun) throw new InvalidOperationException("The TextureBatch has not begun.");
begun = false;
}
protected virtual void Dispose(bool disposing) {
if (disposed) return;
if (disposing) {
}
disposed = true;
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
~TextureBatch() {
Dispose(false);
}
}
}