Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
OpenGL Development Cookbook

You're reading from   OpenGL Development Cookbook OpenGL brings an added dimension to your graphics by utilizing the remarkable power of modern GPUs. This straight-talking cookbook is perfect for intermediate C++ programmers who want to exploit the full potential of OpenGL.

Arrow left icon
Product type Paperback
Published in Jun 2013
Publisher Packt
ISBN-13 9781849695046
Length 326 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
 Movania Movania
Author Profile Icon Movania
Movania
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

OpenGL Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Introduction to Modern OpenGL FREE CHAPTER 2. 3D Viewing and Object Picking 3. Offscreen Rendering and Environment Mapping 4. Lights and Shadows 5. Mesh Model Formats and Particle Systems 6. GPU-based Alpha Blending and Global Illumination 7. GPU-based Volume Rendering Techniques 8. Skeletal and Physically-based Simulation on the GPU Index

Implementing object picking using color


Another method which is used for picking objects in a 3D world is color-based picking. In this recipe, we will use the same scene as in the last recipe.

Getting ready

The code for this recipe is in the Chapter2/Picking_ColorBuffer folder. Relevant source files are in the Chapter2/src folder.

How to do it…

To enable picking with the color buffer, the following steps are needed:

  1. Disable dithering. This is done to prevent any color mismatch during the query:

    glDisable(GL_DITHER);
  2. In the mouse down event handler, read the color value at the clicked position from the color buffer using the glReadPixels function:

    GLubyte pixel[4];
    glReadPixels(x, HEIGHT-y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
  3. Compare the color value at the clicked point to the color values of all objects to find the intersection:

    selected_box=-1;
    if(pixel[0]==255 && pixel[1]==0 && pixel[2]==0) {
      cout<<"picked box 1"<<endl;
      selected_box = 0;
    }
    if(pixel[0]==0 &amp...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images