Background: I've started playing around with raytracing and thought it'll be a good idea to write the raytracer using GLSL shaders as it'll optimize the performance a lot.
Problem: I've written (more of a assemble from parts of code on the internet) a simple program to test GLSL shaders but it isn't working. It runs but just shows the clear color. I'm using OpenTK and C#. My video card supports OpenGL 2.1 and I've seen some GLSL shaders running on it (though I don't have their source code).
Here is my code.
The Shader.cs file can be found
here.
My main class is this (It's the OpenTK's Game.cs file with some code added:
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;
namespace GLSLTest
{
class Game : GameWindow
{
public Shader Shader;
public Game()
: base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
{
VSync = VSyncMode.On;
Shader = new Shader(
"void main(void){ gl_Position = gl_Vertex;}",
"void main(void) {"
+ "vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);"
+ "float dist_squared = dot(pos, pos);"
+ "gl_FragColor = (dist_squared < 400.0) "
+ "? vec4(.90, .90, .90, 1.0)"
+ ": vec4(.20, .20, .40, 1.0);"
+ "}");
Shader.Activate();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0.1f, 0.2f, 0.5f, 0.0f);
GL.Enable(EnableCap.DepthTest);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Keyboard[Key.Escape])
Exit();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
SwapBuffers();
}
[STAThread]
static void Main()
{
using (Game game = new Game())
{
game.Run(30.0);
}
}
}
}