C# OpenGL 纹理不显示

发布于 2024-11-04 15:17:58 字数 1653 浏览 3 评论 0原文

在这个例子中,我已尽力简化问题,希望有人能为我解决它。

我找不到用于显示纹理的 C# OpenGL 代码的简单工作示例。 所以我尝试在这里做一个。 不幸的是,这段代码对我不起作用。 我没有收到任何错误,但也没有纹理。

也许我设置不正确。 也许有人可以粘贴此代码并查看它是否运行。

using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;

namespace TextureTest
{
public partial class Form1 : Form
{
    int imageID = 1;
    int texture = 1;

    public Form1()
    {
        InitializeComponent();
        OpenGlControl.InitializeContexts();

        Il.ilInit();
        Il.ilBindImage(imageID);

        bool success = Il.ilLoadImage(@"test.bmp");

        Il.ilConvertImage(Il.IL_RGBA, Il.IL_UNSIGNED_BYTE);

        Gl.glEnable(Gl.GL_TEXTURE_2D);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Il.ilGetInteger(Il.IL_IMAGE_BPP), 
                        Il.ilGetInteger(Il.IL_IMAGE_WIDTH), Il.ilGetInteger(Il.IL_IMAGE_HEIGHT), 0, 
                        Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Gl.GL_UNSIGNED_BYTE, Il.ilGetData());

        Il.ilDeleteImage(imageID);

        Gl.glBegin(Gl.GL_QUADS);

            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);

        Gl.glEnd();

        Gl.glFlush();
        Gl.glDeleteTextures(1, ref texture); 
     }
}
}

In this example I've tried as much as I can to simplify the problem in the hope that someone can solve it for me.

I could not find a simple working example of C# OpenGL code to display a texture.
So I've tried to make one here.
Unfortunatley, this code does not work for me.
I get no errors, but no texture either.

Maybe I set it up incorrectly.
Perhaps someone could paste this code and see if it runs.

using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;

namespace TextureTest
{
public partial class Form1 : Form
{
    int imageID = 1;
    int texture = 1;

    public Form1()
    {
        InitializeComponent();
        OpenGlControl.InitializeContexts();

        Il.ilInit();
        Il.ilBindImage(imageID);

        bool success = Il.ilLoadImage(@"test.bmp");

        Il.ilConvertImage(Il.IL_RGBA, Il.IL_UNSIGNED_BYTE);

        Gl.glEnable(Gl.GL_TEXTURE_2D);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Il.ilGetInteger(Il.IL_IMAGE_BPP), 
                        Il.ilGetInteger(Il.IL_IMAGE_WIDTH), Il.ilGetInteger(Il.IL_IMAGE_HEIGHT), 0, 
                        Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Gl.GL_UNSIGNED_BYTE, Il.ilGetData());

        Il.ilDeleteImage(imageID);

        Gl.glBegin(Gl.GL_QUADS);

            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);

        Gl.glEnd();

        Gl.glFlush();
        Gl.glDeleteTextures(1, ref texture); 
     }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-11-11 15:17:58

尝试加载纹理而不干扰 glEnable。然后,当您准备好绘制时,调用 glEnable(GL_TEXTURE_2D); 并再次绑定纹理。

另外,我看不到您是否配置为单缓冲或双缓冲使用。您可能需要调用 SwapBuffers

Try loading the texture without messing with glEnable. Then when you're ready to draw, call glEnable(GL_TEXTURE_2D); and bind the texture again.

Also, I can't see whether you're configured for single-buffered or double-buffered usage. You might need a call to SwapBuffers.

风吹过旳痕迹 2024-11-11 15:17:58

我偶然发现了这个问题的一个非常简单的解决方案。

使用 Ulut 函数“Ilut.ilutGLLoadImage”几乎可以处理所有事情。
至少在这里给出的简单示例中是这样。

希望其他人发现这段代码有用。

using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;

namespace TextureTest
{
    public partial class Form1 : Form
    {
        int texture;

        public Form1()
        {
            InitializeComponent();
            OpenGlControl.InitializeContexts();

            Il.ilInit();
            Ilut.ilutInit();

            texture = Ilut.ilutGLLoadImage("test.bmp");

            Gl.glBegin(Gl.GL_QUADS);

                Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
                Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
                Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f); 
                Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);

            Gl.glEnd(); 
        }
    } 
}

I stumbled upon a very simple solution to this problem.

Using the Ulut function "Ilut.ilutGLLoadImage" pretty much takes care af everything.
At least in the simple example given here.

Hopefully someone else finds this code useful.

using System;
using System.Drawing;
using Tao.OpenGl;
using Tao.DevIl;
using System.Windows.Forms;

namespace TextureTest
{
    public partial class Form1 : Form
    {
        int texture;

        public Form1()
        {
            InitializeComponent();
            OpenGlControl.InitializeContexts();

            Il.ilInit();
            Ilut.ilutInit();

            texture = Ilut.ilutGLLoadImage("test.bmp");

            Gl.glBegin(Gl.GL_QUADS);

                Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex2f(-1.0f, -1.0f);
                Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex2f(1.0f, -1.0f);
                Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex2f(1.0f, 1.0f); 
                Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex2f(-1.0f, 1.0f);

            Gl.glEnd(); 
        }
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文