File: test-env-map.cxx

package info (click to toggle)
flightgear 1:2016.4.4+dfsg-3+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,512 kB
  • ctags: 71,727
  • sloc: cpp: 195,395; ansic: 177,152; sh: 13,661; perl: 4,475; python: 2,381; asm: 642; makefile: 398; java: 314; xml: 85
file content (274 lines) | stat: -rw-r--r-- 7,571 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#ifdef HAVE_WINDOWS_H
#  include <windows.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include <simgear/compiler.h>
#if defined( __APPLE__)
# include <OpenGL/OpenGL.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# ifdef HAVE_GLUT_H
#  include <GL/glut.h>
# endif
#endif

#define TEXRES_X 256
#define TEXRES_Y 256

#ifdef HAVE_GLUT_H
unsigned char env_map[TEXRES_X][TEXRES_Y][4];
GLuint texName;
int window_x = 640, window_y = 480;
float alpha = 0.0, beta = 0.0;

  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/
void setColor(float x, float y, float z, float angular_size, float r, float g, float b, float a)
{
  //normalize
  float inv_length = 1.0 / sqrt(x*x + y*y + z*z);
  x *= inv_length; y *= inv_length; z *= inv_length;
  printf("x = %f, y = %f, z = %f\n", x, y, z);

  float cos_angular_size = cos(angular_size*3.1415/180.0);
  printf("cos_angular_size = %f\n", cos_angular_size);

  for(float tz_sign = -1.0; tz_sign < 3.0; tz_sign += 2.0)
  {
    for(float tx = -1.0; tx <= 1.0; tx += 0.01)
    {
      for(float ty = -1.0; ty <= 1.0; ty += 0.01)
      {
        if ((1.0 - tx*tx - ty*ty)<0.0)
          continue;

        float tz = tz_sign * sqrt(1.0 - tx*tx - ty*ty);

        float cos_phi = x*tx + y*ty + z*tz;
       
        if (cos_angular_size < cos_phi)
        {
          float rx = tx;  //mirroring on the z=0 plane
          float ry = ty;
          float rz = -tz;

          float inv_m = 1.0 / (2.0 * sqrt(rx*rx + ry*ry + (rz + 1)*(rz + 1)));
          int s = (int)(TEXRES_X * (rx * inv_m + 0.5));
          int t = (int)(TEXRES_Y * (ry * inv_m + 0.5));

          //seg_fault protection:
          if (s<0) s=0; 
          if (s>=TEXRES_X) s=TEXRES_X-1; 

          if (t<0) t=0;
          if (t>=TEXRES_Y) t=TEXRES_Y-1;

          env_map[s][t][0] = (unsigned char)(r * 255);
          env_map[s][t][1] = (unsigned char)(g * 255);
          env_map[s][t][2] = (unsigned char)(b * 255);
          env_map[s][t][3] = (unsigned char)(a * 255);
        }
      }
    }
  }
}
  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/

void init(void)
{
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glShadeModel(GL_FLAT);
  glEnable(GL_DEPTH_TEST);

  for(int x=0; x<TEXRES_X; x++)
  {
    for(int y=0; y<TEXRES_Y; y++)
    {
      env_map[x][y][0] = x;  //a few colors
      env_map[x][y][1] = y;
      env_map[x][y][2] = 128;
      env_map[x][y][3] = 128;
    }
  }

  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/
  //       x          y    z           ang   r    g    b    a
  setColor(0.0,       0.0, -1.0      , 23.0, 1.0, 1.0, 1.0, 1.0);
  setColor(0.7071067, 0.0, -0.7071067, 23.0, 1.0, 0.0, 0.0, 1.0);
  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glGenTextures(1, &texName);
  glBindTexture(GL_TEXTURE_2D, texName);
  
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
}

void reshape(int w, int h)
{
  window_x = w; window_y = h;
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective((GLdouble) 60.0, ((GLdouble)w)/((GLdouble)h), (GLdouble) 10.0, (GLdouble) 1000.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0, 0.0, -20.0);
}

void display()
{
  glClear  ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;

  //show light
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective((GLdouble) 60.0, (float)window_x/window_y, (GLdouble) 1.0, (GLdouble) 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0.0, 0.0, -10.0);

  glRotatef(alpha, 1.0, 0.0, 0.0);
  glRotatef(beta, 0.0, 1.0, 0.0);

  glBegin(GL_LINES);
  glColor3f(1.0, 1.0, 1.0);
  glVertex3f(0.0, 0.0, 0.0);
  glVertex3f(0.0, 0.0, 1.0);
  glEnd();

  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/
  glEnable(GL_TEXTURE_2D);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  glBindTexture(GL_TEXTURE_2D, texName);

  glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  glEnable(GL_TEXTURE_GEN_S);
  glEnable(GL_TEXTURE_GEN_T);

  glBegin(GL_QUADS);
  glNormal3f(0.0, 0.0, 1.0);
  glVertex3f(1.0, 1.0, 0.0);
  glVertex3f(-1.0, 1.0, 0.0);
  glVertex3f(-1.0, -1.0, 0.0);
  glVertex3f(1.0, -1.0, 0.0);
  glEnd();

  /*
  glPointSize(48.0);
  glBegin(GL_POINTS);
  glNormal3f(0.0, 0.0, 1.0);
  glVertex3f(0.0, 0.0, 0.0);
  glEnd();
  */

  glDisable(GL_TEXTURE_GEN_S);
  glDisable(GL_TEXTURE_GEN_T);
  glDisable(GL_TEXTURE_2D);
  /*****************************************************************/
  /*****************************************************************/
  /*****************************************************************/

  //show how the map looks like
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //gluOrtho2D(0.0  -10.0, 1.0 +10.0, -(float)window_x/window_y -10.0,0.0 +10.0);
  //glOrtho(0.0, 5.0, -5.0, 0.0, -10.0, 10.0);
  glOrtho(0.0, 5.0*((float)window_x/window_y), -5.0, 0.0, -10.0, 10.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_TEXTURE_2D);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  glBindTexture(GL_TEXTURE_2D, texName);

  glBegin(GL_QUADS);
  glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
  glTexCoord2f(0.0, 1.0); glVertex3f(0.0, -1.0, 0.0);
  glTexCoord2f(1.0, 1.0); glVertex3f(1.0, -1.0, 0.0);
  glTexCoord2f(1.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
  glEnd();
  glFlush();
  glDisable(GL_TEXTURE_2D);

  glutPostRedisplay () ;
  glutSwapBuffers () ;
}

void keyboard (unsigned char key, int x, int y)
{
  switch(key)
  {
  case 27:
    exit(0);

  case '8':
    alpha += 1.0;
    if (alpha >= 360.0) alpha -= 360.0;
    break;

  case '2':
    alpha -= 1.0;
    if (alpha < 0.0) alpha += 360.0;
    break;

  case '4':
    beta -= 1.0;
    if (beta <= -90.0) beta = -90.0;
    break;

  case '6':
    beta += 1.0;
    if (beta >= 90.0) beta = 90.0;
    break;

  case '5':
    alpha = 0.0; beta = 0.0;
    break;
  }
}
#endif /* HAVE_GLUT_H */

int main(int argc, char** argv)
{
#ifdef HAVE_GLUT_H
  glutInitWindowSize(window_x, window_y);
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow(argv[0]);
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);

  glutMainLoop();
#else

  printf("GL Utility Toolkit (glut) was not found on this system.\n");
#endif

  return 0;
}