firstly I'm really new to opengl on androidand I'm still looking though courses online
I'm trying to make a simply appthat has a sequre in the middle of the screen and on touch it's moves to the touch event coordinate this is my sureface view
public class MyGLSurfaceView extends GLSurfaceView {
private final MyGLRenderer mRenderer;
public MyGLSurfaceView(Context context) {
super(context);
// Set the Renderer for drawing on the GLSurfaceView
mRenderer = new MyGLRenderer();
setRenderer(mRenderer);
// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent e)
{
float x =e.getX();
float y =e.getY();
if(e.getAction()==MotionEvent.ACTION_MOVE)
{
mRenderer.setLoc(x,y);
requestRender();
}
return true;
}
}
and this is me renderer class
public class MyGLRenderer implements GLSurfaceView.Renderer {
private float mAngle;
public PVector pos;
public Rectangle r;
public Square sq;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background frame color
gl.glClearColor(1f, 1f, 1f, 1.0f);
pos=new PVector();
r=new Rectangle(0.5f,0.4f);
sq=new Square(0.3f);
}
@Override
public void onDrawFrame(GL10 gl) {
// Draw background color
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Set GL_MODELVIEW transformation mode
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity(); // reset the matrix to its default state
// When using GL_MODELVIEW, you must set the view point
GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glTranslatef(pos.x,pos.y,0f);
//r.draw(gl);
sq.draw(gl);
}//rend
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// Adjust the viewport based on geometry changes
// such as screen rotations
gl.glViewport(0, 0, width, height);
// make adjustments for screen ratio
float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // apply the projection matrix
}
/**
* Returns the rotation angle of the triangle shape (mTriangle).
*
* @return - A float representing the rotation angle.
*/
public float getAngle() {
return mAngle;
}
/**
* Sets the rotation angle of the triangle shape (mTriangle).
*/
public void setAngle(float angle) {
mAngle = angle;
}
public void setLoc(float x,float y)
{
pos.x=x; pos.y=y;
}
}
When asking a question you should add what is your current result and what is your expected result.
From a short glance at your code I would expect the square is drawn correctly until you touch the screen after which it disappears completely (unless you press on a top left part of the screen actually).
If this is the case your problem is only that you are not transforming the touch coordinates to the openGL coordinate system. You openGL coordinate system is by default in range [-1,1]
in all axis but you may change it (as you do) with matrices. The 2 most common are glFrustum
and glOrtho
both of these accept 4 border coordinates which are left, right, bottom and top which represent what value is at corresponding border of the view.
So to compute x from the touch for instance you would first normalize it to see what part of the screen you pressed by relativeX = touch.x / view.size.width
then this will present a coordinate in openGL such that you do glX = left + (right-left)*relativeX
. Similar is for the vertical coordinate.
But in your case it would be better to use 2D and use glOrtho
while using view coordinates. This means replacing the frustum call with this one and setting left=.0
, right=viewWidth
, top=.0
, bottom=viewHeight
. Now you will have the same coordinate system in openGL as you do on the view. You will need to increase the square size to see it since it is very small at this point. Also you should then remove the lookAt
and just use identity + translate.