Giter Club home page Giter Club logo

Comments (12)

javagl avatar javagl commented on August 27, 2024

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).

from obj.

daifrrr avatar daifrrr commented on August 27, 2024

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).

from obj.

javagl avatar javagl commented on August 27, 2024

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...

from obj.

daifrrr avatar daifrrr commented on August 27, 2024

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 :)

from obj.

javagl avatar javagl commented on August 27, 2024

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.

from obj.

daifrrr avatar daifrrr commented on August 27, 2024

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

from obj.

daifrrr avatar daifrrr commented on August 27, 2024

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?

from obj.

javagl avatar javagl commented on August 27, 2024

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.

from obj.

daifrrr avatar daifrrr commented on August 27, 2024

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

thank you very much :)

from obj.

javagl avatar javagl commented on August 27, 2024

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.

from obj.

javagl avatar javagl commented on August 27, 2024

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...

from obj.

javagl avatar javagl commented on August 27, 2024

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);

from obj.

Related Issues (19)

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.