blob: e184eaa755010db20e24a8b05b35b33da690ba20 [file] [log] [blame]
Brian Paul976c26c2001-08-20 16:07:10 +00001
2/* uglpoint.c - WindML/Mesa example program */
3
4/* Copyright (C) 2001 by Wind River Systems, Inc */
5
6/*
7 * Mesa 3-D graphics library
8 * Version: 3.5
9 *
10 * The MIT License
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30/*
31 * Authors:
32 * Stephane Raimbault <stephane.raimbault@windriver.com>
33 */
34
35/*
36DESCRIPTION
37Draw a single point.
38*/
39
40#include <stdio.h>
41#include <math.h>
42
43#include <ugl/ugl.h>
44#include <ugl/uglevent.h>
45#include <ugl/uglinput.h>
46
47#include <GL/uglmesa.h>
48
49#define DOUBLE_BUFFER GL_TRUE
50
51enum {
52 BLACK = 0,
53 RED,
54 GREEN,
55 BLUE,
56 WHITE
57};
58
59UGL_LOCAL GLuint rgb;
60UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
61UGL_LOCAL UGL_EVENT_Q_ID qId;
62UGL_LOCAL UGL_MESA_CONTEXT umc;
63UGL_LOCAL GLint angleT;
64
65UGL_LOCAL void initGL (void)
66 {
67 /* By passed in RGB mode */
68 uglMesaSetColor(BLACK, 0.0, 0.0, 0.0);
69 uglMesaSetColor(RED, 1.0, 0.0, 0.0);
70 uglMesaSetColor(GREEN, 0.0, 1.0, 0.0);
71 uglMesaSetColor(BLUE, 0.0, 0.0, 1.0);
72 uglMesaSetColor(WHITE, 1.0, 1.0, 1.0);
73
74 glOrtho(0.0, 1.0, 0.0, 1.0, -20.0, 20.0);
75
76 glClearColor(0.0, 0.0, 0.0, 0.0);
77 glClearIndex(BLACK);
78 }
79
80UGL_LOCAL void drawGL (void)
81 {
82 GLint i;
83 GLfloat x, y;
84
85 /* Avoid blinking in single buffer */
86
87 if (DOUBLE_BUFFER)
88 glClear(GL_COLOR_BUFFER_BIT);
89
90 /* Random points */
91
92 glBegin(GL_POINTS);
93 (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED);
94
95 for (i=0; i<150; i++)
96 {
97 x = rand() / (RAND_MAX+1.0);
98 y = rand() / (RAND_MAX+1.0);
99 glVertex2f(x, y);
100 }
101
102 (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN);
103
104 for (i=0; i<150; i++)
105 {
106 x = (rand() / (RAND_MAX+1.0));
107 y = (rand() / (RAND_MAX+1.0));
108 glVertex2f(x, y);
109 }
110
111 (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE);
112 glVertex2f(0.5,0.5);
113
114 for (i=0; i<150; i++)
115 {
116 x = rand() / (RAND_MAX+1.0);
117 y = rand() / (RAND_MAX+1.0);
118 glVertex2f(x, y);
119 }
120
121 glEnd();
122
123 /* Smooth triangle */
124
125 glPushMatrix();
126 glTranslatef(0.5, 0.5, 0);
127 glRotatef(angleT, 1.0, -1.0, 0.0);
128 angleT = angleT++ % 360;
129 glBegin(GL_TRIANGLES);
130 (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED);
131 glVertex2f(0.75, 0.25);
132 (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN);
133 glVertex2f(0.75, 0.75);
134 (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE);
135 glVertex2f(0.25, 0.75);
136 glEnd();
137 glPopMatrix();
138
139 /* Flush and swap */
140
141 glFlush();
142
143 if(DOUBLE_BUFFER)
144 uglMesaSwapBuffers();
145 }
146
147/************************************************************************
148*
149* getEvent
150*
151* RETURNS: true or false
152*
153* NOMANUAL
154*
155*/
156
157UGL_LOCAL int getEvent(void)
158 {
159 UGL_EVENT event;
160 UGL_STATUS status;
161 int retVal = 0;
162
163 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
164
165 while (status != UGL_STATUS_Q_EMPTY)
166 {
167 UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
168
169 if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
170 retVal = 1;
171
172 status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
173 }
174
175 return(retVal);
176 }
177
178void windMLPoint (void);
179
180void uglpoint (void)
181 {
182 taskSpawn("tPoint", 210, VX_FP_TASK, 100000,
183 (FUNCPTR)windMLPoint, 0,1,2,3,4,5,6,7,8,9);
184 }
185
186void windMLPoint(void)
187 {
188 GLubyte pPixels[4];
189 GLsizei width, height;
190 UGL_INPUT_DEVICE_ID keyboardDevId;
191
192 angleT = 0;
193
194 uglInitialize();
195
196 uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
197
198 if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
199 (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
200 {
201 qId = uglEventQCreate (eventServiceId, 100);
202 }
203 else
204 {
205 eventServiceId = UGL_NULL;
206 }
207
208 if (DOUBLE_BUFFER)
209 umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
210 else
211 umc = uglMesaCreateNewContext(UGL_MESA_SINGLE, NULL);
212
213 if (umc == NULL)
214 {
215 uglDeinitialize();
216 return;
217 }
218
219 /* Fullscreen */
220
221 uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
222 UGL_MESA_FULLSCREEN_HEIGHT);
223
224 /* RGB or CI ? */
225
226 uglMesaGetIntegerv(UGL_MESA_RGB, &rgb);
227
228 initGL();
229
230 while (!getEvent())
231 drawGL();
232
233 uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
234 uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
235
236 printf ("glReadPixel return ");
237 if (rgb)
238 {
239 glReadPixels(width/2, height/2,
240 1, 1, GL_RGB,
241 GL_UNSIGNED_BYTE, pPixels);
242 glFlush();
243 printf ("R:%i G:%i B:%i (RGB)", pPixels[0], pPixels[1], pPixels[2]);
244 }
245 else
246 {
247 glReadPixels(width/2, height/2,
248 1, 1, GL_COLOR_INDEX,
249 GL_UNSIGNED_BYTE, pPixels);
250 glFlush();
251 if (pPixels[0] == BLUE)
252 printf ("BLUE (CI)");
253 else
254 printf ("%i (CI))", pPixels[0]);
255 }
256
257 printf(" for %ix%i\n", width/2, height/2);
258
259 if (eventServiceId != UGL_NULL)
260 uglEventQDestroy (eventServiceId, qId);
261
262 uglMesaDestroyContext();
263 uglDeinitialize();
264
265 return;
266 }