Tutorial: OpenGL / GLSL flat shading using an indexed ofMesh

Hi everyone,

I wrote a brief walk-through on how to flat-shade an indexed ofMesh using a GLSL flat phong shader and provoking vertices.

Here’s a link to the article: http://poniesandlight.co.uk/notes/flat-shading-on-osx/

And here’s a link to the demo project I wrote, if you’d like to jump ahead: http://github.com/tgfrerer/flatShadedMeshExample/tags

Works excellently on wilful OS X.

I’d be interested to hear if the shader directive “GL_EXT_gpu_shader4” is supported (and hardware-accelerated) on Linux and Windows, too!

1 Like

thanks for this, not enough good shader tutorials out there!

Hi there,

here’s a sketch I’ve made recently that uses flatshading in tandem with openGL’s glDrawElementsInstanced(). It’s interesting how much performance you can get out of this.


I’ve made the instanced drawer into an addon with the above project as the Example Project.

1 Like

Nice example!

I looked at your code and there is one step that I don’t get. In your instanced.vert shader, you declare a uniform for the texture containing the coordinates

uniform sampler2DRect rnm;  

but in your code you never set the uniform

mShdInstanced->setUniformTexture("rnm", mFboLength[mPingPong], 0);  

Still, it works without that line. It seems that the shader is using the last texture the GPU has written on. Is that correct? I am a little bit confused!

Hey @dantam, thanks for pointing this out!

You are right, the shader (re-)uses the last texture that was bound in cpp, since openGL doesn’t really care about texture names, but only cares about its internal state. Since the texture was bound just in a step earlier, we technically don’t need to bind (use setUniformTexture) it again, which could be seen as a quick & dirty optimisation (but it really was just me being a bit sloppy and not noticing it

The clean way to do it would be how you suggest in listing 2.