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:
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:
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();
}
}