简体   繁体   English

如何在OpenGL中的3D对象上应用纹理

[英]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. 我尝试将纹理(png文件)应用于以Java导入的3d对象。 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. 尝试将n个纹理坐标与v个纹理坐标切换。 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. 我认为您仅在法线和顶点使用了一半的纹理坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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