1/*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
11 *
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States.  Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
34 *
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
36 */
37
38/*
39 *  unproject.c
40 *  When the left mouse button is pressed, this program
41 *  reads the mouse position and determines two 3D points
42 *  from which it was transformed.  Very little is displayed.
43 */
44#include "glut_wrap.h"
45#include <stdlib.h>
46#include <stdio.h>
47
48static void display(void)
49{
50   glClear(GL_COLOR_BUFFER_BIT);
51   glFlush();
52}
53
54/* Change these values for a different transformation  */
55static void reshape(int w, int h)
56{
57   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
58   glMatrixMode(GL_PROJECTION);
59   glLoadIdentity();
60   gluPerspective (45.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
61   glMatrixMode(GL_MODELVIEW);
62   glLoadIdentity();
63}
64
65static void mouse(int button, int state, int x, int y)
66{
67   GLint viewport[4];
68   GLdouble mvmatrix[16], projmatrix[16];
69   GLint realy;  /*  OpenGL y coordinate position  */
70   GLdouble wx, wy, wz;  /*  returned world x, y, z coords  */
71
72   switch (button) {
73      case GLUT_LEFT_BUTTON:
74         if (state == GLUT_DOWN) {
75            glGetIntegerv (GL_VIEWPORT, viewport);
76            glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
77            glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
78/*  note viewport[3] is height of window in pixels  */
79            realy = viewport[3] - (GLint) y - 1;
80            printf ("Coordinates at cursor are (%4d, %4d)\n", x, realy);
81            gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
82               mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
83            printf ("World coords at z=0.0 are (%f, %f, %f)\n",
84               wx, wy, wz);
85            gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
86               mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
87            printf ("World coords at z=1.0 are (%f, %f, %f)\n",
88               wx, wy, wz);
89         }
90         break;
91      case GLUT_RIGHT_BUTTON:
92         if (state == GLUT_DOWN)
93            exit(0);
94         break;
95      default:
96         break;
97   }
98}
99
100/* ARGSUSED1 */
101static void keyboard(unsigned char key, int x, int y)
102{
103   switch (key) {
104      case 27:
105         exit(0);
106         break;
107   }
108}
109
110/*
111 *  Open window, register input callback functions
112 */
113int main(int argc, char** argv)
114{
115   glutInit(&argc, argv);
116   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
117   glutInitWindowSize (500, 500);
118   glutInitWindowPosition (100, 100);
119   glutCreateWindow (argv[0]);
120   glutDisplayFunc(display);
121   glutReshapeFunc(reshape);
122   glutKeyboardFunc (keyboard);
123   glutMouseFunc(mouse);
124   glutMainLoop();
125   return 0;
126}
127