Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:52:03+00:00 2026-05-26T19:52:03+00:00

I have a simple Square with openGL es 1 and Android 1.5. The square

  • 0

I have a simple Square with openGL es 1 and Android 1.5. The square is painted on the center of the screen.

I want that when the user press on the screen, or moves the finger on the screen, the square get’s moved to that position. For do this i tryed with GLuUnProject, i tryed to obtain the opengl coordinate that matches with the window XY coordinate touched with the finger (to translate the polygon to that coordinate in the future), and i’m writting the coordinates on the LogCat.

The coordinate’s i’m receiving are not true cordinates, are wrong coordinates. For example this logcat represents a horizontal movement to the right of the screen:

11-07 15:54:37.221: DEBUG/XXXXXXXXX(213): X: -0.003236022
11-07 15:54:37.221: DEBUG/XXXXXXXXX(213): Y: -0.047979668
11-07 15:54:37.241: DEBUG/XXXXXXXXX(213): X: -0.003236022
11-07 15:54:37.251: DEBUG/XXXXXXXXX(213): Y: -0.047807075
11-07 15:54:39.110: DEBUG/XXXXXXXXX(213): X: 0.03469036
11-07 15:54:39.110: DEBUG/XXXXXXXXX(213): Y: 0.04418271
11-07 15:54:43.021: DEBUG/XXXXXXXXX(213): X: -0.029469538
11-07 15:54:43.021: DEBUG/XXXXXXXXX(213): Y: -0.034172554
11-07 15:54:43.051: DEBUG/XXXXXXXXX(213): X: -0.026708115
11-07 15:54:43.051: DEBUG/XXXXXXXXX(213): Y: -0.034172554
11-07 15:54:43.081: DEBUG/XXXXXXXXX(213): X: -0.018596433
11-07 15:54:43.081: DEBUG/XXXXXXXXX(213): Y: -0.034172554
11-07 15:54:43.111: DEBUG/XXXXXXXXX(213): X: -0.013073588
11-07 15:54:43.111: DEBUG/XXXXXXXXX(213): Y: -0.034172554
11-07 15:54:43.141: DEBUG/XXXXXXXXX(213): X: -0.0039263717
11-07 15:54:43.141: DEBUG/XXXXXXXXX(213): Y: -0.033999965
11-07 15:54:43.162: DEBUG/XXXXXXXXX(213): X: -0.0011649576
11-07 15:54:43.162: DEBUG/XXXXXXXXX(213): Y: -0.033827372
11-07 15:54:43.191: DEBUG/XXXXXXXXX(213): X: 7.335304E-4
11-07 15:54:43.191: DEBUG/XXXXXXXXX(213): Y: -0.033654787

This is the source code:

public class MySurfaceView extends GLSurfaceView implements Renderer {  
private float INITIAL_Z = -35.0f;   
private Context context;
private Square square;
private float xrot;                 //X Rotation
private float yrot;                 //Y Rotation
private float zrot;                 //Z Rotation    
private float z = INITIAL_Z;            //Profundidad en el eje Z
private float x = 0.0f;             //eje X
private float y = 0.0f;             //eje Y

private MatrixGrabber mg = new MatrixGrabber(); //create the matrix grabber object in your initialization code    
byte horizontal=-1; //0: LEFT  1:CENTER  2:RIGHT
byte vertical=-1; //0: TOP  1:CENTER  2:BOTTOM
float startX=-1;
float startY=-1;
float xMovement=0.0f;
float yMovement=0.0f;
private boolean movement_mode=false;

public MySurfaceView(Context context, Bitmap image, int width, byte horizontal, byte vertical) {
    super(context);
    this.context = context;
    setEGLConfigChooser(8, 8, 8, 8, 16, 0); //fondo transparente
    getHolder().setFormat(PixelFormat.TRANSLUCENT); //fondo transparente
    //Transformamos esta clase en renderizadora
    this.setRenderer(this);
    //Request focus, para que los botones reaccionen
    this.requestFocus();
    this.setFocusableInTouchMode(true);
    square = new Square(image);
    this.horizontal=horizontal;
    this.vertical=vertical;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    MyGl=gl;
    gl.glDisable(GL10.GL_DITHER);               //dithering OFF
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Texture Mapping ON
    gl.glShadeModel(GL10.GL_SMOOTH);            //Smooth Shading 
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Depth Testing ON
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glClearColor(0,0,0,0); //fondo transparente
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);         
    //Cargamos la textura del cubo.
    square.loadGLTexture(gl, this.context);     
}

public void onDrawFrame(GL10 gl) {
    //Limpiamos pantalla y Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    //Dibujado
    gl.glTranslatef(x, y, z);           //Move z units into the screen
    //gl.glScalef(0.8f, 0.8f, 0.8f);            //Escalamos para que quepa en la pantalla
    //Rotamos sobre los ejes.
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
    gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);   //Z
    //Dibujamos el cuadrado
    square.draw(gl);    
    mg.getCurrentProjection(gl);
    mg.getCurrentModelView(gl);

}

//si el surface cambia, resetea la vista, imagino que esto pasa cuando cambias de modo portrait/landscape o sacas el teclado físico en móviles tipo Droid.
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       
        height = 1;                         
    }
    gl.glViewport(0, 0, width, height);     //Reset Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select Projection Matrix
    gl.glLoadIdentity();                    //Reset Projection Matrix
    //Aspect Ratio de la ventana
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select Modelview Matrix
    gl.glLoadIdentity();                    //Reset Modelview Matrix
}

public boolean onTouchEvent(MotionEvent event) {
    float [] outputCoords=getOpenGLCoords(event.getX(), event.getY(), 0);
    x=(outputCoords[0]/outputCoords[3]);
    y=(outputCoords[1]/outputCoords[3]);
    //z=outputCoords[2]/outputCoords[3];
    Log.d("XXXXXXXXX", "X: "+x);
    Log.d("XXXXXXXXX", "Y: "+y);        
    return true; //El evento ha sido manejado
}

public float[] getOpenGLCoords(float xWin,float yWin,float zWin)
{
    int screenW=SectionManager.instance.getDisplayWidth();
    int screenH=SectionManager.instance.getDisplayHeight();
    //CODE FOR TRANSLATING FROM SCREEN COORDINATES TO OPENGL COORDINATES
    float [] modelMatrix = new float[16];
    float [] projMatrix = new float[16];
    modelMatrix=mg.mModelView;
    projMatrix=mg.mProjection;          
    int [] mView = new int[4];
    mView[0] = 0;
    mView[1] = 0;
    mView[2] = screenW; //width
    mView[3] = screenH; //height
    float [] outputCoords = new float[4];
    GLU.gluUnProject(xWin, ((float)screenH)-yWin, zWin, modelMatrix, 0, projMatrix, 0, mView, 0, outputCoords, 0);
    return outputCoords;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:52:03+00:00Added an answer on May 26, 2026 at 7:52 pm

    Your values seem quite reasonable. You get an x value that becomes larger and larger (negative values with decreasing absolute value) and a y value that stays roughly the same, which corresponds to a movement to the right (assuming you don’t have any non-zero rotation in your transformation pipeline).

    And a resulting z coordinate of 4.7 is also reasonable, given a z-translation of -4.8 and a near value of 0.1. And with such a z-translation and a field of view of 45 degrees, your resulting x and y values should indeed be quite small (roughly around [-1,1], I think).

    So I think your code works just like it should. What coordinates would you have expected? Please don’t say something on the order of pixels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that creates a square that covers the entire screen,
I have a simple rectangle i have drawn on screen in opengl. My target
I have some simple code that represents a graph using a square boolean matrix
I have a simple Android OpenGL-ES app, and as all the models are very
i have an application that shows a simple square polygon represented on a GLSurfaceView.
I have simple win service, that executes few tasks periodically. How should I pass
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have a simple Javascript program that displays a small rectangle in a canvas.
I have a table that is 26 squares by 26 squares. Each square is
I am trying to write a simple matrixMultiplication application that multiplies two square matrices

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.