Skip to main content
GameDev.net gamedev.net
🔒 Locked

dot product and projection matrix

Started by giugio Sep 7, 2011 at 12:59 PM 6 replies 2k views
Original Post
giugio
giugio
hello.
For each triangle in my mesh i do the dot product of -normal of the triangle and camera vector(in the shader).
I do this because when the result of the dot product inform me of the front/back face related to the camera.
If the result of the dot product for example is 0 mean that the normal is perpendicular of my camera vector, then i must change to drawing from front face(for example red)and start to drawing back face(for example yellow if the two face colors are red and yellow).

My problem is this:
when the faces or triangles are far away all work fine, but when the faces are near the camera the change of back/front face is incorrect, i think that this may be relative to the projection matrix, but if this is true , how i can correct?
I can multiply the camera vector for the projection matrix?
thanks.
krienie
krienie
maybe you should post some code. Like how you create your perspective matrix and the face drawing code.
dpadam450
dpadam450
Try outputting the view vector (camera vector) in your shader and then move forward in your scene to make sure it does not change (only move forward though).
NBA2K, Madden, Maneater, Killing Floor, Sims 
giugio
giugio
hello.
I post the shader and the matrix that i use for multiply the normal:

<script id="DoubleColorf" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
uniform vec3 CameraModelPosition;
uniform vec3 ColorFront;
uniform vec3 ColorBack;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
varying vec3 vNormal;
varying vec3 transformedNormal;


void main(void) {
float Dir = dot(transformedNormal,CameraModelPosition);

if(Dir >0.0){
gl_FragColor = vec4(ColorFront.xyz,1.0);
}
else {
gl_FragColor = vec4(ColorBack.xyz,1.0);
}
}
</script>

<script id="DoubleColorv" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat3 uNMatrix;

uniform vec3 uAmbientColor;
uniform vec3 uLightingDirection;
uniform vec3 uLightingDirection2;
uniform vec3 CameraModelPosition;

uniform vec3 ColorFront;
uniform vec3 ColorBack;
varying vec3 transformedNormal;
varying vec3 vLightWeighting;
varying vec3 vLightWeightingback;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);

vec3 uDirectionalColor = vec3(1,1,1);

transformedNormal = uNMatrix * aVertexNormal;

float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0) ;
vLightWeighting = uDirectionalColor * directionalLightWeighting * ColorFront ;

float directionalLightWeighting2 = max(dot(transformedNormal, uLightingDirection2), 0.0) ;
vLightWeighting += uDirectionalColor * directionalLightWeighting2 * ColorFront ;

float directionalLightWeightingback = max(dot(transformedNormal, uLightingDirection), 0.0) ;
vLightWeightingback = uDirectionalColor * directionalLightWeightingback * ColorBack ;

float directionalLightWeighting2back = max(dot(transformedNormal, uLightingDirection2), 0.0) ;
vLightWeightingback += uDirectionalColor * directionalLightWeighting2back * ColorBack ;
}
</script>

uNMatrix is calculated from the modelview matrix(this.m_MVmatrix) like:


this.m_InvMatrix = mat3.create();
mat4.toInverseMat3(this.m_MVmatrix, this.m_InvMatrix);
mat3.transpose(this.m_InvMatrix);


the mesh is displayed uncorrectly , there are some change of front/back face when i move the camera .
thanks
theZapper
theZapper
Can you not just use [font=Arial, Lucida, sans-serif]gl_FrontFacing in the pixel shader?[/font]
---When I'm in command, every mission's a suicide mission!
Kian
Kian
As the above poster said, the graphic card should know which side is showing. That's how it performs back face culling after all.

But even if you're unsure of how to use this property, you shouldn't be using the fragment shader to choose the color. The fragment shader is going to get run once per fragment, and a triangle will have several fragments. Since you only ever see one side of the triangle, there's no way one fragment of the triangle will show the front color and another the back. Thus, you should be working out which side you're on in the vertex program, working it out once, and pass the result to the fragment shader. Even better, rather than set the color of each fragment, set the colors of the vertices and pass that along to the fragment shader.
giugio
giugio
yes , yours are right i'm understand.
rather it occurs to me that in my project i do a global transformation of all meshes multiply it for the parent world transform.
I do this for have all the geometri in the same matrix coords , then i group the geometry for material, i do this for do minus gpu calls.
Indeed sketchup have a lot of simple faces not complex meshes.
I must transform also the normal by the parent world?
Now i'm not multipling the normal by the world matrix in this process.
thanks

Topic Locked

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

Sign in to reply to this topic.