Joshua Waring
Joshua Waring

Reputation: 619

Converting Points to Triangle_strip in geometry shader, how to pass UV's

I'm currently trying to pass points into a custom wrote sprite batcher, right now I have it so the points get the position and use the dimensions to create a triangle strip of a quad. my question though is if the geometry shader gets a single point then how do I pass around texture coordinates to the fragment shader?

I'm also trying to think about texture aliasing so the texture coordinates might not always be 0 to 1

Upvotes: 1

Views: 1940

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43389

Well, you emit 4 unique vertices right? Just assign each of the corners you emit in the geometry shader a different coordinate in the range [0.0,1.0] where (0,0) is the bottom-left of the image and (1,1) is the top-right.

Consider the following Geometry Shader:

#version 330

layout (points) in;
layout (triangle_strip, max_vertices = 4) out;

out vec2 tex_coord;

void main (void) {
  const vec2 coordinates [] = vec2 [] (vec2 (0.0f, 0.0f),
                                       vec2 (1.0f, 0.0f),
                                       vec2 (1.0f, 1.0f),
                                       vec2 (0.0f, 1.0f));

  for (int i = 0; i < 4; i++) {
    gl_Position = gl_in [0].gl_Position + vec4 (coordinates [i], 0.0f, 0.0f);
    tex_coord   =                               coordinates [i];
    EmitVertex ();
  }
}

Upvotes: 2

Related Questions