mirror of
https://github.com/UOH-CS-Level5/551455-graphics-programming-2526-the-repo-Zyb3rWolfi.git
synced 2025-11-29 00:43:08 +00:00
Compare commits
No commits in common. "d71a0aab5efa72088213f026f315fc56cf12e5ee" and "8e794f47fa742eb2b7d213e5170b895d90643303" have entirely different histories.
d71a0aab5e
...
8e794f47fa
@ -1,27 +0,0 @@
|
|||||||
using OpenTK.Mathematics;
|
|
||||||
namespace TheLabs;
|
|
||||||
|
|
||||||
public class BoxCollider
|
|
||||||
{
|
|
||||||
public Vector3 Center;
|
|
||||||
|
|
||||||
public Vector3 HalfSize;
|
|
||||||
|
|
||||||
public BoxCollider(Vector3 center, Vector3 halfSize)
|
|
||||||
{
|
|
||||||
Center = center;
|
|
||||||
HalfSize = halfSize * 0.5f;
|
|
||||||
}
|
|
||||||
public Vector3 Min => Center - HalfSize;
|
|
||||||
public Vector3 Max => Center + HalfSize;
|
|
||||||
|
|
||||||
public bool CheckCollision(BoxCollider other)
|
|
||||||
{
|
|
||||||
|
|
||||||
bool collisionX = Max.X >= other.Min.X && Min.X <= other.Max.X;
|
|
||||||
bool collisionY = Max.Y >= other.Min.Y && Min.Y <= other.Max.Y;
|
|
||||||
bool collisionZ = Max.Z >= other.Min.Z && Min.Z <= other.Max.Z;
|
|
||||||
|
|
||||||
return collisionX && collisionY && collisionZ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,64 +0,0 @@
|
|||||||
using Assimp;
|
|
||||||
using LearnOpenTK.Common;
|
|
||||||
using OpenTK.Mathematics;
|
|
||||||
|
|
||||||
namespace TheLabs;
|
|
||||||
|
|
||||||
public class LevelLoader
|
|
||||||
{
|
|
||||||
|
|
||||||
private Shader _shader;
|
|
||||||
private Texture _texture;
|
|
||||||
private Texture _normalTexture;
|
|
||||||
private Material _material;
|
|
||||||
private Lighting _lighting;
|
|
||||||
|
|
||||||
|
|
||||||
public LevelLoader(Shader shader, Texture texture, Texture normalTexture, Material material, Lighting lighting)
|
|
||||||
{
|
|
||||||
_shader = shader;
|
|
||||||
_texture = texture;
|
|
||||||
_normalTexture = normalTexture;
|
|
||||||
_material = material;
|
|
||||||
_lighting = lighting;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void LoadLevel(string filepath, SceneNode rootenode, List<BoxCollider> colliders)
|
|
||||||
{
|
|
||||||
Mesh _mesh = ShapeFactory.CreateTexturedCube();
|
|
||||||
RenderObject renderObject = new RenderObject(_mesh, _shader, _texture, _material, _lighting);
|
|
||||||
renderObject.NormalMap = _normalTexture;
|
|
||||||
renderObject.UseTexture = true;
|
|
||||||
|
|
||||||
if (!File.Exists(filepath))
|
|
||||||
{
|
|
||||||
throw new FileNotFoundException("The specified level file was not found.", filepath);
|
|
||||||
}
|
|
||||||
string[] lines = File.ReadAllLines(filepath);
|
|
||||||
float spacing = 1.0f;
|
|
||||||
|
|
||||||
for (int z = 0; z < lines.Length; z++)
|
|
||||||
{
|
|
||||||
string line = lines[z];
|
|
||||||
for (int x = 0; x < line.Length; x++)
|
|
||||||
{
|
|
||||||
char tile = line[x];
|
|
||||||
|
|
||||||
SceneNode floorNode = new SceneNode(renderObject);
|
|
||||||
floorNode.Position = new Vector3(x * spacing, -0.5f, z * spacing);
|
|
||||||
floorNode.Scale = new Vector3(1.0f, 0.1f, 1.0f);
|
|
||||||
rootenode.AddChild(floorNode);
|
|
||||||
|
|
||||||
if (tile == '1')
|
|
||||||
{
|
|
||||||
BoxCollider wallCollider = new BoxCollider(new Vector3(x, 0, z), new Vector3(1, 1, 1));
|
|
||||||
colliders.Add(wallCollider);
|
|
||||||
SceneNode wallNode = new SceneNode(renderObject);
|
|
||||||
wallNode.Position = new Vector3(x * spacing, 0.0f, z * spacing);
|
|
||||||
|
|
||||||
rootenode.AddChild(wallNode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
1111111111
|
|
||||||
1000000001
|
|
||||||
1011101101
|
|
||||||
1010000101
|
|
||||||
1010110101
|
|
||||||
1000000001
|
|
||||||
1111111111
|
|
||||||
@ -38,14 +38,11 @@ namespace TheLabs
|
|||||||
private float _mouseSensitivity = 0.1f;
|
private float _mouseSensitivity = 0.1f;
|
||||||
private float _pitch = 0.0f;
|
private float _pitch = 0.0f;
|
||||||
private float _rotation = 0.0f;
|
private float _rotation = 0.0f;
|
||||||
Vector3 playerSize = new Vector3(0.5f, 1.0f, 0.5f);
|
|
||||||
|
|
||||||
// Materials and Lighting
|
// Materials and Lighting
|
||||||
private Lighting _mainLight;
|
private Lighting _mainLight;
|
||||||
private Material _defaultMaterial;
|
private Material _defaultMaterial;
|
||||||
|
|
||||||
private List<BoxCollider> _wallColliders = new List<BoxCollider>();
|
|
||||||
|
|
||||||
// This prevents a large camera jump when the window first gets focus
|
// This prevents a large camera jump when the window first gets focus
|
||||||
private bool _firstMove = true;
|
private bool _firstMove = true;
|
||||||
|
|
||||||
@ -63,9 +60,9 @@ namespace TheLabs
|
|||||||
// --- Set up Lighting and Material ---
|
// --- Set up Lighting and Material ---
|
||||||
_mainLight = new Lighting(new Vector3(
|
_mainLight = new Lighting(new Vector3(
|
||||||
1.2f, 1.0f, 2.0f),
|
1.2f, 1.0f, 2.0f),
|
||||||
new Vector3(5.0f),
|
new Vector3(0.5f),
|
||||||
new Vector3(1.5f),
|
new Vector3(1.5f),
|
||||||
new Vector3(4.0f));
|
new Vector3(1.0f));
|
||||||
|
|
||||||
_defaultMaterial = new Material(
|
_defaultMaterial = new Material(
|
||||||
new Vector3(0.5f, 0.5f, 0.5f) * 0.1f,
|
new Vector3(0.5f, 0.5f, 0.5f) * 0.1f,
|
||||||
@ -78,25 +75,27 @@ namespace TheLabs
|
|||||||
GL.Enable(EnableCap.DepthTest);
|
GL.Enable(EnableCap.DepthTest);
|
||||||
|
|
||||||
// --- Set up Camera ---
|
// --- Set up Camera ---
|
||||||
|
_camera = new Camera(_cameraPosition, _cameraFront, _cameraUp);
|
||||||
|
|
||||||
// -- Load Shaders, Textures, and Create Scene Objects ---
|
// -- Load Shaders, Textures, and Create Scene Objects ---
|
||||||
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
|
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
|
||||||
_texture = new Texture("Textures/cobble.jpg");
|
_texture = new Texture("Textures/cobble.jpg");
|
||||||
Texture _normalMap = new Texture("Textures/cobbleNormal.jpg");
|
Texture _normalMap = new Texture("Textures/cobbleNormal.jpg");
|
||||||
|
|
||||||
LevelLoader levelLoader = new LevelLoader(_shader, _texture, _normalMap, _defaultMaterial, _mainLight);
|
|
||||||
|
|
||||||
|
// --- Create Scene Graph ---
|
||||||
_rootNode = new SceneNode();
|
_rootNode = new SceneNode();
|
||||||
|
|
||||||
string levelFilePath = "Levels/level1.txt";
|
// Create Example Object
|
||||||
levelLoader.LoadLevel(levelFilePath, _rootNode, _wallColliders);
|
_buildingObject = ShapeFactory.CreateTexturedCube();
|
||||||
|
_buildingRender = new RenderObject(_buildingObject, _shader, _texture, _defaultMaterial, _mainLight);
|
||||||
|
_buildingRender.NormalMap = _normalMap;
|
||||||
|
_buildingNode = new SceneNode(_buildingRender);
|
||||||
|
_buildingNode.Position = new Vector3(2.0f, -1.0f, 0.0f);
|
||||||
|
_buildingNode.Scale = new Vector3(0.1f, 0.1f, 0.1f);
|
||||||
|
_rootNode.AddChild(_buildingNode);
|
||||||
|
|
||||||
|
|
||||||
// --- Create Scene Graph ---
|
|
||||||
|
|
||||||
// Create Character Object
|
// Create Character Object
|
||||||
_cameraPosition = new Vector3(-2.5f, 0.0f, 1.5f);
|
|
||||||
_camera = new Camera(_cameraPosition, _cameraFront, _cameraUp);
|
|
||||||
CursorState = CursorState.Grabbed;
|
CursorState = CursorState.Grabbed;
|
||||||
|
|
||||||
|
|
||||||
@ -139,15 +138,6 @@ namespace TheLabs
|
|||||||
base.OnUpdateFrame(e);
|
base.OnUpdateFrame(e);
|
||||||
_rotation += (float)e.Time;
|
_rotation += (float)e.Time;
|
||||||
var input = KeyboardState;
|
var input = KeyboardState;
|
||||||
float gravity = -9.81f;
|
|
||||||
|
|
||||||
_camera._position.Y += gravity * (float)e.Time;
|
|
||||||
float floorHeight = 0.0f;
|
|
||||||
|
|
||||||
if (_camera._position.Y < floorHeight)
|
|
||||||
{
|
|
||||||
_camera._position.Y = floorHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input.IsKeyDown(Keys.Escape))
|
if (input.IsKeyDown(Keys.Escape))
|
||||||
{
|
{
|
||||||
@ -196,17 +186,6 @@ namespace TheLabs
|
|||||||
{
|
{
|
||||||
_pitch -= _rotationSpeed * (float)e.Time;
|
_pitch -= _rotationSpeed * (float)e.Time;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input.IsKeyDown(Keys.Space))
|
|
||||||
{
|
|
||||||
|
|
||||||
if (_camera._position.Y <= floorHeight + 0.01f)
|
|
||||||
{
|
|
||||||
_camera._position.Y += 10.0f * (float)e.Time;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
_pitch = MathHelper.Clamp(_pitch, -89.0f, 89.0f);
|
_pitch = MathHelper.Clamp(_pitch, -89.0f, 89.0f);
|
||||||
Vector3 front;
|
Vector3 front;
|
||||||
_camera._target.X = MathF.Cos(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch));
|
_camera._target.X = MathF.Cos(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch));
|
||||||
@ -214,30 +193,8 @@ namespace TheLabs
|
|||||||
_camera._target.Z = MathF.Sin(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch));
|
_camera._target.Z = MathF.Sin(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch));
|
||||||
|
|
||||||
_cameraFront = Vector3.Normalize(_camera._target);
|
_cameraFront = Vector3.Normalize(_camera._target);
|
||||||
var _cameraRight = Vector3.Normalize(Vector3.Cross(_cameraFront, _camera._up));
|
var _cameraRight = Vector3.Normalize(Vector3.Cross(_cameraFront, _camera._up));
|
||||||
if (input.IsKeyDown(Keys.W))
|
if (input.IsKeyDown(Keys.W)) _camera._position += _cameraFront * _cameraSpeed * (float)e.Time;
|
||||||
{
|
|
||||||
Vector3 proposedPosition = _camera._position + _cameraFront * _cameraSpeed * (float)e.Time;
|
|
||||||
BoxCollider playerCollider = new BoxCollider(proposedPosition, playerSize);
|
|
||||||
|
|
||||||
bool safe = true;
|
|
||||||
foreach (var wall in _wallColliders)
|
|
||||||
{
|
|
||||||
if (playerCollider.CheckCollision(wall))
|
|
||||||
{
|
|
||||||
safe = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (safe)
|
|
||||||
{
|
|
||||||
_camera._position = proposedPosition;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input.IsKeyDown(Keys.S)) _camera._position -= _cameraFront * _cameraSpeed * (float)e.Time;
|
if (input.IsKeyDown(Keys.S)) _camera._position -= _cameraFront * _cameraSpeed * (float)e.Time;
|
||||||
if (input.IsKeyDown(Keys.A)) _camera._position -= _cameraRight * _cameraSpeed * (float)e.Time;
|
if (input.IsKeyDown(Keys.A)) _camera._position -= _cameraRight * _cameraSpeed * (float)e.Time;
|
||||||
if (input.IsKeyDown(Keys.D)) _camera._position += _cameraRight * _cameraSpeed * (float)e.Time;
|
if (input.IsKeyDown(Keys.D)) _camera._position += _cameraRight * _cameraSpeed * (float)e.Time;
|
||||||
|
|||||||
@ -36,6 +36,8 @@ public class RenderObject
|
|||||||
|
|
||||||
public void Draw(Matrix4 view, Matrix4 projection, Lighting light)
|
public void Draw(Matrix4 view, Matrix4 projection, Lighting light)
|
||||||
{
|
{
|
||||||
|
Vector3 pink = new Vector3(0.5f, 0.5f, 0.5f);
|
||||||
|
Material mat = new Material(pink * 0.1f, pink * 0.6f, Vector3.One, 32.0f);
|
||||||
|
|
||||||
Shader.Use();
|
Shader.Use();
|
||||||
if (Texture != null && UseTexture)
|
if (Texture != null && UseTexture)
|
||||||
|
|||||||
@ -30,9 +30,9 @@ public class SceneNode
|
|||||||
|
|
||||||
if (RenderObject != null)
|
if (RenderObject != null)
|
||||||
{
|
{
|
||||||
RenderObject.Position = Position;
|
RenderObject.Position = Vector3.Zero;
|
||||||
RenderObject.Rotation = Rotation;
|
RenderObject.Rotation = Quaternion.Identity;
|
||||||
RenderObject.Scale = Scale;
|
RenderObject.Scale = Vector3.One;
|
||||||
|
|
||||||
RenderObject.Draw(view, projection, light);
|
RenderObject.Draw(view, projection, light);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -332,31 +332,26 @@ public static class ShapeFactory
|
|||||||
|
|
||||||
return new Mesh(vertices.ToArray(), indices.ToArray(), Mesh.VertexLayout.PosTexNormal);
|
return new Mesh(vertices.ToArray(), indices.ToArray(), Mesh.VertexLayout.PosTexNormal);
|
||||||
}
|
}
|
||||||
public static Mesh CreateTexturedQuad()
|
public static Mesh createTexturedSquare()
|
||||||
{
|
{
|
||||||
List<Vertex> vertices = new List<Vertex>();
|
float[] vertices = {
|
||||||
List<uint> indices = new List<uint>();
|
// Format: X, Y, Z, U, V
|
||||||
|
|
||||||
// Create a flat quad facing UP (Normal 0, 1, 0)
|
// --- Front Face ---
|
||||||
Helpers.AddFace(vertices, indices,
|
-0.5f, -0.5f, 0.0f, 0f, 0f, // Bottom-left
|
||||||
new Vertex(new Vector3(-0.5f, 0.0f, 0.5f), new Vector3(0, 1, 0), new Vector2(0, 0), Vector3.Zero, Vector3.Zero), // BL
|
0.5f, -0.5f, 0.0f, 1f, 0f, // Bottom-right
|
||||||
new Vertex(new Vector3( 0.5f, 0.0f, 0.5f), new Vector3(0, 1, 0), new Vector2(1, 0), Vector3.Zero, Vector3.Zero), // BR
|
0.5f, 0.5f, 0.0f, 1f, 1f, // Top-right
|
||||||
new Vertex(new Vector3( 0.5f, 0.0f, -0.5f), new Vector3(0, 1, 0), new Vector2(1, 1), Vector3.Zero, Vector3.Zero), // TR
|
-0.5f, 0.5f, 0.0f, 0f, 1f, // Top-left
|
||||||
new Vertex(new Vector3(-0.5f, 0.0f, -0.5f), new Vector3(0, 1, 0), new Vector2(0, 1), Vector3.Zero, Vector3.Zero) // TL
|
};
|
||||||
);
|
|
||||||
|
uint[] indices = {
|
||||||
// Convert to arrays and return
|
// Front face
|
||||||
List<float> finalFloats = new List<float>();
|
0, 2, 1,
|
||||||
foreach (var v in vertices)
|
0, 2, 3,
|
||||||
{
|
};
|
||||||
finalFloats.Add(v.Position.X); finalFloats.Add(v.Position.Y); finalFloats.Add(v.Position.Z);
|
|
||||||
finalFloats.Add(v.Normal.X); finalFloats.Add(v.Normal.Y); finalFloats.Add(v.Normal.Z);
|
return new Mesh(vertices, indices, Mesh.VertexLayout.PosTex);
|
||||||
finalFloats.Add(v.TexCoords.X); finalFloats.Add(v.TexCoords.Y);
|
|
||||||
finalFloats.Add(v.Tangent.X); finalFloats.Add(v.Tangent.Y); finalFloats.Add(v.Tangent.Z);
|
|
||||||
finalFloats.Add(v.Bitangent.X); finalFloats.Add(v.Bitangent.Y); finalFloats.Add(v.Bitangent.Z);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Mesh(finalFloats.ToArray(), indices.ToArray(), Mesh.VertexLayout.PosTexNormalTangent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -33,10 +33,5 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="Shapes\Bus.cs" />
|
<Compile Remove="Shapes\Bus.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<None Update="Levels\**">
|
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
|
||||||
</None>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user