Search code examples
opengltextures

How to apply a texture on a 3D object in OpenGL


I've try to apply a texture (png file) to a 3d object imported in Java. Here is my code, i think i haven't bend it correct.

render block:

while (!Display.isCloseRequested()){

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT
  glPushMatrix();      
  glNewList(treeDisplayList, GL_COMPILE);

  Model m = null;
    try {
          m = OBJLoader.loadModel(new File(ObjectConstants.tree));
        } catch (FileNotFoundException e) {
          e.printStackTrace();
          Display.destroy();
          System.exit(1);
        } catch (IOException e) {
          e.printStackTrace();
          Display.destroy();
          System.exit(1);
        }

    glBegin(GL_TRIANGLES);
    for (Face face : m.faces) {
        Vector3f n1 = m.normals.get((int) face.normals.x - 1);
        glNormal3f(n1.x, n1.y, n1.z);

        Vector3f v1 = m.vertices.get((int) face.vertex.x - 1);
        glTexCoord3f(v1.x, v1.y, v1.z);
        glVertex3f(v1.x, v1.y, v1.z);

        Vector3f v2 = m.vertices.get((int) face.vertex.y - 1);
        glTexCoord3f(v2.x, v2.y, v2.z);
        glVertex3f(v2.x, v2.y, v2.z);

        Vector3f v3 = m.vertices.get((int) face.vertex.z - 1);
        glTexCoord3f(v3.x, v3.y, v3.z);
        glVertex3f(v3.x, v3.y, v3.z);
    }

    glEnd();
    glEndList();

    glPopMatrix();
    glLoadIdentity();

    Display.update();
    Display.sync(60);
  }

where ,

vertex=new Vector3f(); //three indices, not vector
normals= new Vector3f();

after using this piece of code i obtain only half of object rendered

enter image description here


Solution

  • Try switching the n texture coordinates with the v texture coordinates. Does the other side of the tree render? Is it the same? I am thinking that you are using only half the texture coordinates on the normals and half on the vertices.