Skip to content

Instantly share code, notes, and snippets.

@RichardB01
Created February 16, 2015 12:53
Show Gist options
  • Save RichardB01/edf8fe279ac6e4769856 to your computer and use it in GitHub Desktop.
Save RichardB01/edf8fe279ac6e4769856 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net.NetworkInformation;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
namespace Cappucinno
{
class Program
{
static void Main()
{
MainWindow window = new MainWindow();
window.Run();
}
}
class MainWindow : GameWindow
{
/// <summary>
/// ID of our program on the graphics card
/// </summary>
int pgmID;
/// <summary>
/// Address of the vertex shader
/// </summary>
int vsID;
/// <summary>
/// Address of the fragment shader
/// </summary>
int fsID;
/// <summary>
/// Address of the color parameter
/// </summary>
int attribute_vcol;
/// <summary>
/// Address of the position parameter
/// </summary>
int attribute_vpos;
/// <summary>
/// Address of the texture uniform
/// </summary>
int attribute_texcoord;
/// <summary>
/// Address of the modelview matrix uniform
/// </summary>
int uniform_mview;
private int uniform_texture;
/// <summary>
/// Address of the Vertex Buffer Object for our position parameter
/// </summary>
int vbo_position;
/// <summary>
/// Address of the Vertex Buffer Object for our color parameter
/// </summary>
int vbo_color;
/// <summary>
/// Address of the Vertex Buffer Object for our modelview matrix
/// </summary>
int vbo_mview;
/// <summary>
/// Address of the vertex buffer object for our texture paramter
/// </summary>
int vbo_texture;
/// <summary>
/// Array of our vertex positions
/// </summary>
Vector3[] vertdata;
/// <summary>
/// Array of our vertex colors
/// </summary>
Vector3[] coldata;
/// <summary>
/// Array of our texture coords
/// </summary>
Vector2[] texturedata;
/// <summary>
/// Array of our modelview matrices
/// </summary>
Matrix4[] mviewdata;
private int firstImage;
void initProgram()
{
pgmID = GL.CreateProgram();
loadShader("vs.glsl", ShaderType.VertexShader, pgmID, out vsID);
loadShader("fs.glsl", ShaderType.FragmentShader, pgmID, out fsID);
GL.LinkProgram(pgmID);
Console.WriteLine(GL.GetProgramInfoLog(pgmID));
attribute_vpos = GL.GetAttribLocation(pgmID, "vPosition");
attribute_vcol = GL.GetAttribLocation(pgmID, "vColor");
attribute_texcoord = GL.GetAttribLocation(pgmID, "texcoord");
uniform_mview = GL.GetUniformLocation(pgmID, "modelview");
uniform_texture = GL.GetUniformLocation(pgmID, "tex");
GL.GenBuffers(1, out vbo_position);
GL.GenBuffers(1, out vbo_color);
GL.GenBuffers(1, out vbo_texture);
GL.GenBuffers(1, out vbo_mview);
// Create Texture and assign the
firstImage = LoadTexture("sample2.jpg", 0);
}
int LoadTexture(string filename, int unit)
{
if (String.IsNullOrEmpty(filename))
throw new ArgumentException(filename);
GL.ActiveTexture((TextureUnit)unit);
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
// We will not upload mipmaps, so disable mipmapping (otherwise the texture will not appear).
// We can use GL.GenerateMipmaps() or GL.Ext.GenerateMipmaps() to create
// mipmaps automatically. In that case, use TextureMinFilter.LinearMipmapLinear to enable them.
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
Bitmap bmp = (Bitmap) Image.FromFile(filename);
BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bmp_data.Width, bmp_data.Height, 0,
PixelFormat.Bgra, PixelType.UnsignedByte, bmp_data.Scan0);
bmp.UnlockBits(bmp_data);
return id;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Width = 256;
Height = 256;
initProgram();
vertdata = new[]
{
new Vector3(-1f, -1f, 0f),
new Vector3( 1f, -1f, 0f),
new Vector3( 1f, 1f, 0f),
new Vector3(-1f, 1f, 0f)
};
coldata = new[]
{
new Vector3(1f, 0f, 0f),
new Vector3( 0f, 0f, 1f),
new Vector3( 0f, 1f, 0f)
};
texturedata = new[]
{
new Vector2(0f, 0f),
new Vector2(1f, 0f),
new Vector2(1f, 1f),
new Vector2(0f, 1f)
};
mviewdata = new[]{
Matrix4.Identity
};
Title = "Cappucinno";
GL.ClearColor(Color.CornflowerBlue);
GL.PointSize(5f);
}
/// <summary>
/// This creates a new shader (using a value from the ShaderType enum), loads code for it, compiles it, and adds it to our program.
/// It also prints any errors it found to the console, which is really nice for when you make a mistake in a shader (it will also yell at you if you use deprecated code).
/// </summary>
/// <param name="filename">File to load the shader from</param>
/// <param name="type">Type of shader to load</param>
/// <param name="program">ID of the program to use the shader with</param>
/// <param name="address">Address of the compiled shader</param>
void loadShader(String filename, ShaderType type, int program, out int address)
{
address = GL.CreateShader(type);
using (StreamReader sr = new StreamReader(filename))
{
GL.ShaderSource(address, sr.ReadToEnd());
}
GL.CompileShader(address);
GL.AttachShader(program, address);
Console.WriteLine(GL.GetShaderInfoLog(address));
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Viewport(0, 0, Width, Height);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Texture2D);
GL.EnableVertexAttribArray(attribute_vpos);
GL.EnableVertexAttribArray(attribute_vcol);
GL.EnableVertexAttribArray(attribute_texcoord);
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
GL.DisableVertexAttribArray(attribute_vpos);
GL.DisableVertexAttribArray(attribute_vcol);
GL.DisableVertexAttribArray(attribute_texcoord);
GL.Flush();
SwapBuffers();
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_position);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(vertdata.Length * Vector3.SizeInBytes), vertdata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_color);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (IntPtr)(coldata.Length * Vector3.SizeInBytes), coldata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_vcol, 3, VertexAttribPointerType.Float, true, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo_texture);
GL.BufferData<Vector2>(BufferTarget.ArrayBuffer, (IntPtr)(texturedata.Length * Vector2.SizeInBytes), texturedata, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(attribute_texcoord, 2, VertexAttribPointerType.Float, true, 0, 0);
GL.UniformMatrix4(uniform_mview, false, ref mviewdata[0]);
GL.Uniform1(uniform_texture, 0);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, firstImage);
GL.UseProgram(pgmID);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment