简体   繁体   中英

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

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM