View Full Version : How can I reproduce this code in shader?
01-02-2007, 09:50 AM
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?
01-02-2007, 12:13 PM
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
01-02-2007, 12:29 PM
Thanks for the reply, I found my solution. There is a keyword called "discard" I can use in the fragment shader.
01-02-2007, 01:09 PM
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.
01-02-2007, 04:20 PM
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.
01-02-2007, 11:50 PM
you right, it's much better sollution :) , but the restrictions are the same :(
01-02-2007, 11:53 PM
If you want to discard triangle in vertex shader, why not to translate it into infinity? It will not be rasterized at all.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.