2016-03-07 99 views
-1

我對OpenGL相當陌生,並且在與DrawArrays一起使用ColorPointer函數時遇到了一些麻煩......我似乎無法讓對象使用顏色進行渲染..當我使用glColor4f函數,但是這工作..我想這是如何構建我的顏色FloatBuffer ..但我似乎無法弄清楚這一點。GLColorPointer與glDrawArrays不兼容

請看看我的一些代碼,讓我知道我在做什麼錯在這裏...此外,這是我的第一個堆棧溢出後流動(:

// function draws the vertices in 3D space 
public void draw(GL10 gl){ 
    // Enable client side access since arrays are store on client side heap 
    // and OpenGl is considered server side 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY); 
    gl.glEnable(GL10.GL_COLOR_ARRAY); 
    gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face 
    gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display) 
    gl.glEnable(GL10.GL_COLOR_MATERIAL); 

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, binaryStlParser.getVertices()); 
    gl.glNormalPointer(GL10.GL_FLOAT, 0, binaryStlParser.getNormalVectors()); 
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, binaryStlParser.colours); 
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (binaryStlParser.getNumOfFacets() * 3)); 

    //gl.glShadeModel(GL10.GL_SHININESS); 
    // disables access given from client side heap for OpenGL 
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); 
    gl.glDisable(GL10.GL_COLOR_ARRAY); 
    gl.glDisable(GL10.GL_CULL_FACE); 
    gl.glDisable(GL10.GL_COLOR_MATERIAL); 
} 

這從另一個類

numOfFacets = ByteBuffer.wrap(numberOfFacets).order(ByteOrder.nativeOrder()).getInt(); 

     // Allocate memory for the FloatBuffers since we now know the number of Facets 
     // we use a ByteBuffer to ensure native ordering ......... 
     // * 3 for each vertex point in 3D (* 3) space. Size of float is 4 bytes 
     vertices = ByteBuffer.allocateDirect((numOfFacets * 3 * 3) * 4) 
         .order(ByteOrder.nativeOrder()).asFloatBuffer(); 

     // * 3 for each coordinate in vector. Size of float is 4 bytes 
     normalVectors = ByteBuffer.allocateDirect((numOfFacets * 3) * 4) 
         .order(ByteOrder.nativeOrder()).asFloatBuffer(); 

     // * 3 for each vertex point in 4D (* 4) RBGA space. Size of float is 4 bytes 
     colours = ByteBuffer.allocateDirect((numOfFacets * 3 * 4) * 4) 
       .order(ByteOrder.nativeOrder()).asFloatBuffer(); 

     for(int i = 0 ; i < numOfFacets*3; i++){ 
      colours.put(new float[] {0.0f, 1.0f, 0.0f, 0.5f}); 
     } 
     colours.position(0); 
+1

我不明白爲什麼這會一直下降張貼,爲什麼我在負面2.建設性的批評也會有所幫助.. – Arjun

回答

2

我想通了這一點只是想後的答案櫃面它可以幫助別人!(:

愚蠢的錯誤......貌似我用:

gl.glEnable(GL10.GL_COLOR_ARRAY);

代替

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

FloatBuffers存儲在客戶端堆,而OpenGL被認爲是服務器端我想。