]> git.mxchange.org Git - flightgear.git/blob - tests/test-env-map.cxx
Including missing OSG plugins, use LZMA compression
[flightgear.git] / tests / test-env-map.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #ifdef HAVE_WINDOWS_H
6 #  include <windows.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <math.h>
12
13 #include <simgear/compiler.h>
14 #if defined( __APPLE__)
15 # include <OpenGL/OpenGL.h>
16 # include <GLUT/glut.h>
17 #else
18 # include <GL/gl.h>
19 # ifdef HAVE_GLUT_H
20 #  include <GL/glut.h>
21 # endif
22 #endif
23
24 #define TEXRES_X 256
25 #define TEXRES_Y 256
26
27 #ifdef HAVE_GLUT_H
28 unsigned char env_map[TEXRES_X][TEXRES_Y][4];
29 GLuint texName;
30 int window_x = 640, window_y = 480;
31 float alpha = 0.0, beta = 0.0;
32
33   /*****************************************************************/
34   /*****************************************************************/
35   /*****************************************************************/
36 void setColor(float x, float y, float z, float angular_size, float r, float g, float b, float a)
37 {
38   //normalize
39   float inv_length = 1.0 / sqrt(x*x + y*y + z*z);
40   x *= inv_length; y *= inv_length; z *= inv_length;
41   printf("x = %f, y = %f, z = %f\n", x, y, z);
42
43   float cos_angular_size = cos(angular_size*3.1415/180.0);
44   printf("cos_angular_size = %f\n", cos_angular_size);
45
46   for(float tz_sign = -1.0; tz_sign < 3.0; tz_sign += 2.0)
47   {
48     for(float tx = -1.0; tx <= 1.0; tx += 0.01)
49     {
50       for(float ty = -1.0; ty <= 1.0; ty += 0.01)
51       {
52         if ((1.0 - tx*tx - ty*ty)<0.0)
53           continue;
54
55         float tz = tz_sign * sqrt(1.0 - tx*tx - ty*ty);
56
57         float cos_phi = x*tx + y*ty + z*tz;
58        
59         if (cos_angular_size < cos_phi)
60         {
61           float rx = tx;  //mirroring on the z=0 plane
62           float ry = ty;
63           float rz = -tz;
64
65           float inv_m = 1.0 / (2.0 * sqrt(rx*rx + ry*ry + (rz + 1)*(rz + 1)));
66           int s = (int)(TEXRES_X * (rx * inv_m + 0.5));
67           int t = (int)(TEXRES_Y * (ry * inv_m + 0.5));
68
69           //seg_fault protection:
70           if (s<0) s=0; 
71           if (s>=TEXRES_X) s=TEXRES_X-1; 
72
73           if (t<0) t=0;
74           if (t>=TEXRES_Y) t=TEXRES_Y-1;
75
76           env_map[s][t][0] = (unsigned char)(r * 255);
77           env_map[s][t][1] = (unsigned char)(g * 255);
78           env_map[s][t][2] = (unsigned char)(b * 255);
79           env_map[s][t][3] = (unsigned char)(a * 255);
80         }
81       }
82     }
83   }
84 }
85   /*****************************************************************/
86   /*****************************************************************/
87   /*****************************************************************/
88
89 void init(void)
90 {
91   glClearColor(0.0, 0.0, 0.0, 0.0);
92   glShadeModel(GL_FLAT);
93   glEnable(GL_DEPTH_TEST);
94
95   for(int x=0; x<TEXRES_X; x++)
96   {
97     for(int y=0; y<TEXRES_Y; y++)
98     {
99       env_map[x][y][0] = x;  //a few colors
100       env_map[x][y][1] = y;
101       env_map[x][y][2] = 128;
102       env_map[x][y][3] = 128;
103     }
104   }
105
106   /*****************************************************************/
107   /*****************************************************************/
108   /*****************************************************************/
109   //       x          y    z           ang   r    g    b    a
110   setColor(0.0,       0.0, -1.0      , 23.0, 1.0, 1.0, 1.0, 1.0);
111   setColor(0.7071067, 0.0, -0.7071067, 23.0, 1.0, 0.0, 0.0, 1.0);
112   /*****************************************************************/
113   /*****************************************************************/
114   /*****************************************************************/
115
116   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117   glGenTextures(1, &texName);
118   glBindTexture(GL_TEXTURE_2D, texName);
119   
120   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
121   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
122   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
124   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
125 }
126
127 void reshape(int w, int h)
128 {
129   window_x = w; window_y = h;
130   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
131   glMatrixMode(GL_PROJECTION);
132   glLoadIdentity();
133   gluPerspective((GLdouble) 60.0, ((GLdouble)w)/((GLdouble)h), (GLdouble) 10.0, (GLdouble) 1000.0);
134   glMatrixMode(GL_MODELVIEW);
135   glLoadIdentity();
136   glTranslatef(0.0, 0.0, -20.0);
137 }
138
139 void display()
140 {
141   glClear  ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
142
143   //show light
144   glMatrixMode(GL_PROJECTION);
145   glLoadIdentity();
146   gluPerspective((GLdouble) 60.0, (float)window_x/window_y, (GLdouble) 1.0, (GLdouble) 100.0);
147   glMatrixMode(GL_MODELVIEW);
148   glLoadIdentity();
149
150   glTranslatef(0.0, 0.0, -10.0);
151
152   glRotatef(alpha, 1.0, 0.0, 0.0);
153   glRotatef(beta, 0.0, 1.0, 0.0);
154
155   glBegin(GL_LINES);
156   glColor3f(1.0, 1.0, 1.0);
157   glVertex3f(0.0, 0.0, 0.0);
158   glVertex3f(0.0, 0.0, 1.0);
159   glEnd();
160
161   /*****************************************************************/
162   /*****************************************************************/
163   /*****************************************************************/
164   glEnable(GL_TEXTURE_2D);
165   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
166   glBindTexture(GL_TEXTURE_2D, texName);
167
168   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
169   glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
170   glEnable(GL_TEXTURE_GEN_S);
171   glEnable(GL_TEXTURE_GEN_T);
172
173   glBegin(GL_QUADS);
174   glNormal3f(0.0, 0.0, 1.0);
175   glVertex3f(1.0, 1.0, 0.0);
176   glVertex3f(-1.0, 1.0, 0.0);
177   glVertex3f(-1.0, -1.0, 0.0);
178   glVertex3f(1.0, -1.0, 0.0);
179   glEnd();
180
181   /*
182   glPointSize(48.0);
183   glBegin(GL_POINTS);
184   glNormal3f(0.0, 0.0, 1.0);
185   glVertex3f(0.0, 0.0, 0.0);
186   glEnd();
187   */
188
189   glDisable(GL_TEXTURE_GEN_S);
190   glDisable(GL_TEXTURE_GEN_T);
191   glDisable(GL_TEXTURE_2D);
192   /*****************************************************************/
193   /*****************************************************************/
194   /*****************************************************************/
195
196   //show how the map looks like
197   glMatrixMode(GL_PROJECTION);
198   glLoadIdentity();
199   //gluOrtho2D(0.0  -10.0, 1.0 +10.0, -(float)window_x/window_y -10.0,0.0 +10.0);
200   //glOrtho(0.0, 5.0, -5.0, 0.0, -10.0, 10.0);
201   glOrtho(0.0, 5.0*((float)window_x/window_y), -5.0, 0.0, -10.0, 10.0);
202   glMatrixMode(GL_MODELVIEW);
203   glLoadIdentity();
204   glEnable(GL_TEXTURE_2D);
205   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
206   glBindTexture(GL_TEXTURE_2D, texName);
207
208   glBegin(GL_QUADS);
209   glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
210   glTexCoord2f(0.0, 1.0); glVertex3f(0.0, -1.0, 0.0);
211   glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1.0, 0.0);
212   glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
213   glEnd();
214   glFlush();
215   glDisable(GL_TEXTURE_2D);
216
217   glutPostRedisplay () ;
218   glutSwapBuffers () ;
219 }
220
221 void keyboard (unsigned char key, int x, int y)
222 {
223   switch(key)
224   {
225   case 27:
226     exit(0);
227
228   case '8':
229     alpha += 1.0;
230     if (alpha >= 360.0) alpha -= 360.0;
231     break;
232
233   case '2':
234     alpha -= 1.0;
235     if (alpha < 0.0) alpha += 360.0;
236     break;
237
238   case '4':
239     beta -= 1.0;
240     if (beta <= -90.0) beta = -90.0;
241     break;
242
243   case '6':
244     beta += 1.0;
245     if (beta >= 90.0) beta = 90.0;
246     break;
247
248   case '5':
249     alpha = 0.0; beta = 0.0;
250     break;
251   }
252 }
253 #endif /* HAVE_GLUT_H */
254
255 int main(int argc, char** argv)
256 {
257 #ifdef HAVE_GLUT_H
258   glutInitWindowSize(window_x, window_y);
259   glutInit(&argc, argv);
260   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
261   glutCreateWindow(argv[0]);
262   init();
263   glutDisplayFunc(display);
264   glutReshapeFunc(reshape);
265   glutKeyboardFunc(keyboard);
266
267   glutMainLoop();
268 #else
269
270   printf("GL Utility Toolkit (glut) was not found on this system.\n");
271 #endif
272
273   return 0;
274 }