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
);
96 #if 0 /* debug - test culling */
99 glEnable(GL_CULL_FACE
);
103 static void Reshape(int width
, int height
)
106 windW
= (GLint
)width
;
107 windH
= (GLint
)height
;
110 static void Render(GLenum mode
)
114 for (i
= 0; i
< objectCount
; i
++) {
115 if (mode
== GL_SELECT
) {
118 glColor3fv(objects
[i
].color
);
120 glVertex2fv(objects
[i
].v1
);
121 glVertex2fv(objects
[i
].v2
);
122 glVertex2fv(objects
[i
].v3
);
127 static GLint
DoSelect(GLint x
, GLint y
)
131 glSelectBuffer(MAXSELECT
, selectBuf
);
132 (void)glRenderMode(GL_SELECT
);
138 glViewport(0, 0, windW
, windH
);
139 glGetIntegerv(GL_VIEWPORT
, vp
);
141 glMatrixMode(GL_PROJECTION
);
143 gluPickMatrix(x
, windH
-y
, 4, 4, vp
);
144 gluOrtho2D(-175, 175, -175, 175);
145 glMatrixMode(GL_MODELVIEW
);
147 glClearColor(0.0, 0.0, 0.0, 0.0);
148 glClear(GL_COLOR_BUFFER_BIT
);
150 glScalef(zoom
, zoom
, zoom
);
151 glRotatef(zRotation
, 0, 0, 1);
157 hits
= glRenderMode(GL_RENDER
);
162 return selectBuf
[(hits
-1)*4+3];
165 static void RecolorTri(GLint h
)
168 objects
[h
].color
[0] = ((rand() % 100) + 50) / 150.0;
169 objects
[h
].color
[1] = ((rand() % 100) + 50) / 150.0;
170 objects
[h
].color
[2] = ((rand() % 100) + 50) / 150.0;
173 static void DeleteTri(GLint h
)
176 objects
[h
] = objects
[objectCount
-1];
180 static void GrowTri(GLint h
)
186 v
[0] = objects
[h
].v1
[0] + objects
[h
].v2
[0] + objects
[h
].v3
[0];
187 v
[1] = objects
[h
].v1
[1] + objects
[h
].v2
[1] + objects
[h
].v3
[1];
191 for (i
= 0; i
< 3; i
++) {
194 oldV
= objects
[h
].v1
;
197 oldV
= objects
[h
].v2
;
200 oldV
= objects
[h
].v3
;
203 oldV
[0] = 1.5 * (oldV
[0] - v
[0]) + v
[0];
204 oldV
[1] = 1.5 * (oldV
[1] - v
[1]) + v
[1];
208 static void Mouse(int button
, int state
, int mouseX
, int mouseY
)
212 if (state
!= GLUT_DOWN
)
215 hit
= DoSelect((GLint
)mouseX
, (GLint
)mouseY
);
217 if (button
== GLUT_LEFT_BUTTON
) {
220 if (button
== GLUT_MIDDLE_BUTTON
) {
223 if (button
== GLUT_RIGHT_BUTTON
) {
231 static void Draw(void)
236 glViewport(0, 0, windW
, windH
);
237 glGetIntegerv(GL_VIEWPORT
, vp
);
239 glMatrixMode(GL_PROJECTION
);
241 gluOrtho2D(-175, 175, -175, 175);
242 glMatrixMode(GL_MODELVIEW
);
244 glClearColor(0.0, 0.0, 0.0, 0.0);
245 glClear(GL_COLOR_BUFFER_BIT
);
247 glScalef(zoom
, zoom
, zoom
);
248 glRotatef(zRotation
, 0, 0, 1);
257 static void DrawZoom(GLint x
, GLint y
)
262 glViewport(0, 0, windW
, windH
);
263 glGetIntegerv(GL_VIEWPORT
, vp
);
265 glMatrixMode(GL_PROJECTION
);
267 gluPickMatrix(x
, windH
-y
, 4, 4, vp
);
268 gluOrtho2D(-175, 175, -175, 175);
269 glMatrixMode(GL_MODELVIEW
);
271 glClearColor(0.0, 0.0, 0.0, 0.0);
272 glClear(GL_COLOR_BUFFER_BIT
);
274 glScalef(zoom
, zoom
, zoom
);
275 glRotatef(zRotation
, 0, 0, 1);
282 static void DumpFeedbackVert(GLint
*i
, GLint n
)
292 printf(" (%g %g %g), color = (%4.2f %4.2f %4.2f)\n",
303 static void DrawFeedback(GLint n
)
308 printf("Feedback results (%d floats):\n", n
);
309 for (i
= 0; i
< n
; i
++) {
310 switch ((GLint
)feedBuf
[i
]) {
311 case GL_POLYGON_TOKEN
:
315 verts
= (GLint
)feedBuf
[i
];
317 printf(": %d vertices", verts
);
323 DumpFeedbackVert(&i
, n
);
331 DumpFeedbackVert(&i
, n
);
332 DumpFeedbackVert(&i
, n
);
335 case GL_LINE_RESET_TOKEN
:
336 printf("Line Reset:\n");
338 DumpFeedbackVert(&i
, n
);
339 DumpFeedbackVert(&i
, n
);
343 printf("%9.2f\n", feedBuf
[i
]);
353 static void DoFeedback(void)
357 glFeedbackBuffer(MAXFEED
, GL_3D_COLOR
, feedBuf
);
358 (void)glRenderMode(GL_FEEDBACK
);
362 glViewport(0, 0, windW
, windH
);
363 glGetIntegerv(GL_VIEWPORT
, vp
);
365 glMatrixMode(GL_PROJECTION
);
367 gluOrtho2D(-175, 175, -175, 175);
368 glMatrixMode(GL_MODELVIEW
);
370 glClearColor(0.0, 0.0, 0.0, 0.0);
371 glClear(GL_COLOR_BUFFER_BIT
);
373 glScalef(zoom
, zoom
, zoom
);
374 glRotatef(zRotation
, 0, 0, 1);
380 x
= glRenderMode(GL_RENDER
);
385 DrawFeedback((GLint
)x
);
388 static void Key2(int key
, int x
, int y
)
404 static void Key(unsigned char key
, int x
, int y
)
422 linePoly
= !linePoly
;
424 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
426 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
436 int main(int argc
, char **argv
)
440 glutInit(&argc
, argv
);
444 glutInitWindowPosition(0, 0); glutInitWindowSize( windW
, windH
);
446 type
= GLUT_RGB
| GLUT_SINGLE
;
447 glutInitDisplayMode(type
);
449 if (glutCreateWindow("Select Test") == GL_FALSE
) {
455 glutReshapeFunc(Reshape
);
456 glutKeyboardFunc(Key
);
457 glutSpecialFunc(Key2
);
458 glutMouseFunc(Mouse
);
459 glutDisplayFunc(Draw
);