1

I'm trying to figure out programming with GLSL and I'm slightly confused on what I'm supposed to do. I've figured out how to pass a vertex array, an array of texture coordinates (per vertex), an element array and two textures to a simple GLSL program. I'm using pygame for the simplicity while I'm learning.

I create vertices, texture coordinates and an element array like this:

w = 16
h = 12

vertex_array = []
tex_coord_array = []
element_array = []

for i in range(h):
    for j in range(w):
        vertex_array += [-1.0+j*2.0/(w-1), 1.0-i*2.0/(h-1), 0.0, 1.0]

        if j%2:
            tex_coord_array += [1.0]
        else:
            tex_coord_array += [0.0]

        if i%2:
            tex_coord_array += [0.0]
        else:
            tex_coord_array += [1.0]

for i in range(1,h):
    for j in range(1,w):
        element_array += [(i-1)*w+(j-1), i*w+(j-1), (i-1)*w+j]
        element_array += [i*w+(j-1), i*w+j, (i-1)*w+j]

... giving me a simple "wall" of textured triangles using the following shaders:

vertex_shader = '''\
#version 110

attribute vec2 tex_coord;
attribute vec4 position;

varying vec2 texcoord;

void main()
{
    gl_Position = position;
    texcoord = tex_coord;
}
'''

and

fragment_shader = '''\
#version 110

uniform sampler2D textures[2];

varying vec2 texcoord;

void main()
{
    gl_FragColor = texture2D(textures[0], texcoord);
}
'''

Right now I get the texture mirroring in every direction (on every 4 quads) since I only supply one texture coord per vertex instead of passing one per element of the element array. I want to have all quads textured with the correct aligned texture (since I told myself that was a good start in GLSL ^^), what would be the way to do this?

Thanks!

UPDATE:

As Nicol Bolas pointed out - this isn't really a GLSL problem. For now I've just redone the vertex/coord/element generation to this:

for i in range(h):
    for j in range(w):
        # first triangle
        vertex_array += [-1.0+j*2.0/(w-1), 1.0-i*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [0.0, 1.0]
        vertex_array += [-1.0+j*2.0/(w-1), 1.0-(i+1)*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [0.0, 0.0]
        vertex_array += [-1.0+(j+1)*2.0/(w-1), 1.0-i*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [1.0, 1.0]
        # second triangle
        vertex_array += [-1.0+j*2.0/(w-1), 1.0-(i+1)*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [0.0, 0.0]
        vertex_array += [-1.0+(j+1)*2.0/(w-1), 1.0-(i+1)*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [1.0, 0.0]
        vertex_array += [-1.0+(j+1)*2.0/(w-1), 1.0-i*2.0/(h-1), 0.0, 1.0]
        tex_coord_array += [1.0, 1.0]

element_array = range(h*w*6)

And it works - but it feels sort of wasteful to use six times as many vertices just to get the texture coordinates right...

Norling
  • 1,024
  • 2
  • 10
  • 22
  • This has nothing to do with GLSL. This is just about passing vertex data; by the time GLSL gets it, it's too late. – Nicol Bolas Feb 20 '13 at 01:12
  • very true - but still, how do I generate vertex/coordinate/element arrays to get the behavior I want? I figured I could do it by passing "more" vertices (right now I only pass a grid instead of every vertex in every triangle), but if there's any better way I'd love to learn about it! =) – Norling Feb 20 '13 at 01:16

0 Answers0