# How can I reproduce this code in shader?

I have this piece of code that check if a triangle is facing the light, if true, draw, else don’t draw. What’s the best way to do this in a shader?

If you have can use the geometry shaders then this would be a simple thing as you have access to all the relevant data and tools for working with polygons.

However 99% of all computers still does not have this capability.
so if you want to discard them in shaders you have to discard each fragment individually since you can’t discard vertics.
It’s a bit complicated, but it could work if you have really heavy shaders

Thanks for the reply, I found my solution. There is a keyword called “discard” I can use in the fragment shader.

what about send 3 streams of vertices down the vertex pipeline, where only the first stream represents the real vertex position and the second+third represent the other vertices of triangle. Then in vertex shader you can simply use the formula for calculating if the triangle is facing light or not. If it isnt, then set W coordinate of vertex to 0. This will hapend on all the three vertices forming one triangle and effectively culling out the triangle. But if you are vertex bound or trafic bound(I mean PCI-E, AGP trafic) this wont help and ofcourse you cant use indices to index vertex array, only vertices can be processed this way, so no vertex caching . But I think, if you are using a long fragment shader, it will help a lot.

actually you only need to send the normal of the polygon to all 3 vertics, and for that you can use an extra set of texture coordinates, and that works well with VBO’s.

it’s also advisable to do most of the math in the vertex shader and just discard in the fragment shader.

you right, it’s much better sollution , but the restrictions are the same

If you want to discard triangle in vertex shader, why not to translate it into infinity? It will not be rasterized at all.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.