PDA

View Full Version : Depth problem on rotate (or always?)



Skeletor11
01-13-2010, 10:18 AM
I have been messing with this for days now and just can't seem to figure it out. I am trying to render the tail of a dart. The rendering seems to work, but as soon as I rotate it, the polygons seem to bleed over one another. I have made sure that my frustum is not too close to 0 (as I heard this can cause some depth issues). I am assuming it has something to do with my depth buffer test, but at this point I have no idea. Could it be the normals as well???

Here is the code:

public abstract class AbstractRenderer
implements GLSurfaceView.Renderer
{
public int[] getConfigSpec()
{
int[] configSpec =
{
EGL10.EGL_DEPTH_SIZE,0,EGL10.EGL_NONE
};
return configSpec;
}
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig)
{
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10 .GL_FASTEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glClearColor(.5f,.5f,.5f,1);
gl.glShadeModel(GL10.GL_SMOOTH);
}
public void onSurfaceChanged(GL10 gl,int w, int h)
{
gl.glViewport(0,0,w,h);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-1f,1f,-1f,1f,2f,8f);
}
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl,0f,1f,5f,0f,0f,0f,0.0f,1.0f,0.0f) ;
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthMask(true);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
float ambientLight[] = { 0.0f, 0.0f, 0.0f, 1.0f };
float diffuseLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float position[] = { 0.0f, 2.0f, 0.0f };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, position, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLight, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffuseLight, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specularLight, 0);
draw(gl);
}
protected abstract void draw(GL10 gl);
}


public class SimpleTriangleRenderer extends AbstractRenderer
{
private FloatBuffer mFVertexBuffer;
private FloatBuffer normalBuffer;
short[] myIndecesArray;
public SimpleTriangleRenderer(Context context)
{
OFFFileReader fReader = new OFFFileReader();
fReader.GetInfo3(R.raw.dart, context);
myIndecesArray = fReader.Verts;
float[] coords = fReader.Coords;
float[] NormalCoords = fReader.NormalCoords;

ByteBuffer nbb = ByteBuffer.allocateDirect(myIndecesArray.length * 3 * 4);
nbb.order(ByteOrder.nativeOrder());
normalBuffer = nbb.asFloatBuffer();

ByteBuffer vbb = ByteBuffer.allocateDirect(myIndecesArray.length * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();

ByteBuffer ibb = ByteBuffer.allocateDirect(myIndecesArray.length * 2);
ibb.order(ByteOrder.nativeOrder());
mIndexBuffer = ibb.asShortBuffer();

for (int i = 0; i < coords.length; i++)
{
mFVertexBuffer.put(coords[i]);
normalBuffer.put(NormalCoords[i]);
}

for (int i = 0; i < myIndecesArray.length; i++)
{
mIndexBuffer.put(myIndecesArray[i]);
}

mFVertexBuffer.position(0);
mIndexBuffer.position(0);
normalBuffer.position(0);
}
//overridden method
protected void draw(GL10 gl)
{
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int)time);
gl.glRotatef(-angle, 0,1f, 0);

gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer);

gl.glVertexPointer(3,GL10.GL_FLOAT,0,mFVertexBuffe r);

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, myIndecesArray.length);
}
}

I am pulling the indexes, vertices, normals from an exported obj file from blender. Here is the obj file:

# Blender3D v249 OBJ File: dart1.blend
# www.blender3d.org (http://www.blender3d.org)
v -0.127388 1.778879 -0.119631
v -0.127388 1.488901 -0.119631
v 0.135490 1.488901 0.143248
v 0.135490 1.778879 0.143247
v 0.004051 1.392242 0.011808
v 0.004051 1.875538 0.011808
v -0.004492 1.398525 0.003265
v -0.004492 1.869255 0.003265
v 0.013252 1.399008 0.021009
v 0.013252 1.868772 0.021009
v -0.127388 1.738282 -0.119631
v 0.135490 1.738282 0.143247
v 0.004051 1.807876 0.011808
v -0.004492 1.803353 0.003265
v 0.013252 1.803005 0.021009
v -0.127388 1.525061 -0.119631
v 0.135490 1.525061 0.143247
v 0.004051 1.452509 0.011808
v -0.004492 1.457225 0.003265
v 0.013252 1.457588 0.021009
v 0.018135 1.457588 0.016126
v 0.000391 1.457225 -0.001618
v 0.008934 1.452509 0.006926
v 0.140373 1.525061 0.138365
v -0.122505 1.525061 -0.124513
v 0.018135 1.803005 0.016126
v 0.000391 1.803353 -0.001618
v 0.008934 1.807876 0.006926
v 0.140373 1.738282 0.138365
v -0.122505 1.738282 -0.124513
v 0.018135 1.868772 0.016126
v 0.018135 1.399008 0.016126
v 0.000391 1.869255 -0.001618
v 0.000391 1.398525 -0.001618
v 0.008934 1.875538 0.006926
v 0.008934 1.392242 0.006926
v 0.140373 1.778879 0.138365
v 0.140373 1.488901 0.138365
v -0.122505 1.488901 -0.124513
v -0.122505 1.778879 -0.124513
v 0.135490 1.778879 -0.119631
v 0.135490 1.488901 -0.119631
v -0.127388 1.488901 0.143248
v -0.127388 1.778879 0.143247
v 0.004051 1.392242 0.011808
v 0.004051 1.875538 0.011808
v 0.012595 1.398525 0.003265
v 0.012595 1.869255 0.003265
v -0.005149 1.399008 0.021009
v -0.005149 1.868772 0.021009
v 0.135490 1.738282 -0.119631
v -0.127388 1.738282 0.143247
v 0.004051 1.807876 0.011808
v 0.012595 1.803353 0.003265
v -0.005149 1.803005 0.021009
v 0.135490 1.525061 -0.119631
v -0.127388 1.525061 0.143248
v 0.004051 1.452509 0.011808
v 0.012595 1.457225 0.003265
v -0.005149 1.457588 0.021009
v -0.000267 1.457588 0.025892
v 0.017478 1.457225 0.008148
v 0.008934 1.452509 0.016691
v -0.122505 1.525061 0.148130
v 0.140373 1.525061 -0.114748
v -0.000267 1.803005 0.025892
v 0.017478 1.803353 0.008148
v 0.008934 1.807876 0.016691
v -0.122505 1.738282 0.148130
v 0.140373 1.738282 -0.114748
v -0.000267 1.868772 0.025892
v -0.000267 1.399008 0.025892
v 0.017478 1.869255 0.008148
v 0.017478 1.398525 0.008148
v 0.008934 1.875538 0.016691
v 0.008934 1.392242 0.016691
v -0.122505 1.778879 0.148130
v -0.122505 1.488901 0.148130
v 0.140373 1.488901 -0.114748
v 0.140373 1.778879 -0.114748
vn -0.269295 0.627338 -0.730674
vn -0.016572 0.538804 -0.842250
vn -0.707083 0.000000 -0.707083
vn 0.000000 0.000000 -1.000000
vn 0.196112 0.000000 -0.980560
vn -0.517808 0.791864 -0.323679
vn -0.730674 0.627338 -0.269295
vn -0.957915 0.269387 0.099033
vn -1.000000 0.000000 0.000000
vn -0.980560 0.000000 0.196112
vn 0.099033 -0.269387 -0.957915
vn -0.269295 -0.627338 -0.730674
vn -0.323679 -0.791864 -0.517808
vn -0.730674 -0.627338 -0.269295
vn -0.842250 -0.538804 -0.016572
vn 0.016572 0.538804 0.842250
vn 0.269295 0.627338 0.730674
vn 0.517808 0.791864 0.323679
vn 0.269295 -0.627338 0.730674
vn -0.099033 -0.269387 0.957915
vn 0.323679 -0.791864 0.517808
vn 0.730674 0.627338 0.269295
vn 0.957915 0.269387 -0.099033
vn 0.842250 -0.538804 0.016572
vn 0.730674 -0.627338 0.269295
vn -0.196112 0.000000 0.980560
vn 0.980560 0.000000 -0.196112
vn 0.000000 0.000000 1.000000
vn 1.000000 0.000000 0.000000
vn 0.707083 0.000000 0.707083
vn -0.730674 0.627338 0.269295
vn -0.842250 0.538804 0.016572
vn -0.707083 0.000000 0.707083
vn -0.980560 0.000000 -0.196112
vn -0.323679 0.791864 0.517808
vn -0.269295 0.627338 0.730674
vn 0.099033 0.269387 0.957915
vn 0.196112 0.000000 0.980560
vn -0.957915 -0.269387 -0.099033
vn -0.730674 -0.627338 0.269295
vn -0.517808 -0.791864 0.323679
vn -0.269295 -0.627338 0.730674
vn -0.016572 -0.538804 0.842250
vn 0.842250 0.538804 -0.016572
vn 0.730674 0.627338 -0.269295
vn 0.323679 0.791864 -0.517808
vn 0.730674 -0.627338 -0.269295
vn 0.957915 -0.269387 0.099033
vn 0.517808 -0.791864 -0.323679
vn 0.269295 0.627338 -0.730674
vn -0.099033 0.269387 -0.957915
vn 0.016572 -0.538804 -0.842250
vn 0.269295 -0.627338 -0.730674
vn 0.980560 0.000000 0.196112
vn -0.196112 0.000000 -0.980560
vn 0.707083 0.000000 -0.707083
usemtl Material
s 1
f 72//1 78//2 61//3
f 78//2 64//4 61//3
f 61//3 64//4 66//3
f 64//4 69//5 66//3
f 76//6 72//1 63//3
f 72//1 61//3 63//3
f 63//3 61//3 68//3
f 61//3 66//3 68//3
f 74//7 76//6 63//3
f 74//7 63//3 62//3
f 62//3 63//3 68//3
f 62//3 68//3 67//3
f 79//8 74//7 62//3
f 79//8 62//3 65//9
f 65//9 62//3 67//3
f 65//9 67//3 70//10
f 66//3 69//5 77//11
f 66//3 77//11 71//12
f 68//3 66//3 71//12
f 68//3 71//12 75//13
f 67//3 68//3 73//14
f 68//3 75//13 73//14
f 73//14 80//15 67//3
f 80//15 70//10 67//3
f 42//16 47//17 74//7
f 42//16 74//7 79//8
f 47//17 45//18 76//6
f 47//17 76//6 74//7
f 48//19 41//20 80//15
f 48//19 80//15 73//14
f 46//21 48//19 73//14
f 46//21 73//14 75//13
f 49//22 43//23 78//2
f 49//22 78//2 72//1
f 45//18 49//22 72//1
f 45//18 72//1 76//6
f 44//24 50//25 71//12
f 44//24 71//12 77//11
f 50//25 46//21 75//13
f 50//25 75//13 71//12
f 41//20 51//26 70//10
f 41//20 70//10 80//15
f 52//27 44//24 77//11
f 52//27 77//11 69//5
f 56//28 42//16 79//8
f 56//28 79//8 65//9
f 51//26 56//28 65//9
f 51//26 65//9 70//10
f 43//23 57//29 64//4
f 43//23 64//4 78//2
f 57//29 52//27 69//5
f 57//29 69//5 64//4
f 57//29 43//23 60//30
f 43//23 49//22 60//30
f 52//27 57//29 55//30
f 57//29 60//30 55//30
f 60//30 49//22 58//30
f 49//22 45//18 58//30
f 55//30 60//30 53//30
f 60//30 58//30 53//30
f 58//30 45//18 47//17
f 58//30 47//17 59//30
f 53//30 58//30 59//30
f 53//30 59//30 54//30
f 59//30 47//17 42//16
f 59//30 42//16 56//28
f 54//30 59//30 56//28
f 54//30 56//28 51//26
f 44//24 52//27 55//30
f 44//24 55//30 50//25
f 50//25 55//30 53//30
f 50//25 53//30 46//21
f 46//21 53//30 48//19
f 53//30 54//30 48//19
f 51//26 41//20 54//30
f 41//20 48//19 54//30
f 32//31 38//32 21//33
f 38//32 24//9 21//33
f 21//33 24//9 26//33
f 24//9 29//34 26//33
f 36//35 32//31 23//33
f 32//31 21//33 23//33
f 23//33 21//33 28//33
f 21//33 26//33 28//33
f 34//36 36//35 23//33
f 34//36 23//33 22//33
f 22//33 23//33 28//33
f 22//33 28//33 27//33
f 39//37 34//36 22//33
f 39//37 22//33 25//28
f 25//28 22//33 27//33
f 25//28 27//33 30//38
f 26//33 29//34 37//39
f 26//33 37//39 31//40
f 28//33 26//33 31//40
f 28//33 31//40 35//41
f 27//33 28//33 33//42
f 28//33 35//41 33//42
f 33//42 40//43 27//33
f 40//43 30//38 27//33
f 2//44 7//45 34//36
f 2//44 34//36 39//37
f 7//45 5//46 36//35
f 7//45 36//35 34//36
f 8//47 1//48 40//43
f 8//47 40//43 33//42
f 6//49 8//47 33//42
f 6//49 33//42 35//41
f 9//50 3//51 38//32
f 9//50 38//32 32//31
f 5//46 9//50 32//31
f 5//46 32//31 36//35
f 4//52 10//53 31//40
f 4//52 31//40 37//39
f 10//53 6//49 35//41
f 10//53 35//41 31//40
f 1//48 11//54 30//38
f 1//48 30//38 40//43
f 12//55 4//52 37//39
f 12//55 37//39 29//34
f 16//29 2//44 39//37
f 16//29 39//37 25//28
f 11//54 16//29 25//28
f 11//54 25//28 30//38
f 3//51 17//4 24//9
f 3//51 24//9 38//32
f 17//4 12//55 29//34
f 17//4 29//34 24//9
f 17//4 3//51 20//56
f 3//51 9//50 20//56
f 12//55 17//4 15//56
f 17//4 20//56 15//56
f 20//56 9//50 18//56
f 9//50 5//46 18//56
f 15//56 20//56 13//56
f 20//56 18//56 13//56
f 18//56 5//46 7//45
f 18//56 7//45 19//56
f 13//56 18//56 19//56
f 13//56 19//56 14//56
f 19//56 7//45 2//44
f 19//56 2//44 16//29
f 14//56 19//56 16//29
f 14//56 16//29 11//54
f 4//52 12//55 15//56
f 4//52 15//56 10//53
f 10//53 15//56 13//56
f 10//53 13//56 6//49
f 6//49 13//56 8//47
f 13//56 14//56 8//47
f 11//54 1//48 14//56
f 1//48 8//47 14//56