2009-02-24 62 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