User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Problems after reflecting an object in a vertex shader. [solved]

Sun May 31, 2020 7:32 am

I have two hands in my current project

https://twitter.com/PeterOnion/status/1 ... 3998162944

The right hand is drawn using the same mesh as the left hand but with the x coordinate reversed in the vertex shader.

Code: Select all

	posn = a_position;
	posn.x *= u_mirrorX;
	posn =  (u_handRotate * posn) + u_handTranslate; 
It also reverses the x part of the normals in the same way.

u_mirrorX is 1 for left hand and -1 for right hand.

The result was fine until I turned on glCullFace(GL_BACK);

Now the right hand shows the wrong side of the faces as I assume the reflection has reversed the handedness of the triangles in the mesh.

My quick fix has been to temporarily swap to glCullFace(GL_FRONT); while drawing the right hand, but that feels like a bit of a kludge.

Is there a better way to fix this ?

PeterO
Last edited by PeterO on Mon Jun 01, 2020 12:46 pm, edited 1 time in total.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gbuzogany
Posts: 6
Joined: Mon May 25, 2020 7:34 pm

Re: Problems after reflecting an object in a vertex shader.

Sun May 31, 2020 5:59 pm

Yep, it must be the handedness. Nothing wrong with your fix, but the state update will cost some render time. If you are doing that because you have a memory bottleneck, you nailed it.
The easiest alternative is to have two models, one for each hand :)
A second alternative is to have a different index buffer for each hand, and correct the handedness in the index buffer.
A much harder and not-really-useful but viable alternative, if you are using GLES 3.0, is to read data from a texture on the VS and use that to displace the vertices the right amount to keep the handedness right.

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Problems after reflecting an object in a vertex shader.

Sun May 31, 2020 10:56 pm

You could also swap the winding order, by default front facing triangles have vertices in anti-clockwise order but if you flipped an axis then they will now be clockwise. It makes more sense to tell opengl that front is clockwise for those rather than telling it to cull front.

Code: Select all

glFrontFace(GL_CW);  // default is GL_CCW
She who travels light — forgot something.

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problems after reflecting an object in a vertex shader.

Mon Jun 01, 2020 7:57 am

Paeryn wrote:
Sun May 31, 2020 10:56 pm
You could also swap the winding order, by default front facing triangles have vertices in anti-clockwise order but if you flipped an axis then they will now be clockwise. It makes more sense to tell opengl that front is clockwise for those rather than telling it to cull front.

Code: Select all

glFrontFace(GL_CW);  // default is GL_CCW
Ok, I'll give that a try....<fires up emacs, makes the changes, runs make, runs the emulator> Yes, that works and as you say seems a bit more sensible.

Thanks.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problems after reflecting an object in a vertex shader.

Mon Jun 01, 2020 8:06 am

gbuzogany wrote:
Sun May 31, 2020 5:59 pm
The easiest alternative is to have two models, one for each hand :)
That will quickly get messy when I add additional hand poses.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gbuzogany
Posts: 6
Joined: Mon May 25, 2020 7:34 pm

Re: Problems after reflecting an object in a vertex shader.

Mon Jun 01, 2020 12:20 pm

PeterO wrote:
gbuzogany wrote:
Sun May 31, 2020 5:59 pm
The easiest alternative is to have two models, one for each hand :)
That will quickly get messy when I add additional hand poses.

PeterO
Did you read the other alternatives I wrote?

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: Problems after reflecting an object in a vertex shader. [Solved]

Mon Jun 01, 2020 12:36 pm

gbuzogany wrote:
Mon Jun 01, 2020 12:20 pm
PeterO wrote:
gbuzogany wrote:
Sun May 31, 2020 5:59 pm
The easiest alternative is to have two models, one for each hand :)
That will quickly get messy when I add additional hand poses.

PeterO
Did you read the other alternatives I wrote?
Did you read that I've chosen Paeryn's solution ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gbuzogany
Posts: 6
Joined: Mon May 25, 2020 7:34 pm

Re: Problems after reflecting an object in a vertex shader. [solved]

Mon Jun 01, 2020 2:43 pm

PeterO wrote: Did you read that I've chosen Paeryn's solution ?
PeterO
I was just not sure you saw, as you didn't react to them, nothing personal. Happy you found a solution!

Return to “OpenGLES”