
OpenTK. Data crashing in array
Posted Wednesday, 15 August, 2012 - 12:56 by Elun inEnglish not my native language, sorry for mistakes.
I'm using OpenTK for my game on C#.
I wrote wrapper classes for the: ShaderProgram, VAO, VBO. And all works fine. Till today.
I want to draw ellipse, and my method not the best: transfer to shader all pixels from rectangle with parametrs (TopX,TopY,width,height) and in shader determine the points belonging to the ellipse. But, i can't transfert pixels coord to shader, array wit pixel positions crashing.
public void DrawEllipse(Pen pen, float x, float y, float width, float height) { /*GL.Enable(EnableCap.AlphaTest); GL.Enable(EnableCap.Blend);*/ //GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); //It's not looks grate, but it fix memory leak List<float> pos = new List<float>(); List<float> colors = new List<float>(); Color4 color = new Color4(pen.Color.R * ColorFloat, pen.Color.G * ColorFloat, pen.Color.B * ColorFloat, pen.Color.A * ColorFloat); for (int i = 0; i < (int)width; i++) for (int j = 0; j < (int)height; j++) { pos.Add(x + i); pos.Add(y + j); colors.Add(color.R); colors.Add(color.G); colors.Add(color.B); } float[] buffer = pos.ToArray(); if (buffer.Length > 200 && buffer.Any(t => t < 10.0)) System.Windows.Forms.MessageBox.Show("Check before"); _ellipseShader.ChangeData(VBOdata.Positions, buffer); _ellipseShader.ChangeData(VBOdata.Color, colors.ToArray()); _ellipseShader.ChangeAttribute(VBOdata.Positions, "position", 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); _ellipseShader.ChangeAttribute(VBOdata.Color, "color", 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0); /*_ellipseShader.Uniform1("Amax", 4 / (width * width)); _ellipseShader.Uniform1("Amin", 4 / ((width - pen.Width - 1) * (width - pen.Width - 1))); _ellipseShader.Uniform1("Bmax", 4 / (height * height)); _ellipseShader.Uniform1("Bmin", 4 / ((height - pen.Width - 1) * (height - pen.Width - 1))); _ellipseShader.Uniform1("x0", x + width / 2); _ellipseShader.Uniform1("y0", y + height / 2);*/ _ellipseShader.DrawArrays(BeginMode.Points, 0, buffer.Length / 2); /*GL.Disable(EnableCap.Blend); GL.Disable(EnableCap.AlphaTest);*/ }
ChangeData method in ShaderProgram class:
internal bool ChangeData(VBOdata VBOtype, float[] buffer) { if (buffer.Length > 200 && buffer.Any(t => t < 10.0)) System.Windows.Forms.MessageBox.Show("Check after"); UsePorgram(); bool result = _vao.ChangeData(VBOtype, buffer); StopUseProgram(); return result; }
You think if not saw "Check before" message you can't see "Check after"? It's wrong. Screenshot in attachments.
After
if (buffer.Length > 200 && buffer.Any(t => t < 10.0)) System.Windows.Forms.MessageBox.Show("Check after");
buffer array have 1.5 from his real size(in debugger) and looks like: 1.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0.....
Link to test solution with all sources(1,2 Mb).
If someone can't download from this filehosting i can upload it whatever you want.


Comments
Re: OpenTK. Data crashing in array
Problem solved. Projection matrix should calculate with width-1 and height-1.