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

Commit

Permalink
VBO and environment model
Browse files Browse the repository at this point in the history
Resolves #6. Resolves #8.
  • Loading branch information
fonsp committed Oct 26, 2014
1 parent 6b70e94 commit b8dcfbb
Show file tree
Hide file tree
Showing 14 changed files with 186,257 additions and 123,837 deletions.
51 changes: 29 additions & 22 deletions GraphicsEngine/Content/ObjConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
#region Geometric vertex
case "v":
case "V":
Vector3 pos = new Vector3((float)Convert.ToDouble(decomposed[1]),
(float)Convert.ToDouble(decomposed[2]),
(float)Convert.ToDouble(decomposed[3]));
Vector3 pos = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[2].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[3].Replace(",", ".")));

vertices[vCount] = pos - offset;

Expand All @@ -85,8 +85,8 @@ public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
#region Texture vertex
case "vt":

Vector2 tex = new Vector2((float)Convert.ToDouble(decomposed[1]),
1f - (float)Convert.ToDouble(decomposed[2]));
Vector2 tex = new Vector2((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
1f - (float)Convert.ToDouble(decomposed[2].Replace(",", ".")));

textCoords[vtCount] = tex;

Expand All @@ -96,10 +96,9 @@ public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
#endregion
#region Vertex normal
case "vn":

Vector3 nrm = new Vector3((float)Convert.ToDouble(decomposed[1]),
(float)Convert.ToDouble(decomposed[2]),
(float)Convert.ToDouble(decomposed[3]));
Vector3 nrm = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[2].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[3].Replace(",", ".")));

normals[vnCount] = nrm;

Expand Down Expand Up @@ -216,9 +215,9 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
#region Geometric vertex
case "v":
case "V":
Vector3 pos = new Vector3((float)Convert.ToDouble(decomposed[1]),
(float)Convert.ToDouble(decomposed[2]),
(float)Convert.ToDouble(decomposed[3]));
Vector3 pos = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[2].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[3].Replace(",", ".")));

vertices[vCount] = pos - offset;

Expand All @@ -230,8 +229,8 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
#region Texture vertex
case "vt":

Vector2 tex = new Vector2((float)Convert.ToDouble(decomposed[1]),
1f - (float)Convert.ToDouble(decomposed[2]));
Vector2 tex = new Vector2((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
1f - (float)Convert.ToDouble(decomposed[2].Replace(",", ".")));

textCoords[vtCount] = tex;

Expand All @@ -241,10 +240,9 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
#endregion
#region Vertex normal
case "vn":

Vector3 nrm = new Vector3((float)Convert.ToDouble(decomposed[1]),
(float)Convert.ToDouble(decomposed[2]),
(float)Convert.ToDouble(decomposed[3]));
Vector3 nrm = new Vector3((float)Convert.ToDouble(decomposed[1].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[2].Replace(",", ".")),
(float)Convert.ToDouble(decomposed[3].Replace(",", ".")));

normals[vnCount] = nrm;

Expand All @@ -260,9 +258,9 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
int[] vertexIndices = new int[length];
int[] normalIndices = new int[length];
int[] textureIndices = new int[length];
for (int i = 0; i < decomposed.Length - 1; i++)
for(int i = 0; i < decomposed.Length - 1; i++)
{
string[] vertexString = decomposed[i + 1].Split(new string[] {"/"}, StringSplitOptions.None);
string[] vertexString = decomposed[i + 1].Split(new string[] { "/" }, StringSplitOptions.None);
vertexIndices[i] = Convert.ToInt32(vertexString[0]);
textureIndices[i] = Convert.ToInt32(vertexString[1]);
normalIndices[i] = Convert.ToInt32(vertexString[2]);
Expand Down Expand Up @@ -294,19 +292,28 @@ public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
{
Vertex[] vertexArr = new Vertex[f.vIndices.Length];

if (f.vIndices.Length == 3)
if(f.vIndices.Length == 3)
{
for (int i = 0; i < f.vIndices.Length; i++)
for(int i = 0; i < 3; i++)
{
vertexArr[i] = new Vertex(vertices[f.vIndices[i]], normals[f.vnIndices[i]], textCoords[f.vtIndices[i]]);
}
}
else
{
Debugger.Break();
}
vOuput.AddRange(vertexArr);
output.polygonList.Add(new Polygon(vertexArr));
}
output.vertexArray = vOuput.ToArray();
Debug.WriteLine("Obj conversion complete: " + faces.Count + " faces were converted.");
return output;
}
public static Mesh ConvertObjToVboMesh(string inputFile)
{
return ConvertObjToVboMesh(inputFile, Vector3.Zero);
}

public static CollisionAABB[] ConvertObjToAABBarray(string inputFile)
{
Expand Down
5 changes: 4 additions & 1 deletion GraphicsEngine/Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public void Reload()
case ";":
//Comment
break;
case "":
//Empty line
break;
case "bool":
//Boolean
try
Expand Down Expand Up @@ -127,7 +130,7 @@ public void Reload()
}
if(error)
{
Debug.WriteLine("Error in config file " + fileName + " at line: " + line);
Debug.Write("Error in config file " + fileName + " at line: " + line);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion GraphicsEngine/Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public override void Render(int pass)

if(mesh.useVBO && mesh.hasVBO)
{
//Console.WriteLine("Rendering {0} using VBO", this.name);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.VertexArray);
Expand All @@ -84,7 +85,7 @@ public override void Render(int pass)
GL.BindBuffer(BufferTarget.ArrayBuffer, mesh.VBOids[0]);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.VBOids[1]);
GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f, 0, IntPtr.Zero);
GL.DrawElements(BeginMode.Triangles, mesh.vertexArray.Length, DrawElementsType.UnsignedShort, 0);
GL.DrawElements(BeginMode.Triangles, mesh.vertexArray.Length, DrawElementsType.UnsignedInt, 0);

GL.DisableClientState(ArrayCap.TextureCoordArray);
GL.DisableClientState(ArrayCap.NormalArray);
Expand Down
20 changes: 17 additions & 3 deletions GraphicsEngine/Core/Mesh.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics.OpenGL;

Expand Down Expand Up @@ -32,10 +33,22 @@ public override string ToString()

public void GenerateVBO()
{
if(!useVBO || hasVBO)
if(!useVBO)
{
Debug.WriteLine("WARNING: VBO generation failed: Mesh is using immediate mode");
return;
}
if(hasVBO)
{
Debug.WriteLine("WARNING: VBO generation failed: VBO already exists");
return;
}
if(vertexArray == null)
{
Debug.WriteLine("WARNING: VBO generation failed: vertexArray is null");
return;
}

int stride = BlittableValueType.StrideOf(vertexArray);


Expand All @@ -45,9 +58,10 @@ public void GenerateVBO()

GL.GenBuffers(1, out VBOids[1]);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOids[1]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(vertexArray.Length * sizeof(ushort)), IntPtr.Zero, BufferUsageHint.StaticDraw);
GL.BufferSubData(BufferTarget.ElementArrayBuffer, IntPtr.Zero, (IntPtr)(vertexArray.Length * sizeof(ushort)), RenderWindow.Instance.elementBase);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(vertexArray.Length * sizeof(uint)), IntPtr.Zero, BufferUsageHint.StaticDraw);
GL.BufferSubData(BufferTarget.ElementArrayBuffer, IntPtr.Zero, (IntPtr)(vertexArray.Length * sizeof(uint)), RenderWindow.Instance.elementBase);
hasVBO = true;
Debug.WriteLine("VBO generation complete");
}
}
}
4 changes: 2 additions & 2 deletions GraphicsEngine/Core/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void main()
gl_Position = gl_ProjectionMatrix * v;*/
gl_Position = ftransform();
gl_FrontColor = gl_Color;
gl_FrontColor = vec4(vec3(1.0 / (gl_Position.w / 2000.0 + 1.0)), 1.0);
gl_TexCoord[0] = gl_MultiTexCoord0;
}",
fragmentShader = @"
Expand All @@ -146,7 +146,7 @@ void main()
void main()
{
float a = sqrt(intensity);
gl_FragColor = texture2D(tex, gl_TexCoord[0].xy) * (vec4(a, a, a, 1.0) * gl_LightSource[0].diffuse + vec4(1.0-a, 1.0-a, 1.0-a, 1.0) * gl_LightSource[0].ambient);
gl_FragColor = gl_Color * (texture2D(tex, gl_TexCoord[0].xy) * (vec4(a, a, a, 1.0) * gl_LightSource[0].diffuse + vec4(1.0-a, 1.0-a, 1.0-a, 1.0) * gl_LightSource[0].ambient));
}"
};
}
Expand Down
6 changes: 3 additions & 3 deletions GraphicsEngine/Core/Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace GraphicsLibrary.Core
{
public struct Vertex
{
public Vector3 pos;
public Vector3 nrm;
public Vector2 tex;

public Vector3 nrm;
public Vector3 pos;

public Vertex(Vector3 pos, Vector3 nrm, Vector2 tex)
{
this.pos = pos;
Expand Down
4 changes: 2 additions & 2 deletions GraphicsEngine/RenderWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static RenderWindow Instance
public double timeMultiplier = 1;
public int amountOfRenderPasses = 3;
public Shader defaultShader = Shader.diffuseShader;
public ushort[] elementBase = new ushort[ushort.MaxValue];
public uint[] elementBase = new uint[1000000];

public RenderWindow(string windowName, int width, int height)
: base(width, height, GraphicsMode.Default, windowName
Expand Down Expand Up @@ -75,7 +75,7 @@ protected override void OnLoad(EventArgs e)
GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);

for(ushort i = 0; i < ushort.MaxValue; i++)
for(uint i = 0; i < elementBase.Length; i++)
{
elementBase[i] = i;
}
Expand Down
Loading

0 comments on commit b8dcfbb

Please sign in to comment.