marcoleon's picture

hidden facets

I load a STL file. It is not displayed correctly because the faces overlap and those that should be hidden, are still visible.

        private void SetupViewport()
        {
            GL.ClearColor(Color.LightGray); //Establece color de fondo
            GL.Enable(EnableCap.DepthTest);
 
            float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
            float[] mat_shininess = { 50.0f };
            float[] light_position = { 1.0f, 0.0f, 1.0f, 0.0f };
            float[] light_ambient = { 0.5f, 0.5f, 0.5f, 1.0f };
 
            //GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.ShadeModel(ShadingModel.Smooth);
 
            //GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular);
            GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess);
            GL.Light(LightName.Light0, LightParameter.Position, light_position);
            GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient);
            GL.Light(LightName.Light0, LightParameter.Diffuse, mat_specular);
 
            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.Light0);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.ColorMaterial);
            GL.Enable(EnableCap.CullFace);
 
            float aspectRatio = (float)glControl1.Width / (float)glControl1.Height;
            GL.Viewport(0, 0, glControl1.Width, glControl1.Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            OpenTK.Matrix4 perspective = OpenTK.Matrix4.CreatePerspectiveFieldOfView
                ((float)(System.Math.PI / 4f), aspectRatio, 0.01f, 1000f);
 
            GL.MultMatrix(ref perspective);
            GL.MatrixMode(MatrixMode.Modelview);
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
 
            OpenTK.Vector3 posCamara= new OpenTK.Vector3(OpenTK.Vector3.Zero);
            if (modeloSTL != null) 
            {
                posCamara = modeloSTL._camara;
            }
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            OpenTK.Matrix4 lookat = OpenTK.Matrix4.LookAt(posCamara, OpenTK.Vector3.Zero, OpenTK.Vector3.UnitZ);  
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);
            GL.Rotate(_angulo, dirGiro); 
 
            GL.Begin(BeginMode.Triangles);  
            {
                GL.Color3(Color.Orange);
                for (int norm = 0; norm < _normales.Count; norm++) //Para cada vector normal al plano
                {
                    GL.Normal3(_normales[norm]);        //Carga vector normal del triangulo
                    for (int vert = 0; vert < 3; vert++)    //Tres vertices por cada triangulo
                        GL.Vertex3(_vertices[norm * 3 + vert]);
                }
            }
            GL.End();
 
            glControl1.SwapBuffers();
        }

Comments

Comment viewing options

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

Depending if all your faces have the same winding,

GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back); //or CullfaceMode.Front