13 #include <simgear/compiler.h>
14 #if defined( __APPLE__)
15 # include <OpenGL/OpenGL.h>
16 # include <GLUT/glut.h>
28 unsigned char env_map[TEXRES_X][TEXRES_Y][4];
30 int window_x = 640, window_y = 480;
31 float alpha = 0.0, beta = 0.0;
33 /*****************************************************************/
34 /*****************************************************************/
35 /*****************************************************************/
36 void setColor(float x, float y, float z, float angular_size, float r, float g, float b, float a)
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);
43 float cos_angular_size = cos(angular_size*3.1415/180.0);
44 printf("cos_angular_size = %f\n", cos_angular_size);
46 for(float tz_sign = -1.0; tz_sign < 3.0; tz_sign += 2.0)
48 for(float tx = -1.0; tx <= 1.0; tx += 0.01)
50 for(float ty = -1.0; ty <= 1.0; ty += 0.01)
52 if ((1.0 - tx*tx - ty*ty)<0.0)
55 float tz = tz_sign * sqrt(1.0 - tx*tx - ty*ty);
57 float cos_phi = x*tx + y*ty + z*tz;
59 if (cos_angular_size < cos_phi)
61 float rx = tx; //mirroring on the z=0 plane
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));
69 //seg_fault protection:
71 if (s>=TEXRES_X) s=TEXRES_X-1;
74 if (t>=TEXRES_Y) t=TEXRES_Y-1;
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);
85 /*****************************************************************/
86 /*****************************************************************/
87 /*****************************************************************/
91 glClearColor(0.0, 0.0, 0.0, 0.0);
92 glShadeModel(GL_FLAT);
93 glEnable(GL_DEPTH_TEST);
95 for(int x=0; x<TEXRES_X; x++)
97 for(int y=0; y<TEXRES_Y; y++)
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;
106 /*****************************************************************/
107 /*****************************************************************/
108 /*****************************************************************/
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 /*****************************************************************/
116 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
117 glGenTextures(1, &texName);
118 glBindTexture(GL_TEXTURE_2D, texName);
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);
127 void reshape(int w, int h)
129 window_x = w; window_y = h;
130 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
131 glMatrixMode(GL_PROJECTION);
133 gluPerspective((GLdouble) 60.0, ((GLdouble)w)/((GLdouble)h), (GLdouble) 10.0, (GLdouble) 1000.0);
134 glMatrixMode(GL_MODELVIEW);
136 glTranslatef(0.0, 0.0, -20.0);
141 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
144 glMatrixMode(GL_PROJECTION);
146 gluPerspective((GLdouble) 60.0, (float)window_x/window_y, (GLdouble) 1.0, (GLdouble) 100.0);
147 glMatrixMode(GL_MODELVIEW);
150 glTranslatef(0.0, 0.0, -10.0);
152 glRotatef(alpha, 1.0, 0.0, 0.0);
153 glRotatef(beta, 0.0, 1.0, 0.0);
156 glColor3f(1.0, 1.0, 1.0);
157 glVertex3f(0.0, 0.0, 0.0);
158 glVertex3f(0.0, 0.0, 1.0);
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);
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);
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);
184 glNormal3f(0.0, 0.0, 1.0);
185 glVertex3f(0.0, 0.0, 0.0);
189 glDisable(GL_TEXTURE_GEN_S);
190 glDisable(GL_TEXTURE_GEN_T);
191 glDisable(GL_TEXTURE_2D);
192 /*****************************************************************/
193 /*****************************************************************/
194 /*****************************************************************/
196 //show how the map looks like
197 glMatrixMode(GL_PROJECTION);
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);
204 glEnable(GL_TEXTURE_2D);
205 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
206 glBindTexture(GL_TEXTURE_2D, texName);
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);
215 glDisable(GL_TEXTURE_2D);
217 glutPostRedisplay () ;
221 void keyboard (unsigned char key, int x, int y)
230 if (alpha >= 360.0) alpha -= 360.0;
235 if (alpha < 0.0) alpha += 360.0;
240 if (beta <= -90.0) beta = -90.0;
245 if (beta >= 90.0) beta = 90.0;
249 alpha = 0.0; beta = 0.0;
253 #endif /* HAVE_GLUT_H */
255 int main(int argc, char** argv)
258 glutInitWindowSize(window_x, window_y);
259 glutInit(&argc, argv);
260 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
261 glutCreateWindow(argv[0]);
263 glutDisplayFunc(display);
264 glutReshapeFunc(reshape);
265 glutKeyboardFunc(keyboard);
270 printf("GL Utility Toolkit (glut) was not found on this system.\n");