0

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.

Community
  • 1
  • 1
iansmathew
  • 367
  • 1
  • 3
  • 13
  • Consider a vertex to have (x,y,z,u,v) coordinates rather than just (x,y,z). What changes would you need to make? – 1201ProgramAlarm Apr 07 '18 at 21:27
  • @1201ProgramAlarm That would work, but the only issue like I mentioned above is that some vertices are shared between triangles, hence for the first triangle the uv would be different for that vertex for the next triangle. – iansmathew Apr 07 '18 at 21:33
  • 1
    @iansmathew What I'm trying to hint at is if you consider the coordinates to be 5D instead of 3D you _don't_ have duplicates. – 1201ProgramAlarm Apr 07 '18 at 21:39
  • @Rabbid76 I dont entirely understand how I would go about switching between u1 and u2. My current understanding is that I have a vertex array, an index array and a texCoord array. – iansmathew Apr 07 '18 at 22:38
  • @1201ProgramAlarm I think I understand you meant. So I would go about generating my vertices as normal, but for each vertex I would push another vertex with the same x,y,z but different uv coordinates? Is that right? – iansmathew Apr 07 '18 at 22:39
  • So why would you even need any discontinuities in the texture coordinates when texture-mapping a plane? – derhass Apr 07 '18 at 22:44
  • @derhass because if a vertex is shared between two triangles (as is the case in triangle strip) wouldnt you need different texture coordinates? The bottom right vertex of one triangle could be the bottom left vertex of another – iansmathew Apr 07 '18 at 22:48
  • I don't see how this would be relevant. You have not described what kind of mapping you actually want to achieve, so it is natural to make plausible assumptions. And when mapping a 2D texture to 2D flat rectangluar shape (it isn't exactly a plane as it is not infinite), the plausible cases are a) the 2D texture is to be mapped onto the full area of the rectangle, or b) the 2D texture is to be mapped to each "quad" of the rectangle. In both cases, there is no need for any discontinuities in the parametrization. b) is actually only a scaled variant of a) when taking the wrap modes into account. – derhass Apr 07 '18 at 22:55
  • 1
    @Rabbid76: If that is the case, he should describe it in the question. – derhass Apr 08 '18 at 12:00

0 Answers0