1

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

in Blender I can see that the problem is tight where the model is cut: Blender

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.

  • Did you write your own OBJ reader? If yes, I suspect that you might not correctly handle the case where there are multiple vertices with the same positions, but different texture coordinates. – Reto Koradi Jul 26 '16 at 02:12
  • Yes, I wrote my own. Please check the update for more details. – elody wenge Jul 26 '16 at 08:46
  • Yes, that's exactly the problem. This won't work if you have faces with vertices that use the same position index, but different texture indices. What you're doing looks somewhat similar to this one: http://stackoverflow.com/questions/28370813/what-could-be-wrong-with-my-obj-parser. See my answer there for an explanation, and links to other related answers. – Reto Koradi Jul 28 '16 at 02:52
  • @RetoKoradi Thanks a lot, I thought that the list of vertices in the obj file contains all the mesh vertices but it only links positions to indices so that we can use those indices to create faces. – elody wenge Aug 02 '16 at 09:58
  • @RetoKoradi Hi, I noticed that duplicating vertices has a strong effect on performance. I go from 70fps to 20fps and I create 3times more vertices. Is there any way to combine different texture coordinates in a single vertex? thanks a lot. – elody wenge Oct 14 '16 at 13:25

0 Answers0