int main(int argc,char* argv[]) { SDL_Surface* screen=SDL_SetVideoMode(SIZE,SIZE,32,SDL_DOUBLEBUF); if(screen==NULL)return 1; init_terrain(); while(!SDL_GetKeyState(NULL)[SDLK_SPACE]) { SDL_PumpEvents(); draw_terrain(screen); do_step(); SDL_Flip(screen); int x,y; for(y=0;y<SIZE;y++) for(x=0;x<SIZE;x++) { float val=terrain[x][y].height; write(STDOUT_FILENO,&val,sizeof(float)); val=terrain[x][y].water.depth; write(STDOUT_FILENO,&val,sizeof(float)); } } return 0; }
int main () { double prev_time = 0.0; double accum_sim_time = 0.0; if (!start_gl (800, 800)) { fprintf (stderr, "ERROR: could not start opengl\n"); return 1; } init_cam (); init_terrain (); init_dash (); glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS); glClearColor (0.0, 0.5, 0.5, 1.0); prev_time = glfwGetTime (); while (!glfwWindowShouldClose (gl_window)) { // work out how much time has passed double curr_time = glfwGetTime (); double elapsed = curr_time - prev_time; prev_time = curr_time; accum_sim_time += elapsed; // work out simulation time steps while (accum_sim_time > TIME_STEP_SIZE) { accum_sim_time -= TIME_STEP_SIZE; update_player (TIME_STEP_SIZE); } glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport (0, 0, gl_width, gl_height); draw_terrain (); // i dont want the dashboard to ever intersect with background so // i do a clear of the depth buffer glClear (GL_DEPTH_BUFFER_BIT); draw_dash (); // can expect everything has updated camera matrices by now cam_P_dirty = false; cam_V_dirty = false; glfwPollEvents (); glfwSwapBuffers (gl_window); } return 0; }
static void draw_terrain(int i, int j, int f, float a) { init_terrain(i); glPushMatrix(); { float V[6][4], P[16], M[16], X[16], v[4]; float t; float p; /* Apply the local coordinate system transformation. */ transform_entity(j); /* Acquire the model-view-projection matrix. */ glGetFloatv(GL_PROJECTION_MATRIX, P); glGetFloatv(GL_MODELVIEW_MATRIX, M); mult_mat_mat(X, P, M); get_viewfrust(V); get_viewpoint(v); /* Set the far clipping plane to the center of the planet. */ /* TODO: fix */ /* V[5][3] = 0.0f; */ /* Determine the viewpoint in cylindrical coordinates. */ normalize(v); t = (float) DEG(atan2(v[0], v[2])); p = (float) DEG(acos(-v[1])); if (t < 0) t += 360.0f; /* Draw. */ glPushAttrib(GL_POLYGON_BIT | GL_ENABLE_BIT); glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); { float y0 = (p > 90.0f) ? 0.0f : 90.0f; float y1 = (p > 90.0f) ? 90.0f : 0.0f; float x0 = (float) fmod(floor(t / 90.0f) * 90.0 + 180.0, 360.0); float x1 = (float) fmod(floor(t / 90.0f) * 90.0 + 90.0, 360.0); float x2 = (float) fmod(floor(t / 90.0f) * 90.0 + 270.0, 360.0); float x3 = (float) fmod(floor(t / 90.0f) * 90.0 + 0.0, 360.0); int t0 = (int) (y0 / 90.0f); int t1 = (int) (y1 / 90.0f); int s0 = (int) (x0 / 90.0f); int s1 = (int) (x1 / 90.0f); int s2 = (int) (x2 / 90.0f); int s3 = (int) (x3 / 90.0f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_COLOR_MATERIAL); glEnable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); enqueue_area(x0, y0, 90.0f, terrain[i].tex[s0][t0]); enqueue_area(x1, y0, 90.0f, terrain[i].tex[s1][t0]); enqueue_area(x2, y0, 90.0f, terrain[i].tex[s2][t0]); enqueue_area(x3, y0, 90.0f, terrain[i].tex[s3][t0]); enqueue_area(x0, y1, 90.0f, terrain[i].tex[s0][t1]); enqueue_area(x1, y1, 90.0f, terrain[i].tex[s1][t1]); enqueue_area(x2, y1, 90.0f, terrain[i].tex[s2][t1]); enqueue_area(x3, y1, 90.0f, terrain[i].tex[s3][t1]); count = 0; glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, terrain[i].ibo); draw_areas(V, X, i, p, t); /* printf("%d\n", count); */ glBindTexture (GL_TEXTURE_2D, 0); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); } glPopClientAttrib(); glPopAttrib(); /* Render all child entities in this coordinate system. */ draw_entity_tree(j, f, a * get_entity_alpha(j)); } glPopMatrix(); }