SharpGL Simple Geometry Scene

 In science projects several times we have a 3d geometry that we need to modify, read some values or just visualize it. There are many applications that do this, but sometimes we only want to be able to open a geometry, see it and modify it a “little” without the thousand options available.

In the other hand, if we want to learn OpenGL (or graphics) we need the simplest case available to understand the key concepts. This is the idea or basis reason behind the tutorials we will be making. (although more code than tutorial with some side notes).

SharpGL is an extension available from visual studio. As from this tutorial June 20, 2015 available here:

https://visualstudiogallery.msdn.microsoft.com/b61cc443-4790-42b7-b7ab-2691119667d2

and supported by visual studio express community 2013. Normally this means no errors, so install the extension and run the example. Then, just run our codes. This code is based on the code and explanation from :

Open GL Transformation from Song Ho Ahn.

http://www.songho.ca/opengl/gl_transform.html

 I strongly recommend you to check it out to understand the concepts of modelview, view and model matrix.  As a summary the model matrix will be the movement of the geometry. The view matrix will be the model of the camera in the scene, and the model view the two of them together.

First, a little basics on geometries.  Any geometry in 3d is composed by nodes and triangles.

The basic code from the template is :

OpenGL gl = openGLControl.OpenGL;

 

            //  Clear the color and depth buffer.

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

 

            //  Load the identity matrix.

            gl.LoadIdentity();

 

            //  Rotate around the Y axis.

            gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);

 

            //  Draw a coloured pyramid.

            gl.Begin(OpenGL.GL_TRIANGLES);

            gl.Color(1.0f, 0.0f, 0.0f);

            gl.Vertex(0.0f, 1.0f, 0.0f);

            gl.Color(0.0f, 1.0f, 0.0f);

            gl.Vertex(-1.0f, -1.0f, 1.0f);

            gl.Color(0.0f, 0.0f, 1.0f);

            gl.Vertex(1.0f, -1.0f, 1.0f);

            gl.Color(1.0f, 0.0f, 0.0f);

            gl.Vertex(0.0f, 1.0f, 0.0f);

            gl.Color(0.0f, 0.0f, 1.0f);

            gl.Vertex(1.0f, -1.0f, 1.0f);

            gl.Color(0.0f, 1.0f, 0.0f);

            gl.Vertex(1.0f, -1.0f, -1.0f);

            gl.Color(1.0f, 0.0f, 0.0f);

            gl.Vertex(0.0f, 1.0f, 0.0f);

            gl.Color(0.0f, 1.0f, 0.0f);

            gl.Vertex(1.0f, -1.0f, -1.0f);

            gl.Color(0.0f, 0.0f, 1.0f);

            gl.Vertex(-1.0f, -1.0f, -1.0f);

            gl.Color(1.0f, 0.0f, 0.0f);

            gl.Vertex(0.0f, 1.0f, 0.0f);

            gl.Color(0.0f, 0.0f, 1.0f);

            gl.Vertex(-1.0f, -1.0f, -1.0f);

            gl.Color(0.0f, 1.0f, 0.0f);

            gl.Vertex(-1.0f, -1.0f, 1.0f);

            gl.End();

 

            //  Nudge the rotation.

            rotation += 3.0f;

This is the code from the template directly from the extension.    “gl.Begin(OpenGL.GL_TRIANGLES);” means from here each three “gl.Vertex(1.0f, -1.0f, 1.0f);” will make a triangle.  “gl.Color(1.0f, 0.0f, 0.0f);” is the color in that vertex.  

 

Description: https://visualstudiogallery.msdn.microsoft.com/site/view/file/127861/1/WpfApp.png

 

This pyramid is made by 4 Triangles and 5 Vertex (Nodes). If we use the following notation (double[,] Nodes=new double[5,3], int[,] Triangles=new int[4,3]).

this.Nodes = new double[5, 3];) The pyramid will be:

 

            Nodes[0, 0] = 0;

            Nodes[0, 1] = 1;

            Nodes[0, 2] = 0;

 

            Nodes[1, 0] = -1;

            Nodes[1, 1] = -1;

            Nodes[1, 2] = 1;

 

            Nodes[2, 0] = 1;

            Nodes[2, 1] = -1;

            Nodes[2, 2] = 1;

 

            Nodes[3, 0] = 1;

            Nodes[3, 1] = -1;

            Nodes[3, 2] = -1;

 

            Nodes[4, 0] = -1;

            Nodes[4, 1] = -1;

            Nodes[4, 2] = -1;

 

            this.Triangles = new int[4, 3];

 

            Triangles[0, 0] = 0;

            Triangles[0, 1] = 1;

            Triangles[0, 2] = 2;

 

            Triangles[1, 0] = 0;

            Triangles[1, 1] = 2;

            Triangles[1, 2] = 3;

 

            Triangles[2, 0] = 0;

            Triangles[2, 1] = 3;

            Triangles[2, 2] = 4;

 

            Triangles[3, 0] = 0;

            Triangles[3, 1] = 4;

            Triangles[3, 2] = 1;

 

From here we call our geometry creator:

Plot3D geo = new Plot3D(Nodes, Triangles);

That basically will make this:

this.Normals = GetNodesNormals(Nodes, Triangles); 

       this.Nodes = NodesInit(Nodes);

       this.Triangles = Triangles;

So this is the basic of a geometry, Nodes and Triangles. Then from this information we will be able to construct the normals vectors in each of the points.