MeshBatch now renders with or without textures.
General structure clean up as well.
This commit is contained in:
parent
6a19d1f5c7
commit
e781aea776
@ -7,7 +7,6 @@ using Serilog.Core;
|
|||||||
using SlatedGameToolkit.Framework.Exceptions;
|
using SlatedGameToolkit.Framework.Exceptions;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Textures;
|
using SlatedGameToolkit.Framework.Graphics.Textures;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Window;
|
using SlatedGameToolkit.Framework.Graphics.Window;
|
||||||
using SlatedGameToolkit.Framework.Input;
|
|
||||||
using SlatedGameToolkit.Framework.Input.Devices;
|
using SlatedGameToolkit.Framework.Input.Devices;
|
||||||
using SlatedGameToolkit.Framework.StateSystem;
|
using SlatedGameToolkit.Framework.StateSystem;
|
||||||
using SlatedGameToolkit.Framework.StateSystem.States;
|
using SlatedGameToolkit.Framework.StateSystem.States;
|
||||||
@ -19,7 +18,6 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class GameEngine {
|
public static class GameEngine {
|
||||||
private const int GL_MAJOR_VER = 3, GL_MINOR_VER = 3;
|
private const int GL_MAJOR_VER = 3, GL_MINOR_VER = 3;
|
||||||
public static TextureData FillerTextureData {get; private set;}
|
|
||||||
public static bool Debugging { get; set; }
|
public static bool Debugging { get; set; }
|
||||||
public static Logger Logger { get; private set; }
|
public static Logger Logger { get; private set; }
|
||||||
private static readonly object ignitionLock = new object();
|
private static readonly object ignitionLock = new object();
|
||||||
@ -33,8 +31,8 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
/// The amount of updates per second.
|
/// The amount of updates per second.
|
||||||
///
|
///
|
||||||
/// If greater than 0, game loop updates will be fixed (time steps).
|
/// If greater than 0, game loop updates will be fixed (time steps).
|
||||||
/// More specifically, it will figure out how many updates to perform per frame to allow for physics steps
|
/// More specifically, the amount of updates performed per frame to allow for physics calculations
|
||||||
/// to be calculated based on a constant time step.
|
/// to be calculated based on a constant time step will be calculated automatically.
|
||||||
///
|
///
|
||||||
/// If set to 0 or less, updates will be as fast as possible.
|
/// If set to 0 or less, updates will be as fast as possible.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -164,7 +162,7 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
timePassedFromLastUpdate += difference;
|
timePassedFromLastUpdate += difference;
|
||||||
while (timePassedFromLastUpdate > updateDeltaTime) {
|
while (timePassedFromLastUpdate > updateDeltaTime) {
|
||||||
//Updates.
|
//Updates.
|
||||||
manager.update(updateDeltaTime.TotalMilliseconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds);
|
manager.update(updateDeltaTime.TotalSeconds <= 0 ? timePassedFromLastUpdate.TotalSeconds : updateDeltaTime.TotalSeconds);
|
||||||
timePassedFromLastUpdate -= updateDeltaTime;
|
timePassedFromLastUpdate -= updateDeltaTime;
|
||||||
if (updateDeltaTime.TotalSeconds <= 0) break;
|
if (updateDeltaTime.TotalSeconds <= 0) break;
|
||||||
}
|
}
|
||||||
@ -172,7 +170,7 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
timePassedFromLastRender += difference;
|
timePassedFromLastRender += difference;
|
||||||
if (timePassedFromLastRender > frameDeltaTime) {
|
if (timePassedFromLastRender > frameDeltaTime) {
|
||||||
//Draw calls.
|
//Draw calls.
|
||||||
manager.render(timePassedFromLastUpdate.TotalSeconds);
|
manager.render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime));
|
||||||
timePassedFromLastRender = TimeSpan.Zero;
|
timePassedFromLastRender = TimeSpan.Zero;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -242,8 +240,6 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE) < 0)
|
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE) < 0)
|
||||||
throw new FrameworkSDLException(string.Format("Unable to load correct OpenGL version {0}.{1}.0 Core.", GL_MINOR_VER, GL_MAJOR_VER));
|
throw new FrameworkSDLException(string.Format("Unable to load correct OpenGL version {0}.{1}.0 Core.", GL_MINOR_VER, GL_MAJOR_VER));
|
||||||
|
|
||||||
LoadEngineAssets();
|
|
||||||
|
|
||||||
thread = new Thread(Loop);
|
thread = new Thread(Loop);
|
||||||
thread.Name = "SGTK-Engine";
|
thread.Name = "SGTK-Engine";
|
||||||
thread.Priority = ThreadPriority.AboveNormal;
|
thread.Priority = ThreadPriority.AboveNormal;
|
||||||
@ -252,30 +248,6 @@ namespace SlatedGameToolkit.Framework {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private unsafe static void LoadEngineAssets() {
|
|
||||||
byte[] buffer = EmbeddedResourceUtils.ReadEmbeddedResourceData("filler.png");
|
|
||||||
fixed (void* ptr = &buffer[0]) {
|
|
||||||
IntPtr rwOps = SDL.SDL_RWFromConstMem(new IntPtr(ptr), buffer.Length * sizeof(byte));
|
|
||||||
IntPtr surfacePtr = SDL_image.IMG_Load_RW(rwOps, 1);
|
|
||||||
SDL.SDL_Surface surface = Marshal.PtrToStructure<SDL.SDL_Surface>(surfacePtr);
|
|
||||||
|
|
||||||
SDL.SDL_PixelFormat pixelFormat = Marshal.PtrToStructure<SDL.SDL_PixelFormat>(surface.format);
|
|
||||||
if (pixelFormat.format != SDL.SDL_PIXELFORMAT_RGBA8888) {
|
|
||||||
IntPtr convertedPtr = SDL.SDL_ConvertSurfaceFormat(surfacePtr, SDL.SDL_PIXELFORMAT_RGBA8888, 0);
|
|
||||||
if (convertedPtr == null) throw new OptionalSDLException();
|
|
||||||
SDL.SDL_FreeSurface(surfacePtr);
|
|
||||||
surfacePtr = convertedPtr;
|
|
||||||
surface = Marshal.PtrToStructure<SDL.SDL_Surface>(surfacePtr);
|
|
||||||
}
|
|
||||||
byte[] data = new byte[surface.pitch * surface.h];
|
|
||||||
|
|
||||||
Marshal.Copy(surface.pixels, data, 0, data.Length);
|
|
||||||
FillerTextureData = new TextureData(surface.w, surface.h, data);
|
|
||||||
|
|
||||||
SDL.SDL_FreeSurface(surfacePtr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool IsRunning() {
|
public static bool IsRunning() {
|
||||||
return !stopped;
|
return !stopped;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
private float width, height;
|
private float width, height;
|
||||||
private Matrix4x4 lookAtMatrix = Matrix4x4.Identity;
|
private Matrix4x4 lookAtMatrix = Matrix4x4.Identity;
|
||||||
private Matrix4x4 view = Matrix4x4.Identity, projection;
|
private Matrix4x4 view = Matrix4x4.Identity, projection;
|
||||||
|
|
||||||
|
public event EventHandler projectionUpdateEvent;
|
||||||
|
|
||||||
public bool Orthographic {
|
public bool Orthographic {
|
||||||
get {
|
get {
|
||||||
return orthographic;
|
return orthographic;
|
||||||
@ -125,5 +128,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
this.Position = new Vector3(0, 0, 3);
|
this.Position = new Vector3(0, 0, 3);
|
||||||
this.CameraFront = -Vector3.UnitZ;
|
this.CameraFront = -Vector3.UnitZ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnProjectionUpdated() {
|
||||||
|
projectionUpdateEvent?.Invoke(this, new EventArgs());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,9 +4,9 @@ using System.Drawing;
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Textures;
|
using SlatedGameToolkit.Framework.Graphics.Textures;
|
||||||
|
|
||||||
namespace SlatedGameToolkit.Framework.Graphics.Meshes
|
namespace SlatedGameToolkit.Framework.Graphics.Render
|
||||||
{
|
{
|
||||||
public interface IMeshable
|
public interface IMesh
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -25,7 +25,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Meshes
|
|||||||
/// The texture for this mesh.
|
/// The texture for this mesh.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>Texture for this mesh.</value>
|
/// <value>Texture for this mesh.</value>
|
||||||
Texture Texture { get; }
|
ITexture Texture { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The blended color of this mesh.
|
/// The blended color of this mesh.
|
@ -0,0 +1,8 @@
|
|||||||
|
namespace SlatedGameToolkit.Framework.Graphics.Render
|
||||||
|
{
|
||||||
|
public interface ITexture
|
||||||
|
{
|
||||||
|
uint Handle {get;}
|
||||||
|
void Use();
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Meshes;
|
|
||||||
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Render.Programs;
|
using SlatedGameToolkit.Framework.Graphics.Render.Programs;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Render.Shaders;
|
using SlatedGameToolkit.Framework.Graphics.Render.Shaders;
|
||||||
@ -14,11 +13,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
public class MeshBatch : IDisposable {
|
public class MeshBatch : IDisposable {
|
||||||
public GLContext GLContext {get; private set; }
|
public GLContext GLContext {get; private set; }
|
||||||
private const int VERTEX_LENGTH = 9;
|
private const int VERTEX_LENGTH = 9;
|
||||||
private int projLoc, viewLoc, modelLoc;
|
private int projALoc, viewALoc, modelALoc, texturedALoc;
|
||||||
private Camera camera;
|
private Camera camera;
|
||||||
private RenderProgram renderProgram;
|
private RenderProgram renderProgram;
|
||||||
private Texture texture;
|
private ITexture texture;
|
||||||
private Texture fillerTexture;
|
|
||||||
private bool disposed;
|
private bool disposed;
|
||||||
private VertexArrayBuffers vertexBuffers;
|
private VertexArrayBuffers vertexBuffers;
|
||||||
public bool Batching { get; private set; }
|
public bool Batching { get; private set; }
|
||||||
@ -39,7 +37,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
}
|
}
|
||||||
this.renderProgram = renderProgram;
|
this.renderProgram = renderProgram;
|
||||||
this.GLContext = renderProgram.GLContext;
|
this.GLContext = renderProgram.GLContext;
|
||||||
this.camera = camera;
|
this.camera = camera ?? throw new ArgumentNullException("camera");
|
||||||
indices = new uint[BatchVertexSize];
|
indices = new uint[BatchVertexSize];
|
||||||
lengths = new int[BatchVertexSize];
|
lengths = new int[BatchVertexSize];
|
||||||
indiceOffsets = new int[BatchVertexSize];
|
indiceOffsets = new int[BatchVertexSize];
|
||||||
@ -51,12 +49,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
definitions[1] = new VertexAttributeDefinition((uint)GLContext.GetAttribLocation(renderProgram.Handle, "aColor"), 4);
|
definitions[1] = new VertexAttributeDefinition((uint)GLContext.GetAttribLocation(renderProgram.Handle, "aColor"), 4);
|
||||||
definitions[2] = new VertexAttributeDefinition((uint)GLContext.GetAttribLocation(renderProgram.Handle, "aTexCoord"), 2);
|
definitions[2] = new VertexAttributeDefinition((uint)GLContext.GetAttribLocation(renderProgram.Handle, "aTexCoord"), 2);
|
||||||
renderProgram.Use();
|
renderProgram.Use();
|
||||||
modelLoc = GLContext.GetUniformLocation(renderProgram.Handle, "models");
|
modelALoc = GLContext.GetUniformLocation(renderProgram.Handle, "models");
|
||||||
viewLoc = GLContext.GetUniformLocation(renderProgram.Handle, "view");
|
viewALoc = GLContext.GetUniformLocation(renderProgram.Handle, "view");
|
||||||
projLoc = GLContext.GetUniformLocation(renderProgram.Handle, "projection");
|
projALoc = GLContext.GetUniformLocation(renderProgram.Handle, "projection");
|
||||||
|
texturedALoc = GLContext.GetUniformLocation(renderProgram.Handle, "textured");
|
||||||
vertexBuffers.defineVertexAttributes(definitions: definitions);
|
vertexBuffers.defineVertexAttributes(definitions: definitions);
|
||||||
|
|
||||||
fillerTexture = new Texture(GameEngine.FillerTextureData);
|
camera.projectionUpdateEvent += ProjectionMatrixChanged;
|
||||||
|
GLContext.UniformMatrix4fv(projALoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Begin(Matrix4x4 modelsMatrix) {
|
public virtual void Begin(Matrix4x4 modelsMatrix) {
|
||||||
@ -65,15 +65,18 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
this.modelsMatrix = modelsMatrix;
|
this.modelsMatrix = modelsMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Draw(IMeshable meshable) {
|
/// <summary>
|
||||||
if (meshable.Texture != this.texture) {
|
/// Draws the given mesh.
|
||||||
if (this.texture != null) {
|
/// </summary>
|
||||||
Flush();
|
/// <param name="mesh">Draws the given mesh.</param>
|
||||||
}
|
public virtual void Draw(IMesh mesh) {
|
||||||
this.texture = meshable.Texture ?? fillerTexture;
|
if (mesh.Texture?.Handle != this.texture?.Handle) {
|
||||||
|
Flush();
|
||||||
|
this.texture = mesh.Texture;
|
||||||
|
GLContext.Uniform1i(texturedALoc, texture == null ? 0 : 1);
|
||||||
}
|
}
|
||||||
ValueTuple<Vector3, Vector2>[] vertices = meshable.Vertices;
|
ValueTuple<Vector3, Vector2>[] vertices = mesh.Vertices;
|
||||||
uint[] indices = meshable.Elements;
|
uint[] indices = mesh.Elements;
|
||||||
if (vertices.Length * VERTEX_LENGTH + dataIndex >= data.Length || indices.Length + indicesIndex >= this.indices.Length)
|
if (vertices.Length * VERTEX_LENGTH + dataIndex >= data.Length || indices.Length + indicesIndex >= this.indices.Length)
|
||||||
Flush();
|
Flush();
|
||||||
for (int i = 0; i < vertices.Length; i++) {
|
for (int i = 0; i < vertices.Length; i++) {
|
||||||
@ -84,13 +87,13 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
data[dataIndex] = vertices[i].Item1.Z;
|
data[dataIndex] = vertices[i].Item1.Z;
|
||||||
dataIndex++;
|
dataIndex++;
|
||||||
|
|
||||||
data[dataIndex] = meshable.Color.RedAsFloat();
|
data[dataIndex] = mesh.Color.RedAsFloat();
|
||||||
dataIndex++;
|
dataIndex++;
|
||||||
data[dataIndex] = meshable.Color.GreenAsFloat();
|
data[dataIndex] = mesh.Color.GreenAsFloat();
|
||||||
dataIndex++;
|
dataIndex++;
|
||||||
data[dataIndex] = meshable.Color.BlueAsFloat();
|
data[dataIndex] = mesh.Color.BlueAsFloat();
|
||||||
dataIndex++;
|
dataIndex++;
|
||||||
data[dataIndex] = meshable.Color.AlphaAsFloat();
|
data[dataIndex] = mesh.Color.AlphaAsFloat();
|
||||||
dataIndex++;
|
dataIndex++;
|
||||||
|
|
||||||
data[dataIndex] = vertices[i].Item2.X;
|
data[dataIndex] = vertices[i].Item2.X;
|
||||||
@ -116,22 +119,22 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Flush() {
|
protected virtual void Flush() {
|
||||||
texture.Use();
|
texture?.Use();
|
||||||
renderProgram.Use();
|
renderProgram.Use();
|
||||||
vertexBuffers.BufferVertices(data, true);
|
vertexBuffers.BufferVertices(data, true);
|
||||||
vertexBuffers.BufferIndices(indices, true);
|
vertexBuffers.BufferIndices(indices, true);
|
||||||
GLContext.UniformMatrix4fv(modelLoc, 1, false, modelsMatrix.ToColumnMajorArray());
|
GLContext.UniformMatrix4fv(modelALoc, 1, false, modelsMatrix.ToColumnMajorArray());
|
||||||
GLContext.UniformMatrix4fv(viewLoc, 1, false, camera.ViewMatrix.ToColumnMajorArray());
|
GLContext.UniformMatrix4fv(viewALoc, 1, false, camera.ViewMatrix.ToColumnMajorArray());
|
||||||
GLContext.UniformMatrix4fv(projLoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
|
|
||||||
// GLContext.UniformMatrix4fv(modelLoc, 1, false, Matrix4x4.Identity.ToColumnMajorArray());
|
|
||||||
// GLContext.UniformMatrix4fv(viewLoc, 1, false, Matrix4x4.Identity.ToColumnMajorArray());
|
|
||||||
// GLContext.UniformMatrix4fv(projLoc, 1, false, Matrix4x4.Identity.ToColumnMajorArray());
|
|
||||||
GLContext.MultiDrawElementsBaseVertex(PrimitiveType.Triangles, lengths, DrawElementsType.UnsignedInt, indiceOffsets, offsetIndex, verticeOffsets);
|
GLContext.MultiDrawElementsBaseVertex(PrimitiveType.Triangles, lengths, DrawElementsType.UnsignedInt, indiceOffsets, offsetIndex, verticeOffsets);
|
||||||
dataIndex = 0;
|
dataIndex = 0;
|
||||||
indicesIndex = 0;
|
indicesIndex = 0;
|
||||||
offsetIndex = 0;
|
offsetIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ProjectionMatrixChanged(object sender, EventArgs args) {
|
||||||
|
GLContext.UniformMatrix4fv(projALoc, 1, false, camera.ProjectionMatrix.ToColumnMajorArray());
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (!disposed)
|
if (!disposed)
|
||||||
@ -144,22 +147,22 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
|
|||||||
disposed = true;
|
disposed = true;
|
||||||
vertexBuffers.Dispose();
|
vertexBuffers.Dispose();
|
||||||
renderProgram.Dispose();
|
renderProgram.Dispose();
|
||||||
fillerTexture.Dispose();
|
|
||||||
data = null;
|
data = null;
|
||||||
indices = null;
|
indices = null;
|
||||||
lengths = null;
|
lengths = null;
|
||||||
verticeOffsets = null;
|
verticeOffsets = null;
|
||||||
|
camera.projectionUpdateEvent -= ProjectionMatrixChanged;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~MeshBatch()
|
~MeshBatch()
|
||||||
{
|
{
|
||||||
Dispose(disposing: false);
|
Dispose(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(disposing: true);
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,9 @@ using System.Drawing;
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Textures;
|
using SlatedGameToolkit.Framework.Graphics.Textures;
|
||||||
|
|
||||||
namespace SlatedGameToolkit.Framework.Graphics.Meshes
|
namespace SlatedGameToolkit.Framework.Graphics.Render
|
||||||
{
|
{
|
||||||
public class RectangleMesh : IMeshable
|
public class RectangleMesh : IMesh
|
||||||
{
|
{
|
||||||
private Matrix4x4 matRot = Matrix4x4.Identity;
|
private Matrix4x4 matRot = Matrix4x4.Identity;
|
||||||
private bool changed;
|
private bool changed;
|
||||||
@ -95,10 +95,29 @@ namespace SlatedGameToolkit.Framework.Graphics.Meshes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Texture Texture { get; set; }
|
public ITexture Texture { get; set; }
|
||||||
|
|
||||||
public Color Color { get; set; }
|
public Color Color { get; set; }
|
||||||
|
|
||||||
|
public RectangleMesh(ITexture texture, Color color)
|
||||||
|
{
|
||||||
|
this.Texture = texture;
|
||||||
|
this.Color = color;
|
||||||
|
|
||||||
|
this.textureCoords[0].X = 0;
|
||||||
|
this.textureCoords[0].Y = 1;
|
||||||
|
|
||||||
|
this.textureCoords[1].X = 1;
|
||||||
|
this.textureCoords[1].Y = 1;
|
||||||
|
|
||||||
|
this.textureCoords[2].X = 1;
|
||||||
|
this.textureCoords[2].Y = 0;
|
||||||
|
|
||||||
|
this.textureCoords[3].X = 0;
|
||||||
|
this.textureCoords[3].Y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void CalculateVertices() {
|
private void CalculateVertices() {
|
||||||
if (!changed) return;
|
if (!changed) return;
|
||||||
Vector3[] baseVerts = new Vector3[4];
|
Vector3[] baseVerts = new Vector3[4];
|
||||||
@ -109,7 +128,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Meshes
|
|||||||
|
|
||||||
for (int i = 0; i < vertices.Length; i++)
|
for (int i = 0; i < vertices.Length; i++)
|
||||||
{
|
{
|
||||||
vertices[i] = new ValueTuple<Vector3, Vector2>(Vector3.Transform(baseVerts[i], matRot), textureCoords[i]);
|
vertices[i] = (Vector3.Transform(baseVerts[i], matRot), textureCoords[i]);
|
||||||
}
|
}
|
||||||
changed = false;
|
changed = false;
|
||||||
}
|
}
|
@ -1,28 +0,0 @@
|
|||||||
using System.Drawing;
|
|
||||||
using SlatedGameToolkit.Framework.Graphics.Meshes;
|
|
||||||
using SlatedGameToolkit.Framework.Graphics.Textures;
|
|
||||||
|
|
||||||
namespace SlatedGameToolkit.Framework.Graphics.Render
|
|
||||||
{
|
|
||||||
public class Sprite2D : RectangleMesh
|
|
||||||
{
|
|
||||||
public Sprite2D(Texture texture, Color color) {
|
|
||||||
this.Texture = texture;
|
|
||||||
this.textureCoords[0].X = 0;
|
|
||||||
this.textureCoords[0].Y = 1;
|
|
||||||
|
|
||||||
this.textureCoords[1].X = 1;
|
|
||||||
this.textureCoords[1].Y = 1;
|
|
||||||
|
|
||||||
this.textureCoords[2].X = 1;
|
|
||||||
this.textureCoords[2].Y = 0;
|
|
||||||
|
|
||||||
this.textureCoords[3].X = 0;
|
|
||||||
this.textureCoords[3].Y = 0;
|
|
||||||
|
|
||||||
this.Color = color;
|
|
||||||
this.Width = 1f;
|
|
||||||
this.Height = 1f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +1,19 @@
|
|||||||
using System;
|
using System;
|
||||||
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
using SlatedGameToolkit.Framework.Graphics.OpenGL;
|
||||||
|
using SlatedGameToolkit.Framework.Graphics.Render;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Window;
|
using SlatedGameToolkit.Framework.Graphics.Window;
|
||||||
using SlatedGameToolkit.Framework.Loader;
|
using SlatedGameToolkit.Framework.Loader;
|
||||||
|
|
||||||
namespace SlatedGameToolkit.Framework.Graphics.Textures
|
namespace SlatedGameToolkit.Framework.Graphics.Textures
|
||||||
{
|
{
|
||||||
public class Texture : IAssetUseable {
|
public class Texture : IAssetUseable, ITexture {
|
||||||
public readonly int width, height;
|
public readonly int width, height;
|
||||||
private bool disposed;
|
private bool disposed;
|
||||||
private GLContext glContext;
|
private GLContext glContext;
|
||||||
private uint handle;
|
private uint handle;
|
||||||
|
|
||||||
|
public uint Handle {get { return handle; }}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an OpenGL Texture2D in the given GL Context.
|
/// Creates an OpenGL Texture2D in the given GL Context.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -18,7 +18,7 @@ namespace SlatedGameToolkit.Framework.Loader {
|
|||||||
/// A delegate that performs the loading of the specific asset.
|
/// A delegate that performs the loading of the specific asset.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">The path of the asset.</param>
|
/// <param name="path">The path of the asset.</param>
|
||||||
/// <param name="glContext">The context to be used. May be null if called by user.</param>
|
/// <param name="glContext">The context to be used.</param>
|
||||||
/// <typeparam name="T">The type of the resulting asset.</typeparam>
|
/// <typeparam name="T">The type of the resulting asset.</typeparam>
|
||||||
/// <returns>A loaded, useable asset.</returns>
|
/// <returns>A loaded, useable asset.</returns>
|
||||||
public delegate T LoadAsset<T>(string path, GLContext glContext) where T : IAssetUseable;
|
public delegate T LoadAsset<T>(string path, GLContext glContext) where T : IAssetUseable;
|
||||||
|
@ -16,7 +16,7 @@ namespace SlatedGameToolkit.Framework.Loader
|
|||||||
/// Therefore, technically, this function should be able to laod any format SDL2 Image can load.
|
/// Therefore, technically, this function should be able to laod any format SDL2 Image can load.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path">The path of the texture to load.</param>
|
/// <param name="path">The path of the texture to load.</param>
|
||||||
/// <param name="glContext">The OpenGL context the texture is to be associated with. </param>
|
/// <param name="glContext">The OpenGL context the texture is to be associated with. May be null.</param>
|
||||||
/// <returns>An OpenGL Texture associated with the given context.</returns>
|
/// <returns>An OpenGL Texture associated with the given context.</returns>
|
||||||
public static Texture LoadTexture(string path, GLContext glContext)
|
public static Texture LoadTexture(string path, GLContext glContext)
|
||||||
{
|
{
|
||||||
|
@ -3,9 +3,15 @@ out vec4 outputColor;
|
|||||||
|
|
||||||
in vec2 texCoord;
|
in vec2 texCoord;
|
||||||
in vec4 color;
|
in vec4 color;
|
||||||
|
|
||||||
|
uniform bool textured;
|
||||||
uniform sampler2D texture0;
|
uniform sampler2D texture0;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
outputColor = texture(texture0, texCoord) * color;
|
if (textured) {
|
||||||
|
outputColor = texture(texture0, texCoord) * color;
|
||||||
|
} else {
|
||||||
|
outputColor = color;
|
||||||
|
}
|
||||||
}
|
}
|
@ -46,7 +46,7 @@ namespace SlatedGameToolkit.Framework.StateSystem
|
|||||||
}
|
}
|
||||||
currentState.Update(delta);
|
currentState.Update(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void render(double delta) {
|
internal void render(double delta) {
|
||||||
WindowContext windowContext = WindowContextsManager.CurrentWindowContext;
|
WindowContext windowContext = WindowContextsManager.CurrentWindowContext;
|
||||||
windowContext.Context.ClearColor(backgroundColour.RedAsFloat(), backgroundColour.GreenAsFloat(), backgroundColour.BlueAsFloat(), 1f);
|
windowContext.Context.ClearColor(backgroundColour.RedAsFloat(), backgroundColour.GreenAsFloat(), backgroundColour.BlueAsFloat(), 1f);
|
||||||
|
@ -36,7 +36,7 @@ namespace SlatedGameToolkit.Framework.StateSystem.States
|
|||||||
/// Called in intervals dependent on game loop chose for every render cycle.
|
/// Called in intervals dependent on game loop chose for every render cycle.
|
||||||
/// Only called on if this state is the active state, indicated on the last status method called on this implementation (acitvate, and deactivate).
|
/// Only called on if this state is the active state, indicated on the last status method called on this implementation (acitvate, and deactivate).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="delta">The time in seconds that has passed since the last update cycle.</param>
|
/// <param name="delta">If in time-step mode, normalized value representing the time until the next update. Otherwise, the time passed since the last update.</param>
|
||||||
void Render(double delta);
|
void Render(double delta);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using SDL2;
|
using SDL2;
|
||||||
|
using SlatedGameToolkit.Framework;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Render;
|
using SlatedGameToolkit.Framework.Graphics.Render;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Render.Programs;
|
using SlatedGameToolkit.Framework.Graphics.Render.Programs;
|
||||||
using SlatedGameToolkit.Framework.Graphics.Textures;
|
using SlatedGameToolkit.Framework.Graphics.Textures;
|
||||||
@ -19,8 +20,8 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
|
|||||||
private WindowContext window;
|
private WindowContext window;
|
||||||
private Camera2D camera;
|
private Camera2D camera;
|
||||||
private MeshBatch renderer;
|
private MeshBatch renderer;
|
||||||
private Texture texture;
|
private Texture logoTexture, fillerTexture;
|
||||||
private Sprite2D sprite;
|
private RectangleMesh logo, textureTester, untextured;
|
||||||
|
|
||||||
public WindowContext CurrentWindow { get { return window;}}
|
public WindowContext CurrentWindow { get { return window;}}
|
||||||
|
|
||||||
@ -36,7 +37,8 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
|
|||||||
|
|
||||||
public void Deinitialize()
|
public void Deinitialize()
|
||||||
{
|
{
|
||||||
texture.Dispose();
|
logoTexture.Dispose();
|
||||||
|
fillerTexture.Dispose();
|
||||||
renderer.Dispose();
|
renderer.Dispose();
|
||||||
window.Dispose();
|
window.Dispose();
|
||||||
}
|
}
|
||||||
@ -51,16 +53,34 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
|
|||||||
window = new WindowContext("SlatedGameToolkit Playground");
|
window = new WindowContext("SlatedGameToolkit Playground");
|
||||||
camera = new Camera2D(2, 2);
|
camera = new Camera2D(2, 2);
|
||||||
renderer = new MeshBatch(camera);
|
renderer = new MeshBatch(camera);
|
||||||
texture = CommonLoaders.LoadTexture("Resources/Playground/yhdnbgnc.png", null);
|
|
||||||
sprite = new Sprite2D(texture, Color.White);
|
logoTexture = CommonLoaders.LoadTexture("Resources/Playground/yhdnbgnc.png", null);
|
||||||
sprite.X = -0.5f;
|
logo = new RectangleMesh(logoTexture, Color.White);
|
||||||
sprite.Y = -0.5f;
|
logo.Width = 0.5f;
|
||||||
|
logo.Height = 0.5f;
|
||||||
|
logo.X = -0.25f;
|
||||||
|
logo.Y = -0.25f;
|
||||||
|
|
||||||
|
|
||||||
|
fillerTexture = CommonLoaders.LoadTexture("Resources/Playground/filler.png", null);
|
||||||
|
textureTester = new RectangleMesh(fillerTexture, Color.White);
|
||||||
|
textureTester.Width = 0.15f;
|
||||||
|
textureTester.Height = 0.15f;
|
||||||
|
textureTester.X = 0.25f;
|
||||||
|
|
||||||
|
untextured = new RectangleMesh(null, Color.Red);
|
||||||
|
untextured.Width = 0.15f;
|
||||||
|
untextured.Height = 0.1f;
|
||||||
|
untextured.X = 0.25f;
|
||||||
|
untextured.Y = - 0.15f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Render(double delta)
|
public void Render(double delta)
|
||||||
{
|
{
|
||||||
renderer.Begin(Matrix4x4.Identity);
|
renderer.Begin(Matrix4x4.Identity);
|
||||||
renderer.Draw(sprite);
|
renderer.Draw(logo);
|
||||||
|
renderer.Draw(textureTester);
|
||||||
|
renderer.Draw(untextured);
|
||||||
renderer.End();
|
renderer.End();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user