0

我在 OpenGL,Tao 框架中工作。我已经完成了所有的编程,但我不能插入多个纹理。这是我的代码。为了将另一个图像放入数组中,我应该插入什么?这就是我调用 3 个纹理的方式,但第 3 个和其他纹理总是 owerwriten。

        Gl.glClearDepth(1.0);
        Gl.glClearColor(0.0f, 0.2f, 0.3f, 0.0f);
        Gl.glEnable(Gl.GL_DEPTH_TEST);    
        Gl.glFrontFace(Gl.GL_CW);         
        Gl.glEnable(Gl.GL_COLOR_MATERIAL);
        Gl.glShadeModel(Gl.GL_SMOOTH);
        Gl.glEnable(Gl.GL_LIGHTING);
        Gl.glEnable(Gl.GL_LIGHT0);

        Gl.glDepthFunc(Gl.GL_LEQUAL);                             
        Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);   

        Gl.glEnable(Gl.GL_TEXTURE_2D);

        texture = new uint[5];

        Bitmap image = new Bitmap("1.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);
        System.Drawing.Imaging.BitmapData bitmapdata;
        Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);


        Gl.glGenTextures(1, texture);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[1]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);     // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);     // Linear Filtering

        Gl.glViewport(0, 0, Size.Width, Size.Height);
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        Gl.glLoadIdentity();
        Glu.gluPerspective(60, ((float)Size.Width / (float)Size.Height), 0.1, 1000);
        Gl.glEnable(Gl.GL_TEXTURE_2D);

        image = new Bitmap("2.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);
        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);


        Gl.glGenTextures(1, texture);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);     // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);     // Linear Filtering

        Gl.glViewport(0, 0, Size.Width, Size.Height);
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        Gl.glLoadIdentity();
        Glu.gluPerspective(60, ((float)Size.Width / (float)Size.Height), 0.1, 1000);
        Gl.glEnable(Gl.GL_TEXTURE_2D);

        image = new Bitmap("3.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);


        Gl.glGenTextures(1, texture);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);     // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);     // Linear Filtering

        Gl.glViewport(0, 0, Size.Width, Size.Height);
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        Gl.glLoadIdentity();
        Glu.gluPerspective(60, ((float)Size.Width / (float)Size.Height), 0.1, 1000);
        Gl.glEnable(Gl.GL_TEXTURE_2D);
4

1 回答 1

1

好的,这就是我的做法,现在它可以工作了。谢谢你 Vallentin 的帮助。我能以某种方式给你积分吗?glBindTexture() 的想法让我想到它是 glGenTextures ,它有点保存纹理的实例。

private void initGL()
    {

        Gl.glClearDepth(1.0);
        Gl.glClearColor(0.0f, 0.2f, 0.3f, 0.0f);
        Gl.glEnable(Gl.GL_DEPTH_TEST);  // Hidden surface removal
        Gl.glFrontFace(Gl.GL_CW);           // clock-wise polygons face out
        Gl.glEnable(Gl.GL_COLOR_MATERIAL);
        Gl.glShadeModel(Gl.GL_SMOOTH);
        Gl.glEnable(Gl.GL_LIGHTING);
        Gl.glEnable(Gl.GL_LIGHT0);

        Gl.glDepthFunc(Gl.GL_LEQUAL);                               // type of depth testing
        Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); // nice perspective calculations

        Gl.glEnable(Gl.GL_TEXTURE_2D);

        texture = new uint[6];

        Bitmap image = new Bitmap("1.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);
        System.Drawing.Imaging.BitmapData bitmapdata;
        Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            // storage for texture for one picture

        Gl.glGenTextures(5, texture);
        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[5]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);       // Linear Filtering

         image = new Bitmap("2.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

         rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // storage for texture for one picture

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[4]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

        image = new Bitmap("3.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // storage for texture for one picture

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[3]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

        image = new Bitmap("4.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // storage for texture for one picture

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[2]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

        image = new Bitmap("5.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // storage for texture for one picture

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[1]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

        image = new Bitmap("6.jpg");
        image.RotateFlip(RotateFlipType.RotateNoneFlipY);

        rect = new Rectangle(0, 0, image.Width, image.Height);

        bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        // storage for texture for one picture

        Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);
        Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8, image.Width, image.Height,
            0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);       // Linear Filtering
        Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

        Gl.glViewport(0, 0, Size.Width, Size.Height);
        Gl.glMatrixMode(Gl.GL_PROJECTION);
        Gl.glLoadIdentity();

        Glu.gluPerspective(60, ((float)Size.Width / (float)Size.Height), 0.1, 1000);

    }
于 2013-10-17T14:37:57.370 回答