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

Commit

Permalink
Shaders and other
Browse files Browse the repository at this point in the history
Added shader support, some bug fixes and code cleanup
Resolves #5. Resolves #2.
  • Loading branch information
fonsp committed Oct 9, 2014
1 parent 327d4a4 commit e75341c
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 49 deletions.
4 changes: 2 additions & 2 deletions GraphicsEngine/Content/ObjConverter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using GraphicsLibrary.Core;
using OpenTK;
using System.Threading;
using System.Globalization;
using OpenTK;
using GraphicsLibrary.Core;
using GraphicsLibrary.Collision;

namespace GraphicsLibrary.Content
Expand Down
3 changes: 0 additions & 3 deletions GraphicsEngine/Content/TextureManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#region References
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;

using OpenTK.Graphics.OpenGL;
#endregion

namespace GraphicsLibrary.Content
{
Expand Down
8 changes: 2 additions & 6 deletions GraphicsEngine/Core/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#region References
using GraphicsLibrary.Content;

using OpenTK;
using OpenTK.Graphics;
using OpenTK;
using OpenTK.Graphics.OpenGL;
#endregion
using GraphicsLibrary.Content;

namespace GraphicsLibrary.Core
{
Expand Down
4 changes: 2 additions & 2 deletions GraphicsEngine/Core/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,26 @@ public void RemoveChild(string childName)
{
if(children.ContainsKey(childName))
{
children[childName] = null;
children.Remove(childName);
}
else
{
//TODO: Child does not exits
}
//TODO: Cleanup
}

public void RemoveChild(Node node)
{
if(children.ContainsValue(node))
{
node = null;
children.Remove(node.name); //TODO: performance
}
else
{
//TODO: Child does not exist
}
//TODO: Cleanup
}

public bool Equals(Node obj)
Expand Down
121 changes: 121 additions & 0 deletions GraphicsEngine/Core/Shader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Diagnostics;
using OpenTK.Graphics.OpenGL;

namespace GraphicsLibrary.Core
{
public class Shader
{
public string vertexShader, fragmentShader;
private int vso, fso, sp;
private bool created;

public int vertexShaderObject
{
get { return vso; }
}

public int fragmentShaderObject
{
get { return fso; }
}

public int shaderProgram
{
get { return sp; }
}

public Shader(string vertexShader, string fragmentShader)
{
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
}

public Shader()
{
vertexShader = @"
#version 120
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}";
fragmentShader = @"
#version 120
uniform sampler2D tex;
void main()
{
gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
}";
}

public void GenerateShaders()
{
int statusCode;
string info;

//Create
vso = GL.CreateShader(ShaderType.VertexShader);
fso = GL.CreateShader(ShaderType.FragmentShader);

//Load & compile vertex shader
GL.ShaderSource(vso, vertexShader);
GL.CompileShader(vso);
GL.GetShader(vso, ShaderParameter.CompileStatus, out statusCode);

if(statusCode != 1)
{
GL.GetShaderInfoLog(vso, out info);
throw new ApplicationException(info);
}

//Load & compile fragment shader
GL.ShaderSource(fso, fragmentShader);
GL.CompileShader(fso);
GL.GetShader(fso, ShaderParameter.CompileStatus, out statusCode);

if(statusCode != 1)
{
GL.GetShaderInfoLog(fso, out info);
throw new ApplicationException(info);
}

sp = GL.CreateProgram();
GL.AttachShader(sp, fso);
GL.AttachShader(sp, vso);

created = true;
}

public void Enable()
{
if(created)
{
GL.LinkProgram(sp);
GL.UseProgram(sp);
}
else
{
Debug.WriteLine("WARNING: failed to enable shader: shader does not exist");
}
}

public void Remove()
{
if(created)
{
GL.DetachShader(sp, vso);
GL.DetachShader(sp, fso);
GL.DeleteShader(vso);
GL.DeleteShader(fso);
created = false;
}
else
{
Debug.WriteLine("WARNING: failed to remove shader: shader does not exist");
}
}
}
}
1 change: 1 addition & 0 deletions GraphicsEngine/GraphicsLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Core\Entity.cs" />
<Compile Include="Core\Face.cs" />
<Compile Include="Core\Material.cs" />
<Compile Include="Core\Shader.cs" />
<Compile Include="Hud\HudConsole.cs" />
<Compile Include="Hud\HudElement.cs" />
<Compile Include="Core\Mesh.cs" />
Expand Down
4 changes: 1 addition & 3 deletions GraphicsEngine/Hud/HudElement.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#region References
using System.Collections.Generic;
using System.Collections.Generic;
using OpenTK;
#endregion

namespace GraphicsLibrary.Hud
{
Expand Down
4 changes: 2 additions & 2 deletions GraphicsEngine/Hud/HudImage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GraphicsLibrary.Content;
using OpenTK.Graphics;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using GraphicsLibrary.Content;

namespace GraphicsLibrary.Hud
{
Expand Down
4 changes: 2 additions & 2 deletions GraphicsEngine/Hud/TextField.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Drawing;
using System.Text;
using GraphicsLibrary.Content;
using GraphicsLibrary.Core;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using GraphicsLibrary.Content;
using GraphicsLibrary.Core;

namespace GraphicsLibrary.Hud
{
Expand Down
36 changes: 23 additions & 13 deletions GraphicsEngine/RenderWindow.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
//#define FULLSCREEN

#region References
using System;
using System.Diagnostics;
using System.Drawing;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

using GraphicsLibrary.Timing;
using GraphicsLibrary.Content;
using GraphicsLibrary.Core;
using GraphicsLibrary.Hud;
#endregion

namespace GraphicsLibrary
{
Expand All @@ -30,17 +26,14 @@ public static RenderWindow Instance
get { return instance ?? (instance = new RenderWindow()); }
}
#endregion
#region Variables
public bool escapeOnEscape = true;

public bool escapeOnEscape = true;
private readonly GameTimer updateSw = new GameTimer();
public bool enableVelocity = true;

protected double timeSinceLastUpdate = 0;
public double timeMultiplier = 1;

public int amountOfRenderPasses = 3;
#endregion
public Shader defaultShader = new Shader();

public RenderWindow(string windowName, int width, int height)
: base(width, height, GraphicsMode.Default, windowName
Expand All @@ -64,8 +57,8 @@ protected override void OnLoad(EventArgs e)
{
#region General
Debug.WriteLine("Initializing OpenGL..");
WindowBorder = WindowBorder.Resizable;

WindowBorder = WindowBorder.Resizable;
try
{
GL.ClearColor(Color.Black);
Expand Down Expand Up @@ -112,7 +105,8 @@ protected override void OnLoad(EventArgs e)
{
GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { .4f, .4f, .4f, 0.0f });
GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { .95f, .95f, .95f, 0.0f });
GL.Light(LightName.Light0, LightParameter.Position, new float[] { .0f, .0f, .0f, 0.0f });
//GL.Light(LightName.Light0, LightParameter.Position, new float[] { .8f, .9f, 1.0f, 0.0f });
GL.Light(LightName.Light0, LightParameter.Position, Vector4.Normalize(new Vector4(.4f, -.9f, .5f, 0.0f)));

GL.Enable(EnableCap.Lighting);
GL.Enable(EnableCap.Light0);
Expand All @@ -123,6 +117,21 @@ protected override void OnLoad(EventArgs e)
Exit();
}

#endregion
#region Default shaders init
Debug.WriteLine("Initializing default shader..");

try
{
defaultShader.GenerateShaders();
defaultShader.Enable();
}
catch(Exception exception)
{
Debug.WriteLine("WARNING: default shader could not be initialized: {0}", exception.Message);
Exit();
}

#endregion
#region Camera init
Debug.WriteLine("Initializing camera..");
Expand Down Expand Up @@ -244,12 +253,13 @@ protected override void OnRenderFrame(FrameEventArgs e)
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);

GL.Light(LightName.Light0, LightParameter.Position, new float[] { .8f, .9f, 1.0f, 0.0f });
//Update shaders
int loc = GL.GetUniformLocation(defaultShader.shaderProgram, "time");
GL.Uniform1(loc, 0.0f);

for(int i = 0; i < amountOfRenderPasses; i++)
{
RootNode.Instance.StartRender(i);

}

/*modelview = Matrix4.LookAt(Camera.Instance.position, Camera.Instance.position - Vector3.UnitZ, Vector3.UnitY);
Expand Down
8 changes: 4 additions & 4 deletions Resim/Program/InitGame.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
using GraphicsLibrary;
using GraphicsLibrary.Core;
using GraphicsLibrary.Hud;
using GraphicsLibrary.Input;
using OpenTK;
using OpenTK.Graphics;
using GraphicsLibrary.Collision;
using System.Collections.Generic;

namespace Resim.Program
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public override void InitGame()
HudBase.Instance.Add(comboTextFields[i]);
}

HudBase.Instance.Add(grainImage);
//HudBase.Instance.Add(grainImage);
HudBase.Instance.Add(hudDebug);
HudBase.Instance.Add(crossHair);

Expand Down
2 changes: 1 addition & 1 deletion Resim/Program/InitNetwork.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using GraphicsLibrary.Core;
using OpenTK;
using GraphicsLibrary.Core;

namespace Resim.Program
{
Expand Down
3 changes: 1 addition & 2 deletions Resim/Program/LoadResources.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.IO;
using OpenTK;
using GraphicsLibrary.Content;
using GraphicsLibrary.Core;
using GraphicsLibrary.Hud;
using OpenTK;
using GraphicsLibrary.Collision;
using System;

namespace Resim.Program
{
Expand Down
2 changes: 1 addition & 1 deletion Resim/Program/ProgramEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Globalization;
using System.Threading;
using OpenTK;
using GraphicsLibrary;
using GraphicsLibrary.Hud;
using OpenTK;

namespace Resim.Program
{
Expand Down
Loading

0 comments on commit e75341c

Please sign in to comment.