Skip to main content
gamedev.net
🔒 Locked

two face

Started by giugio Sep 4, 2011 at 5:45 PM 10 replies 2.2k views
Original Post
giugio
giugio
hello .
I'm trying to create a front/back material system.
I have this code for check if a face is in front or back to the camera:
i calculate the parameter in the vertex shader.
with(in pseudo code):
vec3 transformedNormal = uNMatrix * aVertexNormal;// in the vertex shader uNMatrix is the inverse word matrix

float Dir = dot(transformedNormal,CameraModelPosition);//in the pixelShader

//then i do this test in the fragment shader:
if(Dir > (CameraModelPosition.z)-DELTA){
Out.Color......= vector4(ColorFront.xyz,1.0);
}
else {
Out.color ...... = vector4(ColorBack.xyz,1.0);
}


and all works fine with big DELTAs , but how must be big the Delta? is correct the inserting of CameraModelPosition in the test of Dir?
my problem is solved if there is a mathematical formula for do this.
There is the FACE params in hlsl , i not remember fine, but is related to the winding order and that is bad for my problem.
i i specify that i'm not interested the winding order of the triangles , i would only know if a face is a front face or a back face to the camera.
I would use this for example in a two face (with front color and back color for example) , if i look at it when is in front of the camera position display a color(the front color) and if i rotate the camera of 180 degree show the back color
thanks.



Thanks.
B_old
B_old
Why is the [font="'Courier New"]SV_IsFrontFace[/font] parameter not working for you? You could use it to achieve what you are describing. Rendering a certain color when looking at a surface from the front and rendering a different color when looking at it from behind.
giugio
giugio

Why is the [font="Courier New"]SV_IsFrontFace[/font] parameter not working for you? You could use it to achieve what you are describing. Rendering a certain color when looking at a surface from the front and rendering a different color when looking at it from behind.


thanks.
But my problem is another , sorry if i'm explain bad.
I would know in the fragment shader if the face is in front of my camera or back.
I do not care of the winding order(like SV_IsFrontFace), only of the front / back, is for this that i wouldn't use gl_frontfacing but something like :
float Dir = dot(-transformedNormal,CameraModelPosition);
if(Dir > 0) draw as front face
if(Dir <0) draw back as back face

but i don't know how use it correctly , because some times this calculation is not correct , and some faces that should be front are drawed as back.
is possible to use a delta for correct the instruction?
how?
undead
undead
It looks like you are checking the vertex normal, you should check the polygon normal and you won't need delta anymore.

EDIT:

maybe I didn't get what you need... do you need to know if a polygon is BEHIND the camera?
undead
undead
Anyhow if you need to know if a point is behind the camera do this.

Get the camera plane using camera position and view vector (normalize it).

You create a plane equation this way.

http://en.wikipedia....a_normal_vector

Once you have Ax + By + Cz = D substitute (x,y,z) with your vertex position.

If D == 0 then point lies in the plane.
If D < 0 then point is behind camera plane
if D > 0 then point is in front of camera plane

Since you talk about drawing and front/back material system I don't get how can you draw something behind the camera but maybe I didn't understand what you mean.
B_old
B_old
I also don't understand the problem.
Wanting to know whether a face is behind the camera makes no sense in the shader, so I assume that is not what you want.
The winding order is what is used to determine whether a face is facing the camera or not, so I don't understand how you don't care about it. You must have cared about it when calculating the normal of your face, right?
giugio
giugio
sorry ,i already open another discussion in the xna forum of microsoft ,the link is this:
link
i'm not understand at all , so i ask to you.
thanks.
B_old
B_old
Hm, I'm starting to get really confused about what this topic is about. But I recommend that you give gl_frontfacing a try with regard to your very first question. If you then come to find that it really doesn't give you what you want, maybe it is easier to explain what you actually are looking for.
giugio
giugio

Hm, I'm starting to get really confused about what this topic is about. But I recommend that you give gl_frontfacing a try with regard to your very first question. If you then come to find that it really doesn't give you what you want, maybe it is easier to explain what you actually are looking for.

yes , b_old , i'm creating a sketchup importer in c++ for opengl.
Sketchup export faces and faces have four type of materials , for each one i have a shader:
1)texture front , texture back
2)texture front, color back
3)color front, texture back
4)and color front / color back.

so
if I have my camera is in front of face with material 1 i would see the texture front.
if I have my camera is in back of face with material 1 i would see the texture back.
if I have my camera is in front of face of face with material 3 i would see the color front.
ecc....

i would know if the camera is in front or back for switch the fragment shader draw.

for ex, for the material 4 pixel shader


float Dir = dot(transformedNormal,CameraModelPosition);//in the pixelShader

//then i do this test in the fragment shader:
if(Dir > 0){ //i'm in front of the face then i draw front color
Out.Color......= vector4(ColorFront.xyz,1.0);
}
else {
//i'm back to the facee then i draw back color

Out.color ...... = vector4(ColorBack.xyz,1.0);
}




is completly wrong?
I do this beecause there are some models that have alternated winding order , so the faces are displayed front and back , not only front because all are these faces are in front of the camera, but with windong orders that derive from gl_frontfacing in opengl of FACE in cg
thanks.
B_old
B_old
If I understand you correctly this is exactly what you could use gl_FrontFacing for.
giugio
giugio

If I understand you correctly this is exactly what you could use gl_FrontFacing for.


Thanks B_old , i show you an example with two color, front = red yellow = back , in this model the faces in front of the camera have different winding order ,
and this is the gl_frontfacing result:
Immagine.jpg

i would render the model with the same color front , but for the winding order i get mixed content
How i can omit the winding order if is front / back ? and get the result for front /back face related to the camera?
thanks.
B_old
B_old
This puzzles me. What is the point of having faces with ever changing winding order?

Anyway, if you do have access to the face-normal you could use it to determine which color to use, the way you planned to. But I wonder where these normals come from, because the calculation of those is also dependent on the winding order of the face. So checking the normal would give the same results. After all the front/back is defined by the winding order!

Maybe the sketchup format stores some additional information with its faces?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.