Comments (12)
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.
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.
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
- https://blender.stackexchange.com/questions/68143/all-textures-upside-down
- https://www.gamedev.net/forums/topic/605768-obj-loader-texture-coordinates-upside-down/
- CesiumGS/obj2gltf#13
- https://stackoverflow.com/questions/5316529/opengl-texture-inverted
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.
Here are some pictures.
The "chalice.txt" is an *.obj.
Maybe you see the issue. Thank you for your support :)
from obj.
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.
I will try and give info :) do not wonder the chalice is pretty ugly, it was just for testing.
from obj.
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.
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.
one last thing:
...
texCoords.put(i * 2 + 1, 1.0f - y);
...
thank you very much :)
from obj.
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.
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.
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)
- Cannot load an MTL with MTL Reader, application crashes with NullPointerException HOT 5
- object not loading correctly HOT 43
- Invalid material names when leading white spaces are present HOT 3
- Obj with complex faces not loading correctly HOT 7
- Incorrect object normals HOT 5
- How to get transparency from mtl file using simple Wavefront OBJ file loader library and apply to the 3d object in opengl es android? HOT 4
- Large size obj's not loading its causes java.lang.OutOfMemoryError. HOT 10
- MtlReader wrong parses at least map_Kd tag HOT 2
- Is there any way to get calculated tangent and bitangent for normal mapping? HOT 2
- How do you use this? HOT 1
- Can I use this library for load obj and mtl files in Android for vuforia? HOT 3
- ignore me HOT 1
- Obj#setActiveGroupNames() fails if used on an immutable collection HOT 6
- The filename of mtllib written in the obj file is not used HOT 2
- Is really need the initial value (kd, ks, ka) of DefaultMtl HOT 2
- Propose: support physically-based rendering parameters in de.javagl.obj.Mtl HOT 6
- osm to obj HOT 2
- Java Modules HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from obj.