mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added GLTF submodule and start of ModelLoader
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
{
|
||||
"asset": {
|
||||
"generator": "COLLADA2GLTF",
|
||||
"version": "2.0"
|
||||
},
|
||||
"scene": 0,
|
||||
"scenes": [
|
||||
{
|
||||
"nodes": [
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"children": [
|
||||
1
|
||||
],
|
||||
"matrix": [
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
-1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0
|
||||
]
|
||||
},
|
||||
{
|
||||
"mesh": 0
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"NORMAL": 1,
|
||||
"POSITION": 2
|
||||
},
|
||||
"indices": 0,
|
||||
"mode": 4,
|
||||
"material": 0
|
||||
}
|
||||
],
|
||||
"name": "Mesh"
|
||||
}
|
||||
],
|
||||
"accessors": [
|
||||
{
|
||||
"bufferView": 0,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5123,
|
||||
"count": 36,
|
||||
"max": [
|
||||
23
|
||||
],
|
||||
"min": [
|
||||
0
|
||||
],
|
||||
"type": "SCALAR"
|
||||
},
|
||||
{
|
||||
"bufferView": 1,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"max": [
|
||||
1.0,
|
||||
1.0,
|
||||
1.0
|
||||
],
|
||||
"min": [
|
||||
-1.0,
|
||||
-1.0,
|
||||
-1.0
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 1,
|
||||
"byteOffset": 288,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"max": [
|
||||
0.5,
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"min": [
|
||||
-0.5,
|
||||
-0.5,
|
||||
-0.5
|
||||
],
|
||||
"type": "VEC3"
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"pbrMetallicRoughness": {
|
||||
"baseColorFactor": [
|
||||
0.800000011920929,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0
|
||||
],
|
||||
"metallicFactor": 0.0
|
||||
},
|
||||
"name": "Red"
|
||||
}
|
||||
],
|
||||
"bufferViews": [
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 576,
|
||||
"byteLength": 72,
|
||||
"target": 34963
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 0,
|
||||
"byteLength": 576,
|
||||
"byteStride": 12,
|
||||
"target": 34962
|
||||
}
|
||||
],
|
||||
"buffers": [
|
||||
{
|
||||
"byteLength": 648,
|
||||
"uri": "Box0.bin"
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
cbuffer SceneConstants
|
||||
{
|
||||
float4x4 ViewProjection = float4x4(1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
cbuffer ObjectConstants
|
||||
{
|
||||
float4x4 Transform;
|
||||
}
|
||||
|
||||
cbuffer Constants
|
||||
{
|
||||
float4 BaseColor;
|
||||
float3 LightDir;
|
||||
}
|
||||
|
||||
struct VS_IN
|
||||
{
|
||||
float3 Position : POSITION;
|
||||
float3 Normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PS_IN
|
||||
{
|
||||
float4 Position : SV_POSITION;
|
||||
float3 Normal : NORMAL;
|
||||
};
|
||||
|
||||
PS_IN VS(VS_IN input)
|
||||
{
|
||||
PS_IN output;
|
||||
|
||||
float4 worldPosition = mul(Transform, float4(input.Position, 1));
|
||||
|
||||
output.Position = mul(ViewProjection, worldPosition);
|
||||
output.Normal = mul((float3x3)Transform, input.Normal);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PS(PS_IN input) : SV_TARGET
|
||||
{
|
||||
input.Normal = normalize(input.Normal);
|
||||
|
||||
float shading = dot(LightDir, input.Normal);
|
||||
shading = clamp(shading, 0.0f, 1.0f);
|
||||
|
||||
return BaseColor * shading;
|
||||
}
|
||||
|
||||
#effect[VS=VS,PS=PS]
|
||||
@@ -8,6 +8,8 @@ using ImGui;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.World;
|
||||
using System.Collections;
|
||||
using GlitchyEngine.Content;
|
||||
|
||||
namespace Sandbox
|
||||
{
|
||||
@@ -55,8 +57,10 @@ namespace Sandbox
|
||||
GeometryBinding _quadGeometryBinding ~ _?.ReleaseRef();
|
||||
|
||||
RasterizerState _rasterizerState ~ _?.ReleaseRef();
|
||||
RasterizerState _rasterizerStateClockWise ~ _?.ReleaseRef();
|
||||
|
||||
GraphicsContext _context ~ _?.ReleaseRef();
|
||||
DepthStencilTarget _depthTarget ~ _?.ReleaseRef();
|
||||
|
||||
Texture2D _texture ~ _?.ReleaseRef();
|
||||
Texture2D _ge_logo ~ _?.ReleaseRef();
|
||||
@@ -84,9 +88,13 @@ namespace Sandbox
|
||||
_effectLibrary = new EffectLibrary(_context);
|
||||
|
||||
_effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
|
||||
|
||||
_effectLibrary.LoadNoRefInc("content\\Shaders\\testShader.hlsl");
|
||||
|
||||
var textureEffect = _effectLibrary.Load("content\\Shaders\\textureShader.hlsl");
|
||||
|
||||
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
|
||||
|
||||
// Create Input Layout
|
||||
|
||||
VertexLayout vertexLayout = new VertexLayout(_context, VertexColorTexture.VertexElements, false, textureEffect.VertexShader);
|
||||
@@ -163,6 +171,9 @@ namespace Sandbox
|
||||
RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
|
||||
_rasterizerState = new RasterizerState(_context, rsDesc);
|
||||
|
||||
rsDesc.FrontCounterClockwise = false;
|
||||
_rasterizerStateClockWise = new RasterizerState(_context, rsDesc);
|
||||
|
||||
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
|
||||
_ge_logo = new Texture2D(_context, "content/Textures/GE_Logo.dds");
|
||||
|
||||
@@ -190,6 +201,53 @@ namespace Sandbox
|
||||
_cameraController = new .(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
||||
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
|
||||
_cameraController.CameraPosition = .(0, 0, -5);
|
||||
|
||||
TestLoadModel();
|
||||
}
|
||||
|
||||
//GeometryBinding _modelTest ~ _?.ReleaseRef();
|
||||
|
||||
List<(Matrix Transform, GeometryBinding Model)> _modelTest = new .() ~ UnloadModelTest!();
|
||||
|
||||
mixin UnloadModelTest()
|
||||
{
|
||||
for(var entry in _modelTest)
|
||||
{
|
||||
entry.Model?.ReleaseRef();
|
||||
}
|
||||
|
||||
delete _modelTest;
|
||||
}
|
||||
|
||||
void TestLoadModel()
|
||||
{
|
||||
var testEffect = _effectLibrary.Get("testShader");
|
||||
|
||||
ModelLoader.LoadModel("content\\Models\\box.gltf", _context, testEffect, _modelTest);
|
||||
|
||||
testEffect.ReleaseRef();
|
||||
|
||||
/*
|
||||
CGLTF.Options options = .();
|
||||
CGLTF.Data* data;
|
||||
CGLTF.Result result = CGLTF.ParseFile(options, "content\\Models\\box.gltf", out data);
|
||||
|
||||
CGLTF.LoadBuffers(options, data, "content\\Models\\box.gltf");
|
||||
|
||||
Log.EngineLogger.Assert(result == .Success, "Failed to load model");
|
||||
|
||||
var mesh = data.Meshes[0];
|
||||
var primitive = mesh.Primitives[0];
|
||||
|
||||
var testEffect = _effectLibrary.Get("testShader");
|
||||
|
||||
_modelTest = ModelLoader.PrimitiveToGeoBinding(_context, primitive, testEffect);
|
||||
|
||||
testEffect.ReleaseRef();
|
||||
|
||||
/* TODO make awesome stuff */
|
||||
CGLTF.Free(data);
|
||||
*/
|
||||
}
|
||||
|
||||
void InitEcs()
|
||||
@@ -217,13 +275,13 @@ namespace Sandbox
|
||||
_cameraController.Update(gameTime);
|
||||
|
||||
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
|
||||
_depthTarget.Clear(1.0f, 0, .Depth);
|
||||
|
||||
// Draw test geometry
|
||||
_context.SetRenderTarget(null);
|
||||
_depthTarget.Bind();
|
||||
_context.BindRenderTargets();
|
||||
|
||||
_context.SetRasterizerState(_rasterizerState);
|
||||
|
||||
_context.SetViewport(_context.SwapChain.BackbufferViewport);
|
||||
|
||||
Renderer.BeginScene(_cameraController.Camera);
|
||||
@@ -233,6 +291,24 @@ namespace Sandbox
|
||||
var basicEffect = _effectLibrary.Get("basicShader");
|
||||
var textureEffect = _effectLibrary.Get("textureShader");
|
||||
|
||||
// Model test
|
||||
{
|
||||
_context.SetRasterizerState(_rasterizerStateClockWise);
|
||||
|
||||
var testEffect = _effectLibrary.Get("testShader");
|
||||
testEffect.Variables["BaseColor"].SetData(Color.White);
|
||||
testEffect.Variables["LightDir"].SetData(Vector3(-1, 1, -0.5f).Normalized());
|
||||
|
||||
for(var entry in _modelTest)
|
||||
{
|
||||
Renderer.Submit(entry.Model, testEffect, entry.Transform);
|
||||
}
|
||||
|
||||
testEffect.ReleaseRef();
|
||||
}
|
||||
|
||||
_context.SetRasterizerState(_rasterizerState);
|
||||
|
||||
for(var entity in _world.Enumerate(typeof(TransformComponent)))
|
||||
{
|
||||
var transform = _world.GetComponent<TransformComponent>(entity);
|
||||
@@ -281,6 +357,7 @@ namespace Sandbox
|
||||
EventDispatcher dispatcher = EventDispatcher(event);
|
||||
|
||||
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
|
||||
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
|
||||
}
|
||||
|
||||
private bool OnImGuiRender(ImGuiRenderEvent e)
|
||||
@@ -295,6 +372,14 @@ namespace Sandbox
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool OnWindowResize(WindowResizeEvent e)
|
||||
{
|
||||
_depthTarget.ReleaseRef();
|
||||
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class SandboxApp : Application
|
||||
|
||||
Reference in New Issue
Block a user