aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa_6.4.1/progs/windml/uglpoint.c
blob: 49c432c3a41b8c98cc5ef66211f44f470907ae78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

/* uglpoint.c - WindML/Mesa example program */

/* Copyright (C) 2001 by Wind River Systems, Inc */

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
 * Authors:
 * Stephane Raimbault <stephane.raimbault@windriver.com> 
 */

/*
DESCRIPTION
Draw a single point.
*/

#include <stdio.h>
#include <math.h>

#include <ugl/ugl.h>
#include <ugl/uglevent.h>
#include <ugl/uglinput.h>

#include <GL/uglmesa.h>

#define DOUBLE_BUFFER GL_TRUE

enum {
    BLACK = 0,
    RED,
    GREEN,
    BLUE,
    WHITE
};

UGL_LOCAL GLuint rgb;
UGL_LOCAL UGL_EVENT_SERVICE_ID eventServiceId;
UGL_LOCAL UGL_EVENT_Q_ID qId;
UGL_LOCAL UGL_MESA_CONTEXT umc;
UGL_LOCAL GLint angleT;

UGL_LOCAL void initGL (void)
    {
    /* By passed in RGB mode */
    uglMesaSetColor(BLACK, 0.0, 0.0, 0.0);
    uglMesaSetColor(RED, 1.0, 0.0, 0.0);
    uglMesaSetColor(GREEN, 0.0, 1.0, 0.0);
    uglMesaSetColor(BLUE, 0.0, 0.0, 1.0);
    uglMesaSetColor(WHITE, 1.0, 1.0, 1.0);
    
    glOrtho(0.0, 1.0, 0.0, 1.0, -20.0, 20.0);
    
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearIndex(BLACK);
    }

UGL_LOCAL void drawGL (void)
    {
    GLint i;
    GLfloat x, y;

    /* Avoid blinking in single buffer */
    
    if (DOUBLE_BUFFER)
	glClear(GL_COLOR_BUFFER_BIT);    

    /* Random points */

    glBegin(GL_POINTS);
    (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED);

    for (i=0; i<150; i++)
	{
	x = rand() / (RAND_MAX+1.0);
        y = rand() / (RAND_MAX+1.0);
	glVertex2f(x, y);
	}

    (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN);

    for (i=0; i<150; i++)
	{
	x = (rand() / (RAND_MAX+1.0));
        y = (rand() / (RAND_MAX+1.0));
	glVertex2f(x, y);
	}

    (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE);
    glVertex2f(0.5,0.5);
    
    for (i=0; i<150; i++)
	{
	x = rand() / (RAND_MAX+1.0);
        y = rand() / (RAND_MAX+1.0);
	glVertex2f(x, y);
	}

    glEnd();

    /* Smooth triangle */

    glPushMatrix();
    glTranslatef(0.5, 0.5, 0);    
    glRotatef(angleT, 1.0, -1.0, 0.0);
    angleT = angleT++ % 360;
    glBegin(GL_TRIANGLES);
    (rgb) ? glColor3f(1.0, 0.0, 0.0): glIndexi(RED);  
    glVertex2f(0.75, 0.25);
    (rgb) ? glColor3f(0.0, 1.0, 0.0): glIndexi(GREEN);  
    glVertex2f(0.75, 0.75);
    (rgb) ? glColor3f(0.0, 0.0, 1.0): glIndexi(BLUE);  
    glVertex2f(0.25, 0.75);
    glEnd();
    glPopMatrix();

    /* Flush and swap */

    glFlush();
    
    uglMesaSwapBuffers();
    }

/************************************************************************
*
* getEvent
*
* RETURNS: true or false
*
* NOMANUAL
*
*/

UGL_LOCAL int getEvent(void)
    {
    UGL_EVENT event;
    UGL_STATUS status;
    int retVal = 0;

    status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);

    while (status != UGL_STATUS_Q_EMPTY)
        {
	UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)&event;
	
	if (pInputEvent->modifiers & UGL_KEYBOARD_KEYDOWN)
	    retVal = 1;

	status = uglEventGet (qId, &event, sizeof (event), UGL_NO_WAIT);
        }
 
    return(retVal);
    }

void windMLPoint (UGL_BOOL windMLMode);

void uglpoint (void)
    {
    taskSpawn ("tPoint", 210, VX_FP_TASK, 100000,
	       (FUNCPTR)windMLPoint, UGL_FALSE,1,2,3,4,5,6,7,8,9);
    }

void windMLPoint (UGL_BOOL windMLMode)
    {
    GLubyte pPixels[4];
    GLsizei width, height;
    UGL_INPUT_DEVICE_ID keyboardDevId;
    
    angleT = 0;

    uglInitialize();
    
    uglDriverFind (UGL_KEYBOARD_TYPE, 0, (UGL_UINT32 *)&keyboardDevId);
    
    if (uglDriverFind (UGL_EVENT_SERVICE_TYPE, 0,
                       (UGL_UINT32 *)&eventServiceId) == UGL_STATUS_OK)
        {
        qId = uglEventQCreate (eventServiceId, 100);
        }
    else 
        {
        eventServiceId = UGL_NULL;
        }
    
    if (DOUBLE_BUFFER)
	{
	if (windMLMode)
	    umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE
					  | UGL_MESA_WINDML_EXCLUSIVE, NULL);
	else
	    umc = uglMesaCreateNewContext(UGL_MESA_DOUBLE, NULL);
	}
    else
	{
	if (windMLMode)
	    umc = uglMesaCreateNewContext(UGL_MESA_SINGLE
					  | UGL_MESA_WINDML_EXCLUSIVE, NULL);
	else
	    umc = uglMesaCreateNewContext(UGL_MESA_SINGLE, NULL);
	}

    if (umc == NULL)
        {
	uglDeinitialize();
	return;
        }

    /* Fullscreen */

    uglMesaMakeCurrentContext(umc, 0, 0, UGL_MESA_FULLSCREEN_WIDTH,
			      UGL_MESA_FULLSCREEN_HEIGHT);
    
    /* RGB or CI ? */

    uglMesaGetIntegerv(UGL_MESA_RGB, &rgb);
    
    initGL();

    while (!getEvent())
	drawGL();
    
    uglMesaGetIntegerv(UGL_MESA_WIDTH, &width);
    uglMesaGetIntegerv(UGL_MESA_HEIGHT, &height);
    
    printf ("glReadPixel return ");
    if (rgb)
	{
	glReadPixels(width/2, height/2,
		     1, 1, GL_RGB,
		     GL_UNSIGNED_BYTE, pPixels);  
	glFlush();
	printf ("R:%i G:%i B:%i (RGB)", pPixels[0], pPixels[1], pPixels[2]);
	}
    else
	{
	glReadPixels(width/2, height/2,
		     1, 1, GL_COLOR_INDEX,
		     GL_UNSIGNED_BYTE, pPixels);  
	glFlush();
	if (pPixels[0] == BLUE)
	    printf ("BLUE (CI)");
	else
	    printf ("%i (CI))", pPixels[0]);
	}

    printf(" for %ix%i\n", width/2, height/2);
    
    if (eventServiceId != UGL_NULL)
	uglEventQDestroy (eventServiceId, qId);
    
    uglMesaDestroyContext();
    uglDeinitialize();
    
    return;
    }