1

我正在尝试从 OpenGL 1.5 规范转换以下代码。OpenGLES 1.1 规范

(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 坐标的浮点数组
  • 所有需要的顶点的索引数组。
  • 然后将两个数组传递给渲染它们的 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;
 }

如果要传递的指标,我还需要构建一个数组。我从 iphone 'teapot' 示例中“借用”了数组结构。

在每一行中,我们都有索引的数量,然后是引用的索引。

 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;

请将代码问题添加为评论,而不是答案

4

1 回答 1

1

我可以看到您转换的代码有几个问题:

1.- 对变量verticies使用错误的类型,它应该是:

GLfloat* verticies = new float[iNumOfVerticies];

2.- 不正确地填充顶点,第二个循环应该是:

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.-索引填写不正确,我认为您应该删除以下行:

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

4.- glDrawElements使用不正确,用这一行替换循环:

::glDrawElements(  GL_LINES, iNumOfIndicies, GL_UNSIGNED_SHORT, indicies);
于 2009-03-04T16:31:19.747 回答