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

Commit

Permalink
Merge pull request #10 from fons-/VBO_dev
Browse files Browse the repository at this point in the history
VBO merge
fonsp committed Oct 26, 2014
2 parents a3afc65 + b8dcfbb commit 4870376
Showing 20 changed files with 214,175 additions and 46 deletions.
173 changes: 164 additions & 9 deletions GraphicsEngine/Content/ObjConverter.cs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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;

@@ -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;

@@ -160,6 +159,162 @@ public static Mesh ConvertObjToMesh(string inputFile, Vector3 offset)
return output;
}

public static Mesh ConvertObjToVboMesh(string inputFile, Vector3 offset)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

Mesh output = new Mesh();
List<Vertex> vOuput = new List<Vertex>();

string[] inputElements = inputFile.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);

int vCount = 1;
int vnCount = 1;
int vtCount = 1;

int totalVertices = 0;
int totalNormals = 0;
int totalTextCoordinates = 0;

foreach(string s in inputElements)
{
string[] decomposed = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

if(decomposed[0] == "v" | decomposed[0] == "V")
{
totalVertices++;
}
else if(decomposed[0] == "vt")
{
totalTextCoordinates++;
}
else if(decomposed[0] == "vn")
{
totalNormals++;
}
}

//Vertex[] vertices = new Vertex[totalVertices + 1];
Vector3[] vertices = new Vector3[totalVertices + 1];
Vector3[] normals = new Vector3[totalNormals + 1];
Vector2[] textCoords = new Vector2[totalTextCoordinates + 1];

List<Face> faces = new List<Face>();

foreach(string s in inputElements)
{
string[] decomposed = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

switch(decomposed[0])
{
#region Comment
case "#":

break;
#endregion
#region Geometric vertex
case "v":
case "V":
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;

vCount++;


break;
#endregion
#region Texture vertex
case "vt":

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

textCoords[vtCount] = tex;

vtCount++;

break;
#endregion
#region Vertex normal
case "vn":
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;

vnCount++;

break;
#endregion
#region Face
case "f":
case "F":

int length = decomposed.Length - 1;
int[] vertexIndices = new int[length];
int[] normalIndices = new int[length];
int[] textureIndices = new int[length];
for(int i = 0; i < decomposed.Length - 1; i++)
{
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]);
}
faces.Add(new Face(vertexIndices, textureIndices, normalIndices));



break;
#endregion
#region Mtl library
case "mtllib":

//TODO: MTLLIB

break;
#endregion
#region Mtl library reference
case "usemtl":

//TODO: USEMTL

break;
#endregion
}
}

foreach(Face f in faces)
{
Vertex[] vertexArr = new Vertex[f.vIndices.Length];

if(f.vIndices.Length == 3)
{
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)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
5 changes: 4 additions & 1 deletion GraphicsEngine/Core/Config.cs
Original file line number Diff line number Diff line change
@@ -79,6 +79,9 @@ public void Reload()
case ";":
//Comment
break;
case "":
//Empty line
break;
case "bool":
//Boolean
try
@@ -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);
}
}
}
45 changes: 33 additions & 12 deletions GraphicsEngine/Core/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenTK;
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using GraphicsLibrary.Content;

@@ -34,7 +35,7 @@ public override void Render(int pass)
{
if(mesh.shader == null)
{
if (isLit)
if(isLit)
{
Shader.diffuseShaderCompiled.Enable();
}
@@ -55,7 +56,7 @@ public override void Render(int pass)
GL.DepthMask(false);
}

if (!readDepthBuffer)
if(!readDepthBuffer)
{
GL.Disable(EnableCap.DepthTest);
}
@@ -73,18 +74,38 @@ public override void Render(int pass)

GL.Material(MaterialFace.Front, MaterialParameter.Diffuse, mesh.material.GetCurrentColor());

foreach(Polygon poly in mesh.polygonList)
if(mesh.useVBO && mesh.hasVBO)
{
GL.Begin(PrimitiveType.Polygon);

for(int i = 0; i < poly.vertices.Length; i++)
//Console.WriteLine("Rendering {0} using VBO", this.name);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.VertexArray);
GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f, 0, IntPtr.Zero);

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.UnsignedInt, 0);

GL.DisableClientState(ArrayCap.TextureCoordArray);
GL.DisableClientState(ArrayCap.NormalArray);
GL.DisableClientState(ArrayCap.VertexArray);
}
else
{
foreach(Polygon poly in mesh.polygonList)
{
GL.Normal3(poly.vertices[i].nrm);
GL.TexCoord2(poly.vertices[i].tex);
GL.Vertex3(poly.vertices[i].pos);
}
GL.Begin(PrimitiveType.Polygon);

for(int i = 0; i < poly.vertices.Length; i++)
{
GL.Normal3(poly.vertices[i].nrm);
GL.TexCoord2(poly.vertices[i].tex);
GL.Vertex3(poly.vertices[i].pos);
}

GL.End();
GL.End();
}
}
if(wireFrame)
{
44 changes: 43 additions & 1 deletion GraphicsEngine/Core/Mesh.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace GraphicsLibrary.Core
{
@@ -7,6 +11,11 @@ public class Mesh
public List<Polygon> polygonList = new List<Polygon>();
public Material material = new Material();
public Shader shader;
//VBO
public Vertex[] vertexArray;
public bool useVBO = false;
public bool hasVBO = false;
public uint[] VBOids = new uint[2];

public override string ToString()
{
@@ -21,5 +30,38 @@ public override string ToString()
}
return output;
}

public void GenerateVBO()
{
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);


GL.GenBuffers(1, out VBOids[0]);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOids[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertexArray.Length * stride), vertexArray, BufferUsageHint.StaticDraw);

GL.GenBuffers(1, out VBOids[1]);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOids[1]);
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
@@ -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 = @"
@@ -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));
}"
};
}
6 changes: 3 additions & 3 deletions GraphicsEngine/Core/Vertex.cs
Original file line number Diff line number Diff line change
@@ -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;
Loading
Oops, something went wrong.

0 comments on commit 4870376

Please sign in to comment.