Restructed project in preparation for unit testing.
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
using System;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class ElementBufferHandle : IDisposable {
|
||||
private bool disposed;
|
||||
private int handle;
|
||||
public ElementBufferHandle() {
|
||||
handle = GL.GenBuffer();
|
||||
}
|
||||
public void Bind() {
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle);
|
||||
}
|
||||
|
||||
public void Unbind() {
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
|
||||
}
|
||||
|
||||
public void Flush(uint[] indices, BufferUsageHint hint = BufferUsageHint.StaticDraw) {
|
||||
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, hint);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposed) return;
|
||||
if (disposing) {
|
||||
|
||||
}
|
||||
disposed = true;
|
||||
Unbind();
|
||||
GL.DeleteBuffer(handle);
|
||||
}
|
||||
|
||||
~ElementBufferHandle() {
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
using OpenTK;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
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 TextureData textureData);
|
||||
}
|
||||
}
|
@@ -1,64 +0,0 @@
|
||||
using RecrownedGTK.Types;
|
||||
using OpenTK.Graphics;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class RectTexture : Rectangle, IDrawable {
|
||||
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;
|
||||
}
|
||||
public void Draw(out VertexInformation[] vertices, out TextureData textureData) {
|
||||
vertices = this.vertices;
|
||||
textureData = this.textureData;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,89 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
namespace RecrownedGTK.Graphics.Render.Shader {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
out vec4 outputColor;
|
||||
|
||||
in vec2 texCoord;
|
||||
in vec4 color;
|
||||
uniform sampler2D texture0;
|
||||
|
||||
void main()
|
||||
{
|
||||
outputColor = texture(texture0, texCoord) * color;
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
#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;
|
||||
}
|
@@ -1,34 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK.Graphics;
|
||||
using System;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class TextureData : IDisposable {
|
||||
private bool disposed;
|
||||
int handle;
|
||||
|
||||
public TextureWrapMode textureWrapModeWidth, textureWrapModeHeight;
|
||||
public Color4 borderColor;
|
||||
public TextureMinFilter textureMinFilter;
|
||||
public TextureMagFilter textureMagFilter;
|
||||
|
||||
public TextureData(string path) {
|
||||
byte[] textureData = null;
|
||||
int width;
|
||||
int height;
|
||||
using (FileStream file = new FileStream(path, FileMode.Open)) {
|
||||
using (Bitmap bitmap = new Bitmap(file)) {
|
||||
ImageConverter converter = new ImageConverter();
|
||||
textureData = (byte[]) converter.ConvertTo(bitmap, typeof(byte[]));
|
||||
width = bitmap.Width;
|
||||
height = bitmap.Height;
|
||||
}
|
||||
}
|
||||
|
||||
GenerateTexture(textureData, width, height);
|
||||
}
|
||||
|
||||
public TextureData(byte[] textureData, int width, int height) {
|
||||
textureWrapModeHeight = TextureWrapMode.ClampToBorder;
|
||||
textureWrapModeWidth = TextureWrapMode.ClampToBorder;
|
||||
textureMinFilter = TextureMinFilter.LinearMipmapLinear;
|
||||
textureMagFilter = TextureMagFilter.Linear;
|
||||
|
||||
GenerateTexture(textureData, width, height);
|
||||
}
|
||||
|
||||
private void GenerateTexture(byte[] textureData, int width, int height) {
|
||||
if (handle != 0) throw new InvalidOperationException("This texture data already holds a texture.");
|
||||
handle = GL.GenTexture();
|
||||
Use();
|
||||
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, textureData);
|
||||
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
|
||||
}
|
||||
|
||||
internal void Use() {
|
||||
GL.BindTexture(TextureTarget.Texture2D, handle);
|
||||
}
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposed) return;
|
||||
if (disposing) {
|
||||
}
|
||||
GL.BindTexture(TextureTarget.Texture2D, 0);
|
||||
GL.DeleteTexture(handle);
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
~TextureData() {
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,55 +0,0 @@
|
||||
using System;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK;
|
||||
using RecrownedGTK.Graphics.Render.Shader;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class VertexAttributesArrayHandle : IDisposable {
|
||||
private bool disposed;
|
||||
private int handle;
|
||||
public VertexAttributesArrayHandle() {
|
||||
handle = GL.GenVertexArray();
|
||||
}
|
||||
|
||||
public void Bind() {
|
||||
GL.BindVertexArray(handle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a basic vertex attributes object handle that takes in vertices, texture coordinates, and a color.
|
||||
/// First 3 values are normalized vertex position (x, y, z), second two are texture coordinates, and last 4 are color values.
|
||||
/// </summary>
|
||||
/// <param name="shader">The shader program used. Default is the one created from <see cref="RecrownedGTK.Graphics.Render.Shader.Shader.CreateBasicShader"/>.</param>
|
||||
/// <param name="positionAttribName">The name of the attribute for the vertex's position in the shader. Default is "aPosition".</param>
|
||||
/// <param name="textureAttribName">The name of the attribute for the texture's coordinate. Default is "aTexture".</param>
|
||||
/// <param name="colorAttribName">The name of the attribute for color mixture. Default is "aColor".</param>
|
||||
/// <returns>The built <see cref="RecrownedGTK.Graphics.VertexAttributesArrayHandle"/>.</returns>
|
||||
public static VertexAttributesArrayHandle CreateBasicVA(Shader shader = null, string positionAttribName = "aPosition", string textureAttribName = "aTexture", string colorAttribName = "aColor", string transformUnifName = "transform", Matrix4 transformMat = default(Matrix4)) {
|
||||
if (shader == null) shader = Shader.CreateBasicShader();
|
||||
VertexAttributesArrayHandle vertexAttribs = new VertexAttributesArrayHandle();
|
||||
vertexAttribs.Bind();
|
||||
if (transformMat == default(Matrix4)) transformMat = Matrix4.Identity;
|
||||
GL.UniformMatrix4(shader.GetAttribLocation(transformUnifName), true, ref transformMat);
|
||||
GL.VertexAttribPointer(shader.GetAttribLocation(positionAttribName), 3, VertexAttribPointerType.Float, false, 9 * sizeof(float), 0);
|
||||
GL.VertexAttribPointer(shader.GetAttribLocation(textureAttribName), 2, VertexAttribPointerType.Float, false, 9 * sizeof(float), 3 * sizeof(float));
|
||||
GL.VertexAttribPointer(shader.GetAttribLocation(colorAttribName), 4, VertexAttribPointerType.Float, false, 9 * sizeof(float), 5 * sizeof(float));
|
||||
return vertexAttribs;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposed) return;
|
||||
if (disposing) {
|
||||
|
||||
}
|
||||
GL.DeleteVertexArray(handle);
|
||||
disposed = true;
|
||||
}
|
||||
~VertexAttributesArrayHandle() {
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
using System;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public class VertexBufferArrayHandle : IDisposable {
|
||||
private bool disposed;
|
||||
private int handle;
|
||||
public VertexBufferArrayHandle() {
|
||||
handle = GL.GenBuffer();
|
||||
}
|
||||
public void Bind() {
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, handle);
|
||||
}
|
||||
public void Flush(float[] vertices, BufferUsageHint hint = BufferUsageHint.StaticDraw) {
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, hint);
|
||||
}
|
||||
public void Dispose() {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposed) return;
|
||||
if (disposing) {
|
||||
|
||||
}
|
||||
GL.DeleteBuffer(handle);
|
||||
disposed = true;
|
||||
}
|
||||
~VertexBufferArrayHandle() {
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
namespace RecrownedGTK.Graphics {
|
||||
public struct VertexInformation {
|
||||
public Vector3 coords;
|
||||
public uint[] indices;
|
||||
public Color4 color;
|
||||
public Vector2 textureCoords;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user