Skip to main content
 首页 » 编程设计

c#之如何改变 opengl 中的相机以便我可以在屏幕上看到我的整个曲面图

2024年02月13日11soundcode

我在我的 wpf 应用程序中使用 sharpgl,并且我有一个曲面图。问题是当我尝试将 lookat 相机移开时,我的很多情节都消失了......我是 opengl 的新手,我该如何调整我的相机以便我可以从多个方面看到我的情节?

namespace SharpGLWPFApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
 
     ///this is a set of 29 by 121 arrays cant put all data here on stackoverflow 
        double[][] surface = new double[29][]  
            {new double[] { 0.157502594025719, 0.160975938019107, 0.168564003076642, 0.171284471913381, 0.174722441310675, 0.182467533566265, 0.190681891360462, 0.197452867532186, ..... }}; 
 
 
 
        /// <summary>Initializes a new instance of the <see cref="MainWindow"/> class.</summary> 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        /// <summary> 
        /// Handles the OpenGLDraw event of the openGLControl1 control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param> 
        private void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args) 
        { 
            //  Get the OpenGL object. 
            OpenGL gl = openGLControl.OpenGL; 
 
            //  Clear the color and depth buffer. 
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); 
 
            //  Load the identity matrix. 
            gl.LoadIdentity(); 
 
            //  Rotate around the Y axis. 
            gl.Rotate(rotation, 0.0f, 1.0f, 0.0f); 
 
            //  Nudge the rotation. 
            rotation += 3.0f; 
 
            float R = 0.3f; 
            float G = 0.2f; 
            float B = 0.1f; 
 
            gl.Translate(-10f, 0f, 0f); 
            gl.Begin(OpenGL.GL_TRIANGLES); 
            for (int x = 1; x < surface.GetUpperBound(0); ++x) 
            { 
 
                for (int y = 1; y < surface[0].GetUpperBound(0) + 1; ++y) 
                { 
                    gl.Color(R, G, B); 
                    double a = surface[x - 1][y - 1]; 
                    double b = surface[x][y - 1]; 
                    double c = surface[x][y]; 
                    double d = surface[x - 1][y]; 
                    // get four points on the surface (they form a quad) 
 
                    gl.Vertex(x - 1, a, y - 1); 
                    gl.Vertex(x, b, y - 1); 
                    gl.Vertex(x, c, y); 
                    // draw triangle abc 
 
                    gl.Vertex(x - 1, a, y - 1); 
                    gl.Vertex(x, c, y); 
                    gl.Vertex(x - 1, d, y); 
                    // draw triangle acd 
                    R += y; 
                    B += .0003f; 
                    G += .0001f; 
                } 
                //R += .03f; 
                //B += .03f; 
                //G += .03f; 
            } 
 
            gl.End(); 
 
 
 
 
        } 
 
        /// <summary> 
        /// Handles the OpenGLInitialized event of the openGLControl1 control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param> 
        private void openGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args) 
        { 
            //  TODO: Initialise OpenGL here. 
 
            //  Get the OpenGL object. 
            OpenGL gl = openGLControl.OpenGL; 
 
            //  Set the clear color. 
            gl.ClearColor(0, 0, 0, 0); 
        } 
 
        /// <summary> 
        /// Handles the Resized event of the openGLControl1 control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param> 
        private void openGLControl_Resized(object sender, OpenGLEventArgs args) 
        { 
            //  TODO: Set the projection matrix here. 
 
            //  Get the OpenGL object. 
            OpenGL gl = openGLControl.OpenGL; 
 
            //  Set the projection matrix. 
            gl.MatrixMode(OpenGL.GL_PROJECTION); 
 
            //  Load the identity. 
            gl.LoadIdentity(); 
 
            //  Create a perspective transformation. 
            gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0); 
 
            //  Use the 'look at' helper function to position and aim the camera. 
            gl.LookAt(-105, 105, -105, 50, 0, 0, 0, 1, 0); 
 
            //  Set the modelview matrix. 
            gl.MatrixMode(OpenGL.GL_MODELVIEW); 
        } 
 
        /// <summary> 
        /// The current rotation. 
        /// </summary> 
        private float rotation = 0.0f; 
    } 
} 

请您参考如下方法:

不确定是否使用 SharpGL,但在 Perspective 中您声明您希望远剪裁平面位于 100,现在您将相机设置为 -105、105、-105,这比 100 更远。所以您可以通过将远裁剪平面设置得更远来解决问题吗?