2009-02-24 61 views
1

我試圖從OpenGL 1.5 spec轉換下面的代碼。到OpenGLES 1.1規範將OpenGL原始圖轉換爲OpenGLES

(num_x and num_y are passed into the function by arguments) 

::glBegin(GL_LINES); 
for (int i=-num_x; i<=num_x; i++) 
{ 
    glVertex3i(i, 0, -num_y); 
    glVertex3i(i, 0, num_y); 
} 

for (int i=-num_y; i<=num_y; i++) 
{ 
    glVertex3i(-num_x, 0, i); 
    glVertex3i(num_x, 0, i); 
} 

::glEnd(); 

這裏是我的相應轉換的代碼:(忽略我的循環效率低下,我試圖讓轉換爲正常工作第一)

我想建兩個事情這裏:

  • 所有的x,y的一個浮點陣列,z座標需要提請網格
  • 所需的所有verticies的索引陣列。
  • 這兩個數組然後被傳遞給OpenGL來渲染它們。

的陣列應該是什麼樣子的一個例子:

GLshort indices[] = {3, 0, 1, 2, 
        3, 3, 4, 5, 
        3, 6, 7, 8, }; 
GLfloat vertexs[] = {3.0f, 0.0f, 0.0f, 
        6.0f, 0.0f, -0.5f , 
        0, 0,  0, 
        6.0f, 0.0f, 0.5f, 
        3.0f, 0.0f, 0.0f, 
        0, 0,  0, 
        3, 0,  0, 
        0, 0,  0, 
        0, 6,  0}; 
 
int iNumOfVerticies = (num_x + num_y)*4*3; 
int iNumOfIndicies = (iNumOfVerticies/3)*4; 

GLshort* verticies = new short[iNumOfVerticies]; 
GLshort* indicies = new short[iNumOfIndicies]; 
int j = 0; 
for(int i=-num_x; j < iNumOfVerticies && i<=num_x; i++,j+=6) 
{ 
    verticies[j] = i; 
    verticies[j+1] = 0; 
    verticies[j+2] = -num_y; 

    verticies[j+3] = i; 
    verticies[j+4] = 0; 
    verticies[j+5] = num_y; 
} 

for(int i=-num_y; j < iNumOfVerticies && i<=num_y;i++,j+=6) 
{ 
    verticies[j] = i; 
    verticies[j+1] = 0; 
    verticies[j+2] = -num_x; 

    verticies[j+3] = i; 
    verticies[j+4] = 0; 
    verticies[j+5] = num_x; 
} 

我還需要建立一個數組,如果indicies轉嫁。我'從'茶壺'的例子'借用'陣列結構。

在每一行中,我們都有引用的次數跟隨的引用次數。

int k = 0; 
for(j = 0; j < iNumOfIndicies; j++) 
{ 
     if (j%4==0) 
     { 
     indicies[j] = 3; 
     } 
     else 
     { 
     indicies[j] = k++; 
     } 

} 
::glEnableClientState(GL_VERTEX_ARRAY); 
::glVertexPointer(3 ,GL_FLOAT, 0, verticies); 

for(int i = 0; i < iNumOfIndicies;i += indicies[i] + 1) 
{ 
     ::glDrawElements( GL_LINES, indicies[i], GL_UNSIGNED_SHORT, &indicies[i+1]); 
} 

delete [] verticies; 
delete [] indicies; 

請添加代碼的問題作爲註釋,不回答

回答

1

我可以看到幾件事情不對您的變換代碼:

1.-使用了錯誤的類型變量verticies ,應該是:

GLfloat* verticies = new float[iNumOfVerticies]; 

2.-錯誤填充個verticies,第二循環應該是:

for(int i=-num_y; j < iNumOfVerticies && i<=num_y;i++,j+=6) 
{ 
    verticies[j] = -num_x; 
    verticies[j+1] = 0; 
    verticies[j+2] = i; 

    verticies[j+3] = num_x; 
    verticies[j+4] = 0; 
    verticies[j+5] = i; 
} 

3.-的indicies不正確的填充,我想你應該刪除該行:

if (j%4==0) 
{ 
    indicies[j] = 3; 
} 
else 

4.-使用不當的glDrawElements ,用這一行替換循環:

::glDrawElements( GL_LINES, iNumOfIndicies, GL_UNSIGNED_SHORT, indicies); 
+0

謝謝,我最終重寫了一些我的代碼,但你的答案是有幫助的 – cbrulak 2009-03-12 19:49:31