indexing description: "[ An example scene representing a nice OpenGl Scene with some fog, light and nice cubes ]" date: "$Date$" revision: "$Revision$" class CUBE inherit EM_3D_COMPONENT redefine prepare_drawing end EM_SHARED_BITMAP_FACTORY EM_TIME_SINGLETON SINGLE_MATH export {NONE} all end create make feature {NONE} -- Initialisation make is -- Initialise default values. do make_3d_component create source_pos.make_xyz (0,0,0) create destination_pos.make_xyz (0,0,0) create direction.make_xyz (1,0,0) init_shapes gl_line_width (2.0) -- init fog gl_enable (em_gl_fog) gl_fogi (em_gl_fog_mode, em_gl_exp2) gl_fogf (Em_gl_fog_density, 0.35) gl_fogf (em_gl_fog_start, 2.0) gl_fogf (em_gl_fog_end, 6.0) gl_hint (Em_gl_fog_hint, Em_gl_nicest) -- init lightning gl_enable (em_gl_light0) gl_enable (em_gl_lighting) gl_enable (em_gl_blend) gl_blend_func (em_gl_src_alpha, em_gl_one) -- init textures gl_enable (em_gl_texture_2d) gl_enable (em_gl_color_material) -- init environment mapping gl_enable (em_gl_texture_gen_s) gl_enable (em_gl_texture_gen_t) gl_tex_geni (em_gl_s, em_gl_texture_gen_mode, em_gl_sphere_map) gl_tex_geni (em_gl_t, em_gl_texture_gen_mode, em_gl_sphere_map) gl_hint (em_gl_perspective_correction_hint, em_gl_nicest) gl_shade_model (em_gl_smooth) -- init depth buffer gl_shade_model (em_gl_depth_test) gl_depth_func (em_gl_lequal) -- set perspective set_field_of_view (60) set_min_view_distance (0.1) set_max_view_distance (100.0) end feature -- Initialization init_shapes is -- Init drawing list local pos_x, pos_y, pos_z: INTEGER do -- create display list display_list := gl_gen_lists (1) gl_new_list (display_list, em_gl_compile) -- put cubes into the list from pos_x := 0 until pos_x = max_cube loop from pos_y := 0 until pos_y = max_cube loop from pos_z := 0 until pos_z = max_cube loop gl_push_matrix gl_translatef (pos_x, pos_y, pos_z) gl_translatef (-4.5, -4.5, -4.5) render_cube gl_pop_matrix pos_z := pos_z + 1 end pos_y := pos_y + 1 end pos_x := pos_x + 1 end gl_end_list end feature -- Drawing prepare_drawing is -- Prepare for drawing `Current'. do if width > 0 and height > 0 then -- Reset depth buffer gl_clear (Em_gl_depth_buffer_bit) -- Opengl settings gl_enable (Em_gl_depth_test) -- Enable antialiasing gl_enable (Em_gl_line_smooth) gl_hint (Em_gl_line_smooth, Em_gl_nicest) -- Setup the projection matrix gl_matrix_mode (Em_gl_projection) gl_load_identity glu_perspective (field_of_view, width/height, min_view_distance, max_view_distance) -- Setup the model view matrix gl_matrix_mode (Em_gl_modelview) gl_load_identity end end draw is -- Draw Scene do last_redraw_time := actual_redraw_time actual_redraw_time := time.ticks / 2000 gl_rotatef (actual_redraw_time * 7.0, 1, 0, 0) gl_rotatef (actual_redraw_time * 8.0, 0, 1, 0) gl_rotatef (actual_redraw_time * 9.0, 0, 0, 1) -- gl_translatef (0, 0, (actual_redraw_time - 0.5) * 3.0) gl_translatef (vx,vy,0) gl_color4f (0.0, 0.5, 1.0, 0.4) gl_bind_texture (em_gl_texture_2d, 0) gl_call_list (display_list) gl_color4f (0.0, 0.8, 0.8, 0.1); gl_polygon_mode (em_gl_front_and_back, em_gl_line) gl_call_list (display_list) gl_polygon_mode (em_gl_front_and_back, em_gl_fill) end feature {NONE} -- Implementation vx, vy: DOUBLE render_cube is -- Render a single cube with normals do gl_begin (em_gl_quads) gl_normal3f (1.0, 0.0, 0.0) gl_tex_coord2f (0.0, 0.0); gl_vertex3f ( 0.25, -0.25, -0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f ( 0.25, 0.25, -0.25) gl_tex_coord2f (1.0, 1.0); gl_vertex3f ( 0.25, 0.25, 0.25) gl_tex_coord2f (0.0, 1.0); gl_vertex3f ( 0.25, -0.25, 0.25) gl_normal3f (-1.0, 0.0, 0.0) gl_tex_coord2f (0.0, 1.0); gl_vertex3f (-0.25, -0.25, 0.25) gl_tex_coord2f (0.0, 0.0); gl_vertex3f (-0.25, -0.25, -0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f (-0.25, 0.25, -0.25) gl_tex_coord2f (1.0, 1.0); gl_vertex3f (-0.25, 0.25, 0.25) gl_normal3f (0.0, 0.0, -1.0) gl_tex_coord2f (0.0, 1.0); gl_vertex3f (-0.25, 0.25, -0.25) gl_tex_coord2f (1.0, 1.0); gl_vertex3f ( 0.25, 0.25, -0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f ( 0.25, -0.25, -0.25) gl_tex_coord2f (0.0, 0.0); gl_vertex3f (-0.25, -0.25, -0.25) gl_normal3f (0.0, 0.0, 1.0) gl_tex_coord2f (0.0, 0.0); gl_vertex3f (-0.25, -0.25, 0.25) gl_tex_coord2f (0.0, 1.0); gl_vertex3f (-0.25, 0.25, 0.25) gl_tex_coord2f (1.0, 1.0); gl_vertex3f ( 0.25, 0.25, 0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f ( 0.25, -0.25, 0.25) gl_normal3f (0.0, 1.0, 0.0) gl_tex_coord2f (0.0, 1.0); gl_vertex3f ( 0.25, 0.25, -0.25) gl_tex_coord2f (1.0, 1.0); gl_vertex3f ( 0.25, 0.25, 0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f (-0.25, 0.25, 0.25) gl_tex_coord2f (0.0, 0.0); gl_vertex3f (-0.25, 0.25, -0.25) gl_normal3f (0.0, -1.0, 0.0) gl_tex_coord2f (1.0, 1.0); gl_vertex3f ( 0.25, -0.25, 0.25) gl_tex_coord2f (1.0, 0.0); gl_vertex3f (-0.25, -0.25, 0.25) gl_tex_coord2f (0.0, 0.0); gl_vertex3f (-0.25, -0.25, -0.25) gl_tex_coord2f (0.0, 1.0); gl_vertex3f ( 0.25, -0.25, -0.25) gl_end end display_list: INTEGER -- Identifier for display list max_cube: INTEGER is 10 -- maximal number of cubes to be drawn last_redraw_time, actual_redraw_time: DOUBLE -- tariable for time step: DOUBLE -- for camera position source_pos, destination_pos: GL_VECTOR_3D [DOUBLE] -- source and destionation position direction: GL_VECTOR_3D [DOUBLE] -- direction the camera is looking end