Skip to content

Commit

Permalink
Updated the way objects were stored making it easier for the user.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyprus327 committed Dec 11, 2022
1 parent 6584fec commit 31a60ea
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 39 deletions.
36 changes: 36 additions & 0 deletions Basic3DEngine/BasicExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Vanilla3DEngine.Classes;
using Vanilla3DEngine.Structs;

namespace Vanilla3DEngine {
public class BasicExample : Engine{
public BasicExample(Size screenSize, string title) : base(screenSize, title) {
}

private readonly GameObject sphere = new GameObject(Mesh.SphereHighPoly) {
Transform = new Transform() { Pos = new Vector3(2.5f, 0f, 0f) },
Dynamic = false
};
private readonly GameObject cube = new GameObject(Mesh.Cube) {
Transform = new Transform() { Pos = new Vector3(-2.5f, 0f, 0f) },
Dynamic = false
};

// You can either instantiate an object and have it stored and rendered until
// you remove it, or just Draw a certain object.
public override void Awake() {
Instantiate(cube, 0);
}

public override void Update(Graphics g, float deltaTime) {
HandleInput();
g.DrawString("Press F to show a sphere", ScreenTextFont, ScreenTextBrush, 0, 0);
if (Input.GetKeyDown('F')) DrawObject(sphere);
}
}
}
6 changes: 5 additions & 1 deletion Basic3DEngine/Classes/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public GameObject(Mesh mesh) {
Vel = Vector3.Zero;
Transform = new Transform() { Pos = Vector3.Zero, Rot = Vector3.Zero };
Col = Color.White;
Dynamic = true;
}

public float Mass { get; set; }
Expand All @@ -23,8 +24,11 @@ public GameObject(Mesh mesh) {
public Color Col { get; set; }
public Mesh Mesh { get; set; }
public Transform Transform { get; set; }
public bool Dynamic { get; set; }

public virtual void Step(float deltaTime, Vector3 gravity) {
if (!Dynamic) return;

public void Step(float deltaTime, Vector3 gravity) {
Force = gravity * Mass;
Vel += Force / Mass * deltaTime;
Transform.Pos += Vel * deltaTime;
Expand Down
42 changes: 29 additions & 13 deletions Basic3DEngine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public Engine(Size screenSize, string title) {
private float _delta = 0f;
private readonly DeltaTime _deltaTime = new DeltaTime();

private static readonly List<GameObject> _objects = new List<GameObject>(); // gets cleared and remade every frame
private static readonly Dictionary<int, GameObject> _dict = new Dictionary<int, GameObject>();

private readonly TriangleDepthComparer _depthComparer = new TriangleDepthComparer();
private readonly string _title = "";
private readonly Canvas _window = null;
private readonly Thread _mainThread = null;
private static readonly List<Triangle> _renderStack = new List<Triangle>();
private static readonly List<GameObject> _objects = new List<GameObject>(); // gets cleared and remade every frame
private int _renderType = 2;
private string _currentRenderType;
private Mat4x4 _projectionMat = Mat4x4.New;
Expand Down Expand Up @@ -76,22 +78,15 @@ private void MainLoop() {

public abstract void Update(Graphics g, float deltaTime);

public void HandleObjects(List<GameObject> objects) {
for (int i = objects.Count - 1; i >= 0; i--) {
objects[i].Step(_delta, Gravity);
DrawObject(objects[i], objects[i].Transform.Pos);
}
}

private void Renderer(object sender, PaintEventArgs e) {
_mainGraphics = e.Graphics;

_mainGraphics.Clear(Color.FromArgb(37, 37, 37));

_delta = _deltaTime.Get();

HandleObjects();
Update(_mainGraphics, _delta);

Render(_mainGraphics, ShowDebugInfo);
}

Expand All @@ -111,7 +106,8 @@ private void Render(Graphics g, bool showDebugInfo = true) {
$"Camera Rot: {MainCamera.Transform.Rot}\n\n" +
$"Render Mode: {_currentRenderType}\n" +
$"Triangles: {_renderStack.Count}\n" +
$"Meshes: {_objects.Count}",
$"Meshes: {_objects.Count}\n" +
$"Objects: {_dict.Count}",
ScreenTextFont, ScreenTextBrush, 0, 0);
}

Expand Down Expand Up @@ -161,8 +157,28 @@ private void DrawTriangle(Graphics g, Triangle tri) {
}
}

public void DrawObject(GameObject obj, Vector3 position) =>
RasterizeTris(GetTrisToRaster(obj, position));
public void DrawObject(GameObject obj) {
RasterizeTris(GetTrisToRaster(obj, obj.Transform.Pos));
}

private void HandleObjects() {
foreach (var value in _dict.Values) {
value.Step(_delta, Gravity);
DrawObject(value);
}
}

public void Instantiate(GameObject obj, int id) {
if (_dict.ContainsKey(id)) throw new ArgumentException($"ID must be unique, {id} is already in use.");
_dict.Add(id, obj);
}

public void Destroy(int id) {
if (!_dict.ContainsKey(id)) throw new ArgumentException($"ID {id} provided does not exist.");
_dict.Remove(id);
}

public Dictionary<int, GameObject> GetObjects => _dict;

public void ShowMessageOnCenter(Graphics g, string message) {
if (g == null) return;
Expand All @@ -171,7 +187,7 @@ public void ShowMessageOnCenter(Graphics g, string message) {
ScreenSize.Height / 2);
}

public void HandleInput(float moveSpeed, float turnSpeed) {
public void HandleInput(float moveSpeed = 10f, float turnSpeed = 2.5f) {
if (Input.GetKeyDown('V')) // sprint
moveSpeed *= 3f;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using Vanilla3DEngine.Structs;
using Vanilla3DEngine.Classes;

namespace Vanilla3DEngine {
public class UsageExample : Engine {
public UsageExample(Size screenSize, string title) : base(screenSize, title) {
public class PrimitivesExplosionExample : Engine {
public PrimitivesExplosionExample(Size screenSize, string title) : base(screenSize, title) {
}

private readonly Random _r = new Random();
private readonly List<GameObject> _gameObjects = new List<GameObject>();
private readonly GameObject _teapot = new GameObject(Mesh.LoadFromOBJFile(@"teapot.obj")) {
Col = Color.LightCyan,
Transform = new Transform() { Pos = new Vector3(0f, 0f, 15f) },
};

public override void Awake() {
ShowDebugInfo = true;
MainCamera.Transform.Pos = new Vector3(0f, 20f, -10f);
_gameObjects.Add(_teapot);
}

public override void Update(Graphics g, float deltaTime) {
base.HandleInput(10f, 2.5f);
base.HandleObjects(_gameObjects);

// pseudo-physics, real physics with collision later
HandlePseudoPhysics(deltaTime);

if (Input.GetKeyUp('\t')) {
Expand All @@ -35,26 +26,29 @@ public override void Update(Graphics g, float deltaTime) {
}
if (Input.GetKeyDown('R')) {
MainCamera.Transform.Pos = Vector3.Zero;
_gameObjects.Clear();
}

g.DrawString("Press tab to generate again", ScreenTextFont, ScreenTextBrush, 0, ScreenSize.Height - 65);
}

private void HandlePseudoPhysics(float deltaTime) {
for (int i = _gameObjects.Count - 1; i >= 0; i--) {
_gameObjects[i].Transform.Rot += new Vector3(1f, 0.5f, 1f) * deltaTime;
if (_gameObjects[i].Transform.Pos.Y <= 0f) {
_gameObjects[i].Transform.Pos = new Vector3(_gameObjects[i].Transform.Pos.X, 0f, _gameObjects[i].Transform.Pos.Z);
_gameObjects[i].Vel = new Vector3(_gameObjects[i].Vel.X, Math.Abs(_gameObjects[i].Vel.Y / 2.2f), _gameObjects[i].Vel.Z);
GameObject obj;
foreach (var key in GetObjects.Keys) {
obj = GetObjects[key];
if (!obj.Dynamic) continue;
obj.Transform.Rot += new Vector3(1f, 0.5f, 1f) * deltaTime;
if (obj.Transform.Pos.Y <= 0f) {
obj.Transform.Pos = new Vector3(obj.Transform.Pos.X, 0f, obj.Transform.Pos.Z);
obj.Vel = new Vector3(obj.Vel.X, Math.Abs(obj.Vel.Y / 2.2f), obj.Vel.Z);
}
}
}

private void Generate(int objectCount, int size, Vector3 offset) {
_gameObjects.Clear();
_teapot.Transform.Pos = offset;
_gameObjects.Add(_teapot);
ClearRenderStack();
for (; _gameObjects.Count < objectCount;) {
GetObjects.Clear();

for (int i = 1; GetObjects.Count < objectCount; i++) {
Vector3 v = Vector3.Random(-size, size);
int selected = _r.Next(0, size);
GameObject rand = new GameObject(selected < size / 2 ? Mesh.Cube : Mesh.Sphere) {
Expand All @@ -64,9 +58,9 @@ private void Generate(int objectCount, int size, Vector3 offset) {
Pos = v + offset
},
Mass = _r.Next(1, 10),
Vel = new Vector3(_r.Next(-15, 15), _r.Next(15, 50), _r.Next(-15, 15))//Vector3.Random(-30, 30)
Vel = new Vector3(_r.Next(-15, 15), _r.Next(15, 50), _r.Next(-15, 15))
};
_gameObjects.Add(rand);
base.Instantiate(rand, i);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Basic3DEngine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Vanilla3DEngine {
class Program {
static void Main() {
Engine game = new UsageExample(new Size(1280, 720), "window");
Engine game = new BasicExample(new Size(1280, 720), "window title");
game.Run();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Basic3DEngine/Vanilla3DEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="BasicExample.cs" />
<Compile Include="Classes\Camera.cs" />
<Compile Include="Classes\Collision\Collider.cs" />
<Compile Include="Classes\Collision\CollisionPoint.cs" />
Expand All @@ -70,7 +71,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Structs\Triangle.cs" />
<Compile Include="UsageExample.cs" />
<Compile Include="PrimitivesExplosionExample.cs" />
<Compile Include="Classes\Sprite.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 31a60ea

Please sign in to comment.