PDA

View Full Version : Mysterious vertices after scece is rendered



rpodhajny
01-22-2011, 09:07 AM
I have problem when I render floor on my scene. An extra two points are generated (sometimes more) and OpenGL draws vertices between them and the (0,0,0) point. This causes a wall to be rendered (in GL_TRIANGLES mode) and i don`t want it to heppen.

Here you have a picture where you can see my floor (the smaller horizontal square) and the wall (the big vertical square).
http://img560.imageshack.us/img560/9373/img1412h.jpg

And some code:

View class


package com.wordpress.rpodhajny.openGL;

import android.content.Context;
import android.opengl.GLSurfaceView;

import com.wordpress.rpodhajny.openGL.renderers.GLWave3DR enderer;

public class GLView extends GLSurfaceView {
private GLWave3DRenderer renderer;

public GLView(Context context) {
super(context);

setDebugFlags(DEBUG_CHECK_GL_ERROR); // DEBUG_LOG_GL_CALLS causes Exception when i call (gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length);)

renderer = new GLWave3DRenderer();
setRenderer(renderer);
}

public GLWave3DRenderer getRenderer() {
return renderer;
}
}





Renderer class


package com.wordpress.rpodhajny.openGL.renderers;

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

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

import com.wordpress.rpodhajny.openGL.models.GLWave3d;

public class GLWave3DRenderer implements Renderer {

public static final String TAG = "GLRenderer";

private GLWave3d wave3d = null;

private long startTime;
private float angle = 0;

@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f,0.0f,-40.0f);

angle = (System.currentTimeMillis() - startTime) * 0.03f;

gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
gl.glRotatef(angle, 0.0f, 1.0f, 0.0f);

wave3d.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
wave3d = new GLWave3d();

gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float aspect = 0f;
if(width < height) {
aspect = width / (float)height;
} else {
aspect = height / (float)width;
}
GLU.gluPerspective(gl, 45.0f, aspect, 1.0f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
startTime = System.currentTimeMillis();

gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
// gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public GLWave3d getWave3d() {
return wave3d;
}

public void setWave3d(GLWave3d wave3d) {
this.wave3d = wave3d;
}
}





Model class


package com.wordpress.rpodhajny.openGL.models;

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

import javax.microedition.khronos.opengles.GL10;

public class GLWave3d {

private static final float WIDTH = 5;
private static final float HEIGHT = 5;

private FloatBuffer vertexFloatBuffer;

private float vertices[];

int verticesCounter;

public GLWave3d() {
generujModel();
setBuffers();
}

private void generujModel() {
verticesCounter = 0;
vertices = new float[(int)(WIDTH * HEIGHT * 6 * 3)];

// Building square
for (float x = 0; x < WIDTH; x ++) {
for (float z = 0; z < HEIGHT; z ++) {
// Triangle 1
addVertexToVertexArray(x, 0, z);
addVertexToVertexArray(x + 1, 0, z + 1);
addVertexToVertexArray(x + 1, 0, z);

// Triangle 2
addVertexToVertexArray(x, 0, z);
addVertexToVertexArray(x, 0, z + 1);
addVertexToVertexArray(x + 1, 0, z + 1);
}
}
}

private void addVertexToVertexArray(float x, float y, float z) {
vertices[verticesCounter] = x;
vertices[verticesCounter + 1] = y;
vertices[verticesCounter + 2] = z;
verticesCounter += 3;
}

public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexFloatBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}

private void setBuffers() {
ByteBuffer verticesByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
verticesByteBuffer.order(ByteOrder.nativeOrder());

vertexFloatBuffer = verticesByteBuffer.asFloatBuffer();
vertexFloatBuffer.put(vertices);
vertexFloatBuffer.position(0);
}
}


Any idea what is wrong ?

rpodhajny
01-26-2011, 10:50 PM
The problem was wrong number of vertices declared in glDrawArray funkction:

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length);
The solution is to divide vertices.length by 3 because vertices array contains coordinates of wertices, not vertices and each coordinate (one point) is determined by three values (x, y, z):

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length/3);