Change width, color, line type in SharpGL

172 views Asked by At

Need help because I'm new to OpenGL. So, my task is to create a control that will draw in real time the process of cutting a workpiece on a CNC machine.

I tried to do it classically through glBegin, glEnd and everything works fine, but due to the large number of vertices, it starts to work slowly at the end. Therefore, I decided to try using VertexBufferArray () and VertexBuffer () - this also works, but in this situation I do not understand how to change the line width and its type (specifically, I have two types - a regular line and a dash-dotted line).

Here is my method where i use array

                private void CreateBufferAndDraw(OpenGL GL)
    {
        try
        {
            if (shaderProgram == null && vertexBufferArray == null)
            {
                var vertexShaderSource = ManifestResourceLoader.LoadTextFile("vertex_shader.glsl");
                var fragmentShaderSource = ManifestResourceLoader.LoadTextFile("fragment_shader.glsl");
                shaderProgram = new ShaderProgram();
                shaderProgram.Create(gL, vertexShaderSource, fragmentShaderSource, null);
                attribute_vpos = (uint)gL.GetAttribLocation(shaderProgram.ShaderProgramObject, "vPosition");
                attribute_vcol = (uint)gL.GetAttribLocation(shaderProgram.ShaderProgramObject, "vColor");
                shaderProgram.AssertValid(gL);
            }

            if (_data == null)
            {
                _data = new vec3[_Points.Count];
            }
            else
            {
                if (_data.Length != _Points.Count)
                {
                    _data = (vec3[])ResizeArray(_data, _Points.Count);
                }
            }

            if (_dataColor == null)
            {
                _dataColor = new vec3[_Points.Count];
            }
            else
            {
                if (_dataColor.Length != _Points.Count)
                {
                    _dataColor = (vec3[])ResizeArray(_dataColor, _Points.Count);
                }
            }

            for (int i = _dataTail; i < _Points.Count; i++)
            {
                _data[i].y = _Points[i].Y;
                _data[i].x = _Points[i].X;
                _data[i].z = _Points[i].Z;

                _dataColor[i] = new vec3(_Points[i].ToolColor.R / 255.0f, _Points[i].ToolColor.G / 255.0f, _Points[i].ToolColor.B / 255.0f);
            }

            _dataTail = _Points.Count;

            //  Create the vertex array object.
            vertexBufferArray = new VertexBufferArray();
            vertexBufferArray.Create(GL);
            vertexBufferArray.Bind(GL);

            //  Create a vertex buffer for the vertex data.
            var vertexDataBuffer = new VertexBuffer();
            vertexDataBuffer.Create(GL);
            vertexDataBuffer.Bind(GL);
            vertexDataBuffer.SetData(GL, attribute_vpos, _data, false, 3);

            //  Now do the same for the colour data.
            var colourDataBuffer = new VertexBuffer();
            colourDataBuffer.Create(GL);
            colourDataBuffer.Bind(GL);
            colourDataBuffer.SetData(GL, attribute_vcol, _dataColor, false, 3);

            //  Unbind the vertex array, we've finished specifying data for it.
            vertexBufferArray.Unbind(GL);

            //  Bind the shader, set the matrices.
            shaderProgram.Bind(GL);

            // Set matrixs for shader program
            float rads = (90.0f / 360.0f) * (float)Math.PI * 2.0f;
            mat4 _mviewdata = glm.translate(new mat4(1f), new vec3(_track_X, _track_Y, _track_Z));
            mat4 _projectionMatrix = glm.perspective(rads, (float)this.trackgl_control.Height / (float)this.trackgl_control.Width, 0.001f, 1000f);
            mat4 _modelMatrix = glm.lookAt(new vec3(0, 0, -1), new vec3(0, 0, 0), new vec3(0, -1, -1));

            shaderProgram.SetUniformMatrix4(GL, "projectionMatrix", _projectionMatrix.to_array());
            shaderProgram.SetUniformMatrix4(GL, "viewMatrix", _modelMatrix.to_array());
            shaderProgram.SetUniformMatrix4(GL, "modelMatrix", _mviewdata.to_array());

            //  Bind the out vertex array.
            vertexBufferArray.Bind(GL);

            GL.LineWidth(5.0f);
            //  Draw the square.
            GL.DrawArrays(OpenGL.GL_LINE_STRIP, 0, _dataTail);

            //  Unbind our vertex array and shader.
            vertexBufferArray.Unbind(GL);
            shaderProgram.Unbind(GL);
        }
        catch (Exception ex) { MessageBox.Show("CreateAndPlotData" + "\n" + ex.ToString()); }
    }

This is what i get
enter image description here

As you see, all lines have the same width. So, my question is: Does anyone know what i can do about this? And another one: what if i need to show a point of the current location of the instrument? I should create another array with one Vertex ?

p.s. Sry for my english, and here here is picture of what i get with glBegin/glEnd
enter image description here


No tag spamming intended.
I'm doing this project partially guided by tutorials in C ++ and C #. Therefore, if someone knows how to do this in C ++, then in this case I will just try to implement the same in C#.

0

There are 0 answers