在OpenGL ES 2 Android中分配.mtl文件的纹理

时间:2016-03-05 02:04:20

标签: java android opengl-es

我已经成功地将.obj文件加载到Java中的顶点数组中。但是我想知道是否有人可以指导我如何根据我的mtl文件将纹理指定给我的对象。

到目前为止,我可以使用以下代码将一个纹理分配给整个obj文件:

public static int loadTexture(final Context context, final int resourceId)
{
final int[] textureHandle = new int[1];

GLES20.glGenTextures(1, textureHandle, 0);

if (textureHandle[0] != 0)
{
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;   // No pre-scaling

    // Read in the resource
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

    // Bind to the texture in OpenGL
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

    // Set filtering
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    // Load the bitmap into the bound texture.
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    // Recycle the bitmap, since its data has been loaded into OpenGL.
    bitmap.recycle();
}

if (textureHandle[0] == 0)
{
    throw new RuntimeException("Error loading texture.");
}

return textureHandle[0];

}

顶点着色器:

uniform mat4 u_MVPMatrix;       // A constant representing the combined model/view/projection matrix.                  
uniform mat4 u_MVMatrix;        // A constant representing the combined model/view matrix.              

attribute vec4 a_Position;      // Per-vertex position information we will pass in.                 
attribute vec4 a_Color;         // Per-vertex color information we will pass in.                
attribute vec3 a_Normal;        // Per-vertex normal information we will pass in.      
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in.       

varying vec3 v_Position;        // This will be passed into the fragment shader.            
varying vec4 v_Color;           // This will be passed into the fragment shader.                
varying vec3 v_Normal;          // This will be passed into the fragment shader.  
varying vec2 v_TexCoordinate;   // This will be passed into the fragment shader.            

// The entry point for our vertex shader.  
void main()                                                     
{                                                         
    // Transform the vertex into eye space.     
    v_Position = vec3(u_MVMatrix * a_Position);            

    // Pass through the color.
    v_Color = a_Color;

    // Pass through the texture coordinate.
    v_TexCoordinate = a_TexCoordinate;                                      

    // Transform the normal's orientation into eye space.
    v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));

    // gl_Position is a special variable used to store the final position.
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
    gl_Position = u_MVPMatrix * a_Position;                               
}  

片段着色器

    precision mediump float;        // Set the default precision to medium. We don't need as high of a 
                                        // precision in the fragment shader.
        uniform vec3 u_LightPos;        // The position of the light in eye space.
        uniform sampler2

D u_Texture;    // The input texture.

    varying vec3 v_Position;        // Interpolated position for this fragment.
    varying vec4 v_Color;           // This is the color from the vertex shader interpolated across the 
                                    // triangle per fragment.
    varying vec3 v_Normal;          // Interpolated normal for this fragment.
    varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.

    // The entry point for our fragment shader.
    void main()                         
    {                              
        // Will be used for attenuation.
        float distance = length(u_LightPos - v_Position);                  

        // Get a lighting direction vector from the light to the vertex.
        vec3 lightVector = normalize(u_LightPos - v_Position);                  

        // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
        // pointing in the same direction then it will get max illumination.
        float diffuse = max(dot(v_Normal, lightVector), 0.0);                                                                                 

        // Add attenuation. 
        diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));

        // Add ambient lighting
        diffuse = diffuse + 0.2;

        // Multiply the color by the diffuse illumination level and texture value to get final output color.
        gl_FragColor = (v_Color * diffuse * texture2D(u_Texture, v_TexCoordinate));                                         
    }   

0 个答案:

没有答案