繁体   English   中英

为什么我不能在C#中使用OpenGL设置Texture Wrap参数? (SharpGL dll)

[英]Why can't I set Texture Wrap parameters using OpenGL in C#? (SharpGL dll)

我一直在构建一种使用SharpGL在WPF应用程序中为数字地形模型的顶点着色(我是澳大利亚人)的方法。 我面临的问题是,我似乎无法使用附加到活动OpenGL实例的TexParameter方法将GL_TEXTURE_WRAP_T和S参数更改为GL_REPEAT以外的任何参数。

如果无法将纹理环绕设置为GL_CLAMP_TO_EDGE,则访问最低颜色时会出现最高颜色的出血,除非我将纹理坐标偏移半个像素(这很笨拙,但我知道它将起作用)。

该窗口当前呈现如下:

当前错误的结果

当它应该有彩虹在中间的三分之一与红色的固体在顶部的三分之一和蓝色的底部三分之一。

我知道我正确使用了TexParameter方法,因为我可以为相同的纹理成功地操纵GL_TEXTURE_MIN_FILTER和GL_TEXTURE_MAG_FILTER参数。 我唯一想到的是,需要在OpenGL实例中启用某些功能才能更改纹理包裹参数。

我已经附上了整个C#测试代码和WPF xaml代码,以便能够重新创建问题。 第52和53行是调用该方法的地方。 我能得到的任何帮助将不胜感激。

using SharpGL;
using System.Windows;
using System.Drawing;
using SharpGL.SceneGraph.Assets;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        Bitmap bitmap;
        Texture texture;

        public MainWindow()
        {
            InitializeComponent();

            //Create a 5-pixel wide bitmap to be used as the texture
            bitmap = new Bitmap(5, 1);
            bitmap.SetPixel(0, 0, Color.Red);
            bitmap.SetPixel(1, 0, Color.Yellow);
            bitmap.SetPixel(2, 0, Color.Green);
            bitmap.SetPixel(3, 0, Color.Cyan);
            bitmap.SetPixel(4, 0, Color.Blue);
        }

        private void GLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            OpenGL gl = args.OpenGL;

            //  Clear the color and depth buffers.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            //Set an ortho projection to be able to see the entire square
            gl.MatrixMode(OpenGL.GL_PROJECTION);
            gl.LoadIdentity();
            gl.Ortho(-2, 2, -2, 2, -2, 2);

            gl.MatrixMode(OpenGL.GL_MODELVIEW);

            //  Reset the modelview.
            gl.LoadIdentity();

            texture.Bind(gl);

            gl.Enable(OpenGL.GL_TEXTURE_2D);

            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
            gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
            //gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST);
            //gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST);

            //Draw the square
            gl.Begin(OpenGL.GL_QUADS);

            gl.Color(1.0f, 1.0f, 1.0f, 1.0f);

            //Texture coordinate outside of 0-1 range to test wrapping method
            gl.TexCoord(-1, 0.5);
            gl.Vertex(1.0f, 1.0f, 1.0f);
            gl.Vertex(-1.0f, 1.0f, 1.0f);
            //Texture coordinate outside of 0-1 range to test wrapping method
            gl.TexCoord(2, 0.5);
            gl.Vertex(-1.0f, -1.0f, 1.0f);
            gl.Vertex(1.0f, -1.0f, 1.0f);

            gl.End();

            gl.Disable(OpenGL.GL_TEXTURE_2D);

            //  Flush OpenGL.
            gl.Flush();
        }

        private void GLInitialize(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            OpenGL gl = args.OpenGL;
            gl.Enable(OpenGL.GL_DEPTH_TEST);
            gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);

            texture = new Texture();
            texture.Create(gl, bitmap);
        }
    }
}

这是xaml代码:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <sharpGL:OpenGLControl OpenGLVersion="OpenGL3_2" OpenGLDraw="GLDraw" OpenGLInitialized="GLInitialize" Cursor="Cross"
                               Background="Transparent"/>
    </Grid>
</Window>

我找到了答案。 我使用的OpenGL版本不支持“ GL_CLAMP_TO_EDGE”进行纹理包装,将其更改为“ GL_CLAMP”(较旧的等效版本)后,代码可以正常工作。

编辑:我找到了一个答案,但答案不完整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM