【问题标题】:Converting OpenGL Primitives to OpenGLES将 OpenGL 基元转换为 OpenGLES
【发布时间】:2009-02-24 18:12:56
【问题描述】:

我正在尝试从 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,由 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;

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

【问题讨论】:

    标签: c++ iphone opengl-es opengl-to-opengles


    【解决方案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);
    

    【讨论】:

    • 谢谢,我最终重新编写了一些代码,但您的回答很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2014-02-05
    • 1970-01-01
    相关资源
    最近更新 更多