/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/libraries/opengl/example/gl.c

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <unistd.h>
 
3
#include <time.h>
 
4
#include <sys/time.h>
 
5
 
 
6
#include <GL/glx.h>
 
7
#include <GL/gl.h>
 
8
#include <GL/glu.h>
 
9
#include <GL/glut.h>
 
10
 
 
11
#include <fontconfig.h>
 
12
#include <ftgl.h>
 
13
 
 
14
#include "config.h"
 
15
 
 
16
#include "functions.h"
 
17
#include "xfunctions.h"
 
18
#include "objects.h"
 
19
 
 
20
#define SPHERE_LIST 1
 
21
 
 
22
 
 
23
int DrawAxis(float size) {
 
24
    float r[] = {1,0,0,0};
 
25
    float g[] = {0,1,0,0};
 
26
    float b[] = {0,0,1,0};
 
27
    
 
28
    glBegin(GL_LINES);
 
29
    glMaterialfv(GL_FRONT, GL_EMISSION, b);
 
30
    glColor3fv(b);
 
31
    glVertex3f(0,0,0);
 
32
    glVertex3f(0,0,size);
 
33
    glMaterialfv(GL_FRONT, GL_EMISSION, g);
 
34
    glColor3fv(g);
 
35
    glVertex3f(0,0,0);
 
36
    glVertex3f(0,size,0);
 
37
    glMaterialfv(GL_FRONT, GL_EMISSION, r);
 
38
    glColor3fv(r);
 
39
    glVertex3f(0,0,0);
 
40
    glVertex3f(size,0,0);
 
41
    glEnd();
 
42
}
 
43
 
 
44
 
 
45
void DrawStatic() {
 
46
}
 
47
 
 
48
 
 
49
void SetFog() {
 
50
    GLfloat color[] = {0.05, 0.05, 0.05, 0};
 
51
    
 
52
    glEnable(GL_FOG);
 
53
    glHint (GL_FOG_HINT, GL_NICEST);
 
54
    glFogi (GL_FOG_MODE, GL_LINEAR);
 
55
    glFogf (GL_FOG_START, 1);
 
56
    glFogf (GL_FOG_END, 2.8);
 
57
 
 
58
    glFogfv (GL_FOG_COLOR, color);
 
59
}
 
60
 
 
61
void SetLight() {
 
62
    GLfloat LightPos[] = {0.7, 0.5, 0.7, 1}; /* within object */
 
63
    GLfloat Direction[] = {-1,0,-1};
 
64
 
 
65
    GLfloat zero[] = { 0, 0, 0, 1.0 };
 
66
    
 
67
    GLfloat fone[] = { 0.1, 0.1, 0.1, 0 };
 
68
 
 
69
    GLfloat ambient[] = {0, 0, 0, 0};
 
70
    GLfloat diffuse[] = {0.5, 0.5, 0.5,  1};
 
71
    GLfloat specular[] = {0.1, 0.1, 0.1, 1};
 
72
 
 
73
    GLfloat src_emit[] = { 1, 1, 1, 1.0 };
 
74
 
 
75
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
 
76
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fone);
 
77
 
 
78
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, zero);
 
79
    glMaterialfv(GL_FRONT, GL_SPECULAR, zero);
 
80
    glMaterialfv(GL_FRONT, GL_EMISSION, src_emit);
 
81
 
 
82
    glPointSize(5);
 
83
    glBegin(GL_POINTS);
 
84
    glVertex4fv(LightPos);
 
85
    glEnd();
 
86
 
 
87
    glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
 
88
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Direction);
 
89
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30);
 
90
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 25);
 
91
        
 
92
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
 
93
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
 
94
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
 
95
    glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.003);
 
96
 
 
97
}
 
98
 
 
99
void SetObjectsLight() {
 
100
    GLfloat zero[] = { 0, 0, 0, 1 };
 
101
    GLfloat mat_specular[] = { 1, 1, 1, 1 };
 
102
 
 
103
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
 
104
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, zero);
 
105
}
 
106
 
 
107
void SetAxisLight() {
 
108
    GLfloat zero[] = { 0, 0, 0, 1.0 };
 
109
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, zero);
 
110
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, zero);
 
111
}
 
112
 
 
113
 
 
114
void SetView(float angle) {
 
115
//    glViewport(0,0,800,600);
 
116
    glMatrixMode(GL_PROJECTION);
 
117
    glLoadIdentity();
 
118
//    glOrtho(-2,2,-2,2,-10,10);
 
119
    gluPerspective(90,800/600,1.05,10);
 
120
 
 
121
    glMatrixMode(GL_MODELVIEW);
 
122
    glLoadIdentity();    
 
123
    
 
124
    gluLookAt(
 
125
        1.5,1.5,1.5,
 
126
        0,0,0,
 
127
        0,1,0
 
128
    );
 
129
 
 
130
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
131
 
 
132
    SetLight();
 
133
    DrawAxis(2);
 
134
    
 
135
    SetObjectsLight();
 
136
}
 
137
 
 
138
void DrawCloack() {
 
139
    GLfloat zero[] = { 0, 0, 0, 0.3 };
 
140
    GLfloat color[] = { 0.1, 0.3, 0.3, 0.3 };
 
141
    GLfloat sky[] = { 0.0, 0.0, 0.8, 0.3 };
 
142
 
 
143
    SetAxisLight();
 
144
 
 
145
    glEnable(GL_BLEND);
 
146
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
147
 
 
148
    glDepthMask(GL_FALSE);
 
149
    
 
150
    glBegin(GL_QUADS);
 
151
    glColor4fv(color);
 
152
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, color);
 
153
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, zero);
 
154
    
 
155
    glVertex3f(0,0,0);
 
156
    glVertex3f(0,3,0);
 
157
    glVertex3f(3,3,0);
 
158
    glVertex3f(3,0,0);
 
159
 
 
160
    glVertex3f(0,0,0);
 
161
    glVertex3f(0,3,0);
 
162
    glVertex3f(0,3,3);
 
163
    glVertex3f(0,0,3);
 
164
    glEnd();
 
165
    
 
166
    glEnable(GL_TEXTURE_2D);    
 
167
    glBegin(GL_QUADS);
 
168
 
 
169
    glTexCoord2f(0.0, 0.0);glVertex3f(0,0,0);
 
170
    glTexCoord2f(0.0, 1.0);glVertex3f(0,0,3);
 
171
    glTexCoord2f(1.0, 1.0);glVertex3f(3,0,3);
 
172
    glTexCoord2f(1.0, 0.0);glVertex3f(3,0,0);
 
173
 
 
174
    glEnd();
 
175
    glDisable(GL_TEXTURE_2D);    
 
176
 
 
177
 
 
178
 
 
179
/*
 
180
    glBegin(GL_QUADS);
 
181
    glColor4fv(color);
 
182
    glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, sky);
 
183
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, zero);
 
184
    glVertex3f(-3,1.2,-3);
 
185
    glVertex3f(3,1.2,-3);
 
186
    glVertex3f(3,1.2,3);
 
187
    glVertex3f(-3,1.2,3);
 
188
    glEnd();
 
189
*/
 
190
 
 
191
    glDepthMask(GL_TRUE);
 
192
    glDisable(GL_BLEND);
 
193
}
 
194
 
 
195
void LoadTexture() {
 
196
    GLubyte tex[TEXTURE_WIDTH*TEXTURE_HEIGHT*3];
 
197
    generateTexture(tex, TEXTURE_WIDTH, TEXTURE_HEIGHT);
 
198
    glTexImage2D(GL_TEXTURE_2D, 
 
199
        0, 3, 
 
200
        TEXTURE_WIDTH, TEXTURE_WIDTH, 
 
201
        0, GL_RGB, GL_UNSIGNED_BYTE, 
 
202
        &tex
 
203
    );
 
204
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 
205
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 
206
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
207
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
208
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
 
209
//    glEnable(GL_TEXTURE_2D);    
 
210
}
 
211
 
 
212
 
 
213
FTGLfont *LoadFont() {
 
214
    FTGLfont *font;
 
215
    
 
216
    FcResult res;
 
217
    FcPattern *pat;
 
218
    FcPattern *match;
 
219
    FcChar8 *file;
 
220
    
 
221
    FcInit();
 
222
 
 
223
    pat = FcNameParse("arial");
 
224
    FcConfigSubstitute (NULL, pat, FcMatchPattern);
 
225
    FcDefaultSubstitute (pat);
 
226
 
 
227
    match = FcFontMatch(0, pat, &res);
 
228
    FcPatternDestroy(pat);
 
229
 
 
230
    FcPatternGetString(match, FC_FILE, 0, &file);
 
231
 
 
232
    font = ftglCreatePolygonFont(file);
 
233
//    font = ftglCreateBitmapFont(file);
 
234
 
 
235
    FcPatternDestroy(match);
 
236
    FcFini();
 
237
 
 
238
//    ftglSetFontDepth(font, 5);
 
239
//    ftglSetFontOutset(font, 0, 3);
 
240
    ftglSetFontCharMap(font, ft_encoding_unicode);
 
241
 
 
242
    return font;
 
243
}
 
244
 
 
245
 
 
246
void DrawFPS(FTGLfont *font, float num) {
 
247
    char stmp[64];
 
248
    GLfloat color[] = {1, 0, 0,  1};
 
249
    
 
250
    sprintf(stmp, "FPS: %.2lf", num);
 
251
 
 
252
    glMatrixMode(GL_PROJECTION);
 
253
    glLoadIdentity();
 
254
    gluPerspective(90,800/600,1.05,1000);
 
255
 
 
256
 
 
257
    glMatrixMode(GL_MODELVIEW);
 
258
    glLoadIdentity();    
 
259
    
 
260
    gluLookAt(
 
261
        150,150,150,
 
262
        0,0,0,
 
263
        0,150,0
 
264
    );
 
265
    glRotatef(90, 0, 1, 0);
 
266
    glTranslatef(-190,2,0);
 
267
    
 
268
 
 
269
    ftglSetFontFaceSize(font, 8, 72);
 
270
    
 
271
 
 
272
    glMaterialfv(GL_FRONT, GL_EMISSION, color);
 
273
 
 
274
    glDisable(GL_FOG);
 
275
    ftglRenderFont(font, stmp, FTGL_RENDER_ALL);
 
276
    glEnable(GL_FOG);
 
277
}
 
278
 
 
279
void Draw(Display *dpy, Window win, FTGLfont *font, long iter) {
 
280
    static double fps = 0;
 
281
    static int n = 0;
 
282
 
 
283
    struct timeval tv1, tv2;
 
284
    double cfps;
 
285
 
 
286
    gettimeofday(&tv1, NULL);
 
287
    
 
288
    SetView(iter);
 
289
 
 
290
    glPushMatrix();
 
291
    if (iter) glRotatef(iter, 0, 1, 0);
 
292
 
 
293
        // Antialiasing    
 
294
 
 
295
    if (ENABLE_ANTIALIASING) {
 
296
        glEnable(GL_BLEND);
 
297
//      glDepthMask(GL_FALSE);
 
298
    }
 
299
 
 
300
    glScalef(0.5,0.5,0.5);
 
301
    glTranslatef(1,0.5,1);
 
302
    glCallList(SPHERE_LIST);
 
303
    glPopMatrix();
 
304
 
 
305
    if (ENABLE_ANTIALIASING) {
 
306
//      glDepthMask(GL_TRUE);
 
307
        glDisable(GL_BLEND);
 
308
    }
 
309
 
 
310
    DrawCloack();
 
311
 
 
312
    gettimeofday(&tv2, NULL);
 
313
    
 
314
    cfps = 1000000. / ((tv2.tv_sec - tv1.tv_sec)*1000000 +  (tv2.tv_usec - tv1.tv_usec));
 
315
    
 
316
    fps = (fps * n + cfps) / (n + 1);
 
317
    n++;
 
318
 
 
319
    DrawFPS(font, fps);
 
320
    
 
321
    glXSwapBuffers(dpy,win);
 
322
}
 
323
 
 
324
 
 
325
int 
 
326
main(int argc, char *argv[]) {
 
327
    Display *dpy;
 
328
    Window win;
 
329
    GLXContext cx;
 
330
    FTGLfont *font;
 
331
    
 
332
    int i,j;
 
333
 
 
334
        // Initialization
 
335
    OpenGLWindow(&dpy, &cx, &win, 800, 600);
 
336
    font = LoadFont();
 
337
 
 
338
    
 
339
        // Enable depth calculation to hide extra stuff
 
340
    glEnable(GL_DEPTH_TEST);
 
341
 
 
342
    glEnable(GL_LIGHTING);
 
343
    glEnable(GL_LIGHT0);
 
344
 
 
345
    glEnable(GL_AUTO_NORMAL);
 
346
    glEnable(GL_NORMALIZE);
 
347
    
 
348
    if (ENABLE_ANTIALIASING) {
 
349
        glEnable(GL_POINT_SMOOTH);
 
350
        if (!DRAW_SKELETON) {
 
351
            glEnable(GL_LINE_SMOOTH);
 
352
        }
 
353
        glEnable(GL_POLYGON_SMOOTH);
 
354
 
 
355
        glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
 
356
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
 
357
        glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
 
358
    }
 
359
 
 
360
 
 
361
    SetFog();
 
362
 
 
363
    LoadTexture();
 
364
 
 
365
    glNewList(SPHERE_LIST, GL_COMPILE);
 
366
    DrawPolyhedron(DETAILS, DRAW_SKELETON);
 
367
    glEndList();
 
368
 
 
369
    Draw(dpy, win, font, 0);
 
370
 
 
371
    for (i=0;i<360;i++) {
 
372
        Draw(dpy, win, font, i);
 
373
        delay(100);
 
374
    }
 
375
 
 
376
    sleep(30);
 
377
}