0

当您为每个点使用三个维度创建点并使用正交投影来查看这些点时,是否会出现仅显示 -near 表面上的点的原因?例如,如果您使用 (the SharpGL method) gl.Ortho(0, width, height, 0, -10, 10),则只有 z=10 处的点(因为近表面位于 -10 处)实际上会显示出来。

我目前正在使用 SharpGL - 但我希望我遇到的问题不在于那个特定的实现/库。

编辑:我在下面添加了演示问题的代码。请注意,此示例需要 SharpGL,实际上是对当前 SharpGL 源代码附带的 WPF 示例项目的修改(原始示例项目称为 TwoDSample)。

该项目需要 MainWindow.xaml 和 MainWindow.xaml.cs。这是xml:

<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>

这是后面的代码:

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);
        }
    }
}
4

1 回答 1

1

感谢 MadcoreTom 关于深度缓冲区的评论和一些谷歌搜索,我想我找到了(或至少是“一个”)解决方案。如果我在绘图程序开始时清除深度缓冲区,(如果 z == -9 而不是 z == -10,则颜色点为红色,因为 random.Next(-10,0) 不会给出 -10 的值),然后事情似乎按预期工作。

要查看所有 z 值处的点(在正交范围内),我只是将 openGlControl1_OpenGLDraw 方法替换为以下内容:

    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++;
            }
        }
于 2012-06-06T04:37:57.627 回答