2014-04-10 2 views
0

이것은 바보 같은 질문처럼 보일지 모르지만 내가 openGl 버퍼 작동 방식을 이해할 수 있도록 요청해야한다고 생각합니다.OpenGL es 2.0 버텍스 및 인덱스 채우기 버퍼

두 버퍼, 정점 버퍼 및 인덱스 버퍼가 있습니다. 이러한 버퍼를 채우기 위해 obj 파일을 구문 분석하고 있습니다.

데이터를 구문 분석 한 후 올바른 데이터 및 데이터 순서라고 생각하는 것으로 버퍼를 채 웁니다.

그러나 코드를 디버깅 할 때 버퍼에 이상한 점이 있습니다.

먼저 정점 버퍼. 내가 Vbuffer에 넣은 첫 3 값은 0.99, -0.99, -0.7741 입니다. 그러나이 값은 FloatBuffer이지만 바이트 버퍼이므로 -92,112,125 입니다.

this.vertexBuffer = ByteBuffer.allocateDirect(VertexList.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();        
    this.vertexBuffer.put(VertexList); 
    this.vertexBuffer.position(0); 

인덱스 버퍼 제가 4,1,2 넣어 상이하고 그것을 확인할 때 각 값 사이의 여분 0을 얻을 수가 4,0,1,0,2

this.IndexBuffer = ByteBuffer.allocateDirect(IndexList.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer(); 
    this.IndexBuffer.put(IndexList); 
    this.IndexBuffer.position(0); 

이 버퍼에서 나는 그것의 바이트 버퍼 비록 ShortBuffer로 데이터를 읽을 수 있습니다. 다른 모든 값은 왜 0입니까?

이 점에 대해 이해하면 도움이 될 것입니다.

업데이트!

이것은 내 obj 파일의 데이터를 구문 분석하고 버퍼를 채우는 데 필요한 전체 코드입니다. 버퍼에 저장되는 데이터가 올바른 것 같습니다. 여기

public class RBLoadModelFromFile 
{ 
/** How many bytes per float. */ 
private final int mBytesPerFloat = 4; 

private Context context; 
public FloatBuffer vertexBuffer; 
public ShortBuffer IndexBuffer; 
public float[] vertices; 
public float[] normals; 
public float[] uVs; 
public short[] Ind; 
private ArrayList<Short> indicies; 
public int NumVertices; 
public int NumIndicies; 

public RBLoadModelFromFile(Context c) 
{ 
    context = c; 
} 


public void loadModel(String filename) 
{ 

    ArrayList<RBVector3> tempVertices = new ArrayList<RBVector3>();   
    ArrayList<RBVector3> tempNormals = new ArrayList<RBVector3>(); 
    ArrayList<RBVector3> tempUVs = new ArrayList<RBVector3>(); 

    ArrayList<Short> vertexIndices = new ArrayList<Short>(); 
    ArrayList<Short> normalIndices = new ArrayList<Short>(); 
    ArrayList<Short> textureIndices = new ArrayList<Short>(); 
    indicies = new ArrayList<Short>(); 

    try { 
     AssetManager manager = context.getAssets(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(manager.open(filename))); 
     String line; 
     while ((line = reader.readLine()) != null) 
     { 
      if (line.startsWith("v ")) 
      {  
       RBVector3 tempVert = new RBVector3(0f,0f,0f); 
       tempVert.add(Float.valueOf(line.split(" ")[2]),Float.valueOf(line.split(" ")[3]),Float.valueOf(line.split(" ")[4])); 
       tempVertices.add(tempVert); 
      } 
      else if (line.startsWith("vn")) 
      { 
       RBVector3 tempNorm = new RBVector3(0f,0f,0f); 
       tempNorm.add(Float.valueOf(line.split(" ")[1]),Float.valueOf(line.split(" ")[2]),Float.valueOf(line.split(" ")[3])); 
       tempNormals.add(tempNorm);        
      } 
      else if (line.startsWith("vt")) 
      { 
       RBVector3 tempUV = new RBVector3(0f,0f,0f); 
       tempUV.add(Float.valueOf(line.split(" ")[1]),Float.valueOf(line.split(" ")[2]), 0f); 
       tempUVs.add(tempUV);        
      } 
      else if (line.startsWith("f")) 
      {     
       String tmp[] = line.split(" ");  
       vertexIndices.add(Short.valueOf(tmp[1].split("/")[0])); 
       textureIndices.add(Short.valueOf(tmp[1].split("/")[1])); 
       normalIndices.add(Short.valueOf(tmp[1].split("/")[2])); 

       vertexIndices.add(Short.valueOf(tmp[2].split("/")[0])); 
       textureIndices.add(Short.valueOf(tmp[2].split("/")[1])); 
       normalIndices.add(Short.valueOf(tmp[2].split("/")[2])); 

       vertexIndices.add(Short.valueOf(tmp[3].split("/")[0])); 
       textureIndices.add(Short.valueOf(tmp[3].split("/")[1])); 
       normalIndices.add(Short.valueOf(tmp[3].split("/")[2])); 
      } 
     } 
     Ind = new short[vertexIndices.size() + normalIndices.size() + textureIndices.size()]; 
     vertices = new float[(vertexIndices.size() * 3) + (normalIndices.size() * 3) + (textureIndices.size() * 2)]; 
     ArrayList<Float> vertInfo = new ArrayList<Float>(); 
     for (int i = 0; i < vertexIndices.size(); i++) 
     {    
      Short v = vertexIndices.get(i); 
      vertInfo.add(tempVertices.get(v-1).x); 
      vertInfo.add(tempVertices.get(v-1).y); 
      vertInfo.add(tempVertices.get(v-1).z); 
      indicies.add((short) (v-1)); 
      Short n = normalIndices.get(i); 
      vertInfo.add(tempNormals.get(n-1).x); 
      vertInfo.add(tempNormals.get(n-1).y); 
      vertInfo.add(tempNormals.get(n-1).z); 
      indicies.add((short) (n-1)); 
      Short t = textureIndices.get(i); 
      vertInfo.add(tempUVs.get(t-1).x); 
      vertInfo.add(tempUVs.get(t-1).y); 
      indicies.add((short) (t-1)); 
     } 
     for(int i = 0; i < indicies.size(); i++) 
     { 
      Ind[i] = indicies.get(i); 
     } 
     for (int i = 0; i < vertInfo.size(); i++) 
     { 
      vertices[i] = vertInfo.get(i); 
     } 
     createIndexBuffer(Ind); 
     createVertexBuffer(vertices); 
     NumIndicies = Ind.length; 
     NumVertices = vertices.length; 
    } 
    catch (Exception e) 
    { 
     Log.d("DEBUG", "Error.", e); 
    } 
} 
private void createVertexBuffer(float[] VertexList) 
{ 
    this.vertexBuffer = ByteBuffer.allocateDirect(VertexList.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();        
    this.vertexBuffer.put(VertexList); 
    this.vertexBuffer.position(0); 
} 
private void createIndexBuffer(short[] IndexList) 
{ 
    this.IndexBuffer = ByteBuffer.allocateDirect(IndexList.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer(); 
    this.IndexBuffer.put(IndexList); 
    this.IndexBuffer.position(0); 
} 
} 

내가 지금 뭔가를 얻고있다

v -0.500000 -0.500000 0.500000 
v 0.500000 -0.500000 0.500000 
v -0.500000 0.500000 0.500000 
v 0.500000 0.500000 0.500000 
v -0.500000 0.500000 -0.500000 
v 0.500000 0.500000 -0.500000 
v -0.500000 -0.500000 -0.500000 
v 0.500000 -0.500000 -0.500000 

vt 0.000000 0.000000 
vt 1.000000 0.000000 
vt 0.000000 1.000000 
vt 1.000000 1.000000 

vn 0.000000 0.000000 1.000000 
vn 0.000000 1.000000 0.000000 
vn 0.000000 0.000000 -1.000000 
vn 0.000000 -1.000000 0.000000 
vn 1.000000 0.000000 0.000000 
vn -1.000000 0.000000 0.000000 

f 1/1/1 2/2/1 3/3/1 
f 3/3/1 2/2/1 4/4/1 
f 3/1/2 4/2/2 5/3/2 
f 5/3/2 4/2/2 6/4/2 
f 5/4/3 6/3/3 7/2/3 
f 7/2/3 6/3/3 8/1/3 
f 7/1/4 8/2/4 1/3/4 
f 1/3/4 8/2/4 2/4/4 
f 2/1/5 8/2/5 4/3/5 
f 4/3/5 8/2/5 6/4/5 
f 7/1/6 1/2/6 5/3/6 
f 5/3/6 1/2/6 3/4/6 

그릴을 구문 분석하려고 간단한 큐브 OBJ 파일이지만 정점 방식으로 해제 될 것으로 보인다.

이 문제가 어디 있는지 판단하는 전체 코드를 가지고 있지

public void DrawModel(float[] mProjectionMatrix, float[] mViewMatrix, float[] mMVPMatrix, int mProgramHandle, int mMVPMatrixHandle, int mMVMatrixHandle, int mLightPosHandle, float[] mLightPosInEyeSpace) 
{ 

    mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position"); 
    mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal"); 
    mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture"); 
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate"); 

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 

    // Bind the texture to this unit. 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); 
    // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0. 
    GLES20.glUniform1i(mTextureUniformHandle, 0); 

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix 
    // (which currently contains model * view). 
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); 

    // Pass in the modelview matrix. 
    GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0); 

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix 
    // (which now contains model * view * projection). 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); 

    // Pass in the combined matrix. 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

    // Pass in the light position in eye space. 
    GLES20.glUniform3f(mLightPosHandle, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);   

    // the vertex coordinates 
    VertexBuffer.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); 
    GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 
      TRIANGLE_VERTICES_DATA_STRIDE_BYTES, VertexBuffer); 
    GLES20.glEnableVertexAttribArray(mPositionHandle); 

    // the normal info 
    VertexBuffer.position(TRIANGLE_VERTICES_DATA_NOR_OFFSET); 
    GLES20.glVertexAttribPointer(mNormalHandle, 3, GLES20.GL_FLOAT, false, 
      TRIANGLE_VERTICES_DATA_STRIDE_BYTES, VertexBuffer); 
    GLES20.glEnableVertexAttribArray(mNormalHandle); 

// texture coordinates 
    VertexBuffer.position(TRIANGLE_VERTICES_DATA_TEX_OFFSET); 
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2, GLES20.GL_FLOAT, false, 
      TRIANGLE_VERTICES_DATA_STRIDE_BYTES, VertexBuffer); 
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);//GLES20.glEnableVertexAttribArray(shader.maTextureHandle); 

    // Draw with indices 
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, NumIndicies , GLES20.GL_UNSIGNED_SHORT, IndexBuffer); 
    //checkGlError("glDrawElements"); 
} 

답변

0

나는 마침내 렌더링 문제를 해결할 수있었습니다. @MaurizioBenedetti에게 감사 드리며, 저의 질문에 대한 답을 해주었습니다.

먼저 버퍼에 넣고있는 것은 어느 정도 정확합니다. 내 문제는 내가 인터리브 된 버텍스 버퍼를 생성했기 때문입니다. 이것은 데이터가 이미 올바른 순서 VVVNNNTT에 있음을 의미합니다. 그런 다음 나는 인터리브되지 않은 버퍼에 대한 표시 인 실제 인덱스 데이터를 포함하는 인덱스 버퍼를 만들었습니다.

예를 들어 얼굴에 2/4/6 4/7/9 2/1/3이 포함 된 경우 인덱스 버퍼에는 이러한 정확한 값이 포함되어 있지만 내 버텍스 버퍼가 인터리브되어 있기 때문에 인디케이트 수의 순서가 지정된 목록을 포함해야합니다. 상기면은 상기에서 요약 한 다음하는 색인 ​​버퍼 0,1,2,3,4,5,6,7,8

포함 했어야 만한다면 모든 인덱스

그래서 이는 코드 내가 정점 정보를 채울 때

나는 말했다해야이

indicies.add(index++); 

같은 대신 각 indicie 값을

I를 할당 이런 종류의 일에 매달려 있고 희망적으로 도움이 될 사람에게는 이것이 분명하기를 바랍니다.

1

내 그리기 방법 지금은 다소 복잡하다.

데이터 유형을 혼합하는 중입니다.

플로트 번호는 다소 까다롭기 때문에 플로트로 처리하지 않으면 플로트 메모리 장치의 내용을 쉽게 읽을 수 없습니다.

플로트는 표준에 따라 3 가지 구성 요소, 비트 기호 (가장 중요한), 지수 및 가수를 기반으로합니다.

보통 현재 가장 일반적인 플랫폼은 32 비트 (4 바이트)로 표현에 떠 여기서

1 부호 대개 가득 23 비트 가수 지수 (대 8 비트 있지만

플로트를 플로트로 읽거나 쓰지 않는 경우 (부동 소수점을 정수로 처리 할 수있는 방법이 있지만 해당 속성을 알고 있지만 너무 많이 사용하지 않을 수 있습니다.) 예측할 수없는 방식으로 해석 할 위험이 있습니다.

제 조언은 그 값을 float로 읽고 쓰고 읽기/쓰기 작업 바이트/정수로하지 않는 것입니다.

+0

좋은 하루 내 친구, 나는 몇 가지를 변경하고 위의 파서를 포함한 모든 코드를 업로드했습니다. 이제 물건을 그려야하지만 꼭지점이 섞여있는 것처럼 보이면 위를 확인하고 문제가 무엇인지 생각해보십시오. 감사 – Rob85

관련 문제