#include #include /* Miscellaneous constnnts we use. */ #define WS_GEOMETRY "500x500+50+50" #define WS_ID (Pint) 1 #define SPHERE (Pint) 1 #define AXES (Pint) 2 #define SPHERE_RADIUS (Pfloat) 1.0 #define PAN_SCALE (M_PI/500) static Display *display; static Window window; static Pvec3 view_up_vec,view_plane_normal; static Ppoint3 view_ref_pt; static Pview_rep3 view_rep; static Pview_map3 view_map; static Pint error; Pxphigs_info xphigs_info; static void build_sphere(float radius, Ppoint_list3 *sphere), handle_input(),build_axes(float length), open_xphigs_and_xlib_ws( Pint ws_id, char *geometry, Display **display, Window *window ); void open_xphigs_and_xlib_ws( Pint ws_id, char *geometry, Display **display, Window *window ) { XEvent event; XSetWindowAttributes win_attrs; int x = 50, y = 50; unsigned int width = 400,height = 400; Pconnid_x_drawable conn_id; Pxphigs_info xphigs_info; unsigned long mask; /* Connect to the server. / fprintf(stderr, "Itt meg jo\n"); if (!( *display = XOpenDisplay(NULL))) { (void)fprintf( stderr, "Can't open default display\n" ) ; exit ( -1 ) ; } fprintf(stderr, "1\n"); /* Create the window.*/ if (geometry) (void)XParseGeometry(geometry,&x,&y,&width,&height ); win_attrs.backing_store = NotUseful; win_attrs.border_pixel = win_attrs.background_pixel = BlackPixel( *display, DefaultScreen(*display) ) ; *window = XCreateWindow( *display, RootWindow( *display, DefaultScreen( *display ) ), x, y, width, height, 1, CopyFromParent,InputOutput,CopyFromParent, CWBackingStore || CWBorderPixel || CWBackPixel,&win_attrs ); fprintf(stderr, "2\n"); XMapWindow(*display,*window ); /* Open PHIGS,telling it not to monitor window events.*/ mask = PXPHIGS_INFO_FLAGS_NO_MON; fprintf(stderr, "3\n"); xphigs_info.flags.no_monitor = 1; popen_xphigs(PDEF_ERR_FILE,PDEF_MEM_SIZE, mask,&xphigs_info ); /* Open a PHIGS workstation that uses the window we created.*/ fprintf(stderr, "4\n"); conn_id.display = *display; conn_id.drawable_id = *window; popen_ws(ws_id,(void *)&conn_id,phigs_ws_type_x_drawable ); fprintf(stderr, "5\n"); } static void build_axes( float length ) { Pptco3 x_axis[2], y_axis[2], z_axis[2]; Pline_vdata_list3 axes[3]; /* Set the axes coordinates and colors. */ x_axis[0].point.x = x_axis[0].point.y = x_axis[0].point.z = 0; x_axis[1].point.x = length; x_axis[1].point.y = 0; x_axis[1].point.z = 0; x_axis[0].colr.direct.rgb.red = 1; x_axis[0].colr.direct.rgb.green = 0; x_axis[0].colr.direct.rgb.blue = 0; x_axis[1].colr.direct.rgb = x_axis[0].colr.direct.rgb; y_axis[0].point.x = y_axis[0].point.y = y_axis[0].point.z = 0; y_axis[1].point.x = 0; y_axis[1].point.y = length; y_axis[1].point.z = 0; y_axis[0].colr.direct.rgb.red = 0; y_axis[0].colr.direct.rgb.green = 1; y_axis[0].colr.direct.rgb.blue = 0; y_axis[1].colr.direct.rgb = y_axis[0].colr.direct.rgb; z_axis[0].point.x = z_axis[0].point.y = z_axis[0].point.z = 0; z_axis[1].point.x = 0; z_axis[1].point.y = 0; z_axis[1].point.z = length; z_axis[0].colr.direct.rgb.red = 0; z_axis[0].colr.direct.rgb.green = 0; z_axis[0].colr.direct.rgb.blue = 1; z_axis[1].colr.direct.rgb = z_axis[0].colr.direct.rgb; /* Build a polyline set for the axes. */ axes[0].num_vertices = 2; axes[1].num_vertices = 2; axes[2].num_vertices = 2; axes[0].vertex_data.ptcolrs = x_axis; axes[1].vertex_data.ptcolrs = y_axis; axes[2].vertex_data.ptcolrs = z_axis; popen_struct(AXES); ppolyline_set3_data( PVERT_COORD_COLOUR, PMODEL_RGB, (Pint) 3, axes ); pclose_struct(); } #define NUM_LAT_SEGS 20 #define NUM_LON_SEGS 30 void build_sphere(float radius, Ppoint_list3 *sphere ) { /* Build a "wire" sphere, using a single spiral polyline. */ double phi, delta_phi, theta, delta_theta; int i ; sphere->num_points = NUM_LAT_SEGS * NUM_LON_SEGS + 1; sphere->points = (Ppoint3 *) malloc( (unsigned)(sphere->num_points * sizeof(Ppoint3)) ); phi = 0; /* start at the North pole */ delta_phi = M_PI / (NUM_LAT_SEGS * NUM_LON_SEGS); theta = 0; delta_theta = (2 * M_PI) / NUM_LON_SEGS; for ( i = 0; i < sphere->num_points; i++ ) { sphere->points[i].x = radius * sin( phi ) * sin( theta ); sphere->points[i].y = radius * cos( phi ); sphere->points[i].z = radius * sin( phi ) * cos( theta ); theta += delta_theta; phi += delta_phi; } } static void handle_input() { XEvent event; int lastx, lasty, done = 0; float theta = M_PI/4, phi = M_PI/4; XSelectInput( display, window, ButtonPressMask | Button1MotionMask | ExposureMask ); while ( !done ) { XNextEvent( display, &event ); if ( event.type == ButtonPress ) { switch ( event.xbutton.button ) { case Button1: /* ReSet last X and last Y. */ lastx = event.xbutton.x; lasty = event.xbutton.y; break; default: done = 1; break; } } else if ( event.type == MotionNotify ) { theta += PAN_SCALE * (event.xmotion.x - lastx); phi += PAN_SCALE * (lasty - event.xmotion.y); view_plane_normal.delta_x = cos( phi ) * sin( theta ); view_plane_normal.delta_y = sin( phi ); view_plane_normal.delta_z = cos( phi ); /* Calculate the new orientation matrix. */ peval_view_ori_matrix3( &view_ref_pt, &view_plane_normal, &view_up_vec, &error, view_rep.ori_matrix ); /* Set the view if orientation is okay. */ if ( !error ) pset_view_rep3 ( WS_ID, ( Pint ) 1, &view_rep ) ; lastx = event.xmotion.x; lasty = event.xmotion.y; } else switch ( event.type ) { case Expose: if ( event.xexpose.count == 0 ) predraw_all_structs( WS_ID, PFLAG_ALWAYS ); break; } } } int main(void) { Ppoint_list3 sphere; int i; /* Open PHIGS and create a workstation.Use the utility function * open xphigs and xlib ws (^U3.5.5) that we wrote in Chapter 3. */ /* open_xphigs_and_xlib_ws(WS_ID, WS_GEOMETRY, &display, &window);*/ fprintf(stderr, "Idaig eljutott.\n"); popen_phigs(PDEF_ERR_FILE, PDEF_MEM_SIZE); /* Create the AXES structure. (see 6.9.6 for build axes */ build_axes( 0.4 ); /* Duild the sphere. (See 10.4.lfor build sphere.) */ build_sphere( SPHERE_RADIUS, &sphere ); popen_struct( SPHERE ); pset_view_ind( (Pint) 1 ); pexec_struct( AXES ); ppolyline3( &sphere ); pclose_struct(); /* Calculate the initial view orientation matrix. */ view_ref_pt.x = 0; view_ref_pt.y = 0; view_ref_pt.z = 0; view_up_vec.delta_x = 0; view_up_vec.delta_y = 1; view_up_vec.delta_z = 0; view_plane_normal.delta_x = 1; view_plane_normal.delta_y = 1; view_plane_normal.delta_z = 1; peval_view_ori_matrix3( &view_ref_pt, &view_plane_normal, &view_up_vec, &error, view_rep.ori_matrix ); /* Define the view mapping. */ view_map.proj_type = PTYPE_PERSPECT; view_map.view_plane = 0.0; view_map.proj_ref_point.x = 0; view_map.proj_ref_point.y = 0; view_map.proj_ref_point.z = 10; view_map.win.x_min = -1.5; view_map.win.x_max = 1.5; view_map.win.y_min = -1.5; view_map.win.y_max = 1.5; view_map.back_plane = -1.5; view_map.front_plane = 1.5; view_map.proj_vp.x_min = 0.0; view_map.proj_vp.x_max = 1.0; view_map.proj_vp.y_min = 0.0; view_map.proj_vp.y_max = 1.0; view_map.proj_vp.z_min = 0.0; view_map.proj_vp.z_max = 1.0; peval_view_map_matrix3( &view_map, &error, view_rep.map_matrix); view_rep.clip_limit = view_map.proj_vp; view_rep.xy_clip = PIND_CLIP; view_rep.front_clip = view_rep.back_clip = PIND_CLIP; popen_ws(WS_ID, (void *)NULL, phigs_ws_type_x_tool); pset_view_rep3( WS_ID, (Pint) 1, &view_rep ); /* Set the update state for automatic updates. */ /*pset_disp_upd_st(WS_ID, PDEFER_ASAP, PMODE_NIVE);*/ /* Post the structure. */ ppost_struct( WS_ID, SPHERE, (Pfloat) 1.0 ); /* Read and respond to X events. */ /*handle_input();*/ fprintf(stderr, "up vector.\n"); for (i = 0; i < 30; i++) { /*punpost_struct(WS_ID, SPHERE);*/ view_up_vec.delta_x = 0; view_up_vec.delta_y = view_up_vec.delta_y + (float)i/10; view_up_vec.delta_z = view_up_vec.delta_z + (float)i/10; peval_view_ori_matrix3( &view_ref_pt, &view_plane_normal, &view_up_vec, &error, view_rep.ori_matrix ); pset_view_rep3 ( WS_ID, ( Pint ) 1, &view_rep ) ; predraw_all_structs( WS_ID, PFLAG_ALWAYS ); /*ppost_struct( WS_ID, SPHERE, (Pfloat) 1.0 );*/ sleep(2); } sleep(20); pclose_ws( WS_ID ); pclose_phigs(); return 0 ; }