PDA

View Full Version : Transfering VBO's from loader to renderer?



Braindrool
09-06-2012, 08:12 PM
New to the forums, short introduction time? Hello. Next on the agenda I'd like clarify that I've been programming for years, but just new to OpenGL. Actually new to 3D graphics in general. Fairly new to Java. To the point, the title says it all.


I previously had a separate class for loading, but I attempted a fix by moving it to the Game class with no success. So here it is.

~Ignore the unnecessary thing, too lazy to fix up now.


import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ReadOnlyBufferException;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.res.AssetManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {
private GLSurfaceView GLView;
FloatBuffer vertexBuffer;
ShortBuffer indexBuffer;
List<Float> vertices = new ArrayList<Float>();
List<Short> indices = new ArrayList<Short>();

@Override
protected void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FL AG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);

GLView = new View(this);
setContentView(GLView);
initializeModels();
}

private void initializeModels() {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.size() * 4);
vbb.order(ByteOrder.nativeOrder());

ByteBuffer ibb = ByteBuffer.allocateDirect(indices.size() * 2);
ibb.order(ByteOrder.nativeOrder());
ibb.position(0);

AssetManager am = getAssets();

try {
String[] files = am.list("models");
System.out.println(files);

for (int i = 0; i < files.length; i++) {
loadModel("models/" + files[i]);
}

float[] floatArray = new float[vertices.size()];

for (int i = 0; i < vertices.size(); i++) {
Float f = vertices.get(i);
floatArray[i] = (f != null ? f : Float.NaN);
}

System.out.println(vertices);
System.out.println(indices);

vbb.position(0);

} catch (IOException e) {
e.printStackTrace();
} catch (BufferOverflowException e) {
e.printStackTrace();
} catch (ReadOnlyBufferException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}

private void loadModel(String modelName) {
AssetManager am = getAssets();
System.out.println("Loading " + modelName);

try {
InputStream input = am.open(modelName);

int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();

String[] text = new String(buffer).split("");

for (int i = 0; i < text.length; i++) {
StringTokenizer vt = new StringTokenizer(text[i]);
StringTokenizer it = new StringTokenizer(text[i]);

while (vt.hasMoreTokens()) {

if (vt.nextToken().endsWith("v")) {
vertices.add(Float.parseFloat(vt.nextToken()));
vertices.add(Float.parseFloat(vt.nextToken()));
vertices.add(Float.parseFloat(vt.nextToken()));
}

}
while (it.hasMoreTokens()) {

if (it.nextToken().endsWith("f")) {
indices.add(Short.parseShort(it.nextToken()));
indices.add(Short.parseShort(it.nextToken()));
indices.add(Short.parseShort(it.nextToken()));
}

}
// System.out.println(vertices);
// System.out.println(indices);
}
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
GLView.onPause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
GLView.onResume();
}

}


The OpenGL renderer is where the issue comes in. How would I transfer VBO's from the loading activity to the actual renderer? I tried placing the loader directly into the renderer and couldn't load assets.

Help appreciated!

~Braindrool