Question

I am generating a lot of points to create an island with GL_POLYGON and I want to bind one texture to the entire island. Right now I set the coordinates every time I create a new square. Right now I have this

 for (int g =0;g<400;g++){
    glBegin(GL_POLYGON);
    glTexCoord2i(1,1);
        glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
        g++;
    glTexCoord2i(1,0);
        glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
        g++;
    glTexCoord2i(0,0);
        glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
        g++;
    glTexCoord2i(0,1);
        glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
        //if(g==399){printf("at 0 =%f,%f,%f\n",islandVert[399][0],islandVert[399][1],islandVert[399][2]);}
    glEnd();
    }

But I don't want to repeat the patern as a whole on every square. I want the pattern to span all of my squares. Also note that all of the square have different y values.

Was it helpful?

Solution

If you want to stretch your texture across the whole island, you need to use texture coordinates that are not (0,0) to (1,1) per polygon, but for the whole model. The easiest way to do that is to use the (x,z) coordinates of the vertex and scale them appropriately, e.g.

glBegin(GL_POLYGON);
glTexCoord2f(islandVert[g][0] / xscale, islandVert[g][2] / zscale);
glVertex3f(islandVert[g][0], islandVert[g][1], islandVert[g][2]);
g++;
...

(Note: you need to use glTexCoord2f or it's not going to work.)

where xscale and zscale are the maximum possible x and z values (assuming (0,0) is the minimum possible value, but you get the idea). In modern programs this would be done in the shader, but it looks like you're not using those.

The disadvantage is that the one texture will be spread out over the whole model. Unless you have a very high resolution texture it is going to be very blurry. The usual response is to repeat the texture. Probably not over every polygon like you did, but a configurable number of times, like this:

glBegin(GL_POLYGON);
glTexCoord2f(islandVert[g][0] / xscale * xrepeat, islandVert[g][2] / zscale * zrepeat);
glVertex3f(islandVert[g][0], islandVert[g][1], islandVert[g][2]);
g++;
...

Texture coordinates can be larger than 1, assuming your texture parameters are set to repeat:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

Check out http://www.glprogramming.com/red/chapter09.html for an introduction to textures, texture coordinates etc.

Sideline 1: if you render quads, use the GL_QUADS primitive for a significant speed boost:

glBegin(GL_QUADS);
for (int g =0;g<400;g++)
{
    glTexCoord2i(1,1);
    glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
    g++;
    glTexCoord2i(1,0);
    glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
    g++;
    glTexCoord2i(0,0);
    glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
    g++;
    glTexCoord2i(0,1);
    glVertex3f(islandVert[g][0],islandVert[g][1],islandVert[g][2]);
}
glEnd();

Sideline 2: you really shouldn't be using Immediate Mode at all in this day and age. Check out Vertex Buffer Objects for the current way to specify geometry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top