Skip to content

Commit

Permalink
some more changes to the shader loader
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-dumitru committed Feb 24, 2012
1 parent 81ba02e commit 2461372
Show file tree
Hide file tree
Showing 33 changed files with 2,174 additions and 579 deletions.
15 changes: 8 additions & 7 deletions App/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@

namespace App {
public class App {
private IGraphics _graphics = null;
private WebGLGraphics _graphics = null;

public void Init() {
/*Initialize the environment*/
this._InitEnvironment();

/*initialize canvas element*/
CanvasElementGl canvasElem = Document.GetElementById("mainCanvas").As<CanvasElementGl>();
if (canvasElem == null)
Expand All @@ -22,7 +19,12 @@ public class App {
this._graphics = new WebGLGraphics(canvasElem);
this._graphics.Clear();

Core.ResourceManager.GetResource("/Data/JSON/test.json").ResourceChanged += delegate(Resource sender, object args) {
/*Initialize the environment*/
this._InitEnvironment();

Resource res = Core.ResourceManager.GetResource("/Data/Shader/test.shader");

res.ResourceChanged += delegate(Resource sender, object args) {
Script.Alert("aaa");
};
}
Expand All @@ -39,10 +41,9 @@ public class App {

resourceManager.AddLoader(new ImageLoader());
resourceManager.AddLoader(new JsonLoader());
resourceManager.AddLoader(new ShaderLoader(this._graphics.Context));

Core.ResourceManager = resourceManager;
}


}
}
2 changes: 2 additions & 0 deletions App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
<Reference Include="Script.Web" />
</ItemGroup>
<ItemGroup>
<Compile Include="Util\Xml\XmlHelper.cs" />
<Content Include="Properties\App.ruleset" />
<Content Include="Properties\App.script" />
<Compile Include="Core\Environment.cs" />
<Compile Include="Data\Image.cs" />
<Compile Include="Data\ResourceManager.cs" />
<Compile Include="Graphics\ActiveInfo.cs" />
<Compile Include="Graphics\Buffer.cs" />
<Compile Include="Graphics\CompiledShader.cs" />
<Compile Include="Graphics\FrameBuffer.cs" />
<Compile Include="Graphics\RenderBuffer.cs" />
<Compile Include="Graphics\Shader.cs" />
Expand Down
101 changes: 101 additions & 0 deletions App/Data/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using System.Linq;
using System.Net;
using System.Serialization;
using GLSharp.Graphics;
using System.Xml;
using GLSharp.Util.Xml;

namespace GLSharp.Data {
public interface IResourceLoader {
Expand Down Expand Up @@ -75,8 +78,106 @@ public class JsonLoader : IResourceLoader {
return ret;
}
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
public class ShaderLoader : IResourceLoader {
private WebGL _gl = null;

public ShaderLoader(WebGL gl) {
this._gl = gl;
}

public List<string> Extension {
get {
List<String> ret = new List<String>();
ret.Add(".shader");
return ret;
}
}

public Resource Load(string url) {
Resource ret = new Resource();
ret.Finished = false;

XmlHttpRequest req = new XmlHttpRequest();

req.Open(HttpVerb.Get, url, true);
req.OnReadyStateChange = delegate() {
if (req.ReadyState == ReadyState.Loaded && req.Status == 200) {
ret.Finished = true;

/*parse the xml document*/
ret.Data = this.ParseShader(XmlDocumentParser.Parse(req.ResponseText));
}
};

req.Send();
return ret;
}

private ICompiledShader ParseShader(XmlDocument doc) {
if(doc.DocumentElement.Name != "shader")
throw new Exception("Invalid shader file");

/*source code for vertex / fragment shaders*/
String sourceFragment = null;
String sourceVertex = null;

CompiledShader ret = new CompiledShader();

/*the name of the shader*/
ret.Name = doc.DocumentElement.Attributes.GetNamedItem("name").Value;

foreach (XmlNode xmlNode in doc.DocumentElement.ChildNodes) {
if (xmlNode.NodeType != XmlNodeType.Element) continue;

if(xmlNode.Name == "vertex_shader") {
sourceVertex = XmlHelper.InnerText(xmlNode);
} else if(xmlNode.Name == "fragment_shader") {
sourceFragment = XmlHelper.InnerText(xmlNode);
} else if(xmlNode.Name == "uniform") {
//ret.Uniforms[XmlHelper.InnerText(xmlNode)]
}
}

if(sourceFragment == null || sourceVertex == null)
throw new Exception("Shader file does not contain fragment and vertex shaders");

/*compile shaders*/
IShader shaderFragment = this.CompileShader(sourceFragment, WebGLE.FragmentShader);
IShader shaderVertex = this.CompileShader(sourceVertex, WebGLE.VertexShader);

/*create the program*/
IShaderProgram shaderProgram = this._gl.CreateProgram();

this._gl.AttachShader(shaderProgram, shaderFragment);
this._gl.AttachShader(shaderProgram, shaderVertex);
this._gl.LinkProgram(shaderProgram);

if (!(bool)this._gl.GetProgramParameter(shaderProgram, WebGLE.LinkStatus)) {
throw new Exception("Cannot link shaders.");
}

ret.ShaderProgram = shaderProgram;

return ret;

}

private IShader CompileShader(String source, int type) {
IShader ret = this._gl.CreateShader(type);

this._gl.ShaderSource(ret, source);
this._gl.CompileShader(ret);

if(!(bool)this._gl.GetShaderParameter(ret, WebGLE.CompileStatus)) {
throw new Exception("Cannot compile shader.");
}


return ret;
}
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

Expand Down
35 changes: 35 additions & 0 deletions App/Graphics/CompiledShader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// CompiledShader.cs
//

using System;
using System.Collections.Generic;

namespace GLSharp.Graphics {
public class CompiledShader : ICompiledShader {

private String _name = "Undefined";
private IShaderProgram _shaderProgram = new ShaderProgram();
private readonly Dictionary<String, IUniform> _uniforms = new Dictionary<string, IUniform>();

//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

public string Name {
get { return this._name; }
set { this._name = value; }
}

public IShaderProgram ShaderProgram {
get { return this._shaderProgram; }
set { this._shaderProgram = value; }
}

public Dictionary<string, IUniform> Uniforms {
get { return this._uniforms; }
}

//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------

}
}
2 changes: 1 addition & 1 deletion App/Graphics/Shader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
using System.Linq;

namespace GLSharp.Graphics {
public class Shader {
public class Shader : IShader{
}
}
2 changes: 1 addition & 1 deletion App/Graphics/ShaderProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;

namespace GLSharp.Graphics {
public class ShaderProgram {
public class ShaderProgram : IShaderProgram{

}
}
5 changes: 4 additions & 1 deletion App/Graphics/UniformLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Collections.Generic;

namespace GLSharp.Graphics {
public class UniformLocation {
/*types of uniforms*/


public class UniformLocation : IUniformLocation {
}
}

0 comments on commit 2461372

Please sign in to comment.