0

I am trying to make a plane using OpenGL, to some extent the code works but I get weird results on some indices. I will try to explain my code the best I can, it's a mixture of my code and what I have found online.

The MAIN:

All the set-up happens in the main so the function would know all the values needed

float zoom = 6.0f;
float vertical = 1.2f;
float horizontal = 1.2f;

const int planeWidth = 4;  //columns
const int planeHeight = 2; //rows

const int totalVertices = (planeWidth + 1) * (planeHeight + 1);

//GLfloat* vertices = new GLfloat[totalVertices];
GLfloat vertices[totalVertices] = { 0.0 };

const int indPerRow = planeWidth * 2 + 2;
const int indDegenReq = (planeHeight - 1) * 2;
const int totalIndices = indPerRow * planeWidth + indDegenReq;

//GLuint* indices = new GLuint[totalIndices];
GLuint indices[totalIndices] = { 0 };

GLfloat texCoords[totalVertices] = { 0 };

makePlane(planeWidth, planeHeight, vertices, indices, texCoords);

The FUNCTION:

First for loop creates vertices and the second does the indices

void makePlane(int width, int height, GLfloat *vertices, GLuint *indices)
{
width++;  //columns
height++; //rows

int size = sizeof(GLfloat);
for (int y = 0; y < height; y++)
{
    int base = y * width;
    for (int x = 0; x < width; x++)
    {
        int index = (base + x) * 2;
        vertices[index]    = (float)x;
        vertices[index +1] = (float)y;
    }
}

int i = 0;
height--;

for (int y = 0; y < height; y++)
{
    int base = y * width;

    for (int x = 0; x < width; x++)
    {
        indices[i++] = base + x;
        indices[i++] = base + width + x; 
    }
    if (y < height - 1)
    {
        indices[i++] = ((y + 1) * width + (width - 1));
        indices[i++] = ((y + 1) * width);
    }   
}
}

The RESULT:

4 x 2

Indices 0, 5, 1, 6, 2, 7, 3, 8, 4, 9, 9, 5, 5, 10, 6, 11, 7, 12, 8, 13, 9, 14, 0, 0, 0, 0, 0, 0, ...} unsigned int[42]

It does 22 values right and then the rest are zeros.

Any Idea why?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user1031204
  • 701
  • 1
  • 8
  • 30
  • I think you need to add 3 elements to indices, because you use triangles (indices[i++] = base + x; indices[i++] = base + width + x; indices[i++] = ((y - 1) * width) + x) – Unick Aug 09 '14 at 10:55
  • Every 3rd value seems to be very weird that way `{0, 5, 4294967291, 1, 6, 4294967292, 2, 7, 4294967293, 3, 8, 4294967294, 4, 9, 4294967295, ...} unsigned int[42]` – user1031204 Aug 09 '14 at 12:22
  • Did you see this post: http://stackoverflow.com/questions/5915753/generate-a-plane-with-triangle-strips? And how did you render triangels? – Unick Aug 09 '14 at 13:01
  • Yeah I did see, I got most of the code from it but I keep getting this odd issue of indices array not being filled properly. Just can't work out what's going wrong. – user1031204 Aug 09 '14 at 13:37
  • Those weird values, when you add the extra index in, are negative numbers. y-1 is negative for the first row. – CynicismRising Aug 09 '14 at 15:17

2 Answers2

0

I think your loop needs to look something like this (beware untested :). Basically think of it as looping over quads and outputting indices for triangle pairs.

for (int y = 0; y < height; y++)
{
    unsigned int base = y * width;
    unsigned int top = base + width;
    for (int x = 0; x < (width-1); x++)
    {
        indices[i++] = base + x;
        indices[i++] = top + x; 
        indices[i++] = top + x + 1; 

        indices[i++] = top + x + 1;
        indices[i++] = base + x + 1; 
        indices[i++] = base + x; 
    }
}
CynicismRising
  • 940
  • 4
  • 5
0

I realised that vertices were being created from bottom to top and indices were ordered to create triangles from top to bottom. So I changed the vertex creation for loop.

int index = 0;
for (int y = height; y >= 0; y--)
{

    for (int x = 0; x <= width; x++)
    {
        vertices[index] = ((float)x/4)-0.5;
        vertices[index +1] = ((float)y/4)-0.5;
        vertices[index + 2] = 0.0f;
        index += 3;
    }
}

It creates the vertices from top left to right bottom. Loop for the indices stayed the same:

int i = 0;
++width;
GLuint indices2[170] = { 0 };

for (int y = 0; y < height; y++)
{
    int base = y * width;

    for (int x = 0; x < width; x++)
    {
        indices[i++] = base + x;
        indices[i++] = base + width + x;
    }
    if (y < height - 1)
    {
        indices[i++] = ((y + 1) * width + (width - 1));
        indices[i++] = ((y + 1) * width);
    }
}
user1031204
  • 701
  • 1
  • 8
  • 30