0
votes

I have a floor in my game, which is made of 100x100 quads. I couldn't do it just with 1 quad, because it would ruin lighting. And here is the problem - how do I put a single texture on all of them? This code puts texture on each of the quads, but it is not the effect I want.

glBegin(GL_QUADS);
glNormal3f(0,0,1);

for(float j = 0; j < 1; j+=0.01)
{       
      for(float i = 0; i < 1; i+=0.01)
    {           
        glTexCoord2f(0.0,0.0); glVertex2f(i,j+0.01);
        glTexCoord2f(1.0,0.0); glVertex2f(i,j);
        glTexCoord2f(1.0,1.0); glVertex2f(i+0.01,j);
        glTexCoord2f(0.0,1.0); glVertex2f(i+0.01,j+0.01);
    }

}
glEnd();
glPopMatrix();
3
The lighting would work fine if you ditched the old fixed-function GL interface and used the programmable pipeline. - JasonD

3 Answers

0
votes

A quick-and-dirty solution can be to use the same values for texture coordinates as for the floor:

glTexCoord2f(i, j+0.01);      glVertex2f(i, j+0.01);
glTexCoord2f(i, j);           glVertex2f(i, j);
glTexCoord2f(i+0.01, j);      glVertex2f(i+0.01, j);
glTexCoord2f(i+0.01, j+0.01); glVertex2f(i+0.01, j+0.01);
0
votes

Well, it looks like you are using an older version of OpenGL so you might not have them available to you, but using a custom fragment shader would solve your problem. Which is what I would do. But I think what you are looking for is you just need to interpolate the UV coords. For example if you have 4 squares, instead of

0.0f,0.0f
1.0f,0.0f
1.0f,1.0f
0.0f,1.0f

the first should be

0.0f,0.0f
.5f,0.0f
.5f,.5f
.0f,.5f

I would also go with integers in your for loop for more predictable behavior but with what you have you could adapt the way you calculate the pos of the vertices to the indices.

    glTexCoord2f(i,j); glVertex2f(i,j+0.01);
    glTexCoord2f(i+.01,j); glVertex2f(i,j);
    glTexCoord2f(i+.01,j+.01); glVertex2f(i+0.01,j);
    glTexCoord2f(i,j+.01); glVertex2f(i+0.01,j+0.01);

Note the order of the tex coords.

-1
votes

I couldn't do it just with 1 quad, because it would ruin lighting.

This is what per-pixel lighting is all about. Per-vertex lighting, where you evaluate the lighting color only at the vertices, and interpolate across the entire triangle, suffers poor results when your triangles are fairly large relative to its distance to the light, as in your floor case. If you use per-pixel lighting, you'll have to write your own fragment shader, but then you can use just one quad for the entire floor and the lighting will be correct no matter how close or far the light is.

See also: http://www.swiftless.com/tutorials/glsl/5_lighting_perpixel.html, http://en.wikipedia.org/wiki/Per-pixel_lighting