user1031204
user1031204

Reputation: 701

Issues with making an OpenGL plane out of triangles

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?

Upvotes: 0

Views: 1129

Answers (2)

user1031204
user1031204

Reputation: 701

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

Upvotes: 0

CynicismRising
CynicismRising

Reputation: 940

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

Upvotes: 0

Related Questions