Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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  
  LWJGL VBO rendering  (Read 387 times)
0 Members and 1 Guest are viewing this topic.
Offline vastrolorde

Junior Member





« Posted 2014-01-23 17:50:09 »

Im trying to render a blender model i downloaded from internet. I have exported it as .obj and .mtl. I wrote the parser for obj and mtl. Iwould like to draw the model using VBO rendering but, for some reason im only getting 1 triangle not the whole model. if i use DL rendering i get the entire model. So what might be the problem here?

Model 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  
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  
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
 
import test.OBJloader.Face;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
 
public class Model {
    public List<Vector3f> vertices = new ArrayList<Vector3f>();
    public List<Vector3f> normals = new ArrayList<Vector3f>();
    public List<Vector2f> texture = new ArrayList<Vector2f>();
    public Map<String, Material> materials = new HashMap<String, Material>();
    public List<Face> faces = new ArrayList<Face>();
     
    int vboVertexID;
    int vboNormalID;
    int vboColorID;
     
    public void Render(){
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
         
        glBindBuffer(GL_ARRAY_BUFFER, vboNormalID);
        glNormalPointer(GL_FLOAT,0,0);
         
        glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
        glColorPointer(3, GL_FLOAT,0,0);
         
        glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
        glVertexPointer(3, GL_FLOAT,0,0);
         
        glDrawArrays(GL_TRIANGLES, 0, 9 * faces.size());
         
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
    }
     
    public void prepareVBO(){
        vboNormalID = glGenBuffers();
        vboVertexID = glGenBuffers();
        vboColorID = glGenBuffers();
         
        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(9 * faces.size());
        FloatBuffer normalBuffer = BufferUtils.createFloatBuffer(9 * faces.size());
        FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(9 * faces.size());
         
        for(Face face : faces){
            Material material = face.material;
             
            Vector3f v1 = vertices.get((int) face.vertex.x - 1);
            vertexBuffer.put(v1.x).put(v1.y).put(v1.z);
            colorBuffer.put(material.diffuse.x)
                       .put(material.diffuse.y)
                       .put(material.diffuse.z);
             
             
            Vector3f v2 = vertices.get((int) face.vertex.y - 1);
            vertexBuffer.put(v2.x).put(v2.y).put(v2.z);
            colorBuffer.put(material.diffuse.x)
                       .put(material.diffuse.y)
                       .put(material.diffuse.z);
            Vector3f n2 = normals.get((int) face.normal.y -1);
            normalBuffer.put(n2.x).put(n2.y).put(n2.z);
             
            Vector3f v3 = vertices.get((int) face.vertex.z - 1);
            vertexBuffer.put(v3.x).put(v3.y).put(v3.z);
            colorBuffer.put(material.diffuse.x)
                       .put(material.diffuse.y)
                       .put(material.diffuse.z);
            Vector3f n3 = normals.get((int) face.normal.z -1);
            normalBuffer.put(n3.x).put(n3.y).put(n3.z);
             
            vertexBuffer.rewind();
            normalBuffer.rewind();
            colorBuffer.rewind();
             
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
            glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
             
            glBindBuffer(GL_ARRAY_BUFFER, vboNormalID);
            glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
             
            glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
            glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
        }
    }
     
    public void dispose(){
        glDeleteBuffers(vboVertexID);
        glDeleteBuffers(vboNormalID);
        glDeleteBuffers(vboColorID);
    }
}

Main class:
Java 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  
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  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.vector.Vector3f;
 
import test.OBJloader.Face;
import test.OBJloader.Model;
import test.OBJloader.OBJLoader;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;
import controller.Controller2;
 
public class Controller {
     
    private static Vector3f position = new Vector3f(0,0,0);
    private static Vector3f rotation = new Vector3f(0,0,0);
    private static long lastFrame;
    private Controller2 control;
     
    private static long getTime(){
        return(Sys.getTime()* 1000) / Sys.getTimerResolution();
    }
     
    private static int getDelta(){
        long currentTime = getTime();
        int delta = (int)(currentTime - lastFrame);
        lastFrame = getTime();
        return delta;
    }
    public Controller(){
         
        try{
            Display.setDisplayMode(new DisplayMode(640,480));
            Display.setTitle("Controller");
            Display.create();
        } catch(LWJGLException e){
            e.printStackTrace();
        }
         
        control = new Controller2(position, rotation);
        Model m = null;
        try{
            m = OBJLoader.loadModel(new File("C:/Users/Marko/Documents/GitHub/Over-The-Galaxy/src/mees.obj"));
        }catch(FileNotFoundException e){
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }catch(IOException e){
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective((float) 30, 640f / 480f, 0.001f, 100);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_ARRAY_BUFFER_BINDING);
         
        /*int objectDisplayList = glGenLists(1);
        glNewList(objectDisplayList, GL_COMPILE);
        {
            Model m = null;
            try{
                m = OBJLoader.loadModel(new File("C:/Users/Marko/Documents/GitHub/Over-The-Galaxy/src/mees.obj"));
            }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.normal.x -1);
                glNormal3f(n1.x,n1.y,n1.z);
                Vector3f v1 = m.vertices.get((int)face.vertex.x -1);
                glVertex3f(v1.x,v1.y,v1.z);
                Vector3f n2 = m.normals.get((int) face.normal.y -1);
                glNormal3f(n2.x,n2.y,n2.z);
                Vector3f v2 = m.vertices.get((int)face.vertex.y -1);
                glVertex3f(v2.x,v2.y,v2.z);
                Vector3f n3 = m.normals.get((int) face.normal.z -1);
                glNormal3f(n3.x,n3.y,n3.z);
                Vector3f v3 = m.vertices.get((int)face.vertex.z -1);
                glVertex3f(v3.x,v3.y,v3.z);
            }
            glEnd();
             
        }
        glEndList();*/

 
        while(!Display.isCloseRequested()){
            int delta = getDelta();        
             
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        //  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
           
            //glCallList(objectDisplayList);
           m.Render();
             
            glLoadIdentity();
            glRotatef(rotation.x,1,0,0);
            glRotatef(rotation.y,0,1,0);
            glRotatef(rotation.z,0,0,1);
            glTranslatef(position.x,position.y,position.z);
             
            boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP) || Keyboard.isKeyDown(Keyboard.KEY_W);
            boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_DOWN) || Keyboard.isKeyDown(Keyboard.KEY_S);
            boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_A);
            boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D);
            boolean flyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE);
            boolean flyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
            boolean moveFaster = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
            boolean moveSlower = Keyboard.isKeyDown(Keyboard.KEY_TAB);
            float mouseDX = Mouse.getDX();
            float mouseDY = Mouse.getDY();
             
             
            control.Control(keyUp, keyDown, keyLeft, keyRight, flyUp, flyDown, moveFaster, moveSlower,
                    mouseDX, mouseDY, delta);
            position = control.position;
            rotation = control.rotation;  
             
             
                                     
            Display.update();
            Display.sync(60);          
        }
        Display.destroy();
        System.exit(0);
    }
     
     
    public static void main(String[] args){
        new Controller();
    }
     
}
Offline vastrolorde

Junior Member





« Reply #1 - Posted 2014-01-23 21:19:29 »

Solved. In prepareVBO } must be before buffer rewinds not after the buffer bindings.
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.

radar3301 (13 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (104 views)
2014-09-10 06:39:09

Tekkerue (51 views)
2014-09-09 02:24:56
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!