• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Drawing more than one square OpenGLES 2.0

I've been having trouble trying to draw more than one square in OpenGLES 2.0, and have been experimenting for a good hour and a half now. Can anyone else figure out what's going wrong?

Adding vertex data:
Code:
for(int i=0; i<height; i++){
			for(int j=0; j<width; j++){
				
				drawOrder.add((short)(squareCoords.size()+0));
				drawOrder.add((short)(squareCoords.size()+1));
				drawOrder.add((short)(squareCoords.size()+2));
				drawOrder.add((short)(squareCoords.size()+0));
				drawOrder.add((short)(squareCoords.size()+1));
				drawOrder.add((short)(squareCoords.size()+3));
				
				squareCoords.add((float)j*TILE_SIZE);
				squareCoords.add((float)i*TILE_SIZE);
				squareCoords.add(0f);
				squareCoords.add((float)j*TILE_SIZE);
				squareCoords.add((float)(i+1)*TILE_SIZE);
				squareCoords.add(0f);
				squareCoords.add((float)(i+1)*TILE_SIZE);
				squareCoords.add((float)(i+1)*TILE_SIZE);
				squareCoords.add(0f);
				squareCoords.add((float)(i+1)*TILE_SIZE);
				squareCoords.add((float)i*TILE_SIZE);
				squareCoords.add(0f);
				
				cubeTextureCoordinateData.add(0f);
				cubeTextureCoordinateData.add(0f);
				cubeTextureCoordinateData.add(0f);
				cubeTextureCoordinateData.add(1f);
				cubeTextureCoordinateData.add(1f);
				cubeTextureCoordinateData.add(1f);
				cubeTextureCoordinateData.add(1f);
				cubeTextureCoordinateData.add(0f);
			}
		}
		
		// initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
        // (# of coordinate values * 4 bytes per float)
                squareCoords.size() * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
		for(int i=0; i<squareCoords.size(); i++){
	        vertexBuffer.put(squareCoords.get(i));
	        }
		vertexBuffer.position(0);
		
		// initialize byte buffer for the draw list
        ByteBuffer dlb = ByteBuffer.allocateDirect(
        // (# of coordinate values * 2 bytes per short)
                drawOrder.size() * 2);
        dlb.order(ByteOrder.nativeOrder());
        drawListBuffer = dlb.asShortBuffer();
		for(int i=0; i<drawOrder.size(); i++){
	        drawListBuffer.put(drawOrder.get(i));
	        }
		drawListBuffer.position(0);
		
        ByteBuffer tb = ByteBuffer.allocateDirect(cubeTextureCoordinateData.size() * 4);
        tb.order(ByteOrder.nativeOrder());
        mCubeTextureCoordinates = tb.asFloatBuffer();
        for(int i=0; i<cubeTextureCoordinateData.size(); i++){
        mCubeTextureCoordinates.put(cubeTextureCoordinateData.get(i));
        }
        mCubeTextureCoordinates.position(0);

Draw method:
Code:
// Add program to OpenGL ES environment
	    GLES20.glUseProgram(mProgram);
	    
	    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
	    GLES20.glEnable(GLES20.GL_BLEND);

	    // get handle to vertex shader's vPosition member
	    int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

	    // Enable a handle to the triangle vertices
	    GLES20.glEnableVertexAttribArray(mPositionHandle);

	    // Prepare the triangle coordinate data
	    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
	                                 GLES20.GL_FLOAT, false,
	                                 vertexStride, vertexBuffer);

	    // get handle to fragment shader's vColor member
	    int mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

	    // Set color for drawing the triangle
	    GLES20.glUniform4fv(mColorHandle, 1, color, 0);
	    
	  //Set Texture Handles and bind Texture
	    mTextureUniformHandle = GLES20.glGetAttribLocation(mProgram, "u_Texture");
	    mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgram, "a_TexCoordinate");

	    //Set the active texture unit to texture unit 0.
	    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

	    //Bind the texture to this unit.
	    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);

	    //Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
	    GLES20.glUniform1i(mTextureUniformHandle, 0); 

	    //Pass in the texture coordinate information
	    mCubeTextureCoordinates.position(0);
	    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, mTextureCoordinateDataSize, GLES20.GL_FLOAT, false, 0, mCubeTextureCoordinates);
	    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
	    
	    //GLES20.glDisable(GLES20.GL_BLEND);
	    
	 // get handle to shape's transformation matrix
	    int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");

	    // Apply the projection and view transformation
	    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
	    
	  //Enable Alpha blending and set blending function
	    GLES20.glEnable(GLES20.GL_BLEND); 
	    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

	    // Draw the triangle
	    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
	    
	  //Disable Alpha blending
	    GLES20.glDisable(GLES20.GL_BLEND);

	    // Disable vertex array
	    GLES20.glDisableVertexAttribArray(mPositionHandle);
 
Back
Top Bottom