Skip to content

Commit 77c4657

Browse files
committed
fixed some graphical issues with the fly example
1 parent 2b6678f commit 77c4657

1 file changed

Lines changed: 56 additions & 21 deletions

File tree

examples/fly/fly.c

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#define GRID_REP 60
2020
#define GRID_SZ 200
21+
#define GRID_RES 7
2122

2223
void gen_textures(void);
2324
void gen_scene(void);
@@ -35,7 +36,10 @@ void draw_box(float xsz, float ysz, float zsz);
3536
struct spnav_posrot posrot;
3637

3738
unsigned int grid_tex, box_tex;
38-
unsigned int scene;
39+
unsigned int scene, skydome;
40+
41+
float apex_color[] = {0.07, 0.1, 0.4, 1.0f};
42+
float horiz_color[] = {0.5, 0.2, 0.05, 1.0f};
3943

4044

4145
int main(void)
@@ -63,8 +67,9 @@ int main(void)
6367
glEnable(GL_CULL_FACE);
6468

6569
glFogi(GL_FOG_MODE, GL_LINEAR);
66-
glFogf(GL_FOG_START, GRID_SZ / 4);
70+
glFogf(GL_FOG_START, GRID_SZ / 4.0f);
6771
glFogf(GL_FOG_END, GRID_SZ);
72+
glFogfv(GL_FOG_COLOR, horiz_color);
6873

6974
gen_textures();
7075
gen_scene();
@@ -178,30 +183,35 @@ void gen_textures(void)
178183

179184
void gen_scene(void)
180185
{
181-
int i, j;
182-
float x, y, h;
186+
int i, j, k;
187+
float x, y, h, u, v, du;
183188

184189
srand(0);
185190

186191
scene = glGenLists(1);
187192
glNewList(scene, GL_COMPILE);
188193

189-
glEnable(GL_TEXTURE_2D);
190-
glEnable(GL_FOG);
191-
192194
/* grid */
193195
glBindTexture(GL_TEXTURE_2D, grid_tex);
194-
195196
glBegin(GL_QUADS);
196197
glColor3f(1, 1, 1);
197-
glTexCoord2f(0, 0);
198-
glVertex3f(-GRID_SZ, 0, GRID_SZ);
199-
glTexCoord2f(GRID_REP, 0);
200-
glVertex3f(GRID_SZ, 0, GRID_SZ);
201-
glTexCoord2f(GRID_REP, GRID_REP);
202-
glVertex3f(GRID_SZ, 0, -GRID_SZ);
203-
glTexCoord2f(0, GRID_REP);
204-
glVertex3f(-GRID_SZ, 0, -GRID_SZ);
198+
199+
du = 1.0f / (float)GRID_RES;
200+
for(i=0; i<GRID_RES; i++) {
201+
u = (float)i * du;
202+
for(j=0; j<GRID_RES; j++) {
203+
v = (float)j * du;
204+
for(k=0; k<4; k++) {
205+
int gc = k ^ (k >> 1);
206+
float tu = (u + (gc & 1) * du) * GRID_REP;
207+
float tv = (v + (gc >> 1) * du) * GRID_REP;
208+
x = ((u - 0.5f) + (gc & 1) * du) * GRID_SZ * 2.0f;
209+
y = ((v - 0.5f) + (gc >> 1) * du) * GRID_SZ * 2.0f;
210+
glTexCoord2f(tu, tv);
211+
glVertex3f(x, 0, -y);
212+
}
213+
}
214+
}
205215
glEnd();
206216

207217
/* buildings */
@@ -225,21 +235,31 @@ void gen_scene(void)
225235
glPopMatrix();
226236
}
227237
}
238+
glEndList();
228239

229-
glDisable(GL_TEXTURE_2D);
230-
glDisable(GL_FOG);
240+
skydome = glGenLists(1);
241+
glNewList(skydome, GL_COMPILE);
231242

232243
/* skydome */
233244
glBegin(GL_TRIANGLE_FAN);
234-
glColor3f(0.07, 0.1, 0.4);
235-
glVertex3f(0, GRID_SZ/5, 0);
236-
glColor3f(0.5, 0.2, 0.05);
245+
glColor3fv(apex_color);
246+
glVertex3f(0, GRID_SZ / 5.0f, 0);
247+
glColor3fv(horiz_color);
237248
glVertex3f(-GRID_SZ, 0, -GRID_SZ);
238249
glVertex3f(GRID_SZ, 0, -GRID_SZ);
239250
glVertex3f(GRID_SZ, 0, GRID_SZ);
240251
glVertex3f(-GRID_SZ, 0, GRID_SZ);
241252
glVertex3f(-GRID_SZ, 0, -GRID_SZ);
242253
glEnd();
254+
glBegin(GL_TRIANGLE_FAN);
255+
glColor3fv(horiz_color);
256+
glVertex3f(0, -GRID_SZ / 5.0f, 0);
257+
glVertex3f(-GRID_SZ, 0, -GRID_SZ);
258+
glVertex3f(-GRID_SZ, 0, GRID_SZ);
259+
glVertex3f(GRID_SZ, 0, GRID_SZ);
260+
glVertex3f(GRID_SZ, 0, -GRID_SZ);
261+
glVertex3f(-GRID_SZ, 0, -GRID_SZ);
262+
glEnd();
243263

244264
glEndList();
245265
}
@@ -258,7 +278,22 @@ void redraw(void)
258278
glMultMatrixf(xform); /* concatenate our computed view matrix */
259279
glTranslatef(0, -5, 0); /* move the default view a bit higher above the ground */
260280

281+
glPushMatrix();
282+
xform[12] = xform[13] = xform[14] = 0.0f;
283+
glLoadMatrixf(xform);
284+
glTranslatef(0, -5, 0);
285+
286+
glDisable(GL_DEPTH_TEST);
287+
glCallList(skydome);
288+
glEnable(GL_DEPTH_TEST);
289+
290+
glPopMatrix();
291+
292+
glEnable(GL_TEXTURE_2D);
293+
glEnable(GL_FOG);
261294
glCallList(scene);
295+
glDisable(GL_TEXTURE_2D);
296+
glDisable(GL_FOG);
262297

263298
glXSwapBuffers(dpy, win);
264299
}

0 commit comments

Comments
 (0)