I have an issue when I try to bind texture to a 3D model in OpenGL ( I don't have this issue with blender). The texture is correctly placed on the model except for some regions. The problem is visible on the edges of the model:
in Blender I can see that the problem is tight where the model is cut:
I verified that I read correctly the vt fields in my .obj file, then from the face lines I link each vertex to the appropriate texture coordinates.
The code I use to bind the texture is:
glBindTexture(GL_TEXTURE_2D, _texture.getTextureID());
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
GL11.glColor3d(0, 0, 0);
GL11.glMaterial(GL_FRONT_AND_BACK, GL_AMBIENT, _ambiant.getFloatBuffer());
GL11.glMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE, _diffuse.getFloatBuffer());
GL11.glMaterial(GL_FRONT_AND_BACK, GL_SPECULAR, _specular.getFloatBuffer());
GL11.glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, _shininess);
I don't know I have to change parameters or do other changes. Any help will be appreciated.
---- UPDATE ----
I Wrote my own obj reader, for each line I check if it is a vertex, vt, vn or f:
line = next();
String[] arr = line.split("\\s+");
if (arr[0].equals("o")) {
_name = arr[1];
continue;
}
if (arr[0].equals("v"))
_v.add(new Vec3(Double.parseDouble(arr[1]), Double
.parseDouble(arr[2]), Double.parseDouble(arr[3])));
if (arr[0].equals("vt")) {
Vec3 tex_coord = new Vec3(Double.parseDouble(arr[1]), 0, 0);
if (arr.length > 2) {
if (_flip)
tex_coord.setY(1 - Double.parseDouble(arr[2]));
else
tex_coord.setY(Double.parseDouble(arr[2]));
if (arr.length > 3)
tex_coord.setZ(Double.parseDouble(arr[3]));
}
_vt.add(tex_coord);
}
if (arr[0].equals("vn"))
_vn.add(new Vec3(Double.parseDouble(arr[1]), Double
.parseDouble(arr[2]), Double.parseDouble(arr[3])));
if (arr[0].equals("f")){
parseFace(arr);
}
With:
private void parseFace(final String[] arr) {
int[][] face = new int[arr.length - 1][];
for (int i = 0; i < face.length; i++) {
String[] arrf = arr[i + 1].trim().split("/");
face[i] = new int[arrf.length];
face[i][0] = Integer.parseInt(arrf[0]) - 1;
if (arrf.length > 1) {
face[i][1] = Integer.parseInt(arrf[1]) - 1;
if (arrf.length > 2)
face[i][2] = Integer.parseInt(arrf[2]) - 1;
}
_f.add(face);
}
Then I link vertex with vertex coordinates:
private void computeSimplifiedArrays() {
for (int i = 0; i < _v.size(); i++) {
_normals.add(null);
_texture_coordinates.add(null);
}
for (int[][] f : _f) {
int[] face = new int[f.length];
for (int i = 0; i < f.length; i++) {
int index = f[i][0];
face[i] = f[i][0];
if (f[i].length > 1) {
_texture_coordinates.set(index, _vt.get(f[i][1]));
if (f[0].length > 2)
_normals.set(index, _vn.get(f[i][2]));
else
_normals.set(index, new Vec3());
}
}
_faces.add(face);
}
}
So for each vertex (even if they have the same coordinates) I keep the right texture coordinates.