2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
43 GLuint selectBuf
[MAXSELECT
];
44 GLfloat feedBuf
[MAXFEED
];
46 float zRotation
= 90.0;
56 GLenum linePoly
= GL_FALSE
;
59 static void InitObjects(GLint num
)
72 srand((unsigned int)time(NULL
));
73 for (i
= 0; i
< num
; i
++) {
74 x
= (rand() % 300) - 150;
75 y
= (rand() % 300) - 150;
77 objects
[i
].v1
[0] = x
+ (rand() % 50) - 25;
78 objects
[i
].v2
[0] = x
+ (rand() % 50) - 25;
79 objects
[i
].v3
[0] = x
+ (rand() % 50) - 25;
80 objects
[i
].v1
[1] = y
+ (rand() % 50) - 25;
81 objects
[i
].v2
[1] = y
+ (rand() % 50) - 25;
82 objects
[i
].v3
[1] = y
+ (rand() % 50) - 25;
83 objects
[i
].color
[0] = ((rand() % 100) + 50) / 150.0;
84 objects
[i
].color
[1] = ((rand() % 100) + 50) / 150.0;
85 objects
[i
].color
[2] = ((rand() % 100) + 50) / 150.0;
89 static void Init(void)
93 InitObjects(numObjects
);
94 glGetIntegerv(GL_VIEWPORT
, vp
);
97 static void Reshape(int width
, int height
)
100 windW
= (GLint
)width
;
101 windH
= (GLint
)height
;
104 static void Render(GLenum mode
)
108 for (i
= 0; i
< objectCount
; i
++) {
109 if (mode
== GL_SELECT
) {
112 glColor3fv(objects
[i
].color
);
114 glVertex2fv(objects
[i
].v1
);
115 glVertex2fv(objects
[i
].v2
);
116 glVertex2fv(objects
[i
].v3
);
121 static GLint
DoSelect(GLint x
, GLint y
)
125 glSelectBuffer(MAXSELECT
, selectBuf
);
126 (void)glRenderMode(GL_SELECT
);
132 glViewport(0, 0, windW
, windH
);
133 glGetIntegerv(GL_VIEWPORT
, vp
);
135 glMatrixMode(GL_PROJECTION
);
137 gluPickMatrix(x
, windH
-y
, 4, 4, vp
);
138 gluOrtho2D(-175, 175, -175, 175);
139 glMatrixMode(GL_MODELVIEW
);
141 glClearColor(0.0, 0.0, 0.0, 0.0);
142 glClear(GL_COLOR_BUFFER_BIT
);
144 glScalef(zoom
, zoom
, zoom
);
145 glRotatef(zRotation
, 0, 0, 1);
151 hits
= glRenderMode(GL_RENDER
);
156 return selectBuf
[(hits
-1)*4+3];
159 static void RecolorTri(GLint h
)
162 objects
[h
].color
[0] = ((rand() % 100) + 50) / 150.0;
163 objects
[h
].color
[1] = ((rand() % 100) + 50) / 150.0;
164 objects
[h
].color
[2] = ((rand() % 100) + 50) / 150.0;
167 static void DeleteTri(GLint h
)
170 objects
[h
] = objects
[objectCount
-1];
174 static void GrowTri(GLint h
)
180 v
[0] = objects
[h
].v1
[0] + objects
[h
].v2
[0] + objects
[h
].v3
[0];
181 v
[1] = objects
[h
].v1
[1] + objects
[h
].v2
[1] + objects
[h
].v3
[1];
185 for (i
= 0; i
< 3; i
++) {
188 oldV
= objects
[h
].v1
;
191 oldV
= objects
[h
].v2
;
194 oldV
= objects
[h
].v3
;
197 oldV
[0] = 1.5 * (oldV
[0] - v
[0]) + v
[0];
198 oldV
[1] = 1.5 * (oldV
[1] - v
[1]) + v
[1];
202 static void Mouse(int button
, int state
, int mouseX
, int mouseY
)
206 if (state
!= GLUT_DOWN
)
209 hit
= DoSelect((GLint
)mouseX
, (GLint
)mouseY
);
211 if (button
== GLUT_LEFT_BUTTON
) {
214 if (button
== GLUT_MIDDLE_BUTTON
) {
217 if (button
== GLUT_RIGHT_BUTTON
) {
225 static void Draw(void)
230 glViewport(0, 0, windW
, windH
);
231 glGetIntegerv(GL_VIEWPORT
, vp
);
233 glMatrixMode(GL_PROJECTION
);
235 gluOrtho2D(-175, 175, -175, 175);
236 glMatrixMode(GL_MODELVIEW
);
238 glClearColor(0.0, 0.0, 0.0, 0.0);
239 glClear(GL_COLOR_BUFFER_BIT
);
241 glScalef(zoom
, zoom
, zoom
);
242 glRotatef(zRotation
, 0, 0, 1);
251 static void DrawZoom(GLint x
, GLint y
)
256 glViewport(0, 0, windW
, windH
);
257 glGetIntegerv(GL_VIEWPORT
, vp
);
259 glMatrixMode(GL_PROJECTION
);
261 gluPickMatrix(x
, windH
-y
, 4, 4, vp
);
262 gluOrtho2D(-175, 175, -175, 175);
263 glMatrixMode(GL_MODELVIEW
);
265 glClearColor(0.0, 0.0, 0.0, 0.0);
266 glClear(GL_COLOR_BUFFER_BIT
);
268 glScalef(zoom
, zoom
, zoom
);
269 glRotatef(zRotation
, 0, 0, 1);
276 static void DumpFeedbackVert(GLint
*i
, GLint n
)
286 printf(" (%g %g %g), color = (%4.2f %4.2f %4.2f)\n",
297 static void DrawFeedback(GLint n
)
302 printf("Feedback results (%d floats):\n", n
);
303 for (i
= 0; i
< n
; i
++) {
304 switch ((GLint
)feedBuf
[i
]) {
305 case GL_POLYGON_TOKEN
:
309 verts
= (GLint
)feedBuf
[i
];
311 printf(": %d vertices", verts
);
317 DumpFeedbackVert(&i
, n
);
325 DumpFeedbackVert(&i
, n
);
326 DumpFeedbackVert(&i
, n
);
329 case GL_LINE_RESET_TOKEN
:
330 printf("Line Reset:\n");
332 DumpFeedbackVert(&i
, n
);
333 DumpFeedbackVert(&i
, n
);
337 printf("%9.2f\n", feedBuf
[i
]);
347 static void DoFeedback(void)
351 glFeedbackBuffer(MAXFEED
, GL_3D_COLOR
, feedBuf
);
352 (void)glRenderMode(GL_FEEDBACK
);
356 glViewport(0, 0, windW
, windH
);
357 glGetIntegerv(GL_VIEWPORT
, vp
);
359 glMatrixMode(GL_PROJECTION
);
361 gluOrtho2D(-175, 175, -175, 175);
362 glMatrixMode(GL_MODELVIEW
);
364 glClearColor(0.0, 0.0, 0.0, 0.0);
365 glClear(GL_COLOR_BUFFER_BIT
);
367 glScalef(zoom
, zoom
, zoom
);
368 glRotatef(zRotation
, 0, 0, 1);
374 x
= glRenderMode(GL_RENDER
);
379 DrawFeedback((GLint
)x
);
382 static void Key2(int key
, int x
, int y
)
398 static void Key(unsigned char key
, int x
, int y
)
416 linePoly
= !linePoly
;
418 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
420 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
430 int main(int argc
, char **argv
)
434 glutInit(&argc
, argv
);
438 glutInitWindowPosition(0, 0); glutInitWindowSize( windW
, windH
);
440 type
= GLUT_RGB
| GLUT_SINGLE
;
441 glutInitDisplayMode(type
);
443 if (glutCreateWindow("Select Test") == GL_FALSE
) {
449 glutReshapeFunc(Reshape
);
450 glutKeyboardFunc(Key
);
451 glutSpecialFunc(Key2
);
452 glutMouseFunc(Mouse
);
453 glutDisplayFunc(Draw
);