Click here to Skip to main content
15,499,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am producing a Hello Triangle using the code below. SharpGL seems to always plot a vertex at the origin (0,0). This is obvious when the PolygonMode is changed to GL_POINT. When using DrawElements the primitive element shape gets distorted, as it includes the origin vertex. This does not happen with DrawArray, but the origin vertex still appears. Need to get rid of it.

C#
OpenGL gl = openGLControl.OpenGL;

gl.LoadIdentity();
gl.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_LINE);

gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.Color(0f, 1.0f, 0.0f);

float[] VertexData =  {
                          0.5f, 0.5f, 0.0f,
                          0.5f, -0.5f, 0.0f,
                          -0.5f, -0.5f, 0.0f
                      };
uint[] ElementData = {
                        0, 1, 2
                      };

uint[] VertexBuffer = new uint[1];
gl.GenBuffers(1, VertexBuffer);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer[0]);
IntPtr ptr1 = GCHandle.Alloc(VertexData, GCHandleType.Pinned).AddrOfPinnedObject();
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, VertexData.Length * sizeof(float), ptr1, OpenGL.GL_STATIC_DRAW);

uint[] ElementBuffer = new uint[1];
gl.GenBuffers(1, ElementBuffer);
gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ElementBuffer[0]);
IntPtr ptr2 = GCHandle.Alloc(ElementData, GCHandleType.Pinned).AddrOfPinnedObject();
gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ElementData.Length * sizeof(int), ptr2, OpenGL.GL_STATIC_DRAW);

uint[] VertexArrayID = new uint[1];
gl.GenVertexArrays(1, VertexArrayID);
gl.BindVertexArray(VertexArrayID[0]);

gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, true, 0, IntPtr.Zero);

gl.DrawElements(OpenGL.GL_TRIANGLES, VertexData.Length, ElementData);

gl.DisableVertexAttribArray(0);


What I have tried:

Confirmed that the VertexData array is compiled correctly.
Posted
Updated 6-May-20 6:32am
v4

1 solution

Solved. Seems that VertexData.Length should be divided by 3 (as per the number of coords).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900