Understanding ofMesh with ofShader

howdy folks,

this is just a small & hopefully quick question. but I wanted to ask about shaders & using them with ofMesh.

I have a project that generates random addVertex points, which can be controlled via OSC etc. & whilst I love how this has turned out & does have some good results, I’m looking at getting shaders in the mix to extend to further.

can ofMesh bring in ofShader to have shaders on top of the generated ofMesh. the mesh itself is not always filled, but there are points such as using the OF_PRIMITIVE_TRIANGLE_STRIP where it does get filled in, but has no depth [currently].
ive tried a few different options, such as wrapping the shader around the mesh with

shader.begin();
mesh.draw();
shader.end();

without much fanfare. so assuming it will take more than just that. also tried even other setting on the modes, as well as a few other things.
it wasn’t until I got to 2am that I had to stop, come back with a fresh head & try again. but no luck.
im not looking to change the geometry [right now]. it’s only to plaster it over the mesh, with that doing all the other work.
its just simple shader im working with right now, just has a red color.

scratching my head a bit here, so just have to ask. not sure where other steps may be needed, if any

thanks

Hi @lewislepton,

  • great work on the tutorials, love them - ,

I personnally use the ofVboMesh - in OF_PRIMITIVE_TRIANGLE mode - with ofShader :
when creating my mesh, i add the texCoords :

ofVec2f texCoords = {((float)(i) / widthTexture), ((float)(j) / heightTexture)};
vboMesh.addTexCoord(texCoords);

then you pass the right texture coordinates as a variant in the vertex shader :


in vec2 texcoord;
out vec2 texCoordVarying;

void main(){
// [...]
    texCoordVarying = texcoord;
//[...]
}

in the fragment shader :

in vec2 texCoordVarying;

void main(){
// uses tex0 as a seperate texture 
    outputColor = texture(tex0, texCoordVarying);
}

The code for the shader are coming from the simpleTexturing example.
So if you were to use this example, the widthTexture and heightTexture to use for the proportion of the texture would be the height and width of the image used (ofImage img).

Hope this helps,

Best,

P

1 Like

• great work on the tutorials, love them - ,

thanks, that really means a lot

I had been thinking on using ofVboMesh just this morning, was wondering whether it was worth it. since I change the mesh positioning & the mode often [or that is at least planned], so wondered if its still usable.

I think im so used to having something in-between the begin() & end() & it just does it magically, that im just coming back to that point in openFrameworks :wink:

thanks though, ill have a look around & see what I can come up with

xxx

well… it seems as if I was 96% of the way to what I wanted. I did switch to ofVboMesh, since it felt it would fall into what I needed & since id be dealing with a bit more, then it felt the right option.

didn’t even have to do the addTexCoords, as I already had the addVertex. it has gotten the effect I was aiming to at least start with. which is great, because I was really worrying myself as to why it wasn’t working.
it seemed to be related to the shader itself. Im using ofxShader [since I want some sexy live coding with the shaders if need be]. but it was a really silly thing I missed, though no errors at all.

my next steps now is the co-ordinate system, so the shader bends to the shape of the ofVboMesh. so will look into the addTexCoord as you mentioned before.
now… just need to get onto that & solve that :wink:

again, thank you for the help
xxx

2 Likes

yes no probs!!

I’m glad it’s working. texCoords take a float as arguments so casting is important - although it’s an easy mistake to make, i made is several times thinking problem came from somewhere else… - .

++

1 Like

good tutor @lewislepton I am trying to figure out mesh+shaders as well for some live manipulation based on video. Would be grateful if you shared your source code for this work, kinda need all the inspiration that I can get on this topic

Hi,
take a look at the OF book
https://openframeworks.cc/ofBook/chapters/openGL.html
check there also the chapter about shaders and the examples in your_openframeworks_folder/examples/shaders there you will get a lot of info about how to deal with it.

1 Like