OpenGL / SharpGL - 仅指向正射投影的近表面?

时间:2012-06-06 00:26:38

标签: c# opengl orthographic

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

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

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

该项目需要MainWindow.xaml和MainWindow.xaml.cs。这是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>

这是背后的代码:

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

1 个答案:

答案 0 :(得分:1)

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

要查看所有z值的点(在Ortho限制内),我只是将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++;
            }
        }