Can a mesh have Tangents and 2 Texcoords?(Solved)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by The_Glitch »

I've been using a light mapping solution but my bump mapping effects seem a little off. I just curious as I read in the past you can't have both on a mesh in Irrlicht which might explain why my shader looks better in Rendermonkey than once loaded into Irrlicht.

I'm using my Radiosity Normal Mapping project located here: http://irrlicht.sourceforge.net/forum/v ... =2&t=50404
Last edited by The_Glitch on Sun Jul 12, 2015 4:56 pm, edited 1 time in total.
Granyte
Posts: 848
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Can a mesh have Tangents and 2 Texcoords?

Post by Granyte »

in vanilla irrlicht 1.8 no

On the Shader-Pipeline branch yes but you have to create your vertex format your self.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?

Post by The_Glitch »

I use shader pipeline. Do you mean a custom vertex Descriptor? If so I have made one something still seems off though.
luthyr
Posts: 69
Joined: Wed Dec 30, 2009 5:47 pm

Re: Can a mesh have Tangents and 2 Texcoords?

Post by luthyr »

You may also want to consider just generating your tangents in the shader.

http://www.thetenthplanet.de/archives/1180
Granyte
Posts: 848
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Can a mesh have Tangents and 2 Texcoords?

Post by Granyte »

The_Glitch wrote:I use shader pipeline. Do you mean a custom vertex Descriptor? If so I have made one something still seems off though.
Yes you need to declare your own descriptor and create your own structure/class for the vertex buffer.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?

Post by The_Glitch »

Image
RenderMonkey Result Directional Lightmaps shader.


Image
Same Scene and shader in Irrlicht. Normal mapping doesn't look as good.



@Granyte I also have a custom structure I've been using also.

Code: Select all

 
 
struct S3DLightMap : public S3DVertex
    {
        //! default constructor
        S3DLightMap() : S3DVertex() { }
 
        //! constructor
        S3DLightMap(f32 x, f32 y, f32 z, f32 nx = 0.0f, f32 ny = 0.0f, f32 nz = 0.0f,
            SColor c = 0xFFFFFFFF, f32 tu = 0.0f, f32 tv = 0.0f, f32 tu2 = 0.0f, f32 tv2 = 0.0f,
 
            f32 tanx = 0.0f, f32 tany = 0.0f, f32 tanz = 0.0f,
            f32 bx = 0.0f, f32 by = 0.0f, f32 bz = 0.0f)
            : S3DVertex(x, y, z, nx, ny, nz, c, tu, tv), Tangent(tanx, tany, tanz), Binormal(bx, by, bz), TCoords2(tu2, tv2) { }
 
        //! constructor
        S3DLightMap(const core::vector3df& pos, SColor c,
            const core::vector2df& tcoords)
            : S3DVertex(pos, core::vector3df(), c, tcoords), TCoords2(tcoords) { }
 
        //! constructor
        S3DLightMap(const core::vector3df& pos,
            const core::vector3df& normal, SColor c,
            const core::vector2df& tcoords,
            const core::vector2df& tcoords2,
            const core::vector3df& tangent = core::vector3df(),
            const core::vector3df& binormal = core::vector3df())
            : S3DVertex(pos, normal, c, tcoords), Tangent(tangent), Binormal(binormal) { }
 
 
        //! Second set of texture coordinates
        core::vector2d<f32> TCoords2;
 
        //! Tangent vector along the x-axis of the texture
        core::vector3df Tangent;
 
        //! Binormal vector (tangent x normal)
        core::vector3df Binormal;
 
        bool operator==(const S3DLightMap& other) const
        {
            return ((static_cast<S3DVertex>(*this) == other) &&
                (Tangent == other.Tangent) &&
                (Binormal == other.Binormal));
        }
 
        bool operator!=(const S3DLightMap& other) const
        {
            return ((static_cast<S3DVertex>(*this) != other) ||
                (Tangent != other.Tangent) ||
                (Binormal != other.Binormal));
        }
 
        bool operator<(const S3DLightMap& other) const
        {
            return ((static_cast<S3DVertex>(*this) < other) ||
                ((static_cast<S3DVertex>(*this) == other) && (Tangent < other.Tangent)) ||
                ((static_cast<S3DVertex>(*this) == other) && (Tangent == other.Tangent) && (Binormal < other.Binormal)));
        }
 
        E_VERTEX_TYPE getType() const
        {
            return EVT_TANGENTS;
            return EVT_2TCOORDS;
        }
 
        S3DLightMap getInterpolated(const S3DLightMap& other, f32 d)
        {
            d = core::clamp(d, 0.0f, 1.0f);
            return S3DLightMap(Pos.getInterpolated(other.Pos, d),
                Normal.getInterpolated(other.Normal, d),
                Color.getInterpolated(other.Color, d),
                TCoords.getInterpolated(other.TCoords, d),
                TCoords2.getInterpolated(other.TCoords2, d),
                Tangent.getInterpolated(other.Tangent, d),
                Binormal.getInterpolated(other.Binormal, d));
        }
    };
 
 
 
 
    video::IVertexDescriptor* lightmapping_vertex = driver->addVertexDescriptor("For Light mapped Static Meshes");
 
    lightmapping_vertex->addAttribute("Position", 3, video::EVAS_POSITION, video::EVAT_FLOAT, 0);
    lightmapping_vertex->addAttribute("TexCoord0", 2, video::EVAS_TEXCOORD0, video::EVAT_FLOAT, 0);
    lightmapping_vertex->addAttribute("TexCoord1", 2, video::EVAS_TEXCOORD1, video::EVAT_FLOAT, 0);
    lightmapping_vertex->addAttribute("Normal", 3, video::EVAS_NORMAL, video::EVAT_FLOAT, 0);
    lightmapping_vertex->addAttribute("Tangent", 3, EVAS_TANGENT, EVAT_FLOAT, 0);
    lightmapping_vertex->addAttribute("Binormal", 3, EVAS_BINORMAL, EVAT_FLOAT, 0);
 
 
 
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by The_Glitch »

My scene is using HDR Post Processing effect, which was basically brightening up the scene.
Granyte
Posts: 848
Joined: Tue Jan 25, 2011 11:07 pm
Contact:

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by Granyte »

Are you using your own shader? Irrlicht has not material that does both light mapping and bump mapping
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by The_Glitch »

Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by Mel »

If you're not using the vertex colors, you can use them to pack a secondary texture coordinates set there, whenever the texture coordinates are on the range [0,1]

If you're generating lightmaps, the good news is that this texture set uses to be in that range, so you just have to write them into the colors, for example, as this:

Code: Select all

 
//UV MUST be in the range [0,1]. or else THIS WON'T WORK! D:
SColor packTextureCoordinates(vector2df UV)
{
SColor retValue;
u32 r,g,b,a;
f32 x1,x2;
 
x1 = UV.X*255.0;
x2 = 255.0 - x1;
 
r = floor(x1);
g = floor(x2*255.0); //precission loss here, yes! D: but it is hardly noticeable.
 
x1 = UV.Y*255.0;
x2 = 255.0 - x1;
 
b = floor(x1);
a = floor(x2*255.0); //precission loss again :P
 
 
return retValue;
}
 
And store the returned value into the vertex structure color.

Then, in your shader, the values of the colors are normalized, so to un pack the proper values, you have to do this:

Code: Select all

 
float2 unpackTextureCoordinates(float4 vertColor)
{
return (float2(vertColor.x+vertColor.y/256.0 , vertColor.z+vertColor.w/256.0));
}
 
The precission loss is the same as converting from a 32 bit float value into a 16 bit float value, something only noticeable for VERY VERY VERY (65536 pixels wide) VERY VERY large textures

This is how i did this lightmapping with normals
Image
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by The_Glitch »

It working fine now Mel, I'll consider what you said and try that at some point in the future.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by Vectrotek »

Thanks Mel! This works really well!
For the slight loss I unwrapped my UVs in Blender on a
256 x 256 image with "Snap to Pixel" option.
I then baked the Light Map onto a bigger image with those
same UVs and it somehow seemed to improve the
result.
The_Glitch
Competition winner
Posts: 523
Joined: Tue Jan 15, 2013 6:36 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by The_Glitch »

what's the advantage of putting the 2nd uvs in the vertex colors?
So is your light map limited to the size of 256 x 256?
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by Vectrotek »

@ The Glitch..
I think because there isn't a (standard) Vertex Format that can handle 2TCoords AND Tangents & Binrormals..
Last edited by Vectrotek on Tue May 17, 2016 6:11 am, edited 1 time in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Can a mesh have Tangents and 2 Texcoords?(Solved)

Post by Vectrotek »

Another version..

Code: Select all

 
 SColor packTextureCoordinates(vector2df UV)
  {SColor retValue;
   retValue.setRed  (u32(0.00390625 * floor(65535.0 * UV.X)));
   retValue.setGreen(u32(16776960.0 * UV.X - 256.0 * floor(65535.0 * UV.X)));
   retValue.setBlue (u32(0.00390625 * floor(65535.0 * UV.Y)));
   retValue.setAlpha(u32(16776960.0 * UV.Y - 256.0 * floor(65535.0 * UV.Y)));
   return retValue;
  }
 
Last edited by Vectrotek on Tue May 17, 2016 2:32 pm, edited 6 times in total.
Post Reply