Question

When you create points using three dimensions for each point and you use an Ortho projection to view the points, would there be a reason that only the points on the -near surface would appear? For example, if you use (the SharpGL method) gl.Ortho(0, width, height, 0, -10, 10), only the points at z=10 (because the near surface is at -10) actually show up.

I'm currently using SharpGL - but I'm hoping the issue I'm having isn't with that particular implementation/library.

EDIT: I'm adding the code below that demonstrates the issue. Note that this example requires SharpGL and is in fact a modification of a WPF sample project that comes with the current SharpGL source code (the original sample project is called TwoDSample).

The project requires a MainWindow.xaml and a MainWindow.xaml.cs. Here's the xaml:

<Window x:Class="TwoDSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:my="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF">
    <Grid>
        <my:OpenGLControl Name="openGLControl1" OpenGLDraw="openGLControl1_OpenGLDraw" OpenGLInitialized="openGLControl1_OpenGLInitialized"
                          Resized="openGLControl1_Resized"/>
    </Grid>
</Window>

Here is the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharpGL.Enumerations;

namespace TwoDSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // NOTE: I use this to restrict the openGLControl1_OpenGLDraw method to 
        // drawing only once after m_drawCount is set to zero;
        int m_drawCount = 0;
        private void openGLControl1_OpenGLDraw(object sender,     SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            // NOTE: Only draw once after m_drawCount is set to zero
            if (m_drawCount < 1)
            {
                //  Get the OpenGL instance.
                var gl = args.OpenGL;

                gl.Color(1f, 0f, 0f);
                gl.PointSize(2.0f);

                //  Draw 10000 random points.
                gl.Begin(BeginMode.Points);
                Random random = new Random();
                for (int i = 0; i < 10000; i++)
                {
                    double x = 10 + 400 * random.NextDouble();
                    double y = 10 + 400 * random.NextDouble();
                    double z = (double)random.Next(-10, 0);

                    // Color the point according to z value
                    gl.Color(0f, 0f, 1f);  // default to blue
                    if (z == -10)
                        gl.Color(1f, 0f, 0f);   // Red for z = -10
                    else if (z == -1)
                        gl.Color(0f, 1f, 0f);   // Green for z = -1

                    gl.Vertex(x, y, z);
                }

                    gl.End();
                    m_drawCount++;
                }
            }

            private void openGLControl1_OpenGLInitialized(object sender,     SharpGL.SceneGraph.OpenGLEventArgs args)
        {

        }

        private void openGLControl1_Resized(object sender,     SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            // NOTE: force the draw routine to happen again when resize occurs
            m_drawCount = 0;
            //  Get the OpenGL instance.
            var gl = args.OpenGL;

            //  Create an orthographic projection.
            gl.MatrixMode(MatrixMode.Projection);
            gl.LoadIdentity();

            // NOTE: Basically no matter what I do, the only points I see are those at
            // the "near" surface (with z = -zNear)--in this case, I only see green points
            gl.Ortho(0, openGLControl1.ActualWidth, openGLControl1.ActualHeight, 0, 1, 10);

            //  Back to the modelview.
            gl.MatrixMode(MatrixMode.Modelview);
        }
    }
}
Was it helpful?

Solution

Thanks to MadcoreTom's comment about the depth buffer and a bit of Googling, I think I found the (or at least "a") solution. If I clear the depth buffer at the start of the drawing routine, (and color points red if z == -9 rather than z == -10 since random.Next(-10,0) will not give a value of -10), THEN things seem to work as expected.

To see points at all z values (within the Ortho limits) I just replaced the openGlControl1_OpenGLDraw method to the following:

    private void openGLControl1_OpenGLDraw(object sender,     SharpGL.SceneGraph.OpenGLEventArgs args)
    {
        // NOTE: Only draw once after m_drawCount is set to zero
        if (m_drawCount < 1)
        {
            //  Get the OpenGL instance.
            var gl = args.OpenGL;

            // ADDED THIS LINE
            gl.Clear(SharpGL.OpenGL.GL_DEPTH_BUFFER_BIT);

            gl.Color(1f, 0f, 0f);
            gl.PointSize(2.0f);

            //  Draw 10000 random points.
            gl.Begin(BeginMode.Points);
            Random random = new Random();
            for (int i = 0; i < 10000; i++)
            {
                double x = 10 + 400 * random.NextDouble();
                double y = 10 + 400 * random.NextDouble();
                double z = (double)random.Next(-10, 0);

                // Color the point according to z value
                gl.Color(0f, 0f, 1f);  // default to blue
                if (z == -9)
                    gl.Color(1f, 0f, 0f);   // Red for z = -10
                else if (z == -1)
                    gl.Color(0f, 1f, 0f);   // Green for z = -1

                gl.Vertex(x, y, z);
            }

                gl.End();
                m_drawCount++;
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top