An unhandled exception of type ‘System.AccessViolationException’ occurred in OpenTK.dll
private static void Window_Load(object sender, EventArgs e)
{
GL.ClearColor(new Color4(0.2f, 0.3f, 0.3f, 1.0f));
GL.Enable(EnableCap.Texture2D);
vShad = GL.CreateShader(ShaderType.VertexShader);
string verticalShader = File.ReadAllText("Shaders//VShader.glsl");
GL.ShaderSource(vShad, verticalShader);
GL.CompileShader(vShad);
Console.WriteLine(GL.GetShaderInfoLog(vShad));
fShad = GL.CreateShader(ShaderType.FragmentShader);
string fragmentShader = File.ReadAllText("shaders//FShader.glsl");
GL.ShaderSource(fShad, fragmentShader);
GL.CompileShader(fShad);
Console.WriteLine(GL.GetShaderInfoLog(fShad));
programID = GL.CreateProgram();
GL.AttachShader(programID, vShad);
GL.AttachShader(programID, fShad);
GL.LinkProgram(programID);
Console.WriteLine(GL.GetProgramInfoLog(programID));
float[] points = new float[]
{ //POSITION //COLOR //TEXCOORD
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
int[] indices = new int[]
{
0,1,3,
1,3,2
};
vao = GL.GenVertexArray();
GL.BindVertexArray(vao);
vbo = GL.GenBuffer();
ebo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((points.Length) * sizeof(float)), points, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer((GL.GetAttribLocation(programID, "aPos")), 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer((GL.GetAttribLocation(programID, "aCol")), 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 3 * sizeof(float));
GL.EnableVertexAttribArray(1);
GL.VertexAttribPointer((GL.GetAttribLocation(programID, "aTextCoord")), 2, VertexAttribPointerType.Float, false, 8 * sizeof(float), 6 * sizeof(float));
GL.EnableVertexAttribArray(2);
GL.BindVertexArray(0);
textureID = GL.GenTexture();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, textureID);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
//Bitmap picture = (Bitmap)Image.FromFile("container.jpg");
Bitmap picture = new Bitmap("container.jpg");
BitmapData data = picture.LockBits(new Rectangle(0, 0, picture.Width, picture.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
picture.UnlockBits(data);
GL.DeleteShader(vShad);
GL.DeleteShader(fShad);
}
private static void Window_RenderFrame(object sender, FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GameWindow window = (GameWindow)sender;
GL.UseProgram(programID);
GL.BindVertexArray(vao);
GL.BindTexture(TextureTarget.Texture2D, textureID);
GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0);
window.SwapBuffers();
}
Vertex Shader
#version 330 core
in vec3 aPos;
in vec3 aCol;
in vec2 aTextCoord;
out vec4 vertex_To_Frag;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0);
vertex_To_Frag= vec4(aCol.x,aCol.y,aCol.z,1.0);
TexCoord = aTextCoord;
}
Fragment Shader
#version 330 core
in vec4 vertex_To_Frag;
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D ourTexture;
void main()
{
FragColor = texture(ourTexture, TexCoord);
}
I am just trying to make a simple very newbie texture on a rectangle but can’t seem to find the error…