Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why reverse the element order when sampling from normal map #44

Closed
unknownue opened this issue Feb 5, 2020 · 1 comment
Closed

Why reverse the element order when sampling from normal map #44

unknownue opened this issue Feb 5, 2020 · 1 comment

Comments

@unknownue
Copy link

Here is the code of sampling normal from normal map:

tinyrenderer/model.cpp

Lines 86 to 93 in 907bb56

Vec3f Model::normal(Vec2f uvf) {
Vec2i uv(uvf[0]*normalmap_.get_width(), uvf[1]*normalmap_.get_height());
TGAColor c = normalmap_.get(uv[0], uv[1]);
Vec3f res;
for (int i=0; i<3; i++)
res[2-i] = (float)c[i]/255.f*2.f - 1.f;
return res;
}

I can't understand why the element order of normal is reversed, that is, why not write the code like this:

for (int i=0; i<3; i++)
    res[i] = (float)c[i]/255.f*2.f - 1.f;
@ssloy
Copy link
Owner

ssloy commented Feb 5, 2020

It is a simple question of color packing in TGA images; they are stored in the BGR order and not RGB.

@ssloy ssloy closed this as completed Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants