views:

1047

answers:

1

I'm trying to convert the following code from OpenGL 1.5 spec. to the OpenGLES 1.1 spec

(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();

Here is my corresponding converted code: (ignore the inefficiency of my loops, I am trying to get the conversion to work properly first)

I'm trying to build two things here:

  • A float array of all the x,y,z coordinates needed to draw the grid
  • An index array of all the verticies needed.
  • Both arrays are then passed to OpenGL which renders them.

An example of what the arrays should look like:

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;
 }

I need to also build an array if indicies to pass on. I 'borrowed' the array structure from the iphone 'teapot' example.

In each row, we have the number of indicies followed by the indicies referenced.

 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;

Please add code questions as comments, not answers

+1  A: 

I can see several things wrong with your converted code:

1.- Using the wrong type for the variable verticies, it should be:

GLfloat* verticies = new float[iNumOfVerticies];

2.- Incorrectly filling verticies, second loop should be:

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.- Incorrect filling of indicies, I think you should delete the lines:

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

4.- Incorrect use of glDrawElements, replace the loop by this single line:

::glDrawElements(  GL_LINES, iNumOfIndicies, GL_UNSIGNED_SHORT, indicies);
Unbeknown
thanks, I ended up re-writing some of my code, but your answer was helpful
cbrulak