m3g学习笔记5,m3g note 5

m3g学习笔记5,m3g note 5

//////////////////
  note 5
//////////////////
1.纹理
1.1.纹理图像的大小
纹理图像的大小必须是 2 的非负指数幂,如(2,4,8,16,32,64,128,256)。
1.2.比例问题
纹理坐标的值必须在从 0 到 1 的范围内,但是在程序中我们使用 0 到 255 范围内的整数值指定了纹理的坐标,所以我们使用1.0f/255.0f这个因数将这些坐标值等比例缩小。
我们在为棱锥指定纹理坐标时,一定要清楚纹理图像一直是一个正方形,所以正上方中间的纹理坐标应该为0.5,因此我们输入127,127*(1/255) ≈ 0.5。
1.3.创建步骤
1、创建一个图片对象,利用这个图片对象创建纹理对象。纹理图像是以Image2D
用存储的。这个图像可以是Image2D中所规定的任何格式,需要注意的是,图像的宽度和高度都要为2的非负指数幂,但是二者并不一定相等。如果在程序运行中我们对Image2D对象进行了修改,那么立刻就会反映到纹理对象中,但是这个过程会产生很大的开支,例如重新生成纹理和重新分配内存等,因此在建立纹理对象以后最好不要修改Image2D对象。
 2、使用 setWrapping 方法设置纹理图像的重复模式。Texture2D 中定义了两种纹理图片
的重复模式:WRAP_CLAMP和WRAP_REPEAT,前者是指只重复一次,其实也就是没有重复,程序中画的纹理就是这种方式;后者是指无限重复,直到填充这个画面,程序中砖块的纹理就是这种方式。
 3、使用setBlending方法设置混合方式,混合是指过滤后的纹理颜色和引入的片段颜色
合。Texture2D定义了五种混合方式:FUNC_ADD、FUNC_BLEND、FUNC_DECAL、FUNC_MODULATE和FUNC_REPLACE。
 4、使用setFiltering 方 法 设 置 过 滤 方 式 。Texture2D 定 义 三 种 过 滤 方 式 :FILTER_BASE_LEVEL 、FILTER_LINEAR 和FILTER_NEAREST。请注意,这个设置仅仅是一个提示,应用程序在实现时可能忽略这个设置,根据自己的判断选择一种合适的方式。
    /*
     * Create a pyramid
     */
    private Mesh createpyramid(){
       
        // The vertices used by the pyramid. x, y, z
        short []POINTS = new short[] {-1, -1, 1,    1, -1, 1,   0, 1, 0,     // front
                                                           1, -1, 1,    1, -1, -1,  0, 1, 0,    // right
                                                           1, -1, -1,  -1, -1, -1,  0, 1, 0,    // back
                                                          -1, -1, -1,  -1, -1, 1,   0, 1, 0,    // left
                                                          -1, -1, 1,    1, -1, 1,   1, -1, -1,  // bottom right
                                                          -1, -1, 1,    1, -1, -1, -1, -1, -1}; // bottom left
        // The texture coordinates is scaled down to 0-1 values in the setTextCoords method
        short []TEXTURES = new short[] {0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 0,            255, 0,         255, 255,
                                                              0, 0,            255, 255,     0, 255};
                                                           
        // The points sequence.
        int []INDICES = new int[] {0, 1, 2, // front
                                                   3, 4, 5,  // right
                                                   6, 7, 8,  // back
                                                   9, 10, 11, // left
                                                   12, 13, 14, // bottomright
                                                   15, 16, 17}; // bottomleft
        // The length of each sequence in the indices array.
        int []LENGTH = new int[] {3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles
       
        VertexArray POSITION_ARRAY, TEXTURE_ARRAY;
        IndexBuffer INDEX_BUFFER;
       
        // Create a VertexArray to be used by the VertexBuffer
        POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);
        POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);
        TEXTURE_ARRAY = new VertexArray(TEXTURES.length / 2, 2, 2);
        TEXTURE_ARRAY.set(0, TEXTURES.length / 2, TEXTURES);
        INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);
        // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,
        // and texture coordinates for a set of vertices
        VertexBuffer vertexBuffer = new VertexBuffer();
        vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);
        vertexBuffer.setTexCoords(0, TEXTURE_ARRAY, (1.0f/255.0f), null);
        vertexBuffer.setTexCoords(1, TEXTURE_ARRAY, (1.0f/255.0f), null);
        // Create the 3D object defined as a polygonal surface
        Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);
       
        Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh
        PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
        polygonMode.setPerspectiveCorrectionEnable(true);
        polygonMode.setCulling(PolygonMode.CULL_BACK); // Use CULL_BACK for performace.
        appearance.setPolygonMode(polygonMode);
       
        mesh.setAppearance(0, appearance); // Set the appearance to the 3D object
        return mesh;
    }   

One thought on “m3g学习笔记5,m3g note 5

  1. wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold -229417357876531

Leave a Reply

Your email address will not be published. Required fields are marked *