0

Wanting to decal multiple irregular textures onto a curved surface (mesh with xyz vertices and uv specified at each). I am loading the mesh from a model file, and don't have any a priori knowledge of the surface... all we know is that it will have a "reasonable" uv mapping. Want to select a few uv regions and apply textures to them. Each region is specified by a bounding poly in uv coordinates. Don't know the equivalent xyz poly in this case, or I think the answer would be simple.

We have this working for flat surfaces and also simple cylindrical surfaces (which we approximate as a series of flat stripes, smoothed by choosing the normal as averages). In both cases we know a unique mapping from uv to xyz so we set up the stencil buffer to limit drawing to the desired uv region by drawing the equivalent xyz poly to the stencil buffer ahead of binding a texture and drawing the real surface.

We are also using rgba transparency within the textures when decaling those onto the surface. Typically each textured region is a small rotated rectangle so we draw the four vertices to the stencil buffer, then use the texture matrix to rotate that, and use the rgba transparency within the texture to ensure only the right part of the texture is applied. This all works nicely.

Would like to reuse our working code, but now apply these textures to an arbitrary curved surface/mesh. We are loading and drawing these models, and can already apply textures to whole faces [ie uv goes from (0,0) to (1,1) ]. Now we want to extend this and apply "placed" textures to regions of each surface.

Thought it might be possible draw the uv poly to the stencil buffer directly, not even knowing the equivalent xyz poly... then all the existing code would work. Perhaps could use some trick like a frame buffer object, and do the initial draw of the stencil poly to that, then using that as the stencil during the "real" draw of the curved surface mesh. Would that be a good approach? Or is there a better way?

Any advice or url links to relevant samples welcome... PS Have looked at these threads... sort of relevant but not quite the same problem I think...

Binding a stencil render buffer to a frame buffer in opengl

Visualizing the Stencil Buffer to a texture

I am currently looking at some working FBO setup/usage code I have for off-screen shadow mapping, and trying to make it work for this seemingly simpler situation. The bit I'm unclear on is the setup gl calls needed ... I am rather confused about how to set this up. Here's an extract of the hardware shadowing FBO setup with bits chopped out and ?? added... any help on correct sequence here appreciated.

glBindTexture(GL_TEXTURE_2D, tex); 
?? not
::glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, shadowsize, shadowsize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
?? but a more normal binding approp to drawing RGBA textures 
::glBindTexture(GL_TEXTURE_2D, 0); 
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer); 

// Attach everything, tell fbo there will be a drawbuffer, unlike shadows tex draw 
// ?? use GL_COLOR_ATTACHMENT0_EXT 
glDrawBuffer(GL_NONE); 
// no color buffer dest...
??wrong glReadBuffer(GL_NONE); 
// no color buffer src 
?? glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, tex, 0); 
//??

Note: tex, m_Frambuffer are ints, correctly allocated textureid and framebuffer, think that bit is ok. My main points of confusion are

  1. Seems that code does glBindTexture, glTexImage2D, glBindTexture release to 0: is it correct to release this early?
  2. glDrawBuffer + glReadBuffer calls required?
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70
  • Can't you just draw directly to the texture? Otherwise you could probably create a map as follows: In the vertex shader map a vertex' uv to the position. In the fragment shader, draw the vertex' interpolated position to the frame buffer. Then you'll have a map from UVs to model positions. – Nico Schertler Mar 25 '14 at 14:03
  • OK, drawing directly to the texture is what we're trying now. – user3459749 Mar 27 '14 at 01:44
  • Can you send a link to an example of the FBO setup for that? I am currently looking at some working FBO setup/usage code I have for off-screen shadow mapping, and trying to make it work for this seemingly simpler situation. The bit I'm unclear on is the setup gl calls needed ... I am rather confused about how to set this up. Here's an extract of the hardware shadowing FBO setup with bits chopped out and ?? added... any help on correct sequence here appreciated... [continued....] – user3459749 Mar 27 '14 at 01:49
  • glBindTexture(GL_TEXTURE_2D, tex); ?? not ::glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, shadowsize, shadowsize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); ?? but a more normal binding approp to drawing RGBA textures ::glBindTexture(GL_TEXTURE_2D, 0); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer); – user3459749 Mar 27 '14 at 01:54
  • // Attach everything, tell fbo there *will* be a drawbuffer, unlike shadows tex draw // ?? use GL_COLOR_ATTACHMENT0_EXT glDrawBuffer(GL_NONE); // no color buffer dest...??wrong glReadBuffer(GL_NONE); // no color buffer src ?? glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, tex, 0); //?? – user3459749 Mar 27 '14 at 01:55
  • [Sorry, StackOv newbie, code above is not set out correctly] Note: tex,m_Frambuffer are ints, correctly allocated textureid and framebuffer, think that bit is ok. My main points of confusion... 1. Seems that code does glBindTexture, glTexImage2D, glBindTexture release to 0: is it correct to release this early? 2. glDrawBuffer + glReadBuffer calls required? – user3459749 Mar 27 '14 at 02:02
  • = = = = = = = = = = UPDATE: I have found two links which may resolve this and may be helpful to others reading this post... http://www.opentk.com/doc/graphics/frame-buffer-objects ...which explains things very well, but since I am using C++/MFC Visual Studio 2010, I have different .h definitions, and the info here was very useful too: https://www.opengl.org/wiki/Framebuffer_Object_Examples#Color_only Further comments/gotcha-warnings/etc most welcome, and I will update this post again if there is something I can add once this is working. Tks. – user3459749 Mar 27 '14 at 08:15

0 Answers0