2016-08-01 14 views
0

Ich lade Modelle vom Server als JSON-Format, das ich in Three.js BufferGeometry erstellt und Position und uv-Attribute in einer JSON-Datei gespeichert, wenn ich auf openGL ES2 laden alles ist schön, aber einige Gesichter erscheinen nicht, bis ich die Ansicht rotiere, wenn sie eine Textur hat oder halbtransparent erscheint, wenn sie eine Farbe hat. die Vertices Werte und Reihenfolge von Three.js ohne Änderungen generiert, so nehme ich an, dass es in der richtigen Reihenfolge sind, so dass es keine umgekehrten Flächen sein sollte. enter image description hereAndroid OpenGL ES2 Dreiecke gerendert mit Opazität

enter image description here das ist das Fragment-Shader:

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; 
} 

und der Rendering-Code:

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); 
} 

die vertexData Klasse:

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

Haben Sie einen Tiefenpuffer angefordert? Und Tiefenprüfung aktivieren? –

+0

Vielen Dank, das war sehr hilfreich – Mohamed

Antwort

1

Die Tiefe Funktion nicht gesetzt ist , so OpenG L ES zeichnet in der Reihenfolge der Dreiecke basierend auf den Indexdaten. Sie müssen den Tiefen-Test aktivieren, der die Dreiecke basierend auf den Tiefeninformationen der Kamera sortiert.

glEnable(GL_DEPTH_TEST); 
+0

Vielen Dank, perfekte Antwort – Mohamed