3
\$\begingroup\$

I am trying to render a cube by starting of with one vertex into the shader, then geometry shader to expand into a cube, and then apply a different texture to each side. The part I am facing the issue with applying unique textures to each side.

So I am currently doing this by creating it via quads pushing the vertices, and applying the textures one by one. In fact, I drawIndexed() a set of vertices, then switch over to a different textures every 6 vertices. But pushing in so many vertices is expensive for higher FPS. So, I looked in to Geometry shader and it seems like it did the job. After studying it, I understand how to develop the vertices in the geometry shader. However, I don't know how to apply a different texture say every 6 vertices - since we are in the pipeline already.

\$\endgroup\$
  • \$\begingroup\$ More specifics on what you've done already would help someone answer. For example, is your question about expanding the vertex into a cube? Or is it about applying a texture to each side? \$\endgroup\$ – MichaelHouse Jan 8 '14 at 21:18
  • \$\begingroup\$ @Byte56 I have updated the question. Sorry about that. \$\endgroup\$ – bluejamesbond Jan 8 '14 at 21:21
  • \$\begingroup\$ Does that mean you can currently apply the same texture to each side and you want to know how to apply unique textures? If you tell us how you're generating the cube and what you've tried to do to apply textures it would help. \$\endgroup\$ – MichaelHouse Jan 8 '14 at 21:22
  • \$\begingroup\$ @Byte56 I did update the question. Did what I am trying to say make sense? \$\endgroup\$ – bluejamesbond Jan 8 '14 at 21:28
4
\$\begingroup\$

The bound textures in a single draw call cannot be changed. This means that your geometry shader cannot select arbitrarily between different textures.

The solution is to use either texture atlasing or array textures. In either case, for each cube face you generate both new positions and new texture coordinates.

The C++ code must still bind the correct atlas or array that covers the needs of all faces of a cube. Games like Minecraft need only a single atlas for all cubes; they could also have used an array if they targetted newer versions of GL.

In either case, your geometry shader needs to sample some information from the vertex attributes that indicates which 6 slices of the atlas/array are used for each face. It then uses that information to set the proper texture coordinates.

\$\endgroup\$
  • \$\begingroup\$ Before I attempt this, would you say that this sort of technique where you expand the vertices with geometry shader and fill using a texture array be a faster option that rendering each face and shading each phase? And by how much? Say that we are doing around 300,000 - 500,000 cubes (atleast its the plan). \$\endgroup\$ – bluejamesbond Jan 9 '14 at 0:01
  • \$\begingroup\$ It should be faster (though not as fast as generating a single static mesh for the whole chunk after each change), but you will need to test to determine for sure that it is and by how much. It'll vary by specific implementation, application design, hardware vendor, etc. \$\endgroup\$ – Sean Middleditch Jan 9 '14 at 0:08
  • \$\begingroup\$ I saw this video on youtube and I wanted to do something similar but each face has a different texture. I am not sure as to why theirs is so efficient (although the description says it is not optimized). \$\endgroup\$ – bluejamesbond Jan 9 '14 at 0:13

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.