How Quad can be visible both sides

Problems building or running the engine, queries about how to use features etc.
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

How Quad can be visible both sides

Post by Herb »

I'm trying to understand how to duplicate the triangles in a manual object (which is just a simple quad) to have both sides render... Here's my test code snippet (quad - 2 triangles to show up red):

Code: Select all

    Obj->begin("foo", Ogre::RenderOperation::OT_TRIANGLE_LIST);    
    
    Obj->position(0, 0, 10);
    Obj->colour(Ogre::ColourValue::Red);
    Obj->position(0, 0, 0);
    Obj->colour(Ogre::ColourValue::Red);
    Obj->position(0, 10, 0);
    Obj->colour(Ogre::ColourValue::Red);
    Obj->position(0, 10, 10);
    Obj->colour(Ogre::ColourValue::Red);
    
    Obj->triangle(0,1,2);
    Obj->triangle(2,3,0);

    Obj->end();
You can see a red square from one side, and it's invisible from the other side. :?

I understand you can add

cull_software none
cull_hardware none

to your material script to make it work, but there are drawbacks to performance, lighting, etc.... I saw in an old post sinbad indicating you need to draw triangles for the other side as well for it to show-up. I'm struggling to understand how to do this on my manual object, can anyone give me some tips?
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: How Quad can be visible both sides

Post by Kojack »

If you have no lighting, the easy way is to just double up every triangle call with the indices in reverse order:

Code: Select all

Obj->triangle(0,1,2);
Obj->triangle(2,1,0);
Obj->triangle(2,3,0);
Obj->triangle(0,3,2);
That should make them visible from both sides without changing the culling settings.

However there won't be lighting. Your manualobject has no normals in the vertices, so there's no lighting anyway. But if you did really want lighting then you need to double every vertex, one with a forwards normal and one with a backwards normal.

Or in vertex shader 3.0 and above there's a variable that tells you if you are rendering the front or back of a triangle (if culling is turned off). That can be used to invert the normal when rendering the back face, so you don't need double vertices or triangles.
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: How Quad can be visible both sides

Post by Herb »

Awesome. Thanks for the great explanation. :)
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: How Quad can be visible both sides

Post by Herb »

@Kojack or anyone else, I'm have quads rendering by doubling the vertexes as described. It works, but I'd like to get the triangle / vertex count down. Any idea what this vertex shader 3.0 variable is described by Kojack two posts back? Finally getting around to optimizing, so had to revive this older thread of mine. :wink:
scrawl
OGRE Expert User
OGRE Expert User
Posts: 1119
Joined: Sat Jan 01, 2011 7:57 pm
x 217

Re: How Quad can be visible both sides

Post by scrawl »

In GLSL you would use the gl_FrontFacing builtin variable.
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: How Quad can be visible both sides

Post by Herb »

Ok, so not has easy a parameter to a material :roll:

So...here's what I'm understanding. You don't see the other side of the triangle due to back-face culling. Makes sense. But, gl_FrontFacing would only tell me the orientation of a primitive. So, I see I could use that to tell if I'm front or back facing. But, what in the shader causes it not to be culled (ie what would I call when I found gl_FrontFacing telling me I'm on the back side)? I also see that I'd need to take the negative normal from the front face as well, correct?
User avatar
Herb
Orc
Posts: 412
Joined: Thu Jun 04, 2009 3:21 am
Location: Kalamazoo,MI
x 38

Re: How Quad can be visible both sides

Post by Herb »

Okay, maybe I'm getting this a bit more as I think about it... I "keep" culling on in the material script. When in the shader use the gl_FrontFacing to check for back-facing, which in that case would multiply the normal by negative one. Is this the right direction?