Generate and draw a square plane with GL_TRIANGLES?

1k Views Asked by At

Preface:

I have read the post over at Generate a plane with triangle strips. The code below is from there, however it does not allow me to texture or light the grid.

Objective:

Given a number 'n', generate, texture and draw the a plane of width = n &height = n using OpenGL. The plane should be made of individual quads of unit size.

Code so far:

Code to generate vertices

float * Plane::getUniqueVertices()
{
    if (uniqueVertices) return uniqueVertices;

    uniqueVertices = new float[NUM_VERTICES];
    int i = 0;

    for (int row = 0; row<height; row++) {
        for (int col = 0; col<width; col++) {
            uniqueVertices[i++] = (float)col; //x
            uniqueVertices[i++] = (float)row; //y
            uniqueVertices[i++] = 0.0f;        //z
        }
    }

    return uniqueVertices;
} 

Code to generate indices

int * Plane::getIndices()
{
    if (indices) return indices;

    indices = new int[NUM_DRAW_ELEMENTS];
    int i = 0;

    for (int row = 0; row<height - 1; row++) 
    {
        if ((row & 1) == 0) // even rows
        { 
            for (int col = 0; col<width; col++) 
            {
                indices[i++] = col + row * width;
                indices[i++] = col + (row + 1) * width;
            }
        }
        else // odd rows
        { 
            for (int col = width - 1; col>0; col--) 
            {
                indices[i++] = col + (row + 1) * width;
                indices[i++] = col - 1 + +row * width;
            }
        }
    }

    if ((height & 1) && height > 2) 
    {
        indices[i++] = (height - 1) * width;
    }

    return indices;
} 

I then draw the plane using GL_TRIANGLE_STRIP and the index buffer.

What do I need?:

While the above works, I cannot texture / uv map or get normals since it uses GL_TRIANGLE_STRIP thus the uv coordinates are different for some vertices that are shared between triangles. I need some way to get an array of vertices, their texture coordinates and normals so that I can texture and light a plane to end up like this. It doesn't matter how I get them as long as the plane consists of different quads and that I can change the dimensions of the plane.

0

There are 0 best solutions below