19 unsigned char env_map[TEXRES_X][TEXRES_Y][4];
21 int window_x = 640, window_y = 480;
22 float alpha = 0.0, beta = 0.0;
24 /*****************************************************************/
25 /*****************************************************************/
26 /*****************************************************************/
27 void setColor(float x, float y, float z, float angular_size, float r, float g, float b, float a)
30 float inv_length = 1.0 / sqrt(x*x + y*y + z*z);
31 x *= inv_length; y *= inv_length; z *= inv_length;
32 printf("x = %f, y = %f, z = %f\n", x, y, z);
34 float cos_angular_size = cos(angular_size*3.1415/180.0);
35 printf("cos_angular_size = %f\n", cos_angular_size);
37 for(float tz_sign = -1.0; tz_sign < 3.0; tz_sign += 2.0)
39 for(float tx = -1.0; tx <= 1.0; tx += 0.01)
41 for(float ty = -1.0; ty <= 1.0; ty += 0.01)
43 if ((1.0 - tx*tx - ty*ty)<0.0)
46 float tz = tz_sign * sqrt(1.0 - tx*tx - ty*ty);
48 float cos_phi = x*tx + y*ty + z*tz;
50 if (cos_angular_size < cos_phi)
52 float rx = tx; //mirroring on the z=0 plane
56 float inv_m = 1.0 / (2.0 * sqrt(rx*rx + ry*ry + (rz + 1)*(rz + 1)));
57 int s = (int)(TEXRES_X * (rx * inv_m + 0.5));
58 int t = (int)(TEXRES_Y * (ry * inv_m + 0.5));
60 //seg_fault protection:
62 if (s>=TEXRES_X) s=TEXRES_X-1;
65 if (t>=TEXRES_Y) t=TEXRES_Y-1;
67 env_map[s][t][0] = (unsigned char)(r * 255);
68 env_map[s][t][1] = (unsigned char)(g * 255);
69 env_map[s][t][2] = (unsigned char)(b * 255);
70 env_map[s][t][3] = (unsigned char)(a * 255);
76 /*****************************************************************/
77 /*****************************************************************/
78 /*****************************************************************/
82 glClearColor(0.0, 0.0, 0.0, 0.0);
83 glShadeModel(GL_FLAT);
84 glEnable(GL_DEPTH_TEST);
86 for(int x=0; x<TEXRES_X; x++)
88 for(int y=0; y<TEXRES_Y; y++)
90 env_map[x][y][0] = x; //a few colors
92 env_map[x][y][2] = 128;
93 env_map[x][y][3] = 128;
97 /*****************************************************************/
98 /*****************************************************************/
99 /*****************************************************************/
101 setColor(0.0, 0.0, -1.0 , 23.0, 1.0, 1.0, 1.0, 1.0);
102 setColor(0.7071067, 0.0, -0.7071067, 23.0, 1.0, 0.0, 0.0, 1.0);
103 /*****************************************************************/
104 /*****************************************************************/
105 /*****************************************************************/
107 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
108 glGenTextures(1, &texName);
109 glBindTexture(GL_TEXTURE_2D, texName);
111 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
112 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
115 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
118 void reshape(int w, int h)
120 window_x = w; window_y = h;
121 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
122 glMatrixMode(GL_PROJECTION);
124 gluPerspective((GLdouble) 60.0, ((GLdouble)w)/((GLdouble)h), (GLdouble) 10.0, (GLdouble) 1000.0);
125 glMatrixMode(GL_MODELVIEW);
127 glTranslatef(0.0, 0.0, -20.0);
132 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
135 glMatrixMode(GL_PROJECTION);
137 gluPerspective((GLdouble) 60.0, (float)window_x/window_y, (GLdouble) 1.0, (GLdouble) 100.0);
138 glMatrixMode(GL_MODELVIEW);
141 glTranslatef(0.0, 0.0, -10.0);
143 glRotatef(alpha, 1.0, 0.0, 0.0);
144 glRotatef(beta, 0.0, 1.0, 0.0);
147 glColor3f(1.0, 1.0, 1.0);
148 glVertex3f(0.0, 0.0, 0.0);
149 glVertex3f(0.0, 0.0, 1.0);
152 /*****************************************************************/
153 /*****************************************************************/
154 /*****************************************************************/
155 glEnable(GL_TEXTURE_2D);
156 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
157 glBindTexture(GL_TEXTURE_2D, texName);
159 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
160 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
161 glEnable(GL_TEXTURE_GEN_S);
162 glEnable(GL_TEXTURE_GEN_T);
165 glNormal3f(0.0, 0.0, 1.0);
166 glVertex3f(1.0, 1.0, 0.0);
167 glVertex3f(-1.0, 1.0, 0.0);
168 glVertex3f(-1.0, -1.0, 0.0);
169 glVertex3f(1.0, -1.0, 0.0);
175 glNormal3f(0.0, 0.0, 1.0);
176 glVertex3f(0.0, 0.0, 0.0);
180 glDisable(GL_TEXTURE_GEN_S);
181 glDisable(GL_TEXTURE_GEN_T);
182 glDisable(GL_TEXTURE_2D);
183 /*****************************************************************/
184 /*****************************************************************/
185 /*****************************************************************/
187 //show how the map looks like
188 glMatrixMode(GL_PROJECTION);
190 //gluOrtho2D(0.0 -10.0, 1.0 +10.0, -(float)window_x/window_y -10.0,0.0 +10.0);
191 //glOrtho(0.0, 5.0, -5.0, 0.0, -10.0, 10.0);
192 glOrtho(0.0, 5.0*((float)window_x/window_y), -5.0, 0.0, -10.0, 10.0);
193 glMatrixMode(GL_MODELVIEW);
195 glEnable(GL_TEXTURE_2D);
196 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
197 glBindTexture(GL_TEXTURE_2D, texName);
200 glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
201 glTexCoord2f(0.0, 1.0); glVertex3f(0.0, -1.0, 0.0);
202 glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1.0, 0.0);
203 glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
206 glDisable(GL_TEXTURE_2D);
208 glutPostRedisplay () ;
212 void keyboard (unsigned char key, int x, int y)
221 if (alpha >= 360.0) alpha -= 360.0;
226 if (alpha < 0.0) alpha += 360.0;
231 if (beta <= -90.0) beta = -90.0;
236 if (beta >= 90.0) beta = 90.0;
240 alpha = 0.0; beta = 0.0;
245 int main(int argc, char** argv)
247 glutInitWindowSize(window_x, window_y);
248 glutInit(&argc, argv);
249 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
250 glutCreateWindow(argv[0]);
252 glutDisplayFunc(display);
253 glutReshapeFunc(reshape);
254 glutKeyboardFunc(keyboard);