Coder Social home page Coder Social logo

Probably mistake in lookat about tinyrenderer HOT 15 CLOSED

wavvs avatar wavvs commented on May 21, 2024
Probably mistake in lookat

from tinyrenderer.

Comments (15)

ssloy avatar ssloy commented on May 21, 2024

No, that is not a mistake. The goal is to compute a change of coordinates where the center becomes origin and the eye lies on the z-axis. So we subtract center and not eye.

from tinyrenderer.

wavvs avatar wavvs commented on May 21, 2024

@ssloy , Can you explain why other implementations subtract eye instead center? Is it because you have only 1 object on scene?

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

@wavvs, can you give me a link to another implementation so I could check it?

from tinyrenderer.

wavvs avatar wavvs commented on May 21, 2024

@ssloy, https://github.com/g-truc/glm/blob/aa38362b2c80d099a5e3b1645f7d9b2decf5e4bd/glm/gtc/matrix_transform.inl#L754
https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml
https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

@wavvs, thank you. There is no mistake. The difference is I suppose that the object is centered at the origin and the camera is placed at the z-axis facing origin.

The links you gave me suppose the opposite:

Remember that the camera in its default position is assumed to be centred at the origin and aligned along the negative z-axis.

Check the very beginning of their code:

vec<3, T, Q> const f(normalize(center - eye));

Where I do

Vec3f z = (eye-center).normalize();

So at the end it boils down to the same thing.

from tinyrenderer.

wavvs avatar wavvs commented on May 21, 2024

They do vec<3, T, Q> const f(normalize(center - eye));, but take negative forward vector Result[0][2] =-f.x;, and you take eye - center and take positive forward vector in right-handed system, so yes, it's the same.
So you place object at the origin of camera space coordinates, not camera itself. Do I understand right?

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

Yes, that's right.

from tinyrenderer.

wavvs avatar wavvs commented on May 21, 2024

@ssloy, thank you!

from tinyrenderer.

mcejp avatar mcejp commented on May 21, 2024

I am also seeing some unexpected camera behavior -- for example, moving the eye further away from center does not zoom the scene out.

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

I am also seeing some unexpected camera behavior -- for example, moving the eye further away from center does not zoom the scene out.

It is not a bug, it is a feature. The job of the lookat function is to place the camera at the Oz axis, nothing more (e.g. for a parallel projection).
The actual distance between the origin and the camera is set in the perspective matrix.

from tinyrenderer.

mcejp avatar mcejp commented on May 21, 2024

Fair enough, but would you agree that this makes your lookAt different from what is the "usual convention"?

By no means am I trying to say that it is wrong, let alone that you should change anything, I just disagree with your summary that So at the end it boils down to the same thing, which I find quite misleading :-)

In any case, thank you for your great work!

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

I am not sure what you mean when you say "the usual convention". If I am not mistaken, the standard gluLookAt() function does not set the perspective projection, you need to call gluPerspective() for that. I emulate exactly this behaviour.

However, yes, indeed, it would make sense to set the object-to-camera distance directly from lookat(), since we have this information. But it is a non-standard behaviour as far as I know :(

from tinyrenderer.

mcejp avatar mcejp commented on May 21, 2024

The usual convention (LookAt in GLU & glm) includes translating the scene away from the camera by length(eye - center).
gluPerspective does not even have the camera distance as an argument; disregarding the clipping that we don't care about here, the only input is FOV which would be fixed most of the time. How could the camera position be taken into consideration in gluPerspective?

from tinyrenderer.

ssloy avatar ssloy commented on May 21, 2024

Check the code from the guys who define what OpenGL is:
https://www.khronos.org/opengl/wiki/GluLookAt_code, esp the line:
NormalizeVector(forward);

gluPerspective() does not have the camera distance as the argument, however it does have the field of view argument that is equivalent to the distance.

from tinyrenderer.

mcejp avatar mcejp commented on May 21, 2024

My bad, it is not length(eye - center) that matters, only eye (e.g. every vertex is pushed away from the camera proportionally to its distance from the eye plane).

I was still not able to figure out how to convert from a conventional (eye, center, FOV°) tuple to (eye', center', factor) that would achieve the same result in Tinyrenderer, but I was able to work around by just including glm and using its implementation of the matrix calculations :-)

from tinyrenderer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.