Search code examples
c#openglsharpgl

How alter the camera in opengl so I can see my entire surface plot on screen?


I am using sharpgl in my wpf application and I have a surface plot. The problem is when I try to move the lookat camera away a lot of my plot disappears.... I'm new to opengl, how can I adjust my camera so I can see my plot from multiple sides?

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

Solution

  • Not sure about using SharpGL, but in Perspective you state that you want the far clipping plane to be at 100 and now you're setting the camera at -105, 105, -105, which is further away than 100. So you might fix the problem just by setting the far clipping plane further away?