darkprinze's picture

Need help to display points correctly in GLControl from Point Cloud

I am new to opengl and i started to learn the basics. Also, i looked at samples and and i trying to create mesh image from point clouds. I am using WPF and GLControl to displaying the points. In the point cloud (.asc file), i have almost 187512 points. The expected output should be like this (This output from meshlab software using same point cloud). My output is not showing points properly and attached images for reference. The point cloud is generated from Scanned human foot. My Code is below:

 private void GLControl_Load(object sender, EventArgs e)
        {
            loaded = true;
            int w = 818;
            int h = 561;
            //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.MatrixMode(MatrixMode.Projection);
            GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)           
            GL.LoadIdentity();
            GL.ClearColor(1.0f, 1.0f, 1.0f, 0);
            Console.WriteLine(w + " : " + h);
            GL.End();
        }
 
 private void GLControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (!loaded)
                return;
            LoadVertices();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
            GL.MatrixMode(MatrixMode.Modelview);            
            GL.Color3(1.0f, 0.0f, 0.0f);
            GL.Begin(PrimitiveType.Points);
            int j = 0;
            foreach (Vector3 v in vertices)
            {
                GL.Vertex3(v.X, v.Y, v.Z);
                lblCount.Content = j++;
            }
            GL.End();            
            glSurface.SwapBuffers();
            lblMessage.Content = "Completed";
        }
 
private void LoadVertices()
        {
            char[] splitCharacters = new char[] { ' ' };
            TextReader file = File.OpenText(txtFilePath.Text);
            string line;
 
            vertices = new List<Vector3>();
 
            while ((line = file.ReadLine()) != null)
            {
                line = line.Trim(splitCharacters);
                line = line.Replace("  ", " ");
 
                string[] parameters = line.Split(splitCharacters);
 
                float x = float.Parse(parameters[0]);
                float y = float.Parse(parameters[1]);
                float z = float.Parse(parameters[2]);
 
                vertices.Add(new Vector3(x, y, z));
            }
            lblMessage.Content = " Success: " + vertices.Count + "";
        }

My point cloud looks like below:

0.000000 -0.598992 1.650889
0.000000 -0.590218 1.654381
0.000000 -0.581449 1.657872
0.000000 -0.572684 1.661362
0.000000 -0.563925 1.664851
0.000000 -0.559466 1.681758
0.000000 -0.555013 1.698683
0.000000 -0.541970 1.688730
0.000000 -0.533229 1.692215
0.000000 -0.528789 1.709137
0.000000 -0.524354 1.726076
0.000000 -0.519923 1.743032
0.000000 -0.506905 1.733037
0.000000 -0.502483 1.749992
0.000000 -0.485178 1.726539
0.000000 -0.480766 1.743472
0.000000 -0.476358 1.760423
0.000000 -0.471955 1.777392
....
....

I am trying to figure out problem but i cant understand the exact problem. It would be great if someone help me to figure out what i need to to.

Inline Images
Processed Pointcloud in meshlab

Comments

Comment viewing options

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

GL.LoadIdentity() always affects the current matrix mode. In this case, you are doing (pseudo-code):

// Load event
GL.MatrixMode(MatrixMode.Projection);
GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)           
GL.LoadIdentity(); // this resets the projection matrix you just set!
 
// Paint event
GL.LoadIdentity();
GL.MatrixMode(MatrixMode.Modelview);

Do note that (a) the original image is using a perspective projection, not an orthographic projection and (b) the point coordinates you listed have a negative y-coordinate, so they fall outside the orthographic projection you are specifying.

I'd suggest moving both the projection and modelview setup to the Paint event, and trying something like:

// Perspective projection with 45 deg field of view
var aspect_ratio = Width / (float)Height;
var projection = Matrix4.CreatePerspectiveFieldOfView(
    MathHelper.PiOver4, aspect_ratio, 1, 1024);
var modelview = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
 
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);

Try modifying the parameters in Matrix4.LookAt() to move the camera in the scene (change the viewer position using the mouse or arrow keys.)

darkprinze's picture

Thank you very much for the quick reply. Now i got the partial output but it didn't show the all points. It seems like remaining points are cut off from the screen.

I am modifying the Matrix.Lookat function to get the full view of image.

I am playing around with the code to get the better understanding. Feels like i have lot to learn about graphics and math.

I have attached the partial output image for reference.