$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Figure 42
======================
Note: It responds to clicks. The second windows displays which window was clicked and other coordinates.
========================
/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If not, you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
// pick.cpp : Defines the entry point for the console application.
//
#include
#include
#include
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushName(0);
glColor3ub(255, 0, 0);
glRectf(0.25, -0.25, 0.75, 0.25);
glLoadName(1);
glColor3ub(0, 255, 0);
glRectf(-0.25, 0.25, 0.25, 0.75);
glLoadName(2);
glColor3ub(0, 0, 255);
glRectf(-0.75, -0.25, -0.25, 0.25);
glLoadName(3);
glColor3ub(255, 255, 0);
glRectf(-0.25, -0.75, 0.25, -0.25);
glFlush();
if (glGetError() != GL_NO_ERROR)
printf("OpenGL has reported an error.\n");
}
////////////////////////////////
// Keyboard Callback Function //
////////////////////////////////
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q') exit(0);
}
/////////////////////////////
// Mouse Callback Function //
/////////////////////////////
#define SIZE 8
void mouse(int button, int state, int x, int y)
{
GLint i, j, k = 0, nhits, nnames, selectedObjectsInfo[SIZE], viewport[4];
char description[4][7] = {"RED", "GREEN", "BLUE", "YELLOW"};
GLubyte color[3];
float xx, yy;
if (state == GLUT_UP) return;
printf("--------------------------------------------\n");
if (button == GLUT_LEFT_BUTTON)
{
// Use OpenGL's selection mode
GLuint *xz=(GLuint*)selectedObjectsInfo;
glSelectBuffer(SIZE, xz);
glRenderMode(GL_SELECT);
glInitNames();
glPushMatrix();
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(x, viewport[3] - y, 5, 5, viewport);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
display();
nhits = glRenderMode(GL_RENDER);
glPopMatrix();
printf("nhits = %d\n", nhits);
for (i=0; i
nnames = selectedObjectsInfo[k];
printf("nhit %d:\n", i);
printf("k = %d: The number of names is %d\n", k, selectedObjectsInfo[k]); k++;
printf("k = %d: The minimum depth is %d\n", k, selectedObjectsInfo[k]); k++;
printf("k = %d: The maximum depth is %d\n", k, selectedObjectsInfo[k]); k++;
for (j=0; j
printf("k = %d: The selected object has name %d\n", k, selectedObjectsInfo[k]);
k++;
}
printf("\nThe %s rectangle was selected::, k:%d, nhits::%d\n", description[selectedObjectsInfo[k-1]], k-1, nhits);
}
}
else // the right (or middle) mouse button was pressed
{
// Get the color of the pixel that the mouse points to
y = glutGet(GLUT_WINDOW_HEIGHT) - y;
glReadPixels(x, y, 1, 1, GL_RGB ,GL_UNSIGNED_BYTE, color);
if (color[0] > 0 && color[1] > 0) printf("The cursor is on a YELLOW pixel\n");
else if ( color[0] > 0) printf("The cursor is on a RED pixel\n");
else if ( color[1] > 0) printf("The cursor is on a GREEN pixel\n");
else if ( color[2] > 0) printf("The cursor is on a BLUE pixel\n");
// Calculate the world coordinates (xx,yy) and check to see if they are in a colored rectangle
xx = (2.0 / glutGet(GLUT_WINDOW_WIDTH)) * x - 1.0;
yy = (2.0 / glutGet(GLUT_WINDOW_HEIGHT)) * y - 1.0;
printf("\nWorld coordinates: (%f,%f)\n", xx, yy);
if (-0.25 < yy && yy < 0.25)
{
if (-0.75 < xx && xx < -0.25)
printf("That is within the BLUE rectangle\n");
else if (0.25 < xx && xx < 0.75)
printf("That is within the RED rectangle\n");
}
if (-0.25 < xx && xx < 0.25)
{
if (-0.75 < yy && yy < -0.25)
printf("That is within the YELLOW rectangle\n");
else if (0.25 < yy && yy < 0.75)
printf("That is within the GREEN rectangle\n");
}
}
}
//////////////////
// Main Program //
//////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutInitWindowPosition(10,10);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glutMainLoop();
return 0;
}
No comments:
Post a Comment