Results 1 to 2 of 2

Thread: 2D sprite with transparency aera clic detection ?

  1. #1
    Guest

    2D sprite with transparency aera clic detection ?

    Hi,

    i want to detect a clic in a 2D sprite ?
    my sprite is a 2D texture quad (TGA) with transparency.
    i detect a clic correctly in the quad with the picking methode, but how can i detect a clic in the pixels of the sprite ?

    i work in double buffering :
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

    i haved try to draw the sprite in the buffer GL_AUX0, and read the pixel color at the clic coordonate, but i think i dont read the good buffer ?

    GLubyte* pixel;
    GLint* drawBuffer;
    GLint* readBuffer;

    pixel = new GLubyte[4];
    pixel[0] = 0;
    pixel[1] = 0;
    pixel[2] = 0;
    pixel[3] = 0;

    drawBuffer = new GLint[1];
    readBuffer = new GLint[1];
    /* saving the courant buffer */
    glGetIntegerv(GL_DRAW_BUFFER, drawBuffer);
    glGetIntegerv(GL_READ_BUFFER, readBuffer);
    /* draw in the GL_AUX0 buffer */
    glDrawBuffer(GL_AUX0);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_BACK);
    /* drawing the sprite (quad+texture) */
    lb[numEl].afficherElementGraphique();
    /* read in the GL_AUX0 buffer */
    glReadBuffer(GL_AUX0);
    /* read the pixel color */
    glReadPixels(x, y, 1, 1,GL_RGB, GL_UNSIGNED_BYTE, pixel);

    cout << "pixel[0]= " << (unsigned int)pixel[0] << endl;
    cout << "pixel[1]= " << (unsigned int)pixel[1] << endl;
    cout << "pixel[2]= " << (unsigned int)pixel[2] << endl;
    cout << "pixel[3]= " << (unsigned int)pixel[3] << endl;

    /* restore read and draw buffer */
    glDrawBuffer(*drawBuffer);
    glReadBuffer(*readBuffer);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    /* testing if the pixel is background or not */
    if (pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255)
    {
    cout << "non-touche" << endl;
    delete pixel;
    delete drawBuffer;
    delete readBuffer;
    return(0);
    }
    else
    {
    cout << "touche" << endl;
    delete pixel;
    delete drawBuffer;
    delete readBuffer;
    return(1);
    }

    but it is not working ?

    have you a solution ?

    kawito

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    Kristianstad,Skåne,Sweden
    Posts
    1,595

    Re: 2D sprite with transparency aera clic detection ?

    Hi !

    Reading colors from the framebuffer could be tricky, if you have other sprites that may overlap for example and so on.

    If it is all in two why use picking at all,
    it's easy to compare the mouse position with the position/width/height of the sprite and detect a hit, then you can easy find the local x,y coordinate inside the sprite.

    And if you keep a copy of the sprite in ram you can make a fast color lookup in that, that would be much faster then useing picking.

    Mikael

Similar Threads

  1. Depth testing in sprite based game with transparency
    By GeatMaster in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-18-2017, 08:19 PM
  2. Replies: 4
    Last Post: 06-16-2016, 04:47 AM
  3. OpenGL, SDL and sprite Collision detection
    By Andreas K in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 06-21-2010, 08:42 AM
  4. Get the window coordinates when you clic on the OpenGL window
    By tiregarfio in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-17-2006, 01:26 PM
  5. OpenGL window transparency and texture transparency
    By ramalhais in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-15-2002, 09:37 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
  •  
Proudly hosted by Digital Ocean