Results 1 to 3 of 3
  1. #1
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Rep Power

    Default LWJGL VBO multiple textures

    How can i use multiple textures on a model? I have the .OBJ loader and .MTL loader and everything is working fine. I can use a single texture. But i have a model that has 2 textures. How can i use both of the textures?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: LWJGL VBO multiple textures

    Yeah two textures how? Two different textures for two different parts of the model, or are both textures to be applied to all the primitives.

    If you have a different texture for different groups of vertices than you can't just load all the vertices and throw them on the same pile, you have to build your model data structure such that you can know which texture to be used for which vertices so you can render them as part of the same batch without having to switch texture for each pair of vertices.

    Welcome to 3D game programming - the moment where you realize that this stuff is actually VERY difficult. And also why people tend to use pre-built engines which deal with this kind of low level stuff for them.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    vastrolorde is offline Senior Member
    Join Date
    Jan 2013
    Rep Power

    Default Re: LWJGL VBO multiple textures

    This is my best result as far as the model itself goes. Textures dont work properly on this.

    Java Code:
    for (int i = 0; i < vertices.size(); i++){
             vertexBuffer.put(normals.get(i).x). put(normals.get(i).y).put(normals.get(i).z);
    This is my latest test to get drawElements working with textures, but this has several problems. 1:Texture is upside down
    2: And if rendered the model is drawn incorrectly. The code is based around .OBJ file where the line f is something like this 1/7/7. So this connects the vertex with the correct normal and texture. but this doesent work properly.
    Java Code:
    public void prepareVBO(){
          vboIndeciesID = glGenBuffers();
          vboVertexID = glGenBuffers();
          int x = 0;
          int e = 11;
          for(int i = 0; i < faces.size(); i++){
             total += faces.get(i).size();
          float[] buffer = new float[33 * total];
          FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(33* total);
          IntBuffer indeciesBuffer = BufferUtils.createIntBuffer(33*total);
          for(int i = 0; i < faces.size(); i++){
             for(Face face : faces.get(i)){
                Material material = face.material;
                indeciesBuffer.put((int) (face.vertex.x - 1)).put((int) (face.vertex.y - 1)).put((int) (face.vertex.z - 1));
                x = (int) face.vertex.x - 1;
                Vector3f v1 = vertices.get((int) face.vertex.x - 1);
                Vector3f n1 = normals.get((int) face.normal.x -1);
                Vector2f t1 = texture.get((int) face.texture.x - 1);
                buffer[e*x] = v1.x;
                buffer[e*x+1] = v1.y;
                buffer[e*x+2] = v1.z;
                buffer[e*x+3] = n1.x;
                buffer[e*x+4] = n1.y;
                buffer[e*x+5] = n1.z;
                buffer[e*x+6] = material.getDiffuse().x;
                buffer[e*x+7] = material.getDiffuse().y;
                buffer[e*x+8] = material.getDiffuse().z;
                buffer[e*x+9] = t1.x;
                buffer[e*x+10] = t1.y;
                x = (int) face.vertex.y - 1;
                Vector3f v2 = vertices.get((int) face.vertex.y - 1);
                Vector3f n2 = normals.get((int) face.normal.y -1);
                Vector2f t2 = texture.get((int) face.texture.y - 1);
                buffer[e*x+11] = v2.x;
                buffer[e*x+12] = v2.y;
                buffer[e*x+13] = v2.z;
                buffer[e*x+14] = n2.x;
                buffer[e*x+15] = n2.y;
                buffer[e*x+16] = n2.z;
                buffer[e*x+17] = material.getDiffuse().x;
                buffer[e*x+18] = material.getDiffuse().y;
                buffer[e*x+19] = material.getDiffuse().z;
                buffer[e*x+20] = t2.x;
                buffer[e*x+21] = t2.y;
                x = (int) face.vertex.z - 1;
                Vector3f v3 = vertices.get((int) face.vertex.z - 1);
                Vector3f n3 = normals.get((int) face.normal.z -1);
                Vector2f t3 = texture.get((int) face.texture.z - 1);
                buffer[e*x+22] = v3.x;
                buffer[e*x+23] = v3.y;
                buffer[e*x+24] = v3.z;
                buffer[e*x+25] = n3.x;
                buffer[e*x+26] = n3.y;
                buffer[e*x+27] = n3.z;
                buffer[e*x+28] = material.getDiffuse().x;
                buffer[e*x+29] = material.getDiffuse().y;
                buffer[e*x+30] = material.getDiffuse().z;
                buffer[e*x+31] = t3.x;
                buffer[e*x+32] = t3.y;
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndeciesID);
             glBufferData(GL_ELEMENT_ARRAY_BUFFER, indeciesBuffer, GL_STATIC_DRAW);
             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
             glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
             glBufferData(GL_ARRAY_BUFFER, vertexBuffer , GL_STATIC_DRAW);
             glBindBuffer(GL_ARRAY_BUFFER, 0);

Similar Threads

  1. LWJGL - Where to go from here
    By mitchbutter1210 in forum Advanced Java
    Replies: 0
    Last Post: 06-27-2012, 05:37 AM
  2. LWJGL help please
    By Brandon@JavaForums in forum Advanced Java
    Replies: 12
    Last Post: 12-31-2011, 10:19 AM
  3. All textures on one .png?
    By chrisall76 in forum New To Java
    Replies: 0
    Last Post: 12-17-2011, 11:12 PM
  4. lwjgl help PLEASE!
    By Brandon in forum New To Java
    Replies: 1
    Last Post: 10-06-2011, 06:11 AM
  5. cannot load textures
    By zlatko in forum AWT / Swing
    Replies: 6
    Last Post: 02-01-2010, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts