2016-08-01 20 views
0

Open.GL ES2にロードすると、すべてがうまくいくものの、いくつかの面があるので、私はThree.js BufferGeometryで作成したJSON形式のJSON形式としてサーバーからモデルをロードしています。テクスチャがある場合はビューを回転するか、色がある場合は半透明になるまで表示されません。 頂点の値と順序はThree.jsから何も変更せずに生成されますので、正しい順序であると仮定して、反転した面でなければなりません。 enter image description hereopacityでレンダリングされたAndroid OpenGL ES2三角形

enter image description here これは、フラグメントシェーダである:

precision mediump float; 

varying vec4 v_Color; 
uniform sampler2D u_TextureUnit; 
varying vec2 v_TextureCoordinates; 

void main() { 
    vec4 tex=texture2D(u_TextureUnit, v_TextureCoordinates); 
    gl_FragColor=v_Color+tex; 
} 

レンダリングコード:

private static final int BYTES_PER_FLOAT = 4; 
// private static final int BYTES_PER_SHORT = 2; 
private static final int POSITION_COMPONENT_COUNT = 3; 
private static final int COLOR_COMPONENT_COUNT = 4; 
private static final int UV_COMPONENT_COUNT = 2; 
private static final int ALL_COMPONENT_COUNT = POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT + UV_COMPONENT_COUNT; 
private static final int STRIDE = ALL_COMPONENT_COUNT * BYTES_PER_FLOAT; 

private VertexArray vertexArray; 

private float[] modelMatrix = new float[16]; 
private final float[] modelViewProjectionMatrix = new float[16]; 

TextureShaderProgram shaderProgram; 
int textureid; 
// ShortBuffer indexBuffer; 

public Geometry(Context context, JSONObject element) throws JSONException {  
    /* 
    * JSONArray indecies = element.getJSONArray("indexes"); short[] indexes 
    * = new short[indecies.length()]; for (int y = 0; y < 
    * indecies.length(); y++) { indexes[y] = (short) indecies.getInt(y); } 
    */ 
    // indexBuffer = ByteBuffer.allocateDirect(indexes.length * 
    // BYTES_PER_SHORT).order(ByteOrder.nativeOrder()).asShortBuffer(); 
    // indexBuffer.put(indexes).position(0); 
    JSONArray vertices = element.getJSONArray("vertices"); 
    JSONArray uvs = element.getJSONArray("uvs"); 
    JSONArray matrix = element.getJSONArray("matrix"); 
    for (int y = 0; y < matrix.length(); y++) { 
     modelMatrix[y] = (float) matrix.getDouble(y); 
    } 
    JSONObject material = element.getJSONObject("material"); 
    int color = Color.parseColor(material.getString("color")); 
    String bmpString = material.getString("map"); 
    int n = vertices.length()/3; 
    float[] data; 
    data = new float[n * STRIDE]; 
    int k = 0; 
    for (int i = 0; i < n; i++) { 
     data[i * ALL_COMPONENT_COUNT] = (float) vertices.getDouble(i * 3); 
     data[i * ALL_COMPONENT_COUNT + 1] = (float) vertices.getDouble(i * 3 + 1); 
     data[i * ALL_COMPONENT_COUNT + 2] = (float) vertices.getDouble(i * 3 + 2); 
     data[i * ALL_COMPONENT_COUNT + 3] = Color.red(color)/255f; 
     data[i * ALL_COMPONENT_COUNT + 4] = Color.green(color)/255f; 
     data[i * ALL_COMPONENT_COUNT + 5] = Color.blue(color)/255f; 
     data[i * ALL_COMPONENT_COUNT + 6] = Color.alpha(color)/255f; 
     data[i * ALL_COMPONENT_COUNT + 7] = bmpString.equals("") ? 0f : (float) uvs.getDouble(k); 
     data[i * ALL_COMPONENT_COUNT + 8] = bmpString.equals("") ? 0f : (float) uvs.getDouble(k + 1); 
     k += 2; 
    } 
    vertexArray = new VertexArray(data); 

    shaderProgram = new TextureShaderProgram(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader); 
    textureid = bmpString.equals("") ? 0 : TextureHelper.loadTexture(TextureHelper.decodeBase64(bmpString)); 
} 

private void bindData(TextureShaderProgram shaderProgram) { 
    vertexArray.setVertexAttribPointer(0, shaderProgram.getPositionAttributeLocation(), POSITION_COMPONENT_COUNT, STRIDE); 
    vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT, shaderProgram.getColorAttributeLocation(), COLOR_COMPONENT_COUNT, STRIDE); 
    vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT, shaderProgram.getTextureCoordinatesAttributeLocation(), UV_COMPONENT_COUNT, STRIDE); 
} 

public void draw(float[] projectionMatrix) { 
    multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, modelMatrix, 0); 
    shaderProgram.useProgram(); 
    shaderProgram.setUniforms(modelViewProjectionMatrix, textureid); 
    bindData(shaderProgram); 
    glDrawArrays(GL_TRIANGLES, 0, vertexArray.getCapacity()/ALL_COMPONENT_COUNT); 
    // glDrawElements(GL_TRIANGLES, indexBuffer.capacity(), 
    // GL_UNSIGNED_SHORT, indexBuffer); 
} 

vertexDataクラス:

private final FloatBuffer floatBuffer; 
private final int capacity; 

public VertexArray(float[] vertexData) { 
    floatBuffer = ByteBuffer.allocateDirect(vertexData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertexData); 
    capacity = floatBuffer.capacity(); 
} 

public void setVertexAttribPointer(int dataOffset, int attributeLocation, int componentCount, int stride) { 
    floatBuffer.position(dataOffset); 
    glVertexAttribPointer(attributeLocation, componentCount, GL_FLOAT, false, stride, floatBuffer); 
    glEnableVertexAttribArray(attributeLocation); 
    floatBuffer.position(0); 
} 

public final int getCapacity() { 
    return capacity; 
} 
+1

あなたの要求に深さのバッファがありましたか?深度テストを有効にしますか? –

+0

ありがとう、これは非常に役に立ちました – Mohamed

答えて

1

深度関数が設定されていません、だからOpenG L ESは、インデックスデータに基づいて三角形の順に描画します。カメラからの奥行き情報に基づいて三角形をソートする深度テストを有効にする必要があります。

glEnable(GL_DEPTH_TEST); 
+0

ありがとう、完璧な答え – Mohamed