I have been following a tutorial but the triangle doesn't show up for me and I have no idea what is wrong with my code for the triangle not to be appearing.
I am using OpenTK version 4.7.1
Here is my code:
This is Window.cs
Here is where I write OpenGl code
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System;
using System.Collections.Generic;
using System.Text;
namespace Models
{
public class Window : GameWindow
{
private int vertexBufferHandle;
private int shaderProgramHandle;
private int vertexArrayHandle;
public Window() : base(GameWindowSettings.Default, NativeWindowSettings.Default)
{
this.CenterWindow(new Vector2i(1280, 760));
}
protected override void OnResize(ResizeEventArgs e)
{
GL.Viewport(0, 0, e.Width, e.Height);
base.OnResize(e);
}
protected override void OnLoad()
{
GL.ClearColor(new Color4(0.3f, 0.4f, 0.5f, 1f));
float[] vertices =
{
0f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
};
vertexBufferHandle = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferHandle);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
vertexArrayHandle = GL.GenVertexArray();
GL.BindVertexArray(vertexArrayHandle);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferHandle);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.BindVertexArray(0);
string vertexShader =
#"#version 330 core
layout (location = 0) in vec2 aPosition;
void main()
{
gl_Position = vec4(aPosition, 0, 1.0);
}";
string pixelShader =
#"#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.8, 0.2, 0.5, 1);
}";
int vertexShaderHandle = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertexShaderHandle, vertexShader);
GL.CompileShader(vertexShaderHandle);
int pixelShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(pixelShaderHandle, pixelShader);
GL.CompileShader(pixelShaderHandle);
shaderProgramHandle = GL.CreateProgram();
GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
GL.AttachShader(shaderProgramHandle, pixelShaderHandle);
GL.LinkProgram(shaderProgramHandle);
GL.DetachShader(shaderProgramHandle, vertexShaderHandle);
GL.DetachShader(shaderProgramHandle, pixelShaderHandle);
GL.DeleteShader(vertexShaderHandle);
GL.DeleteShader(pixelShaderHandle);
base.OnLoad();
}
protected override void OnUnload()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DeleteBuffer(vertexBufferHandle);
GL.UseProgram(0);
GL.DeleteProgram(shaderProgramHandle);
base.OnUnload();
}
protected override void OnUpdateFrame(FrameEventArgs args)
{
base.OnUpdateFrame(args);
}
protected override void OnRenderFrame(FrameEventArgs args)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.UseProgram(shaderProgramHandle);
GL.BindVertexArray(vertexArrayHandle);
GL.DrawArrays(PrimitiveType.Triangles, 0, 1);
Context.SwapBuffers();
base.OnRenderFrame(args);
}
}
}
and this is the Program.cs
Here is where I run all the code
using OpenTK;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Mathematics;
using System;
namespace Models
{
class Program
{
static void Main(string[] args)
{
using (var window = new Window())
{
window.Run();
}
}
}
}
The last argument of DrawArrays is not the number of primitives but the number of vertices:
GL.DrawArrays(PrimitiveType.Triangles, 0, 1);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
Related
I have been trying to figure out this bug in my code for quite a while, and now I'm seeking help from stackoverflow! Anyway, I have created a class for the creation of a model in openGL. It handles the VBO, VAO, and EBO for a given model, but when I create two and display them, the second always overrides the previous one/ones I have no idea why, I showed it to a friend of mine, and together we figured out nothing. This is the model class:
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using System;
namespace YISOPENGLEVIL.Components.Objects
{
struct Vertex
{
Vector3 Position;
Vector3 Color;
public Vertex(Vector3 position, Vector3 color)
{
Position = position;
Color = color;
}
}
class Model
{
public int VBO, VAO, EBO;
public Vertex[] Data;
public int[] Indices;
public Model() { }
public Model(Vertex[] data, int[] indices)
{
Data = data;
Indices = indices;
VAO = GL.GenVertexArray();
VBO = GL.GenBuffer();
EBO = GL.GenBuffer();
GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, Data.Length * 24, Data, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 24, 0);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 24, 12);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, Indices.Length * 4, Indices, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
GL.EnableVertexAttribArray(1);
}
protected static Vector3 toVec3(Color4 c)
{
return new Vector3(c.R, c.G, c.B);
}
public void Render()
{
//GL.DrawArrays(PrimitiveType.Triangles, 0, Data.Length);
GL.DrawElements(BeginMode.Triangles, Indices.Length, DrawElementsType.UnsignedInt, 0);
}
}
}
and my Element class, which gets passed a Model then is used to display it,
using OpenTK;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
namespace YISOPENGLEVIL.Components.Objects
{
class Element
{
private int transform;
private int s;
public Vector3 Position;
public Vector3 Rotation;
private Matrix4 View;
private Model Model;
public Element(Model m, int Shader)
{
s = Shader;
Model = m;
Position = new Vector3(0, 0, 0);
Rotation = new Vector3(0, 0, 0);
}
public void Render()
{
var t = Matrix4.CreateTranslation(Position.X, Position.Y, Position.Z);
var rx = Matrix4.CreateRotationX(Rotation.X);
var ry = Matrix4.CreateRotationY(Rotation.Y);
var rz = Matrix4.CreateRotationZ(Rotation.Z);
View = t * rx * ry * rz;
transform = GL.GetUniformLocation(s, "transform");
GL.UniformMatrix4(transform, false, ref View);
Model.Render();
}
public static void RenderAll()
{
}
}
}
and this is the implementation:
m1 = new Cube(1.5f, Color4.Aqua);
m2 = new Cube(1f, Color4.Red);
e1 = new Element(m1, s.Program);
e2 = new Element(m2, s.Program);
e1.Position = new Vector3(0, 0, 0);
e2.Position = new Vector3(0, 0, 1);
and:
e1.Render();
e2.Render();
The cube class extends Model, and passes data into the Model constructor by private static methods that create the vertex arrays. The really weird thing is that if I put print statements it shows that both elements are being defined fine and have the correct data, the last always overrides the previous (there are two objects in the correct positions, but both displaying the red smaller cube). I don't get it... Any Ideas?
Screenshot of the program
I am trying to texture triangle with OpenTK ES with this code
using System;
using OpenTK;
using OpenTK.Graphics.ES11;
using OpenTK.Platform.Android;
using Android.Content;
namespace TexturedTriangle
{
class GLView1 AndroidGameView
{
public GLView1(Context context) base(context)
{ }
protected override void CreateFrameBuffer()
{
base.CreateFrameBuffer();
}
Vector2[] TexCoords =
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
};
Vector2[] Vertices =
{
new Vector2(0f, 0f),
new Vector2(1f, 0f),
new Vector2(0f, 1f),
};
int[] Tex = new int[1];
int[] VBOs = new int[2];
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(0, 0, 0, 1f);
GL.Clear((int)All.ColorBufferBit);
GL.MatrixMode(All.Projection);
GL.LoadIdentity();
GL.MatrixMode(All.Modelview);
GL.LoadIdentity();
GL.Enable(All.Texture2D);
GL.GenBuffers(2, VBOs);
GL.BindBuffer(All.ArrayBuffer, VBOs[0]);
GL.BufferData(All.ArrayBuffer,
new IntPtr(Vertices.Length Vector2.SizeInBytes),
Vertices, All.StaticDraw);
GL.BindBuffer(All.ArrayBuffer, VBOs[1]);
GL.BufferData(All.ArrayBuffer,
new IntPtr(TexCoords.Length Vector2.SizeInBytes),
TexCoords, All.StaticDraw);
int[] Data = new int[32 32];
for (int I = 0; I 32 32; I++)
{
if (I % 3 == 0) Data[I] = 255;
if (I % 3 == 1) Data[I] = 65280;
if (I % 3 == 2) Data[I] = 16711680;
}
GL.GenTextures(1, Tex);
GL.BindTexture(All.Texture2D, Tex[0]);
GL.TexParameter(All.Texture2D, All.TextureMinFilter,
(int)All.Nearest);
GL.TexParameter(All.Texture2D, All.TextureMagFilter,
(int)All.Nearest);
GL.TexImage2D(All.Texture2D, 0, 3, 32, 32, 0, All.Rgba,
All.UnsignedByte, Data);
GL.EnableClientState(All.VertexArray);
GL.EnableClientState(All.TextureCoordArray);
Run();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.BindBuffer(All.ArrayBuffer, VBOs[0]);
GL.VertexPointer(2, All.Float,
BlittableValueType.StrideOf(Vertices)
IntPtr.Zero);
GL.BindBuffer(All.ArrayBuffer, VBOs[1]);
GL.TexCoordPointer(2, All.Float,
BilittableValueType.StrideOf(TexCoords),
IntPtr.Zero);
GL.DrawArrays(All.Triangles, 0, 3);
SwapBuffers();
}
}
}
But there is only a white triangle
Textured triangle OpenGL ES
Meanwhile this code:
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenTK;
namespace TexturedTriangle
{
class Program
{
static void Main()
{
GameWindow Window = new GameWindow(512, 512, GraphicsMode.Default, "Textured Triangle");
int[] Tex = new int[1];
int[] VBOs = new int[2];
Vector2[] TexCoords =
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
};
Vector2[] Vertices =
{
new Vector2(0f, 0f),
new Vector2(1f, 0f),
new Vector2(0f, 1f),
};
Window.Load += (sender, e) =>
{
GL.ClearColor(0, 0, 0, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.Enable(EnableCap.Texture2D);
GL.GenBuffers(2, VBOs);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(Vertices.Length * Vector2.SizeInBytes), Vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[1]);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(TexCoords.Length * Vector2.SizeInBytes), TexCoords, BufferUsageHint.StaticDraw);
int[] Data = new int[32 * 32];
for (int I = 0; I < 32 * 32; I++)
{
if (I % 3 == 0) Data[I] = 255;
if (I % 3 == 1) Data[I] = 65280;
if (I % 3 == 2) Data[I] = 16711680;
}
GL.GenTextures(1, Tex);
GL.BindTexture(TextureTarget.Texture2D, Tex[0]);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Three, 32, 32, 0, PixelFormat.Rgba, PixelType.UnsignedByte, Data);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
};
Window.RenderFrame += (sender, e) =>
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
GL.VertexPointer(2, VertexPointerType.Float, BlittableValueType.StrideOf(Vertices), IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[1]);
GL.TexCoordPointer(2, TexCoordPointerType.Float, BlittableValueType.StrideOf(TexCoords), IntPtr.Zero);
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
Window.SwapBuffers();
};
Window.Run();
}
}
}
Works and output is
Textured triangle OpenTK
After GL.TexImage2D();
GL.GetError(); gives InvalidValue
Documentation: www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
What the problem can be?
I find an error, InternalFormat and Format must match, else InvalidValue is given.
I have a sphere and I want to color it and add a light effect on it, so when I shed a light on it, the original color of the sphere is gone, replaced by the color of the illuminating light.
I don't know what is the problem, help me.
Here is my code:
enter code here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using SharpGL;
using SharpGL.SceneGraph;
namespace SharpGLWinformsApplication1
{
public partial class SharpGLForm : Form
{
public SharpGLForm()
{
InitializeComponent();
}
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
IntPtr quad=gl.NewQuadric();
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
gl.Translate(0.0f, 0.0f, 12.0f);
gl.Scale(0.1,0.1,0.1);
//*
gl.PushMatrix();
gl.Color(1.0f, 1.0f, 0.0f);
gl.Sphere(quad, 15, 20, 20);
gl.PopMatrix();
//------------------------------------//
gl.PushMatrix();
gl.Color(0.0f, 1.0f, 0.0f);
gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
gl.Translate(0, 1, 30);
gl.Sphere(quad, 12, 20, 20);
gl.PopMatrix();
rotation += 3.0f;
}
private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
{
// TODO: Initialise OpenGL here.
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Set the clear color.
gl.ClearColor(0, 0, 0, 0);
gl.Enable(OpenGL.GL_LIGHTING);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, ambient);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, diffuse);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPECULAR, specular);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, new float[] { 0.0f, 2f, -30f, 0 });
gl.Enable(OpenGL.GL_LIGHT1);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPOT_CUTOFF, 180.0f);
}
private void openGLControl_Resized(object sender, EventArgs e)
{
// TODO: Set the projection matrix here.
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Set the projection matrix.
gl.MatrixMode(OpenGL.GL_PROJECTION);
// Load the identity.
gl.LoadIdentity();
gl.Viewport(0, 0, (int)Width, (int)Height);
// Create a perspective transformation.
gl.Perspective(45.0f, (double)Width / (double)Height, 1, 200.0);
// Use the 'look at' helper function to position and aim the camera.
gl.LookAt(0, -3, -10, 0, 0, 0, 0, 1, 0);
// Set the modelview matrix.
gl.MatrixMode(OpenGL.GL_MODELVIEW);
}
private float rotation = 0.0f;
private GLColor ambient = new GLColor(1, 1, 1, 1f);
private GLColor diffuse = new GLColor(1, 1, 1, 1);
private GLColor specular = new GLColor(1, 1, 1, 1);
private GLColor shadowColor = new GLColor(0, 0, 0, 0.4f);
}
}
I think you need to setup materials using gl.Materials(). otherwise opengl will take the sphere with a surface that reflects no light.
I'm trying to learn how to draw with VBOs in C# OpenTK - following examples like http://www.opentk.com/node/2292 and VBOs Using Interleaved Vertices in C#.
I'm pretty sure I want the interleaved single array method like this, with a neat struct for each vertex. I got the code to compile with no errors, but it simply draws a blank brown screen, no white triangle. I'm sure I've made a stupid error, please help me learn from it!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Drawing;
using System.Runtime.InteropServices;
namespace VBOTest2
{
[StructLayout(LayoutKind.Sequential)]
public struct Vertex
{
public Vector3 Position;
public byte[] Colour;
public Vertex(byte[] colour, Vector3 position)
{
Colour = colour;
Position = position;
}
public static readonly int Stride = Marshal.SizeOf(default(Vertex));
}
public class VBOTest2 : GameWindow
{
uint vbo;
public VBOTest2() :
base(1, 1, new GraphicsMode(32, 24, 8, 0), "Test")
{
Width = 1500;
Height = 800;
VSync = VSyncMode.On;
ClientSize = new Size(1500, 800);
this.Location = new System.Drawing.Point(100, 300);
GL.Viewport(0, 0, Width, Height);
}
void CreateVertexBuffer()
{
Vertex[] vertices = new Vertex[3];
vertices[0] = new Vertex(new byte[]{255,255,255,255}, new Vector3(-1f, -1f, 0f));
vertices[1] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(1f, -1f, 0f));
vertices[2] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(0f, 1f, 0f));
GL.GenBuffers(1, out vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)Vertex.Stride, vertices, BufferUsageHint.StaticDraw);
}
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(Color.Brown);
CreateVertexBuffer();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, (IntPtr)(0));
GL.ColorPointer(4, ColorPointerType.UnsignedByte, Vertex.Stride, (IntPtr)(3 * sizeof(float)));
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
//release buffer
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
SwapBuffers();
}
}
}
I am trying to draw the triangle with the colours and verticies specified but currently it seems like its picking some colour numbers for the positions and is not doing what its supposed to do
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
namespace newTriangle
{
class Program
{
static void Main(string[] args)
{
MyWindow myWindow = new MyWindow();
myWindow.Run();
}
}
class MyWindow : GameWindow
{
private uint[] vertexBufferObjectIDs = new uint[2];
private int vertexArrayID, vertexShaderID, fragmentShaderID, shaderProgramID;
public MyWindow()
: base(800, // Width
600, // Height
GraphicsMode.Default,
"My OpenTK Window",
GameWindowFlags.Default,
DisplayDevice.Default,
3, // major
0, // minor
GraphicsContextFlags.ForwardCompatible) { }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(Color4.CornflowerBlue);
GL.GenVertexArrays(1, out vertexArrayID);
GL.BindVertexArray(vertexArrayID);
ushort[] indices = new ushort[] { 0, 1, 2 };
float[] vertices = new float[] {-1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 1.0f };
GL.GenBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObjectIDs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjectIDs[1]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, true, 5 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, 5 * sizeof(float), 2 * sizeof(float));
GL.EnableVertexAttribArray(1);
vertexShaderID = GL.CreateShader(ShaderType.VertexShader);
string vertShaderText =
#"
#version 150
in vec3 position;
in vec3 colour;
out vec3 Colour;
void main()
{
Colour = colour;
gl_Position = vec4(position, 1) ;
}";
GL.ShaderSource(vertexShaderID, vertShaderText);
GL.CompileShader(vertexShaderID);
fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader);
string fragShaderText =
#"
#version 150
in vec3 Colour;
out vec4 outputF;
void main()
{
outputF = vec4(Colour, 1.0);
}";
GL.ShaderSource(fragmentShaderID, fragShaderText);
GL.CompileShader(fragmentShaderID);
shaderProgramID = GL.CreateProgram();
GL.AttachShader(shaderProgramID, fragmentShaderID);
GL.AttachShader(shaderProgramID, vertexShaderID);
GL.LinkProgram(shaderProgramID);
GL.UseProgram(shaderProgramID);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
GL.DeleteBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
GL.DeleteVertexArrays(1, ref vertexArrayID);
GL.UseProgram(0); GL.DetachShader(shaderProgramID, vertexShaderID);
GL.DetachShader(shaderProgramID, fragmentShaderID);
GL.DeleteShader(fragmentShaderID);
GL.DeleteShader(vertexShaderID);
GL.DeleteProgram(shaderProgramID);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.DrawElements(BeginMode.Triangles, 3, DrawElementsType.UnsignedShort, IntPtr.Zero);
this.SwapBuffers();
}
}
}
can anyone see my mistake?
You didn't link up the locations of the attributes. You can fix it by using the appropriate calls to BindAttribLocation​, or use layout qualifiers with locations. Also, position is a vec3 but you give it only 2 floats.
Using layout qualifiers is an easy fix:
layout(location = 0) in vec2 position;
layout(location = 1) in vec3 colour;
That gives me this picture: http://i.imgur.com/H9FEXZ0.png which looks like it's probably what you had in mind.
vec4(position, 1)
In GLSL integers are not automatically type-promoted.
Kinda weird, because you got it right in your fragment shader.
Try this:
vec4(position, 1.0)