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

Apps OpenGL Cube

Render the cube at the origin, then use the GL10.glTranslatef() method to move it to the proper location before displaying it. The glTranslatef() method takes 3 float values as its paremeters and these values correspond to the distanaces from the origin to move on th x, y and z axi, respectively.
 
one more question, is I managed to put one square on the top of the screen, and i want to put the other one on the right side of the screen (left side is 6f,9f,0f and right should be -6f,-9f,0f) but it won't work and it stays in the middle of the screen
EDIT: is there way to put a OnClickListener on the square? thank you
 
Can you show the code where you initialize the opengl world and also the onDraw method? Im not quite understanding you.
 
Code:
private GLSquare square;
	private GLCube cube;
	private GLLines lines;
	private GLTriangle3D tri;

	@Override
	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, 0f, -10f, 0f, 0f, 0f, 0f, 2f, 0f);
		gl.glTranslatef(6f, 9f, 0f);
		square.draw(gl, 1, 0, 0, 1);
		long time = SystemClock.uptimeMillis() % 4000L;
		float angle = 0.090f * ((int) time);
		gl.glTranslatef(-6f, -9f, 0f);
		gl.glRotatef(angle, 1, 0, 0);
		lines.draw(gl, 0f, 0f, 0f, 1f);
		cube.draw(gl, 1, 0.5f, 0.25f, 0.5f);
		tri.draw(gl, .25f, .5f, .75f, 1);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int i, int j) {
		gl.glViewport(0, 0, i, j);
		float ratio = (float) i / j;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
		gl.glClearColor(1f, 1f, 1f, 1);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
		gl.glDisable(GL10.GL_DITHER);
		gl.glDisable(GL10.GL_DEPTH_TEST);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
		gl.glClearDepthf(1f);
	}

	public GLGameRenderer() {
		square = new GLSquare();
		cube = new GLCube();
		lines = new GLLines();
		tri = new GLTriangle3D();
	}
I want for every square that i draw to make a listener to handle a touch event (or a click event) for it
 
You cant simply use an onclicklistener with a GLSurfaceView. Youll need to override the onTouch method of Activity, take the point obtained from the touch event and convert it to world coordinates using gluUnproject()
 
Back
Top Bottom