Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Dec 21, 2014
1 parent a19694a commit 56fd72f
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 154 deletions.
43 changes: 33 additions & 10 deletions GraphicsEngine/Collision/CollisionBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@

namespace GraphicsLibrary.Collision
{
/// <summary>
/// A collision AABB, as defined by two corners.
/// </summary>
public class CollisionAABB
{
public Vector3 lb, rt;
/// <summary>
/// The left, bottom, back corner
/// </summary>
public Vector3 lb;
/// <summary>
/// The right, top, front corner
/// </summary>
public Vector3 rt;

public CollisionAABB(Vector3 lb, Vector3 rt)
{
this.lb = lb; //min
this.rt = rt; //max
this.lb = lb; // min
this.rt = rt; // max
}

/// <summary>
/// Intersect with the specified ray
/// </summary>
/// <param name="ray"></param>
/// <returns>Hit distance</returns>
public float Intersect(CollisionRay ray)
{
// Algorithm: http://i.stack.imgur.com/tfvSJ.png
// Algorithm illustration: http://i.stack.imgur.com/tfvSJ.png
Vector3 t = new Vector3();

if(ray.dir == Vector3.Zero)
Expand All @@ -27,8 +42,7 @@ public float Intersect(CollisionRay ray)
Vector3 _lb = ray.eye - lb;
Vector3 _rt = ray.eye - rt;


//X
// X
if(r.X > 0) // Determine candidate plane
{
t.X = Max(-1, _lb.X / -r.X); // Save distance to hit point
Expand All @@ -37,7 +51,7 @@ public float Intersect(CollisionRay ray)
{
t.X = Max(-1, _rt.X / -r.X);
}
//Y
// Y
if(r.Y > 0)
{
t.Y = Max(-1, _lb.Y / -r.Y);
Expand All @@ -46,7 +60,7 @@ public float Intersect(CollisionRay ray)
{
t.Y = Max(-1, _rt.Y / -r.Y);
}
//Z
// Z
if(r.Z < 0)
{
t.Z = Max(-1, _lb.Z / r.Z);
Expand All @@ -55,7 +69,7 @@ public float Intersect(CollisionRay ray)
{
t.Z = Max(-1, _rt.Z / r.Z);
}
r.Z *= -1; //why?
r.Z *= -1; // why?

// The largest hit distance must be that of the hit plane
if(t.X >= t.Y && t.X >= t.Z)
Expand Down Expand Up @@ -86,14 +100,23 @@ public float Intersect(CollisionRay ray)
return t.Z;
}
}
return -1;
return -1; // No intersection
}

/// <summary>
/// Intersect with the specified point
/// </summary>
/// <param name="position">Point coordinates</param>
/// <returns>True if the point lies inside</returns>
public bool isInside(Vector3 position)
{
return position.X >= lb.X && position.X <= rt.X && position.Y >= lb.Y && position.Y <= rt.Y && position.Z >= lb.Z && position.Z <= rt.Z;
}

/// <summary>
/// Translate the AABB
/// </summary>
/// <param name="delta">Movement vector</param>
public void Translate(Vector3 delta)
{
lb += delta;
Expand Down
14 changes: 13 additions & 1 deletion GraphicsEngine/Collision/CollisionRay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ namespace GraphicsLibrary.Collision
{
public class CollisionRay
{
public Vector3 eye, dir;
/// <summary>
/// Ray origin
/// </summary>
public Vector3 eye;
/// <summary>
/// Ray direction
/// </summary>
public Vector3 dir;

/// <summary>
/// Creates a new collision ray
/// </summary>
/// <param name="origin">Ray origin</param>
/// <param name="direction">Ray direction</param>
public CollisionRay(Vector3 origin, Vector3 direction)
{
eye = origin;
Expand Down
28 changes: 28 additions & 0 deletions GraphicsEngine/Content/ObjConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ namespace GraphicsLibrary.Content
{
public static class ObjConverter
{
/// <summary>
/// Converts the given .obj file to a Mesh object
/// </summary>
/// <param name="inputFile">.obj file (not path)</param>
/// <returns>Generated Mesh</returns>
public static Mesh ConvertObjToMesh(string inputFile)
{
return ConvertObjToMesh(inputFile, Vector3.Zero);
}

/// <summary>
/// Converts the given .obj file to a Mesh object
/// </summary>
/// <param name="inputFile">.obj file (not path)</param>
/// <param name="offset">Mesh offset</param>
/// <returns>Generated Mesh</returns>
public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand Down Expand Up @@ -159,6 +170,12 @@ public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
return output;
}

/// <summary>
/// Converts the given .obj file to a Mesh object that supports VBO rendering
/// </summary>
/// <param name="inputFile">.obj file (not path)</param>
/// <param name="offset">Mesh offset</param>
/// <returns>Generated Mesh</returns>
public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand Down Expand Up @@ -310,11 +327,22 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
Debug.WriteLine("Obj conversion complete: " + faces.Count + " faces were converted.");
return output;
}

/// <summary>
/// Converts the given .obj file to a Mesh object that supports VBO rendering
/// </summary>
/// <param name="inputFile">.obj file (not path)</param>
/// <returns>Generated Mesh</returns>
public static Mesh ConvertObjToVboMesh(string inputFile)
{
return ConvertObjToVboMesh(inputFile, Vector3.Zero);
}

/// <summary>
/// Converts the given .obj file to an array of collision boxes. A box is created for every group in the .obj file.
/// </summary>
/// <param name="inputFile">.obj file (not path)</param>
/// <returns>The generated array of collision boxes</returns>
public static CollisionAABB[] ConvertObjToAABBarray(string inputFile)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand Down
37 changes: 32 additions & 5 deletions GraphicsEngine/Content/TextureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

namespace GraphicsLibrary.Content
{
/// <summary>
/// Transfers textures to and from the GPU, and stores the pointers from GPU memory.
/// </summary>
public static class TextureManager
{
static readonly Dictionary<string, int> mTexCache;

/// <summary>
/// The number of loaded textures
/// </summary>
public static int numberOfTextures
{
get
Expand All @@ -32,11 +38,23 @@ static TextureManager()
}
}

/// <summary>
/// Load an image file to GPU memory.
/// </summary>
/// <param name="name">New texture name</param>
/// <param name="path">Path of the image source</param>
public static void AddTexture(string name, string path)
{
AddTexture(name, path, TextureMinFilter.Nearest, TextureMagFilter.Nearest);
}

/// <summary>
/// Load an image file to GPU memory.
/// </summary>
/// <param name="name">New texture name</param>
/// <param name="path">Path of the image source</param>
/// <param name="textureMinFilter">Texture filtering: min</param>
/// <param name="textureMagFilter">Texture filtering: max</param>
public static void AddTexture(string name, string path, TextureMinFilter textureMinFilter, TextureMagFilter textureMagFilter)
{
try
Expand Down Expand Up @@ -73,6 +91,10 @@ public static void AddTexture(string name, string path, TextureMinFilter texture

}

/// <summary>
/// Remove a texture from GPU memory.
/// </summary>
/// <param name="name">Texture name</param>
public static void RemoveTexture(string name)
{
try
Expand All @@ -94,6 +116,9 @@ public static void RemoveTexture(string name)

}

/// <summary>
/// Remove all textures from GPU memory.
/// </summary>
public static void ClearTextureCache()
{
foreach(KeyValuePair<string, int> feTexBuffer in mTexCache)
Expand All @@ -103,17 +128,19 @@ public static void ClearTextureCache()
mTexCache.Clear();
}

/// <summary>
/// Gets the pointer to the texture in GPU memory.
/// </summary>
/// <param name="name">Texture name</param>
/// <returns>Pointer to the texture in GPU memory</returns>
public static int GetTexture(string name)
{
if(mTexCache.ContainsKey(name))
{
return mTexCache[name];
}
else
{
Debug.WriteLine("WARNING: failed to get texture " + name);
return mTexCache["default"];
}
Debug.WriteLine("WARNING: failed to get texture " + name);
return mTexCache["default"];
}
}
}
12 changes: 12 additions & 0 deletions GraphicsEngine/Core/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public static Camera Instance

public Matrix4 modelview;

/// <summary>
/// The OpenGL projection matrix of this camera
/// </summary>
public Matrix4 projection
{
get
Expand All @@ -23,6 +26,9 @@ public Matrix4 projection
}

private float zNear = 1f;
/// <summary>
/// The near clipping plane
/// </summary>
public float ZNear
{
get
Expand All @@ -36,6 +42,9 @@ public float ZNear
}
}
private float zFar = 100000f;
/// <summary>
/// The far clipping plane
/// </summary>
public float ZFar
{
get
Expand All @@ -49,6 +58,9 @@ public float ZFar
}
}
private float fov = 90f;
/// <summary>
/// Vertical field of view, in degrees
/// </summary>
public float Fov
{
get
Expand Down
16 changes: 16 additions & 0 deletions GraphicsEngine/Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ namespace GraphicsLibrary.Core
{
public class Entity:Node
{

public Mesh mesh;
/// <summary>
/// Entity visibility.
/// </summary>
public bool isVisible = true;
/// <summary>
/// Enable/dispable lighting.
/// </summary>
public bool isLit = true;
/// <summary>
/// Write to the OpenGL depth buffer. True by default.
/// </summary>
public bool writeDepthBuffer = true;
/// <summary>
/// Read the OpenGL depth buffer. True by default.
/// </summary>
public bool readDepthBuffer = true;
/// <summary>
/// Render a wireframe after rendering the mesh.
/// </summary>
public bool wireFrame = false;

public float materialAge = 0;
Expand Down
13 changes: 12 additions & 1 deletion GraphicsEngine/Core/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ namespace GraphicsLibrary.Core
{
public class Material
{
/// <summary>
/// Name of the material texture
/// </summary>
public string textureName = "default";
public Color4 baseColor = Color4.White;


public bool enableTextureTransitions = false;
public bool enableColorTransitions = false;
public bool smoothTransitionColors = true;
Expand Down Expand Up @@ -80,6 +82,11 @@ public string GetCurrentTexture()
return textureName;
}

/// <summary>
/// Calculates the current color, using transition colors.
/// </summary>
/// <param name="fraction">Fraction between 0.0 and 1.0</param>
/// <returns>The current color</returns>
public Color4 GetCurrentColor(float fraction)
{
if(enableColorTransitions)
Expand Down Expand Up @@ -114,6 +121,10 @@ public Color4 GetCurrentColor(float fraction)
return baseColor;
}

/// <summary>
/// Returns the base color.
/// </summary>
/// <returns>The base color</returns>
public Color4 GetCurrentColor()
{
return baseColor;
Expand Down
Loading

0 comments on commit 56fd72f

Please sign in to comment.