sharpGL在WPF中的性能很差

本文关键字:性能 WPF sharpGL | 更新日期: 2023-09-27 18:12:08

我遵循了一个SharpGL教程,可以显示一个旋转块。最初,它只有用gl.Color(r, g, b)绘制的默认颜色。在这个成功之后,我尝试用一个uv贴图纹理立方体。

当我全屏运行应用程序时,只对立方体着色(sharpGL组件覆盖整个应用程序内部),仅显示彩色立方体时,我获得70~80 fps。当我启用OpenGL.GL_TEXTURE_2D并在单个立方体上绘制纹理时,我获得了8~9 fps。

当一个位图被加载用作纹理时,它被存储在内存中。这种帧率下降只发生在我启用OpenGL.GL_TEXTURE_2D并为所有坐标调用gl.TexCoord(c1, c2)之后。实际上,使用gl.Rotate(angle, x, y, z)移动对象并不会明显影响性能。

该方法提供的数据包括GetBlockUvCubeCoordinates是静态浮点数组。

是SharpGL应该执行这个差(即在显示一个单一的立方体)还是有其他原因?我是否做错了什么影响性能的事情?应用纹理会像那样影响性能吗?

主绘制事件发生在Block中:

public void DrawBlock(object sender, OpenGLEventArgs args)
    {
        //  Get the OpenGL instance that's been passed to us.
        OpenGL gl = args.OpenGL;
        //  Reset the modelview.
        gl.LoadIdentity();
        //  Move the block to its location
        gl.Translate(Coord.X, Coord.Y, Coord.Z);
        gl.Rotate(angle, 1.0f, 1.0f, 0.5f);
        angle += 3;
        // retrieve the right texture for this block and bind it. 
        Texture blockTex = BlockTexture.GetBlockTexture(gl, _type);
        blockTex.Bind(gl);
        // retrieve the uv map for this block
        float[] uv = BlockTexture.GetBlockUv(_type);
        // retrieve the coordinates for a cube
        float[] cube = CubeCoordinates();

        gl.Enable(OpenGL.GL_TEXTURE_2D);
        //  Draw the cube with the bound texture. 
        gl.Begin(OpenGL.GL_QUADS);
        //
        //
        // Begin by allowing all colors. 
        gl.Color(1.0f, 1.0f, 1.0f);
        // since the uv index increments with 2 each time, we will be keeping track of this separately. 
        int uvInd = 0;
        // i denotes the current coordinate. Each coordinate consists of 3 
        // values (x, y, z), thus letting us skip 3. 
        //
        // Seeing as we are creating quads, it is expected that cube.Length 
        // is 3 * 4 * N (where n is a whole number)
        for (int i = 0; i < cube.Length; i += 3)
        {
            // color experiment
            //if (i < cube.Length / 3)
            //{
            //    gl.Color(1.0f, 0.00f, 0.00f);
            //}
            //else if (i < 2 * (cube.Length / 3))
            //{
            //    gl.Color(0.0f, 1.0f, 0.0f);
            //}
            //else
            //{
            //    gl.Color(0.0f, 0.0f, 1.0f);
            //}
            try
            {
                // set the coordinate for the texture
                gl.TexCoord(uv[uvInd], uv[uvInd + 1]);
                // set the vertex
                gl.Vertex(cube[i], cube[i + 1], cube[i + 2]);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new IndexOutOfRangeException(
                    "This exception is thrown because the cube map and uv map do not match size");
            }
            // increment the uv index
            uvInd += 2;
        }
        gl.End();
        gl.Disable(OpenGL.GL_TEXTURE_2D);
    }

OpenGL在别处初始化

 private void OpenGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
    {
        //  Get the OpenGL instance that's been passed to us.
        OpenGL gl = args.OpenGL;
        //  Clear the color and depth buffers.
        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

        // call the draw method of the GameRunner if the 
        // GameRunner has already been created. 
        game?.DrawOpenGL(sender, args);

        //  Flush OpenGL.
        gl.Flush();
    }
    private void OpenGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
    {
        //  Enable the OpenGL depth testing functionality.
        args.OpenGL.Enable(OpenGL.GL_DEPTH_TEST);
    }

中间GameRunner现在所做的就是调用DrawBlock例程。

我主要想知道的是对openGL/sharpGL的性能的一些见解,以及是否有更好的替代方案。我想继续使用围绕游戏的WPF架构,但如果WPF内部的openGL更像是一个噱头,那可能不是最好的行动方案。

sharpGL在WPF中的性能很差

我一直有完全相同的问题,似乎是SharpGL或WPF控件本身使用软件渲染的情况。我通过在设备管理器中禁用主显示适配器来测试这一点,并获得了与启用它时完全相同的性能。

我不知道如何启用硬件加速,所以我实际上不知道如何解决这个问题