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

my Model stands head #3

Closed
daifrrr opened this issue Nov 14, 2017 · 12 comments
Closed

my Model stands head #3

daifrrr opened this issue Nov 14, 2017 · 12 comments

Comments

@daifrrr
Copy link

daifrrr commented Nov 14, 2017

Hi,

thank you for your Loader. Works fine,

but:

maybe you have an idea why my model stands head.
I am using the plain arrays from the ObjFile.
Another question would be: the parameter dimensions in the ObjData.getTexCoordArrays(obj, dimension) is for the format of my Texture Coordinates (in my case 2).

Thank you very much!

@javagl
Copy link
Owner

javagl commented Nov 14, 2017

Did you confirm that the model is displayed properly in other viewers (for example: Blender or MeshLab)? Or can you upload/provide the model, so that others could check it?

Regarding the second question: I'm not sure whether I understood this right, but you usually pass in 2 as the dimensions parameter.

(In OBJ, texture coordinates could have 1 or 3 dimensions as well, and although I never encountered this, I wanted to have it covered, just in case).

@daifrrr
Copy link
Author

daifrrr commented Nov 15, 2017

Thanks for the fast answer.
I created the model in Maya 2017.
I really cant tell if the model stands head or if it is just the texture i apply.
The model is just a squere and i uv-mapped a worldmap on it.
I am trying another model (totally forget this option).

@javagl
Copy link
Owner

javagl commented Nov 15, 2017

So in this case, it is almost certainly not the model that is flipped, but only the texture.

Unfortunately, flipped textures are distressingly common - Google "texture flipped vertically bug" brings maaany results, and things like

show that this always causes trouble in the Blender/OBJ/OpenGL chain.

I think the main reason is in the way how OpenGL handles textures and texture coordinates, but more about this can be read in a recent issue from glTF: KhronosGroup/glTF#1021


However, there are several options for solving this:

  • Not recommended: You could flip your texture images (i.e. the PNG or JPG files) vertically

  • Better: You could adjust your renderer code. E.g. you could do something like

    vec4 color = texture2D(sampler, vec2(texCoord.x, 1.0 - texCoord.y));

    in your GLSL shader code

  • Possibly not practical: You could flip the texture coordinates of the model (i.e. in the OBJ file)

  • You could flip the texture coordinates in the Obj object. (I could add a few lines of code later, to show how this could be done)


In fact, this issue is so common and so annoying that I'd rather consider adding special versions of the getTexCoords methods in the ObjData class: I could add a flipY parameter, so that it's easy to directly obtain the texture coordinates in a form that is "common" for OpenGL...

@daifrrr
Copy link
Author

daifrrr commented Nov 15, 2017

maya
program
chalice
chalice.txt

Here are some pictures.
The "chalice.txt" is an *.obj.

Maybe you see the issue. Thank you for your support :)

@javagl
Copy link
Owner

javagl commented Nov 15, 2017

It's hard to be sure about this just from the images. But again, I guess it's just the usual y-flip. In this case, you could try to apply this function

private static void flipTexCoordsVertically(FloatBuffer texCoords)
{
    int n = texCoords.capacity();
    for (int i=0; i<n / 2; i++)
    {
        float y = texCoords.get(i * 2 + 1);
        texCoords.put(i * 2 + 1, 1.0f - y);
    }
}

(Updated based on the fixes discussed below)

to your texture coordinates, like this:

    FloatBuffer texCoords = ObjData.getTexCoords(obj, 2);
    flipTexCoordsVertically(texCoords);

(if you're obtaining them as an array, you can just pass a FloatBuffer.wrap(yourArray) to the method).

If this fixes the issue, I'll consider adding methods that do this transparently, as in

public static FloatBuffer getTexCoords(..., boolean flipVertically)

for the next release.

@daifrrr
Copy link
Author

daifrrr commented Nov 16, 2017

I will try and give info :) do not wonder the chalice is pretty ugly, it was just for testing.

@daifrrr
Copy link
Author

daifrrr commented Nov 16, 2017

your method does not work for me - "IndexOutOfBoundException".

My model is textured correct in my game now. I just added a minus in front of the y coordinate ("vt") manually and now it looks like textured correct, but i do not know why .. is it interpretated as 1.0f-y because negative UV-coords?

@javagl
Copy link
Owner

javagl commented Nov 16, 2017

Sorry, the for-loop should have been different:

private static void flipTexCoordsVertically(FloatBuffer texCoords)
{
    int n = texCoords.capacity();
    for (int i=0; i<n / 2; i++)
    {
        float y = texCoords.get(i * 2 + 1);
        texCoords.put(i * 2 + 1, 1.0f - y);
    }
}

(Updated based on the fixes discussed below)

The fix that you described most likely works "accidentally", because of a certain texture wrapping mode: https://webglfundamentals.org/webgl/webgl-3d-textures-repeat-clamp.html

This will not work in all cases.

@daifrrr
Copy link
Author

daifrrr commented Nov 17, 2017

one last thing:
...
texCoords.put(i * 2 + 1, 1.0f - y);
...

thank you very much :)

@javagl
Copy link
Owner

javagl commented Nov 18, 2017

Yes, I should have tested this properly locally, instead of just sketching the code here. Sorry about that.

So eventually, this should work:

private static void flipTexCoordsVertically(FloatBuffer texCoords)
{
    int n = texCoords.capacity();
    for (int i=0; i<n / 2; i++)
    {
        float y = texCoords.get(i * 2 + 1);
        texCoords.put(i * 2 + 1, 1.0f - y);
    }
}

I'll leave this issue open until I decided whether or how to integrate the optional flipping in the ObjData class.

@javagl
Copy link
Owner

javagl commented Nov 27, 2017

I've been hesitating a bit here.

This "texture coordinate flipping" is actually only a workaround: Most image loaders provide the image data with the first pixel being at the upper left. OpenGL assumes that the first pixel is at the lower left. (For whatever reason. I mean, they really screwed this one up, didn't they?). So the "cleanest" solution would usually be to flip the image data, so that it matches the expectation of OpenGL.

However, even if the texture coordinates are supposed to be flipped, I wasn't sure whether it would be more appropriate to do this via an optional/additional parameter, like this:

public static FloatBuffer getTexCoords(
    ReadableObj obj, int dimensions, boolean flipY)
                                     ^------------^

or whether to offer a flipTexCoordsVertically method. This would make a smaller and somewhat cleaner API. But the problem with the latter is that it does not know about the texture coordinate dimensions. Although I never have encountered 1D or 3D texture coordinates in the real world, this would still be dubious.

However, the next release will have the flipY parameter as described above. Maybe this helps to resolve this distressingly common flipping issue...

@javagl
Copy link
Owner

javagl commented Jan 12, 2018

The new method for obtaining the texture coordinates with flipped y-coordinates is part of version 0.3.0:

boolean flipY = true;
float texCoords[] = ObjData.getTexCoordsArray(obj, 2, flipY);

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