Results 1 to 2 of 2

Thread: Mysterious vertices after scece is rendered

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2

    Mysterious vertices after scece is rendered

    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).


    And some code:

    View class
    Code :
    package com.wordpress.rpodhajny.openGL;
     
    import android.content.Context;
    import android.opengl.GLSurfaceView;
     
    import com.wordpress.rpodhajny.openGL.renderers.GLWave3DRenderer;
     
    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
    Code :
    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
    Code :
    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 ?

  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    2

    Re: Mysterious vertices after scece is rendered

    The problem was wrong number of vertices declared in glDrawArray funkction:
    Code :
    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):
    Code :
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length/3);

Similar Threads

  1. Replies: 7
    Last Post: 05-20-2010, 06:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •