0
\$\begingroup\$


currently I'm optimizing my render calls by drawing multiple textures with one shader. The background textures who all share the same position are no problem. But now I want to draw some Textures with different positions with one shader. I think it isn't possible, because the textures don't have the same geometry.
But maybe there is a possible way to do this? I never worked with the geometry shader for example. For example, draw a 512x512 texture to position (100, 50), draw a 256x256 texture to position (500, 300)... , like in this picture: shader

\$\endgroup\$
  • \$\begingroup\$ I'm surely misunderstanding something, but can't you just attach different textures to the same shader and draw geometry as usual. \$\endgroup\$ – HolyBlackCat Aug 14 '17 at 18:56
  • \$\begingroup\$ but the textures are then all drawn at the same position. but I wanted to draw them to different positions each. For example, draw a 512x512 texture to position (100, 50), draw a 256x256 texture to position (500, 300).... \$\endgroup\$ – mrdlink Aug 14 '17 at 19:00
  • \$\begingroup\$ You should edit your question to say that you specifically want all that on a single quad, I had to guess that. \$\endgroup\$ – HolyBlackCat Aug 14 '17 at 19:26
  • \$\begingroup\$ Then, just use separate texture coords for each texture, and in your fragment shader check if tex coords are in [0;1] range before applying it. \$\endgroup\$ – HolyBlackCat Aug 14 '17 at 19:27
  • \$\begingroup\$ This would usually be done by putting the three textures in an atlas, and making a vertex buffer that contains all three quads. Then the whole trio can be drawn in one call with minimal extra computation or overdraw. It sounds like you want something different, but you haven't told us enough about your situation for us to understand why. \$\endgroup\$ – DMGregory Aug 15 '17 at 11:36
0
\$\begingroup\$

You can use the geometry shader to generate new vertices, yes. You can also have multiple textures is the same shader program.

geometry shader:

#version 330 core

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

void main()
{
    // geometry shader has only one vertex input here
    vec4 pos = gl_in[0].gl_Position;

    gl_Position = vec4(pos.x+0.1, pos.yzw);
    EmitVertex();
    gl_Position = vec4(pos.x, pos.y+0.1, pos.zw);
    EmitVertex();
    gl_Position = vec4(pos.x+0.1, pos.y+0.1, pos.zw);
    EmitVertex();
    EndPrimitive();
}

But this is a very general answer, and there are tons of ways to do a thing.

\$\endgroup\$
  • \$\begingroup\$ Well it seems LibGDX doesn't support geometry shaders. \$\endgroup\$ – mrdlink Aug 15 '17 at 10:32
  • \$\begingroup\$ Then I would recommend using an OpenGL API that does ;) I use glfw for my projects ( glfw ), but I'm sure FreeGlut is also a possibility. Otherwise creating extra vertices in the shader programs won't really be an option, and you will have to call the shader program multiple times from your application. \$\endgroup\$ – Alexander Aug 15 '17 at 15:45
  • \$\begingroup\$ Ok, I managed to get the shader program compile with a geometry shader. But the screen is now blank when I draw something \$\endgroup\$ – mrdlink Aug 15 '17 at 16:25
  • \$\begingroup\$ This I cannot help you with, as it could be due to several reasons. Check that all in/out shader variables match, that uniforms are being properly activated from within the application, that textures are loaded, that coordinates are properly normalized before they enter the rasterization process, etc. \$\endgroup\$ – Alexander Aug 17 '17 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.