Rev 5098 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1789 | yogev_ezra | 1 | /* |
2 | Пример взят из набора примеров библиотеки Mesa |
||
3 | |||
4 | iadn |
||
5 | http://www.iadn.narod.ru |
||
6 | iadn@bk.ru |
||
7 | */ |
||
8 | |||
9 | /* |
||
10 | * 3-D gear wheels. This program is in the public domain. |
||
11 | * |
||
12 | * Brian Paul |
||
13 | */ |
||
14 | |||
8528 | maxcodehac | 15 | #include |
1789 | yogev_ezra | 16 | #include |
17 | #include |
||
18 | #include |
||
19 | |||
20 | #include "SysCall.h" |
||
21 | #include "ProcessTab.h" |
||
22 | |||
23 | int Fps (long x, long y); |
||
24 | extern "C"{ |
||
5098 | clevermous | 25 | int main(void); |
1789 | yogev_ezra | 26 | } |
27 | |||
28 | struct { |
||
29 | int x,y; |
||
30 | int dx,dy; |
||
31 | } win; |
||
32 | |||
33 | #define KEY_ESC 1 |
||
34 | #define KEY_F 33 |
||
35 | |||
36 | char *title1 = "TinyGL in KolibriOS"; |
||
37 | char *title2 = "F full screen"; |
||
38 | char *title3 = "ESC - exit"; |
||
39 | char *fps = "FPS:"; |
||
40 | |||
41 | unsigned char FullScreen = 0; |
||
42 | unsigned char skin = 3; |
||
43 | |||
8528 | maxcodehac | 44 | proc_info* pri; |
1789 | yogev_ezra | 45 | KOSGLContext cgl; |
46 | |||
47 | static GLfloat view_rotx=20.0, view_roty=30.0, view_rotz=0.0; |
||
48 | static GLint gear1, gear2, gear3; |
||
49 | static GLfloat angle = 0.0; |
||
50 | |||
51 | static GLuint limit; |
||
52 | static GLuint count = 1; |
||
53 | |||
54 | /* |
||
55 | * Draw a gear wheel. You'll probably want to call this function when |
||
56 | * building a display list since we do a lot of trig here. |
||
57 | * |
||
58 | * Input: inner_radius - radius of hole at center |
||
59 | * outer_radius - radius at center of teeth |
||
60 | * width - width of gear |
||
61 | * teeth - number of teeth |
||
62 | * tooth_depth - depth of tooth |
||
63 | */ |
||
64 | static void gear( GLfloat inner_radius, GLfloat outer_radius, GLfloat width, |
||
65 | GLint teeth, GLfloat tooth_depth ) |
||
66 | { |
||
67 | GLint i; |
||
68 | GLfloat r0, r1, r2; |
||
69 | GLfloat angle, da; |
||
70 | GLfloat u, v, len; |
||
71 | |||
72 | r0 = inner_radius; |
||
73 | r1 = outer_radius - tooth_depth/2.0; |
||
74 | r2 = outer_radius + tooth_depth/2.0; |
||
75 | |||
76 | da = 2.0*M_PI / teeth / 4.0; |
||
77 | |||
78 | glShadeModel( GL_FLAT ); |
||
79 | |||
80 | glNormal3f( 0.0, 0.0, 1.0 ); |
||
81 | |||
82 | /* draw front face */ |
||
83 | glBegin( GL_QUAD_STRIP ); |
||
84 | for (i=0;i<=teeth;i++) { |
||
85 | angle = i * 2.0*M_PI / teeth; |
||
86 | glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 ); |
||
87 | glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 ); |
||
88 | glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 ); |
||
89 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 ); |
||
90 | } |
||
91 | glEnd(); |
||
92 | |||
93 | /* draw front sides of teeth */ |
||
94 | glBegin( GL_QUADS ); |
||
95 | da = 2.0*M_PI / teeth / 4.0; |
||
96 | for (i=0;i |
||
97 | angle = i * 2.0*M_PI / teeth; |
||
98 | |||
99 | glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 ); |
||
100 | glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 ); |
||
101 | glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 ); |
||
102 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 ); |
||
103 | } |
||
104 | glEnd(); |
||
105 | |||
106 | glNormal3f( 0.0, 0.0, -1.0 ); |
||
107 | |||
108 | /* draw back face */ |
||
109 | glBegin( GL_QUAD_STRIP ); |
||
110 | for (i=0;i<=teeth;i++) { |
||
111 | angle = i * 2.0*M_PI / teeth; |
||
112 | glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 ); |
||
113 | glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 ); |
||
114 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 ); |
||
115 | glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 ); |
||
116 | } |
||
117 | glEnd(); |
||
118 | |||
119 | /* draw back sides of teeth */ |
||
120 | glBegin( GL_QUADS ); |
||
121 | da = 2.0*M_PI / teeth / 4.0; |
||
122 | for (i=0;i |
||
123 | angle = i * 2.0*M_PI / teeth; |
||
124 | |||
125 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 ); |
||
126 | glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 ); |
||
127 | glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 ); |
||
128 | glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 ); |
||
129 | } |
||
130 | glEnd(); |
||
131 | |||
132 | /* draw outward faces of teeth */ |
||
133 | glBegin( GL_QUAD_STRIP ); |
||
134 | for (i=0;i |
||
135 | angle = i * 2.0*M_PI / teeth; |
||
136 | |||
137 | glVertex3f( r1*cos(angle), r1*sin(angle), width*0.5 ); |
||
138 | glVertex3f( r1*cos(angle), r1*sin(angle), -width*0.5 ); |
||
139 | u = r2*cos(angle+da) - r1*cos(angle); |
||
140 | v = r2*sin(angle+da) - r1*sin(angle); |
||
141 | len = sqrt( u*u + v*v ); |
||
142 | u /= len; |
||
143 | v /= len; |
||
144 | glNormal3f( v, -u, 0.0 ); |
||
145 | glVertex3f( r2*cos(angle+da), r2*sin(angle+da), width*0.5 ); |
||
146 | glVertex3f( r2*cos(angle+da), r2*sin(angle+da), -width*0.5 ); |
||
147 | glNormal3f( cos(angle), sin(angle), 0.0 ); |
||
148 | glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), width*0.5 ); |
||
149 | glVertex3f( r2*cos(angle+2*da), r2*sin(angle+2*da), -width*0.5 ); |
||
150 | u = r1*cos(angle+3*da) - r2*cos(angle+2*da); |
||
151 | v = r1*sin(angle+3*da) - r2*sin(angle+2*da); |
||
152 | glNormal3f( v, -u, 0.0 ); |
||
153 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), width*0.5 ); |
||
154 | glVertex3f( r1*cos(angle+3*da), r1*sin(angle+3*da), -width*0.5 ); |
||
155 | glNormal3f( cos(angle), sin(angle), 0.0 ); |
||
156 | } |
||
157 | |||
158 | glVertex3f( r1*cos(0.0), r1*sin(0.0), width*0.5 ); |
||
159 | glVertex3f( r1*cos(0.0), r1*sin(0.0), -width*0.5 ); |
||
160 | |||
161 | glEnd(); |
||
162 | |||
163 | glShadeModel( GL_SMOOTH ); |
||
164 | |||
165 | /* draw inside radius cylinder */ |
||
166 | glBegin( GL_QUAD_STRIP ); |
||
167 | for (i=0;i<=teeth;i++) { |
||
168 | angle = i * 2.0*M_PI / teeth; |
||
169 | glNormal3f( -cos(angle), -sin(angle), 0.0 ); |
||
170 | glVertex3f( r0*cos(angle), r0*sin(angle), -width*0.5 ); |
||
171 | glVertex3f( r0*cos(angle), r0*sin(angle), width*0.5 ); |
||
172 | } |
||
173 | glEnd(); |
||
174 | } |
||
175 | |||
176 | void init( void ) |
||
177 | { |
||
178 | static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0 }; |
||
179 | static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0 }; |
||
180 | static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0 }; |
||
181 | static GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0 }; |
||
182 | |||
183 | glLightfv( GL_LIGHT0, GL_POSITION, pos ); |
||
184 | glEnable( GL_CULL_FACE ); |
||
185 | glEnable( GL_LIGHTING ); |
||
186 | glEnable( GL_LIGHT0 ); |
||
187 | glEnable( GL_DEPTH_TEST ); |
||
188 | |||
189 | /* make the gears */ |
||
190 | gear1 = glGenLists(1); |
||
191 | glNewList(gear1, GL_COMPILE); |
||
192 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red ); |
||
193 | gear( 1.0, 4.0, 1.0, 20, 0.7 ); |
||
194 | glEndList(); |
||
195 | |||
196 | gear2 = glGenLists(1); |
||
197 | glNewList(gear2, GL_COMPILE); |
||
198 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green ); |
||
199 | gear( 0.5, 2.0, 2.0, 10, 0.7 ); |
||
200 | glEndList(); |
||
201 | |||
202 | gear3 = glGenLists(1); |
||
203 | glNewList(gear3, GL_COMPILE); |
||
204 | glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue ); |
||
205 | gear( 1.3, 2.0, 0.5, 10, 0.7 ); |
||
206 | glEndList(); |
||
207 | |||
208 | glEnable( GL_NORMALIZE ); |
||
209 | |||
210 | glViewport(0, 0, (GLint)500, (GLint)480); |
||
211 | glMatrixMode(GL_PROJECTION); |
||
212 | glLoadIdentity(); |
||
213 | glFrustum( -1.0, 1.0, -1, 1, 5.0, 60.0 ); |
||
214 | glMatrixMode(GL_MODELVIEW); |
||
215 | glLoadIdentity(); |
||
216 | glTranslatef( 0.0, 0.0, -40.0 ); |
||
217 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
||
218 | } |
||
219 | |||
220 | void reshape() |
||
221 | { |
||
8528 | maxcodehac | 222 | get_proc_info((char*)pri); |
223 | glViewport(0, 0, pri->width, pri->height-20); |
||
1789 | yogev_ezra | 224 | glMatrixMode(GL_PROJECTION); |
225 | glLoadIdentity(); |
||
8528 | maxcodehac | 226 | gluPerspective(45.0, (GLfloat)pri->width/pri->height, 1.0, 60.0); |
1789 | yogev_ezra | 227 | glTranslatef( 0.0, 0.0, 20.0 ); |
228 | glMatrixMode(GL_MODELVIEW); |
||
229 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
||
230 | } |
||
231 | |||
232 | void disabletgl() |
||
233 | { |
||
234 | kosglDestroyContext(cgl); |
||
235 | delete pri; |
||
236 | } |
||
237 | |||
8528 | maxcodehac | 238 | void kos_text(int x, int y, int color, const char* text, int len) |
239 | { |
||
240 | asm volatile ("int $0x40"::"a"(4),"b"((x<<16) | y),"c"(color),"d"((unsigned long)text),"S"(len)); |
||
241 | }; |
||
242 | |||
1789 | yogev_ezra | 243 | void Title() |
244 | { |
||
8528 | maxcodehac | 245 | kos_text(300,8,0x10ffffff,fps,strlen(fps)); |
246 | kos_text(180,8,0x00ffffff,title2,strlen(title2)); |
||
247 | kos_text(600,8,0x00ffffff,title3,strlen(title3)); |
||
1789 | yogev_ezra | 248 | } |
249 | |||
8528 | maxcodehac | 250 | void kol_wnd_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned cs, unsigned b, char *t) |
251 | { |
||
252 | asm volatile ("int $0x40"::"a"(0), "b"(x*65536+w), "c"(y*65536+h), "d"(cs), "D"(t), "S"(b) ); |
||
253 | } |
||
254 | |||
1789 | yogev_ezra | 255 | void draw_window(void) |
256 | { |
||
257 | // start redraw |
||
8528 | maxcodehac | 258 | begin_draw(); |
1789 | yogev_ezra | 259 | // define&draw window |
8528 | maxcodehac | 260 | kol_wnd_define(win.x,win.y,win.dx,win.dy,TYPEWIN(0,0,0,1,skin,0,0,0),0,title1); |
1789 | yogev_ezra | 261 | // end redraw |
8528 | maxcodehac | 262 | end_draw(); |
1789 | yogev_ezra | 263 | // display string |
264 | Title(); |
||
265 | } |
||
266 | |||
8528 | maxcodehac | 267 | int kos_get_key() |
268 | { |
||
269 | unsigned short __ret; |
||
270 | asm volatile("int $0x40":"=a"(__ret):"0"(2)); |
||
271 | if(!(__ret & 0xFF)) return (__ret>>8)&0xFF; else return 0; |
||
272 | } |
||
273 | |||
5098 | clevermous | 274 | int main(void) |
1789 | yogev_ezra | 275 | { |
276 | |||
277 | win.x = 100; |
||
278 | win.y = 100; |
||
279 | win.dx = 400; |
||
280 | win.dy = 400; |
||
281 | |||
282 | draw_window(); |
||
283 | |||
284 | cgl = kosglCreateContext( 0, 0); |
||
285 | kosglMakeCurrent( 0, 20, win.dx, win.dy-20, cgl); |
||
286 | |||
287 | init(); |
||
288 | |||
8528 | maxcodehac | 289 | pri = new proc_info; |
1789 | yogev_ezra | 290 | SysCall(66,1,1); |
291 | |||
292 | reshape(); |
||
293 | |||
294 | do{ |
||
295 | |||
296 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
||
297 | |||
298 | glPushMatrix(); |
||
299 | glRotatef( view_rotx, 1.0, 0.0, 0.0 ); |
||
300 | glRotatef( view_roty, 0.0, 1.0, 0.0 ); |
||
301 | glRotatef( view_rotz, 0.0, 0.0, 1.0 ); |
||
302 | |||
303 | glPushMatrix(); |
||
304 | glTranslatef( -2.0, -2.0, 0.0 ); |
||
305 | glRotatef( angle, 0.0, 0.0, 1.0 ); |
||
306 | glCallList(gear1); |
||
307 | glPopMatrix(); |
||
308 | |||
309 | glPushMatrix(); |
||
310 | glTranslatef( 4.1, -2.0, 0.0 ); |
||
311 | glRotatef( -2.0*angle-9.0, 0.0, 0.0, 1.0 ); |
||
312 | glCallList(gear2); |
||
313 | glPopMatrix(); |
||
314 | |||
315 | glPushMatrix(); |
||
316 | glTranslatef( -2.1, 4.2, 0.0 ); |
||
317 | glRotatef( -2.0*angle-25.0, 0.0, 0.0, 1.0 ); |
||
318 | glCallList(gear3); |
||
319 | glPopMatrix(); |
||
320 | |||
321 | glPopMatrix(); |
||
322 | |||
323 | kosglSwapBuffers(); |
||
324 | |||
325 | angle += 0.01 + 0.3* Fps (330,8); |
||
326 | |||
327 | switch(SysCall(11)) |
||
328 | { |
||
329 | case 1: draw_window(); |
||
330 | reshape(); |
||
331 | break; |
||
332 | |||
333 | case 2: |
||
8528 | maxcodehac | 334 | switch(kos_get_key()){ |
1789 | yogev_ezra | 335 | |
336 | case KEY_F: |
||
337 | if(!FullScreen){ |
||
338 | skin=0; |
||
339 | SysCall(67,0,0,SysCall(14)>>16,SysCall(14)&0xffff); |
||
340 | draw_window(); |
||
341 | reshape(); |
||
342 | FullScreen = 1; |
||
343 | } |
||
344 | else{ |
||
345 | skin=3; |
||
346 | draw_window(); |
||
347 | SysCall(67,win.x,win.y,win.dx,win.dy); |
||
348 | reshape(); |
||
349 | FullScreen = 0; |
||
350 | }; |
||
351 | break; |
||
352 | |||
353 | case KEY_ESC: disabletgl(); |
||
5098 | clevermous | 354 | return 0;} |
1789 | yogev_ezra | 355 | break; |
356 | |||
357 | case 3: disabletgl(); |
||
5098 | clevermous | 358 | return 0; |
1789 | yogev_ezra | 359 | } |
360 | }while(1); |
||
361 | }16)><16)>=teeth;i++)>=teeth;i++)>=teeth;i++)> |