纹理映射的基本概念和优点摘取Nehe的说法如下:
请输入内容学习 texture map 纹理映射(贴图)有很多好处。比方说您想让一颗×××飞过屏幕。根据前几课的知识,我们最可行的办法可能是很多个多边形来构建×××的轮廓并加上有趣的颜色。使用纹理映射,您可以使用真实的×××图像并让它飞过屏幕。您觉得哪个更好看?照片还是一大堆三角形和四边形?使用纹理映射的好处还不止是更好看,而且您的程序运行会更快。×××贴图可能只是一个飞过窗口的四边形。一个由多边形构建而来的×××却很可能包括成百上千的多边形。很显然,贴图极大的节省了CPU时间。
现在我们在第一课的代码开始处增加五行新代码。新增的第一行是 #include <stdio.h> 。它允许我们对文件进行操作,为了在后面的代码中使用 fopen() ,我们增加了这一行。然后我们增加了三个新的浮点变量... xrot , yrot 和 zrot 。这些变量用来使立方体绕X、Y、Z轴旋转。最后一行 GLuint texture[1] 为一个纹理分配存储空间。如果您需要不止一个的纹理,应该将参数1改成您所需要的参数。【form Nehe】
 
在C#中使用纹理映射,这次我们的改动会比较大。
不过这些代码是SharpGL例程中给出的。我基本上未做改动
1.SharpGL中将纹理设计为一个类,定义如下
6.纹理映射_OpenGlpublic class Texture
6.纹理映射_OpenGl        SharpGL.SceneGraph 的成员
       我们可以用对象昂浏览器看Texture类包含的成员函数。
       我们要在图像设计上采用纹理这一特性就应该在类中加入纹理对象即在Form类中加入纹理对象:
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture = new SharpGL.SceneGraph.Texture();
            有了Texture对象我们就会想到用其成员函数将图片读取为纹理,下面为成员函数
6.纹理映射_OpenGlpublic virtual bool Create(SharpGL.OpenGL gl, string path)
6.纹理映射_OpenGl        SharpGL.SceneGraph.Texture 的成员
           由Bitmap文件初始化Texture的代码(应该在Form类的构造函数中完成)
6.纹理映射_OpenGlSharpGL.OpenGL gl = this.openGLControl1.OpenGL;
6.纹理映射_OpenGltexture.Create(gl, "Crate.bmp");
6.纹理映射_OpenGl//启用纹理映射
6.纹理映射_OpenGlgl.Enable(OpenGL.TEXTURE_2D);
  
2  上步完成了纹理变量的初始化,下面就是将纹理对象中所存储的由Crate.bmp转换成的纹理绑定到要创建的三维图像上。需要调用
6.纹理映射_OpenGltexture.Bind(gl);
         如果你的纹理需要用在所有需要贴图的表面上,则此函数的调用可以在Form类的构造函数或OpenGLDraw的事件响应函数中去。添加到OpenGLDraw的事件响应函数中后的代码示例如下
6.纹理映射_OpenGl                private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
6.纹理映射_OpenGl                {
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
6.纹理映射_OpenGl                        
6.纹理映射_OpenGl                        gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);
6.纹理映射_OpenGl                        gl.LoadIdentity();
6.纹理映射_OpenGl                        gl.Translate(0.0f, 0.0f, -6.0f);
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        gl.Rotate(rtri, 1.0f, 1.0f, 1.0f);
6.纹理映射_OpenGl                        
6.纹理映射_OpenGl                        //    将纹理绑定在要绘制的图像表面
6.纹理映射_OpenGl                        texture.Bind(gl);
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Front Face
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Back Face
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Top Face
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Bottom Face
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Right face
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        // Left Face
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl                        gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl                        gl.End();
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        gl.Flush();
6.纹理映射_OpenGl
6.纹理映射_OpenGl                        rtri += 100.0f;// 0.2f;            // Increase The Rotation Variable For The Triangle    
6.纹理映射_OpenGl                }
 
效果如下图:
6.纹理映射_休闲_68
 
此时的源代码命名为:纹理映射_1
 
你可以看出,在这个立方体的所有表面贴的是相同的纹理,
如果每个表面都贴上不同的纹理,我们应该怎么做呢?
 
Nehe的教程当初留下了这个悬念,当初我非常的菜,弄了半天没弄出来(C#和OpenGL都在学习初级阶段,总出错)
 
后来好不容易做出来了,思路很简单,六面体有六个面,我们只需要创建留个纹理对象
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture1= new SharpGL.SceneGraph.Texture();
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture2 = new SharpGL.SceneGraph.Texture();
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture3 = new SharpGL.SceneGraph.Texture();
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture4= new SharpGL.SceneGraph.Texture();
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture5 = new SharpGL.SceneGraph.Texture();
6.纹理映射_OpenGlSharpGL.SceneGraph.Texture texture6 = new SharpGL.SceneGraph.Texture();
 
每个纹理对象在Form的构造函数中用不同的图像创建
 
6.纹理映射_OpenGltexture1.Create(gl, "CreatFront.jpg");
6.纹理映射_OpenGltexture2.Create(gl, "CreatBack.jpg");
6.纹理映射_OpenGltexture5.Create(gl, "CreatRight.jpg");
6.纹理映射_OpenGltexture6.Create(gl, "CreatLeft.jpg");
6.纹理映射_OpenGltexture3.Create(gl, "CreatTop.jpg");
6.纹理映射_OpenGltexture4.Create(gl, "CreatBottom.jpg");
 
在OpenGLDraw的事件响应中在画六面体每个平面之间改变OpenGL所绑定的纹理图片就可以了
6.纹理映射_OpenGl    private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
6.纹理映射_OpenGl    {        
6.纹理映射_OpenGl            //    将纹理绑定在要绘制的图像表面
6.纹理映射_OpenGl            SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
6.纹理映射_OpenGl
6.纹理映射_OpenGl            gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);
6.纹理映射_OpenGl            gl.LoadIdentity();
6.纹理映射_OpenGl            gl.Translate(0.0f, 0.0f, -6.0f);
6.纹理映射_OpenGl            gl.Rotate(rtri, 1.0f, 1.0f, 1.0f);
6.纹理映射_OpenGl
6.纹理映射_OpenGl            //    Bind the texture.
6.纹理映射_OpenGl            texture1.Bind(gl);
6.纹理映射_OpenGl
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl
6.纹理映射_OpenGl            // Front Face
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            texture2.Bind(gl);
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl            // Back Face
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            texture3.Bind(gl);
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl            // Top Face
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            texture4.Bind(gl);
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl            // Bottom Face
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            texture5.Bind(gl);
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl            // Right face
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            texture6.Bind(gl);
6.纹理映射_OpenGl            gl.Begin(OpenGL.QUADS);
6.纹理映射_OpenGl            // Left Face
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);  // Bottom Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);  // Bottom Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);  // Top Right Of The Texture and Quad
6.纹理映射_OpenGl            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);  // Top Left Of The Texture and Quad
6.纹理映射_OpenGl            gl.End();
6.纹理映射_OpenGl            gl.Flush();
6.纹理映射_OpenGl
6.纹理映射_OpenGl            rtri += 100.0f;// 0.2f;            // Increase The Rotation Variable For The Triangle    
6.纹理映射_OpenGl    }
6.纹理映射_C#_146
 
 
源代码命名为  纹理映射2