1

我開始在畫布上用Path對象繪製六邊形網格。我根據六邊形尺寸對所有顯示器上顯示的六邊形數進行了所有計算。將畫布座標系轉換爲OpenGLES 2.0座標

我有我所有的六角形座標計算取決於畫布。但現在,我有嚴重的性能問題,必須將其移植到OpenGL。

由於該算法在畫布中工作,我試圖用GLU.gluUnProject將這些「畫布」六角形座標轉換爲OpenGL座標系。

「for循環」

float near[] = { 0.0f, 0.0f, 0.0f, 0.0f }; 
      GLU.gluUnProject(b.hexes[ii][jj].points[ii].x, 
        b.hexes[ii][jj].points[ii].y, 0, mg.mModelView, 0, 
        mg.mProjection, 0, viewport, 0, near, 0); 
vertices[zz] = (float) ((near[0])/near[3]); 
      zz++; 
      vertices[zz] = (float) ((near[1])/near[3]); 
      zz++; 
      vertices[zz] = 0; 

因爲我缺乏OpenGL的知識,我不知道如何glViewport,gluPerspective,的glTranslatef設置爲2D的世界,是 「相同的」 帆布。

所以我的問題是:

如何設置這三個東西,我的第一個六邊形(在畫布上,第一個是左上)是在OpenGL繪圖表面世界平等嗎?

更新

謝謝大家對你的幫助我的問題不感興趣。但是:

我現在將我的18個浮點頂點數組設置如下: [20.0,0.0,0.0,60.0,0.0,0.0,80.0,34.641018,0.0,60.0,69.282036,0.0,20.0,69.282036,0.0,0.0 ,34.641018,0.0] Z是始終爲0。

ByteBuffer vertexByteBuffer = ByteBuffer 
       .allocateDirect(vertices.length * 4); 
vertexByteBuffer.order(ByteOrder.nativeOrder()); 
vertexBuffer = vertexByteBuffer.asFloatBuffer(); 
vertexBuffer.put(vertices); 
vertexBuffer.position(0); 

和繪製:

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f); 
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); 
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, vertices.length/3); 
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 

繪製之前我設置: onDrawFrame()

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
     gl.glLoadIdentity(); 
gl.glTranslatef(0.0f, 0.0f, -5.0f); 

onSurfaceChanged()

gl.glViewport(0, 0, width, height); 
gl.glMatrixMode(GL10.GL_PROJECTION); 
gl.glLoadIdentity(); 
gl.glOrthof(0, width, height, 0, -1, 1); 
gl.glMatrixMode(GL10.GL_MODELVIEW); 
gl.glLoadIdentity(); 
+0

gluUnProject似乎是錯誤的方式去這裏。如果你只是想從畫布座標系統直接移植到OpenGL,你知道你可以設置opengl座標系爲你想要的任何東西嗎?你應該可以在OpenGL中使用相同的座標而不會出現問題。你能舉一個你正在使用什麼樣的畫布座標的例子,並且想複製嗎? – Tim

+0

好吧,六邊形是由6個x,y座標組成,這是左上角的十六進制座標: 「b.hexes [0] [0] .points [ii] .x」\t 20.0 \t 「b.hexes [0] [0] .points [II] .Y 「\t 0.0 」b.hexes [0] [0] .points [II] .X「 \t \t 60.0 」 b.hexes [0] [0 ] .points [II] .Y 「\t \t 0.0 」b.hexes [0] [0] .points [II] .X「 \t \t 80.0 」 b.hexes [0] [0] .points [ ii] .y「\t 34.641018 \t 「b.hexes [0] [0] .points [II] .X」 \t \t 60.0 「b.hexes [0] [0] .points [II] .Y」 \t \t 69.282036 「 b.hexes [0] [0] .points [II] .X 「\t \t 20.0 」b.hexes [0] [0] .points [II] .Y「 \t \t 69.282036 」 b.hexes [ 0] [0] .points [ii] .x「\t 0.0 \t 」b.hexes [0] [0] .points [ii] .y「\t 34.641018 – Cacamas

回答

1

您有沒有必要改變自己的頂點座標。你所要做的就是向OpenGL提供正確的投影矩陣。

基本上,這將是沿着線的東西:

glViewport(0, 0, canvasWidth, canvasHeight); 
glMatrixMode(GL_PROJECTION);      // or some matrix uniform if using shaders 
glLoadIdentity(); 
glOrtho(0, canvasWidth, canvasHeight, 0, -1, 1); // this will allow to pass vertices in 'canvas pixel' coordinates 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

根據在你傳遞你的頂點的順序,你可能要做出剔除無效。

+0

雖然我同意您的一般建議,但請注意OP提到了OpenGLES ** 2.0 **,這些功能都不存在。如果他之前沒有使用過時的功能,這可能會令人困惑。 – Tim

+0

我被OP提到'glTranslatef'弄糊塗了。 OpenGL ES 2的問題在於,如果沒有任何標準的數學庫,它很難給出合成代碼片段。 – rotoglup

+0

用我上面的代碼,我只需要修復這一行: gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,0,vertices.length/3); 現在它工作得很好!謝謝你們! rotoglup,我標記了你的答案。 – Cacamas