- #include <iostream>
- #include "GLUT/glut.h"
- using namespace std;
- int window;
- /* _______________________________________________________________ */
- /* _____________________ GLOBAL FUNCTIONS ______________________ */
- void reshape(int width, int height) {
- if (height <= 0) height = 1;
- double ratio(width);
- ratio /= height;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, width, height);
- gluPerspective(45.0, ratio, 1.0, 1000.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void idle(void) {
- glutPostRedisplay();
- glutIdleFunc(0);
- }
- void keyboard(unsigned char key, int mouse_x, int mouse_y) {
- switch (key) {
- case 27 :
- case 'q':
- case 'Q':
- glutDestroyWindow(window);
- exit(0);
- break;
- }
- glutPostRedisplay();
- }
- int locate_body(int x, int y);
- void mouse(int button, int state, int x, int y) {
- switch (button) {
- case GLUT_RIGHT_BUTTON:
- if (state==GLUT_DOWN) {
- int index(locate_body(x,y));
- if (index >= 0) {
- }
- cout << "clicked on Body nr "<< index << endl;
- }
- break;
- }
- }
- void display() {
- glMatrixMode(GL_MODELVIEW);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt( 0, 0, -100,
- 0, 30, 0, /////// ********siehe kommentar unten********
- 5, 0, 0);
- glInitNames();
- glPushName(0);
- glPushMatrix(); // save current settings
- glLoadName(1);//add glut identifier
- glColor4d(1.0,1.0,1.0,1.0);
- glutSolidSphere(4, 30, 30);
- glPopMatrix(); //return to previous settings
- glutSwapBuffers();
- }
- int locate_body(int x,int y) {
- // inspired by wiki.delphigl.com/index.php/Tutorial_Selection
- GLint viewport[4];
- cout << "locate_body" << endl;
- glGetIntegerv(GL_VIEWPORT, viewport);
- GLuint buffer[256];
- glSelectBuffer(256,buffer); //Den Puffer zuordnen
- glMatrixMode(GL_PROJECTION); //In den Projektionsmodus
- glRenderMode(GL_SELECT); //In den Selectionsmodus schalten
- glPushMatrix(); //Um unsere Matrix zu sichern
- glLoadIdentity(); //Und dieselbige wieder zurückzusetzen
- gluPickMatrix(x, y, 5.0, 5.0, viewport);
- gluPerspective(45.0, viewport[2]/viewport[3], 1.0, 1000);
- display(); //Die Szene zeichnen
- glMatrixMode(GL_PROJECTION); //Wieder in den Projektionsmodus
- glPopMatrix(); //und unsere alte Matrix wiederherzustellen
- int hits(glRenderMode(GL_RENDER)); //Anzahl der Treffer auslesen
- cout << "#hits: " <<hits << endl;
- if (hits!=0) {
- GLuint touched(buffer[3]);
- GLuint z_coordinate(buffer[1]);
- for (int i(1); i<hits; ++i) {
- if (buffer[(i*4)+1] < z_coordinate) {
- touched = buffer[(i*4)+3];
- z_coordinate = buffer[(i*4)+1];
- }
- }
- return touched-1;
- }
- else return -3;
- }
- int main(int argc, char *argv[]) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- window = glutCreateWindow("Press Q to exit");
- glutDisplayFunc(display);
- glutIdleFunc(idle);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutReshapeFunc(reshape);
- // enables a correct display of the hidden parts
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glutMainLoop();
- return 0;
- }