Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  VBO´s multitexturing  (Read 1029 times)
0 Members and 1 Guest are viewing this topic.
Offline patacz

Junior Newbie





« Posted 2014-03-01 13:11:51 »

Hi everyone!

I have a problem. I use .obj models of Blender. I loads data of file this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
 public static Model LoadModel(String cesta){
        Model model = new Model();
        BufferedReader bf = null;
        material = null;
       
         try{
            bf = new BufferedReader(new FileReader(new File(cesta)));
            String radek;
           
            while((radek=bf.readLine()) != null){
               
                //Označení pro vrcholy
                if(radek.startsWith("v ")){
                    float x = Float.valueOf(radek.split(" ")[1]);
                    float y = Float.valueOf(radek.split(" ")[2]);
                    float z = Float.valueOf(radek.split(" ")[3]);
                    pocet++;
                    model.vertices.add(new Vector3f(x,y,z));
                   
                //Označení normál
                }else if(radek.startsWith("vn ")){
                    float x = Float.valueOf(radek.split(" ")[1]);
                    float y = Float.valueOf(radek.split(" ")[2]);
                    float z = Float.valueOf(radek.split(" ")[3]);
                    model.normals.add(new Vector3f(x,y,z));
                   
                //Označení sořadnic textur
                }else if(radek.startsWith("vt ")){
                    float x = Float.valueOf(radek.split(" ")[1]);
                    float y = Float.valueOf(radek.split(" ")[2]);
                   
                    model.textures.add(new Vector2f(x,y));
                    model.isTextured = true;
                   
                //Ozačení pro ploch stran
                }else if(radek.startsWith("f ")){
                    Vector3f verI = new Vector3f(
                            Float.valueOf(radek.split(" ")[1].split("/")[0]),
                            Float.valueOf(radek.split(" ")[2].split("/")[0]),
                            Float.valueOf(radek.split(" ")[3].split("/")[0]));
                   
                    Vector3f normI = new Vector3f(
                            Float.valueOf(radek.split(" ")[1].split("/")[2]),
                            Float.valueOf(radek.split(" ")[2].split("/")[2]),
                            Float.valueOf(radek.split(" ")[3].split("/")[2]));
                   
                   if(model.isTextured){                  
                    Vector3f textures = new Vector3f(
                            Float.valueOf(radek.split(" ") [1].split("/")[1]),
                            Float.valueOf(radek.split(" ")[2].split("/")[1]),
                            Float.valueOf(radek.split(" ")[3].split("/")[1]));
                   
                            model.faces.add(new Face(verI, normI, textures, material));                  
                   }else
                   {
                       model.faces.add(new Face(verI, normI, null, material));
                   }
                   
                   
                }else if(radek.startsWith("mtllib ")){
                    setMaterial(model, cesta.split("/")[0]+"/"+radek.split(" ")[1]);
                }else if(radek.startsWith("usemtl")){
                    material = model.materials.get(radek.split(" ")[1]);
                }      
            }
            bf.close();
           
        } catch(FileNotFoundException e){
            System.out.println(e);
        } catch(IOException e){
            e.printStackTrace();
        }

        modely.add(model);
       
        return model;
    }
   
    public static void setMaterial(Model m, String cesta){
        material = null;
        BufferedReader bf = null;
       
        String radek = null;
       
       try{
        bf = new BufferedReader(new FileReader(new File(cesta)));
           
        while((radek = bf.readLine()) != null){  
            if(radek.startsWith("newmtl ")){
                if(material != null){                  
                    m.materials.put(material.getPath(), material);
                }
               
                material = new Material(radek.split(" ")[1]);
            }else if(radek.startsWith("Kd ")){
                float r = Float.valueOf(radek.split(" ")[1]);
                float g = Float.valueOf(radek.split(" ")[2]);
                float b = Float.valueOf(radek.split(" ")[3]);

                material.setDiffuse(new Vector3f(r, g, b));
               
            }else if(radek.startsWith("map_Kd ")){
                String c = radek.replace("\\","/");
                System.out.println(cesta.split("/")[0]+"/"+c.split(" ")[1]);
                //m.text = Texture2.loadTexture(cesta.split("/")[0]+"/"+c.split(" ")[1]);
                m.text.add(Texture2.loadTexture(cesta.split("/")[0]+"/"+c.split(" ")[1]));
                //m.text.add(TextureLoader.getTexture("JPG", new FileInputStream(new File(cesta.split("/")[0]+"/"+c.split(" ")[1]))));
            }
           
        }
       
        m.materials.put(material.getPath(),material);
       
        bf.close();
       
       } catch(FileNotFoundException e){
            System.out.println(e);
        } catch(IOException e){
            e.printStackTrace();
        }
    }


All is correct but if I render object, which has several textures so it renders only one of texture.
Any idea how to do it?

Init code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
   FloatBuffer norm = reserveData(faces.size() * 9);
        FloatBuffer vert = reserveData(faces.size() * 9);
        FloatBuffer tex = reserveData(faces.size() * 6);
        FloatBuffer col = reserveData(faces.size() * 9);
       

        for (Face face : faces) {    //Cyklus foreach, který prochází celý List
           
            Material material = face.material;
           
            System.out.println(material);
           
            vert.put(asFloats3f(vertices.get((int) face.vertex.x - 1)));
            vert.put(asFloats3f(vertices.get((int) face.vertex.y - 1)));
            vert.put(asFloats3f(vertices.get((int) face.vertex.z - 1)));
            col.put(material.getDiffuse().x).put(material.getDiffuse().y).put(material.getDiffuse().z);

            norm.put(asFloats3f(normals.get((int) face.normal.x - 1)));
            norm.put(asFloats3f(normals.get((int) face.normal.y - 1)));
            norm.put(asFloats3f(normals.get((int) face.normal.z - 1)));
           
            if(isTextured){
                tex.put(asFloats2f(textures.get((int) face.textures.x - 1)));
                tex.put(asFloats2f(textures.get((int) face.textures.y - 1)));
                tex.put(asFloats2f(textures.get((int) face.textures.z - 1)));
            }
        }

         vert.flip();
         norm.flip();
         tex.rewind();

         vboVertex = glGenBuffers();        
         vertexBufferObjectIDs.add(vboVertex);
         
         vboNormal= glGenBuffers();
         normalBufferObjectIDs.add(vboNormal);
         
         vboTexture = glGenBuffers();
         textureBufferObjectsIDs.add(vboTexture);
         
         vboColor = glGenBuffers();
         
         
         glBindBuffer(GL_ARRAY_BUFFER, vboColor);
         glBufferData(GL_ARRAY_BUFFER, col, GL_STATIC_DRAW);
         glBindBuffer(GL_ARRAY_BUFFER, 0);
         
         
         glBindVertexArray(vboVertex);
         glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
         glBufferData(GL_ARRAY_BUFFER, vert, GL_STATIC_DRAW);
         glBindBuffer(GL_ARRAY_BUFFER, 0);
       
         glBindVertexArray(vboNormal);
         glBindBuffer(GL_ARRAY_BUFFER, vboNormal);
         glBufferData(GL_ARRAY_BUFFER, norm, GL_STATIC_DRAW);
         glBindBuffer(GL_ARRAY_BUFFER, 0);
         
         if(isTextured){
            glBindBuffer(GL_ARRAY_BUFFER, vboTexture);
            glBufferData(GL_ARRAY_BUFFER, tex, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
         }
       
         glBindVertexArray(0);
         
         vert.clear();
         norm.clear();
         tex.clear();


Render code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
     glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
            glVertexPointer(3, GL_FLOAT, 0, 0);

            glBindBuffer(GL_ARRAY_BUFFER, vboNormal);
            glNormalPointer(GL_FLOAT, 0, 0);
           
            glBindBuffer(GL_ARRAY_BUFFER, vboColor);
            glColorPointer(3, GL_FLOAT, 0, 0);
           
       

           
            if(isTextured){
               
                // I dont know how to render it!!
               
               
            }
           
             
           
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
           

           
            glMaterialf(GL_BACK, GL_SHININESS, 1f);
       
            glDrawArrays(GL_TRIANGLES, 0, this.faces.size() * 3);
           
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_NORMAL_ARRAY);
            glDisableClientState(GL_COLOR_ARRAY);
           
            if(isTextured){
                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            }
           
            glBindBuffer(GL_ARRAY_BUFFER, 0);


Thanks for the advices..
Offline SHC
« Reply #1 - Posted 2014-03-01 13:27:28 »

I've understood you were following my tutorial series, thanks for using them. For simplicity, I've kept the tutorial simple, there will be only one Texture field in the Model class. If you want to get multiple textures, you can, for example, do in two ways.

  • Creating a ModelBatcher and optimizing the Model class to use the batcher, drawing the faces that use one texture at a time and the faces that use other another time. I'll be doing a ModelBatcher tutorial soon (Having exams this whole month, so it's gonna take time)
  • Simply create a larger texture that includes both the textures in a single image and use blender to adjust the UV mapping. This is the simplest possible solution and is preferred by most game programmers.

Hope this helps.

Offline patacz

Junior Newbie





« Reply #2 - Posted 2014-03-01 14:01:16 »

Yep, your tutorials are very useful for me :-) I just do a little school project about 3D programming in Java and missing me only texture of model so i will use second (easy)Cheesy method, thank you. And so you will have complete tutorial about ModelBatcher so I want to see him! Good luck with your exams:)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2014-03-01 14:38:14 »

Thanks. I will be posting it soon I've completed the exams. Thanks for wishing me luck.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!