darkprinze's picture

Triangles not displaying properly and GLControl camera rotation

I have the stl file as input and i have the very good stl loader which can provide the normals and vertices. Using those input I am successfully able to draw into GLControl, and its displayed correctly if i set Primitive type into Points, but when i set to triangles it does not show as triangles and instead it displays like attached image.

This is my code.

 private void GLControl_Load(object sender, EventArgs e)
        { 
            loaded = true;
            int w = 876;
            int h = 590;
            //int w = glSurface.Width;
            //int h = glSurface.Height;
            GL.Viewport(0, 0, w, h); // Use all of the glControl painting area (used a square to mantain aspect ratio            
            GL.Enable(EnableCap.DepthTest);
            GL.LoadIdentity();
            GL.ClearColor(System.Drawing.Color.MidnightBlue);
            Console.WriteLine(w + " : " + h);
            GL.End();
        }
 
 private void GLControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (!loaded)
                return;
 
            LoadFootModel(txtFilePath.Text);
        }       
private void LoadFootModel(string fileName)
        {
            STLDocument stl = null;
 
            if (string.IsNullOrEmpty(fileName))
                return;
 
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                {
                    stl = STLDocument.Read(reader);
                }
            }
 
            //LoadVertices();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
 
            UpdateCamera();          
 
            GL.Color3(System.Drawing.Color.DarkGray);
            GL.Begin(PrimitiveType.Triangles);
            int j = 0;
            for (int i = 0; i < stl.Facets.Count; i++)
            {
                GL.Normal3(new Vector3d(stl.Facets[i].Normal.X, stl.Facets[i].Normal.Y, stl.Facets[i].Normal.Z));
                GL.Vertex3(new Vector3d(stl.Facets[i].Vertices[0].X, stl.Facets[i].Vertices[0].Y, stl.Facets[i].Vertices[0].Z));
                GL.Vertex3(new Vector3d(stl.Facets[i].Vertices[1].X, stl.Facets[i].Vertices[1].Y, stl.Facets[i].Vertices[1].Z));
                GL.Vertex3(new Vector3d(stl.Facets[i].Vertices[2].X, stl.Facets[i].Vertices[2].Y, stl.Facets[i].Vertices[2].Z));
                lblCount.Content = j++;
            }
 
            GL.End();
 
            glSurface.SwapBuffers();
            lblMessage.Content = "Completed";
        }
 
void UpdateCamera()
        {
            // Calculate the camera's current position.
            Matrix4 rotationMatrix;          
 
            rotationMatrix = Matrix4.CreateRotationY(avatarYaw); 
 
            // Create a vector pointing the direction the camera is facing.
            Vector3 transformedReference = Vector3.Transform(cameraReference, rotationMatrix);
 
            // Calculate the position the camera is looking at.
            Vector3 cameraLookat = cameraPosition + transformedReference;
 
            // Set up the view matrix and projection matrix.
            var modelview = Matrix4.LookAt(cameraPosition, cameraLookat, new Vector3(0.0f, 1.0f, 0.0f));
            var aspect_ratio = Width / (float)Height;
            var projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, float.Parse(aspect_ratio.ToString()),
                                                          1.0f, 1000.0f);
 
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);
        }

How to set the camera into center screen for different kinds of models because few models cutoff from display. Also, i need to implement the rotation like google earth using mouse. It would be great if someone point me to some tutorial for camera rotation.

My Stl file looks like below.
solid vcg
facet normal -8.784463e-001 -4.520799e-001 1.547763e-001
outer loop
vertex 2.111924e+001 4.900141e+001 9.661947e+001
vertex 2.075470e+001 4.923518e+001 9.523328e+001
vertex 2.214947e+001 4.699955e+001 9.661947e+001
endloop
endfacet
facet normal -8.784463e-001 -4.520800e-001 1.547762e-001
outer loop
vertex 2.214947e+001 4.699955e+001 9.661947e+001
vertex 2.075470e+001 4.923518e+001 9.523328e+001
vertex 2.234610e+001 4.635092e+001 9.584092e+001
endloop
endfacet
facet normal -9.767911e-001 -1.511227e-001 1.517929e-001
outer loop
vertex 2.055255e+001 5.193407e+001 9.661947e+001
vertex 2.041665e+001 5.200893e+001 9.581951e+001
vertex 2.080811e+001 5.028222e+001 9.661947e+001
endloop
endfacet
.....................

Inline Images
Foot

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
darkprinze's picture

I found the solution. What i need is display the model as Wireframe. I need to set the polygon mode to change the model view.

GL.PolygonMode(OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, OpenTK.Graphics.OpenGL.PolygonMode.Line);

I am still working on to implement the camera rotation and zoomin and zoomout functionality.I will post the code once i finish the functionality.