PDA

View Full Version : Basic Shape Rendering from Array



JMoore
10-14-2010, 06:19 PM
So I'm in the process of writing an Android app that will render 3D urban cities created with CityGML software. After feeding it through a parser to get just the vertice coordinates, I plug that array into my function (which I incorrectly assumed would just "work").

Because the scenes are large, there are going to be lots of coordinates/vertices, so I decided to use glDrawArrays, because I don't have a list of indices.

Question 1: Am I even close?

Question 2: Is there a mathematical function that I'm missing that generates the indices array?

Question 3: Should I be doing it differently, and if so, how?

Here's a video showing what's going wrong.

http://www.youtube.com/watch?v=1Iu0YxKK2pU&feature=youtube_gdata_player

Here's the portion of my code that I think it wrong. The current vertex array I'm using is a basic cube I pulled off the interwebs.


package com.android.vortex;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

public class VortexRenderer implements GLSurfaceView.Renderer {
private FloatBuffer _vertexBuffer;

private int _nrOfVertices = 0;

private float _xAngle;
private float _yAngle;

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_CULL_FACE);
gl.glFrontFace(GL10.GL_CCW);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

initShape();
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}

public void setXAngle(float angle) {
_xAngle = angle;
}

public float getXAngle() {
return _xAngle;
}

public void setYAngle(float angle) {
_yAngle = angle;
}

public float getYAngle() {
return _yAngle;
}

public void onDrawFrame(GL10 gl) {
gl.glLoadIdentity();

gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glRotatef(3.0f, 3.0f, 0.0f, 1.0f);

gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);

for(int i = 0; i<=_nrOfVertices; i+=3) {
gl.glDrawArrays(GL10.GL_TRIANGLES, i, 3);
}
}

private void initShape() {
float[] coords = {

1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f,-1.0f,1.0f,1.0f,-1.0f,1.0f,
1.0f,1.0f,1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f,-1.0f,1.0f,1.0f,-1.0f,
1.0f,1.0f,1.0f,1.0f,1.0f,-1.0f,-1.0f,1.0f,-1.0f,-1.0f,1.0f,1.0f,
-1.0f,1.0f,1.0f,-1.0f,1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,
-1.0f,-1.0f,-1.0f,1.0f,-1.0f,-1.0f,1.0f,-1.0f,1.0f,-1.0f,-1.0f,1.0f,
1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,-1.0f,1.0f,-1.0f,1.0f,1.0f,-1.0f

};

_nrOfVertices = coords.length;

ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer = vbb.asFloatBuffer();

_vertexBuffer.put(coords);
_vertexBuffer.position(0);
}
}

JMoore
10-20-2010, 07:35 AM
Okay, so I fixed my problem with how I'm drawing the vertices, but I'm still getting all sorts of garbage on the screen like in the video I posted.

Any ideas why?