How To Handle Index Buffer In Opengl Es 1.1
I want to render a model loaded from an .obj file using OPENGL ES 1.1 for Android. I have vertices, vertice normals and faces. When I render the model using GL10.GL_POINTS the mode
Solution 1:
maybe this will help you i used it to import an .obj file and it worked with a texture and everything, you will have to pick it apart to whats relevant to your project its tied into all these other classes, like the instance of GLGame and GLGraphics are used for file input and getting an instance of GL10, good luck, hope this helps
object loader
publicclassObjLoader {
publicstatic Vertices3 load(GLGame game, String file) {
InputStreamin=null;
try {
in = game.getFileIO().readAsset(file);
List<String> lines = readLines(in);
float[] vertices = newfloat[lines.size() * 3];
float[] normals = newfloat[lines.size() * 3];
float[] uv = newfloat[lines.size() * 2];
intnumVertices=0;
intnumNormals=0;
intnumUV=0;
intnumFaces=0;
int[] facesVerts = newint[lines.size() * 3];
int[] facesNormals = newint[lines.size() * 3];
int[] facesUV = newint[lines.size() * 3];
intvertexIndex=0;
intnormalIndex=0;
intuvIndex=0;
intfaceIndex=0;
for (inti=0; i < lines.size(); i++) {
Stringline= lines.get(i);
if (line.startsWith("v ")) {
String[] tokens = line.split("[ ]+");
vertices[vertexIndex] = Float.parseFloat(tokens[1]);
vertices[vertexIndex + 1] = Float.parseFloat(tokens[2]);
vertices[vertexIndex + 2] = Float.parseFloat(tokens[3]);
vertexIndex += 3;
numVertices++;
continue;
}
if (line.startsWith("vn ")) {
String[] tokens = line.split("[ ]+");
normals[normalIndex] = Float.parseFloat(tokens[1]);
normals[normalIndex + 1] = Float.parseFloat(tokens[2]);
normals[normalIndex + 2] = Float.parseFloat(tokens[3]);
normalIndex += 3;
numNormals++;
continue;
}
if (line.startsWith("vt")) {
String[] tokens = line.split("[ ]+");
uv[uvIndex] = Float.parseFloat(tokens[1]);
uv[uvIndex + 1] = Float.parseFloat(tokens[2]);
uvIndex += 2;
numUV++;
continue;
}
if (line.startsWith("f ")) {
String[] tokens = line.split("[ ]+");
String[] parts = tokens[1].split("/");
facesVerts[faceIndex] = getIndex(parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex(parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex(parts[1], numUV);
faceIndex++;
parts = tokens[2].split("/");
facesVerts[faceIndex] = getIndex(parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex(parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex(parts[1], numUV);
faceIndex++;
parts = tokens[3].split("/");
facesVerts[faceIndex] = getIndex(parts[0], numVertices);
if (parts.length > 2)
facesNormals[faceIndex] = getIndex(parts[2], numNormals);
if (parts.length > 1)
facesUV[faceIndex] = getIndex(parts[1], numUV);
faceIndex++;
numFaces++;
continue;
}
}
float[] verts = newfloat[(numFaces * 3)
* (3 + (numNormals > 0 ? 3 : 0) + (numUV > 0 ? 2 : 0))];
for (inti=0, vi = 0; i < numFaces * 3; i++) {
intvertexIdx= facesVerts[i] * 3;
verts[vi++] = vertices[vertexIdx];
verts[vi++] = vertices[vertexIdx + 1];
verts[vi++] = vertices[vertexIdx + 2];
if (numUV > 0) {
intuvIdx= facesUV[i] * 2;
verts[vi++] = uv[uvIdx];
verts[vi++] = 1 - uv[uvIdx + 1];
}
if (numNormals > 0) {
intnormalIdx= facesNormals[i] * 3;
verts[vi++] = normals[normalIdx];
verts[vi++] = normals[normalIdx + 1];
verts[vi++] = normals[normalIdx + 2];
}
}
Vertices3model=newVertices3(game.getGLGraphics(), numFaces * 3,
0, false, numUV > 0, numNormals > 0);
model.setVertices(verts, 0, verts.length);
return model;
} catch (Exception ex) {
thrownewRuntimeException("couldn't load '" + file + "'", ex);
} finally {
if (in != null)
try {
in.close();
} catch (Exception ex) {
}
}
}
staticintgetIndex(String index, int size) {
intidx= Integer.parseInt(index);
if (idx < 0)
return size + idx;
elsereturn idx - 1;
}
static List<String> readLines(InputStream in)throws IOException {
List<String> lines = newArrayList<String>();
BufferedReaderreader=newBufferedReader(newInputStreamReader(in));
Stringline=null;
while ((line = reader.readLine()) != null)
lines.add(line);
return lines;
}
}
vertices3
publicclassVertices3 {
final GLGraphics glGraphics;
finalboolean hasColor;
finalboolean hasTexCoords;
finalboolean hasNormals;
finalint vertexSize;
final IntBuffer vertices;
finalint[] tmpBuffer;
final ShortBuffer indices;
publicVertices3(GLGraphics glGraphics, int maxVertices, int maxIndices,
boolean hasColor, boolean hasTexCoords, boolean hasNormals) {
this.glGraphics = glGraphics;
this.hasColor = hasColor;
this.hasTexCoords = hasTexCoords;
this.hasNormals = hasNormals;
this.vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0) + (hasNormals ? 3
: 0)) * 4;
this.tmpBuffer = newint[maxVertices * vertexSize / 4];
ByteBufferbuffer= ByteBuffer.allocateDirect(maxVertices * vertexSize);
buffer.order(ByteOrder.nativeOrder());
vertices = buffer.asIntBuffer();
if (maxIndices > 0) {
buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
buffer.order(ByteOrder.nativeOrder());
indices = buffer.asShortBuffer();
} else {
indices = null;
}
}
publicvoidsetVertices(float[] vertices, int offset, int length) {
this.vertices.clear();
intlen= offset + length;
for (inti= offset, j = 0; i < len; i++, j++)
tmpBuffer[j] = Float.floatToRawIntBits(vertices[i]);
this.vertices.put(tmpBuffer, 0, length);
this.vertices.flip();
}
publicvoidsetIndices(short[] indices, int offset, int length) {
this.indices.clear();
this.indices.put(indices, offset, length);
this.indices.flip();
}
publicvoidbind() {
GL10gl= glGraphics.getGL();
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
vertices.position(0);
gl.glVertexPointer(3, GL10.GL_FLOAT, vertexSize, vertices);
if (hasColor) {
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
vertices.position(3);
gl.glColorPointer(4, GL10.GL_FLOAT, vertexSize, vertices);
}
if (hasTexCoords) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(hasColor ? 7 : 3);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertexSize, vertices);
}
if (hasNormals) {
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
intoffset=3;
if (hasColor)
offset += 4;
if (hasTexCoords)
offset += 2;
vertices.position(offset);
gl.glNormalPointer(GL10.GL_FLOAT, vertexSize, vertices);
}
}
publicvoiddraw(int primitiveType, int offset, int numVertices) {
GL10gl= glGraphics.getGL();
if (indices != null) {
indices.position(offset);
gl.glDrawElements(primitiveType, numVertices,
GL10.GL_UNSIGNED_SHORT, indices);
} else {
gl.glDrawArrays(primitiveType, offset, numVertices);
}
}
publicvoidunbind() {
GL10gl= glGraphics.getGL();
if (hasTexCoords)
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
if (hasColor)
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
if (hasNormals)
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
}
publicintgetNumIndices() {
return indices.limit();
}
publicintgetNumVertices() {
return vertices.limit() / (vertexSize / 4);
}
}
Post a Comment for "How To Handle Index Buffer In Opengl Es 1.1"