aquawicket
aquawicket

Reputation: 584

Get (X,Y) Pixel color from drawn Vertex & Color array in OPENGL

I would like to get a pixel color or a drawn object in OpenGL. For example, if I draw a circle to the context, it will be held and drawn in a object class. It will have a width and height. And if I click on the circle, it should return the color. BUT... if I click toward the corner of the object, it should return alpha 0 to let me know I have clicked on the circle object, but in a transparent area of the object. Do I need to draw to a Pixel Buffer Object?

enter image description here

 void Circle::Display()
 {

      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();

      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glVertexPointer(2, GL_FLOAT, 0, circle_vectors);
      glColorPointer(4, GL_FLOAT, 0, circle_colors);

      glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY); // enables the color-array.
      glDisable(GL_BLEND);

      glPopMatrix();
 }


 bool Circle::IsTransparent(int x, int y)
 {
      //We clicked on the circle object. Is the pixel transparent?
 }

Upvotes: 0

Views: 280

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

I would use the general form for any simple geometric shape first and foremost. Circles, squares, triangles, etc... you can find if a point is within the bounds of all of these with some simple math. So if at all possible, subclass your Shape class to speed up testing against simple shapes.

For arbitrary shapes, or shapes with unusual transform characteristics you may need to do a pixel read-back or maybe an occlusion query.

Upvotes: 1

Related Questions