opengl 无法绘制多维数据集

egdjgwm8  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(170)

我使用的是SharpGL
我正在尝试修改here的源代码。他们正在绘制一个金字塔和一个立方体。我正在尝试只绘制一个立方体,如下所示-

我得到以下输出-

请记住,这个输出只有在我使用gl.Rotate()时才是可见的。这意味着,立方体需要放在远离查看器的地方。
然而,我无法做到这一点。

*如何将立方体放置在适当的位置,并绕Y轴旋转,使其始终可见?
我的源代码:

public static class OpenGLhelper
{
    public static void Init(OpenGL gl)
    {
        gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
        gl.ClearDepth(1.0f);                   // Set background depth to farthest
        gl.Enable(OpenGL.GL_DEPTH_TEST);   // Enable depth testing for z-culling
        gl.DepthFunc(OpenGL.GL_LEQUAL);    // Set the type of depth-test
        gl.ShadeModel(OpenGL.GL_SMOOTH);   // Enable smooth shading
        gl.Hint(OpenGL.GL_PERSPECTIVE_CORRECTION_HINT, OpenGL.GL_NICEST);  // Nice perspective corrections         
    }

    public static void Display(OpenGL gl)
    {
        gl.Translate(-1.0f, 0.0f, -1.0f);  // Move into the screen

        gl.Begin(OpenGL.GL_QUADS);                // Begin drawing the color cube with 6 quads
                                          // Top face (y = 1.0f)
                                          // Define vertices in counter-clockwise (CCW) order with normal pointing out
        gl.Color(0.0f, 1.0f, 0.0f);     // Green
        gl.Vertex(1.0f, 1.0f, -1.0f);
        gl.Vertex(-1.0f, 1.0f, -1.0f);
        gl.Vertex(-1.0f, 1.0f, 1.0f);
        gl.Vertex(1.0f, 1.0f, 1.0f);

        // Bottom face (y = -1.0f)
        gl.Color(1.0f, 0.5f, 0.0f);     // Orange
        gl.Vertex(1.0f, -1.0f, 1.0f);
        gl.Vertex(-1.0f, -1.0f, 1.0f);
        gl.Vertex(-1.0f, -1.0f, -1.0f);
        gl.Vertex(1.0f, -1.0f, -1.0f);

        // Front face  (z = 1.0f)
        gl.Color(1.0f, 0.0f, 0.0f);     // Red
        gl.Vertex(1.0f, 1.0f, 1.0f);
        gl.Vertex(-1.0f, 1.0f, 1.0f);
        gl.Vertex(-1.0f, -1.0f, 1.0f);
        gl.Vertex(1.0f, -1.0f, 1.0f);

        // Back face (z = -1.0f)
        gl.Color(1.0f, 1.0f, 0.0f);     // Yellow
        gl.Vertex(1.0f, -1.0f, -1.0f);
        gl.Vertex(-1.0f, -1.0f, -1.0f);
        gl.Vertex(-1.0f, 1.0f, -1.0f);
        gl.Vertex(1.0f, 1.0f, -1.0f);

        // Left face (x = -1.0f)
        gl.Color(0.0f, 0.0f, 1.0f);     // Blue
        gl.Vertex(-1.0f, 1.0f, 1.0f);
        gl.Vertex(-1.0f, 1.0f, -1.0f);
        gl.Vertex(-1.0f, -1.0f, -1.0f);
        gl.Vertex(-1.0f, -1.0f, 1.0f);

        // Right face (x = 1.0f)
        gl.Color(1.0f, 0.0f, 1.0f);     // Magenta
        gl.Vertex(1.0f, 1.0f, -1.0f);
        gl.Vertex(1.0f, 1.0f, 1.0f);
        gl.Vertex(1.0f, -1.0f, 1.0f);
        gl.Vertex(1.0f, -1.0f, -1.0f);
        gl.End();  // End of drawing color-cube

        gl.Flush();
    }

    /* Handler for window re-size event. Called back when the window first appears and
       whenever the window is re-sized with its new width and height */
    public static void Reshape(OpenGL gl, int width, int height)
    {  // GLsizei for non-negative integer
       // Compute aspect ratio of the new window
        if (height == 0) height = 1;                // To prevent divide by 0
        float aspect = (float)width / (float)height;

        // Set the viewport to cover the new window
        gl.Viewport(0, 0, width, height);

        // Set the aspect ratio of the clipping volume to match the viewport
        gl.MatrixMode(OpenGL.GL_PROJECTION);  // To operate on the Projection matrix
        gl.LoadIdentity();             // Reset
                                      // Enable perspective projection with fovy, aspect, zNear and zFar
        gl.Perspective(4.0f, aspect, 0.1f, 10.0f);
    }
}

WinForms程式码:

public partial class SharpGLForm : Form
{
    private float rotation = 0.0f;

    public SharpGLForm()
    {
        InitializeComponent();
    }

    private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
    {
        OpenGL gl = openGLControl.OpenGL;           

        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
        gl.LoadIdentity();
        gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);

        OpenGLhelper.Display(gl);

        rotation += 3.0f;
    }

    private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl.OpenGL;

        OpenGLhelper.Init(gl);
    }

    private void openGLControl_Resized(object sender, EventArgs e)
    {
        OpenGL gl = openGLControl.OpenGL;

        OpenGLhelper.Reshape(gl, Width, Height);
    }
}
jobtbby3

jobtbby31#

矩阵乘法不是Commutative。顺序矩阵. gl.Rotate()gl.Translate()不设置旋转和平移。此函数定义了一个矩阵,并将当前矩阵与新矩阵相乘。如果要绕局部轴旋转模型,则需要在gl.Rotate()之前调用gl.Translate()
第一个

相关问题