#define _POSIX_C_SOURCE 200809L //for fileno and strdup #define _BSD_SOURCE #include #include #include #include #include #include #include #include #define __USE_GNU //for longer math constants #include #include "config.h" #include "common.h" //#include "graphics_cs.h" //this header includes all the functions you'll need to implement if you want to port hackvr to something else #include "graphics_c3.h"//we're defining these functions in this file. #include "graphics_c2.h"//we're using these functions make them. #include "graphics_cs.h"//we'll need generic function that don't give a damn about which dimension it is? //TODO: will have to make some pixmaps get resized when the window does. //for now set them to be as big as you think you'll ever resize the window to. #define SKYRULE 90 #define SKYW (WIDTH*5) #define SKYH (HEIGHT/2) extern struct global global; struct gra_global gra_global; #ifdef GRAPHICAL //used to figure out what c2 values are associated with each edge of the window. //#define TOP 160.0 //#define BOTTOM -160.0 #define TOP 240.0 #define BOTTOM -240.0 #define RIGHT 320.0 #define LEFT -320.0 /* real distance2(c2_t p1,c2_t p2) { return sqrtl(( (p1.x-p2.x)*(p1.x-p2.x) )+( (p1.y-p2.y)*(p1.y-p2.y) )); } */ real distance3(c3_t p1,c3_t p2) { return sqrtl(( (p1.x-p2.x)*(p1.x-p2.x) )+( (p1.y-p2.y)*(p1.y-p2.y) )+( (p1.z-p2.z)*(p1.z-p2.z) )); } /* real points_to_angle(c2_t p1,c2_t p2) { real a=atan2l(p2.y-p1.y,p2.x-p1.x); return a>=0?a:M_PIl+M_PIl+a; } */ /* real d2r(int d) { while(d<0) d+=360; return (real)(d%360) / (real)180 * M_PIl; }*/ int r2d(real r) { return r / M_PIl * 180; } int between_angles(real angle,real lower,real upper) { //lower may be higher than upper. //because lower is < 0 which wraps to higher. lower is 270, upper is 90. 270-90 is in front. if(lower > upper) { if(angle > lower) return 1; if(angle < upper) return 1; } if(upper > lower) { if(angle > lower && angle < upper) return 1; } return 0; } /* B (x2,y2) |-. c a| '-. | '-. '--------~ C b A (x1,y1) */ //I'll need this for collision I guess. /* //use this for finding a line that intersects with what the camera is pointing at c2_t get_c2_intersection(c2_t p1,real theta,c2_t p2) { real c; c=distance2(p1,p2); theta_a=C A B b = (c/1) * (theta_a + theta_offset) } int get_2D_intersection_X(x1,y1,theta_offset,x2,y2) { int x3a,y3a,x3b,y3b; int a,b,c;//lenght of sides. int theta_a,theta_b,theta_c;//angles opposite of same named sides. //get angle C A B theta_a= // x1,y1,x2,y2 //what are these d1 and d2? if(d1==d2) return global.math_error=1; c=dist(x1,y1,x2,y2); b = (c/1) * (theta_a + theta_offset); a = (c/1) * (theta_b + theta_offset); x3b=sin(theta_a) * b; y3b=cos(theta_a) * b; x3a=sin(theta_a) * a; y3a=cos(theta_a) * a; printf("x: %d %d\nx: %d %d\n",x3a,y3a,x3b,y3b); } int get_2D_intersection_Y(x,y,d,x,z,d) { int x3a,y3a,x3b,y3b; int a,b,c;//lenght of sides. int theta_a,theta_b,theta_c;//angles opposite of same named sides. //get angle from A to B. x1,y1,x2,y2 if(d1==d2) return global.math_error=1; c=dist(x1,y1,x2,y2); b = (c/1) * theta_a; a = (c/1) * theta_b; x3b=sin(a) * b; y3b=cos(a) * b; x3a=sin(a) * a; y3a=cos(a) * a; printf("y: %d %d\ny: %d %d\n",x3a,y3a,x3b,y3b); } */ c3_t c3_add(c3_t p1,c3_t p2) { return (c3_t){p1.x+p2.x,p1.y+p2.y,p1.z+p2.z}; } c3_t c3_subtract(c3_t p1,c3_t p2) { return (c3_t){p1.x-p2.x,p1.y-p2.y,p1.z-p2.z}; } #define MAGIC(x) (1.0l-(1.0l/powl(1.01l,(x)))) //??? might want to have some changables in here real magic(real x) { return MAGIC(x); } c2_t c3_to_c2(c3_t p3) { //DO NOT DRAW STUFF IN HERE c2_t p2; // c3_t tmp1; // c3_t tmp2; // c3_t tmp3; c3_t final; //these rotations need to be about the previous axis after the axis itself has been rotated. final=rotate_c3_yr(p3,global.camera.p,d2r(global.camera.r.y));//rotate everything around the camera's location. //now to rotate the shape around it's group's center. // final=rotate_c3_yr(p3,(c3_t){0,0,0},d2r(camera.yr));//rotate everything around the center no matter what. // tmp2=rotate_c3_xr(tmp1,camera.p,d2r(camera.xr)); // final=rotate_c3_zr(tmp2,camera.p,d2r(camera.zr)); real delta_x=(global.camera.p.x - final.x); real delta_y=(global.camera.p.y - final.y); real delta_z=(global.camera.p.z - final.z); // real d=distance3(camera.p,final); p2.x=global.zoom * (delta_x * MAGIC(delta_z) - delta_x); p2.y=global.zoom * (delta_y * MAGIC(delta_z) - delta_y); // p2.x=global.zoom * (delta_x * MAGIC(delta_z)); // p2.y=global.zoom * (delta_y * MAGIC(delta_z));//dunno if this is better or not. return p2; } void draw_c3_shape(c3_s_t s) {//outlined. needs to be filled? //draw minimap shit in here too? probably... int i; c2_s_t s2; s2.id=s.id;//it shouldn't disappear and we shouldn't need to make a copy. s2.len=s.len; c3_group_rot_t *gr=get_group_rotation(s.id); for(i=0;ip,rotate_c3_yr(s.p[i],gr->p,d2r(gr->r.y))):s.p[i]); //s2.p[i]=c3_to_c2(s.p[i]); } if(gra_global.draw3d == 1) { draw_c2_shape(s2); } if(gra_global.draw3d == 2) { //set foreground to a gray based on distance //between 0 to 100 // color_based_on_distance();//I don't have the distance in here. :/ //foreground_set(); //how... I want to draw the outline as one color and the fill as another. draw_c2_filled_shape(s2); set_color();//resets it to the default color. if(!strcmp(global.selected_object,s2.id)) { draw_c2_shape(s2); } } } void draw_graph(real (*fun)(real x)) { c2_t pa; draw_c2_line((c2_t){LEFT,0},(c2_t){RIGHT,0}); draw_c2_line((c2_t){0,TOP},(c2_t){0,BOTTOM}); for(pa.x=LEFT;pa.xlen+(s->len==1);i++) { c3_group_rot_t *gr=get_group_rotation(s->id); total=total+shitdist2( rotate_c3_yr(//we're rotating the point around the camera... gr? c3_add(gr->p,rotate_c3_yr(s->p[i],gr->p,d2r(gr->r.y)))://after applying the rotation of the group it is in. s->p[i],global.camera.p ,d2r(global.camera.r.y)) ,global.camera.p); }//why the fuck are we rotating this around the camera? that shouldn't make any difference to the distance from the camera... return (total) / (real)(s->len+(s->len==1)); } void HatchLines(c2_t p1,c2_t p2,c2_t p3,int density) { real i=0; for(i=1;id) < (b->d)); } /* void draw_c3_point_text(c3_t p,char *text) { char tmp[256]; int direction,ascent,descent; cs_t p2; p2=c3_to_cs(p); snprintf(tmp,sizeof(tmp)-1,"(%Lf,%Lf,%Lf)",p.x,p.y,p.z); XTextExtents(font,text,strlen(text),&direction,&ascent,&descent,&overall); XDrawString(global.dpy,global.backbuffer,global.backgc,p2.x,p2.y+(descent+ascent),tmp,strlen(tmp)); }*/ //push a string back into stdin so it can be read by the file loader. :P int selfcommand(char *s) { char t; if(!strlen(s)) return 0; ungetc(s[strlen(s)-1],stdin); t=s[strlen(s)-1]; s[strlen(s)-1]=0; selfcommand(s); putchar(t); return 0; } #endif void draw_screen() { int i; int cn=0;//camera number. char tmp[256]; zsort_t zs[SHAPES]; clear_backbuffer(); real oldx=global.camera.p.x; real oldz=global.camera.p.z; for(i=0;global.shape[i];i++) zs[i].s=global.shape[i]; for(i=0;global.shape[i];i++) zs[i].d=shitdist(zs[i].s,global.camera.p); qsort(&zs,i,sizeof(zs[0]),(__compar_fn_t)compar);//sort these zs structs based on d. farthest first. if(i > 0 && zs[i-1].s) strcpy(global.selected_object,zs[i-1].s->id); if(gra_global.split_screen > 1) { //oh... this will need to be a couple more lines... of what? I forgot. -Sep 2017 radians tmprad=d2r((degrees){global.camera.r.y.d+180}); radians tmprad2=d2r((degrees){global.camera.r.y.d+180}); global.camera.p.z-=(gra_global.split_flip)*((gra_global.split/gra_global.split_screen)*cosl( tmprad.r )); global.camera.p.x-=(gra_global.split_flip)*((gra_global.split/gra_global.split_screen)*sinl( tmprad2.r )); } for(cn=0;cnid)) { //XSetForeground(global.dpy,global.backgc,global.green.pixel); // } else { // } set_color_based_on_distance(zs[i].d); } //if(between_angles(points_to_angle((c2_t){zs[i].s->p[0].x,zs[i].s->p[0].z},(c2_t){camera.p.x,camera.p.z}),d2r(camera.yr+45),d2r(camera.yr+135))) { // set_color_based_on_distance(zs[i].d); draw_c3_shape(*(zs[i].s)); //} } // XSetForeground(global.dpy, global.backgc, global.green.pixel); radians tmprad=d2r((degrees){global.camera.r.y.d+180}); radians tmprad2=d2r((degrees){global.camera.r.y.d+180}); global.camera.p.z+=(gra_global.split_flip)*(gra_global.split*cosl( tmprad.r )); global.camera.p.x+=(gra_global.split_flip)*(gra_global.split*sinl( tmprad2.r )); } //just draw a line from center to 40 away from the center at the angle of the camera's y-rotation draw_c2_line((c2_t){0,0},rotate_c2((c2_t){40,0},(c2_t){0,0},d2r(global.camera.r.y))); //draw a line from the center to 80 away from the center in the angle of what should point at the mouse. draw_c2_line((c2_t){0,0},rotate_c2((c2_t){80,0},(c2_t){0,0},points_to_angle((c2_t){0,0},cs_to_c2((cs_t){gra_global.mousex,gra_global.mousey})))); global.camera.p.x = oldx; global.camera.p.z = oldz; //-= cn*CAMERA_SEPARATION; flipscreen(); } int graphics_init() { global.zoom=1.0l;//I think if this is set to 1, then 1 3d unit is 1 2d unit? global.camera.r.x.d=0; global.camera.r.y.d=0; global.camera.r.z.d=0; global.mmz=1;//this is minimap zoom. global.camera.p.x=0; global.camera.p.y=0; global.camera.p.z=-6; gra_global.split_screen=SPLIT_SCREEN; gra_global.split_flip=-1; gra_global.split=CAMERA_SEPARATION; gra_global.maxshapes=MAXSHAPES; gra_global.red_and_blue=RED_AND_BLUE; gra_global.greyscale=1; gra_global.zsort=1; if(gra_global.red_and_blue) { gra_global.width=WIDTH; } else { gra_global.width=WIDTH*gra_global.split_screen; } gra_global.height=HEIGHT; gra_global.mapxoff=gra_global.width/2; gra_global.mapyoff=gra_global.height/2; gra_global.drawminimap=DEFAULT_MINIMAP; gra_global.draw3d=1; gra_global.force_redraw=FORCE_REDRAW; graphics_sub_init(); return 0;//we're fine }