开发者

C# OpenGL Texture Does not show up

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

      开发者_StackOverflow社区  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); 
     }
}
}


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.


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(); 
        }
    } 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜