From 781ecd6529505e4bf1fd400634ee83bcb9a07c81 Mon Sep 17 00:00:00 2001 From: epoch Date: Sun, 19 Mar 2017 04:51:01 -0500 Subject: I have no idea why I am doing this... --- filebrowser/run | 2 +- map2globe.py | 6 +- src/HACKING | 20 ++ src/Makefile | 21 +- src/common.h | 19 +- src/config.h | 5 +- src/graphics.c | 480 ------------------------------------------- src/graphics.h | 53 ----- src/graphics_backend.h | 24 --- src/graphics_c2.c | 41 ++++ src/graphics_c2.h | 17 ++ src/graphics_c2_opengl.c | 193 ++++++++++++++++++ src/graphics_c2_opengl.h | 6 + src/graphics_c3.c | 515 +++++++++++++++++++++++++++++++++++++++++++++++ src/graphics_c3.h | 55 +++++ src/graphics_cs.h | 26 +++ src/graphics_cs_opengl.c | 0 src/graphics_cs_x11.c | 423 ++++++++++++++++++++++++++++++++++++++ src/graphics_opengl.c | 151 -------------- src/graphics_opengl.h | 6 - src/graphics_x11.c | 461 ------------------------------------------ src/hackvr.c | 106 ++++++---- src/math.c | 15 +- src/math.h | 1 + src/testit.sh | 4 +- 25 files changed, 1418 insertions(+), 1232 deletions(-) create mode 100644 src/HACKING delete mode 100644 src/graphics.c delete mode 100644 src/graphics.h delete mode 100644 src/graphics_backend.h create mode 100644 src/graphics_c2.c create mode 100644 src/graphics_c2.h create mode 100644 src/graphics_c2_opengl.c create mode 100644 src/graphics_c2_opengl.h create mode 100644 src/graphics_c3.c create mode 100644 src/graphics_c3.h create mode 100644 src/graphics_cs.h create mode 100644 src/graphics_cs_opengl.c create mode 100644 src/graphics_cs_x11.c delete mode 100644 src/graphics_opengl.c delete mode 100644 src/graphics_opengl.h delete mode 100644 src/graphics_x11.c diff --git a/filebrowser/run b/filebrowser/run index d088d2e..c3bc1d3 100755 --- a/filebrowser/run +++ b/filebrowser/run @@ -1,3 +1,3 @@ #!/bin/bash cd "$(dirname "$0")" -./hackvr-frontend.sh +./frontend-hackvr.sh ./backend-gopher.sh diff --git a/map2globe.py b/map2globe.py index 8e58752..7b8ffb0 100755 --- a/map2globe.py +++ b/map2globe.py @@ -16,7 +16,8 @@ z="0" # / 90 * math.pi #they need to be in the range 0 to 2pi? -print("globe addshape 1 0 0 0 1 -1 0") +print("globe addshape 1 0 0 0 0 1 0") +rotation=0 while(1): #print (plat,plon,lat,lon) @@ -42,6 +43,9 @@ while(1): break if(lat and lon and x and y and z and plat and plon and px and py and pz): #if the previouses exist print("globe addshape 3 " + x + " " + y + " " + z + " " + x + " " + y + " " + z + " " + px + " " + py + " " + pz) + print("globe rotate 0 " + str(rotation) + " 0") + rotation+=1 + rotation%=360 #set previouses to currents (plat,plon,px,py,pz)=(lat,lon,x,y,z) diff --git a/src/HACKING b/src/HACKING new file mode 100644 index 0000000..98bac03 --- /dev/null +++ b/src/HACKING @@ -0,0 +1,20 @@ +oh god. I have no idea what I'm doing. + +may the gods have mercy on my soul for this monstrosity.. + +so. + +hackvr.c can optionally use graphics or not. + +if you choose to have graphics you can choose between: + +x11 or opengl+glut + +There are three layers of the graphics code.. + +I'm not exactly sure how I'm going to combine these. + +graphics_c3.c uses functions defined in a graphics_c2* file. +graphics_c2.c uses function defined in a graphics_cs* file. + +some graphical backends may provide their own graphics_c3.c? diff --git a/src/Makefile b/src/Makefile index d3e80a2..c41781e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,17 +1,18 @@ CFLAGS=-Wall -pedantic -std=c99 -DGRAPHICAL -ffast-math -#all: hackvr_headless hackvr_x11 hackvr_opengl slowcat ### when hackvr_opengl gets useful at all I'll start including it in default build. -all: hackvr_headless hackvr_x11 slowcat - ln -s hackvr_x11 hackvr +all: hackvr_headless hackvr_x11 hackvr_opengl slowcat ### when hackvr_opengl gets useful at all I'll start including it in default build. +#all: hackvr_headless hackvr_x11 slowcat + ln -sf hackvr_x11 hackvr hackvr_headless: LDLIBS=-lm hackvr_headless: hackvr_headless.o math.o hackvr_x11: LDLIBS=-lm -lX11 -hackvr_x11: hackvr_x11.o graphics.o graphics_x11.o math.o +hackvr_x11: hackvr_x11.o graphics_c3.o graphics_c2.o graphics_cs_x11.o math.o +#notice how all the targets have generic graphics objects up until a specific one. hackvr_opengl: LDLIBS=-lm -lGL -lGLU -lglut -hackvr_opengl: hackvr_opengl.o graphics.o graphics_opengl.o math.o +hackvr_opengl: hackvr_opengl.o graphics_c3.o graphics_c2_opengl.o graphics_cs_opengl.o math.o hackvr_opengl.o: LDLIBS=-lm -lGL -lGLU -lglut hackvr_x11.o: LDLIBS=-lm -lX11 @@ -19,9 +20,13 @@ hackvr_x11.o: LDLIBS=-lm -lX11 hackvr_headless.o: CFLAGS=-Wall -pedantic -std=c99 -ffast-math hackvr_headless.o: LDLIBS=-lm -graphics.o: LDLIBS=-lm -graphics_x11.o: LDLIBS=-lm -lX11 -graphics_opengl.o: LDLIBS=-lm -lGL -lGLU -lglut +graphics_c3.o: LDLIBS=-lm +graphics_c2.o: LDLIBS=-lm + +graphics_cs_x11.o: LDLIBS=-lm -lX11 + +graphics_c2_opengl.o: LDLIBS=-lm -lGL -lGLU -lglut +graphics_cs_opengl.o: LDLIBS=-lm -lGL -lGLU -lglut math.o: LDLIBS=-lm clean: diff --git a/src/common.h b/src/common.h index 859c826..bdabb4f 100644 --- a/src/common.h +++ b/src/common.h @@ -1,9 +1,15 @@ #include "config.h" +#include "math.h" #ifndef _HACKVR_COMMON_H_ #include #define _HACKVR_COMMON_H_ +#define TOP 240.0 +#define BOTTOM -240.0 +#define RIGHT 320.0 +#define LEFT -320.0 + #define min(a,b) (((a)<(b))?(a):(b)) #define max(a,b) (((a)>(b))?(a):(b)) @@ -33,17 +39,17 @@ typedef struct { short y; } cs_t; -typedef struct c3_rotation { +typedef struct { degrees x; degrees y; degrees z; } c3_rot_t; -struct camera { - c3_t p; +typedef struct { + char *id; c3_rot_t r; - real zoom; -} camera; + c3_t p; +} c3_group_rot_t; typedef struct c3_line { char *id; @@ -80,6 +86,9 @@ struct global { real mmz; struct c3_shape *shape[SHAPES]; int shapes; + c3_group_rot_t *group_rot[256];//meh + c3_group_rot_t camera; + real zoom; int derp; real split; }; diff --git a/src/config.h b/src/config.h index 08ce918..2ef2d1a 100644 --- a/src/config.h +++ b/src/config.h @@ -8,14 +8,15 @@ #define DEBUG 1 #define WALK_SPEED 1 -#define SPLIT_SCREEN 2 +#define SPLIT_SCREEN 1 #define CAMERA_SEPARATION -1 -#define RED_AND_BLUE 1 +#define RED_AND_BLUE 0 #define SHAPES 16386 #define MAX_SIDES 8 #define WIDTH 4000 #define HEIGHT 3000 #define DEFAULT_MINIMAP 0 #define FORCE_REDRAW 1 +#define MAXSHAPES 3000 #endif diff --git a/src/graphics.c b/src/graphics.c deleted file mode 100644 index 3cb973f..0000000 --- a/src/graphics.c +++ /dev/null @@ -1,480 +0,0 @@ -#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_backend.h" //this header includes all the functions you'll need to implement if you want to port hackvr to something else -#include "graphics.h" - -//typedef float real; //think this conflicts? - -//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 - -int c2sX(real x) { return (gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen: 1))) * ((x + RIGHT) / (RIGHT *2)) + gra_global.xoff; } -int s2cX(real x) { return (x/(gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue?gra_global.split_screen :1))))*(RIGHT*2)-RIGHT; } - -int c2sY(real y) { return gra_global.height * ((TOP-y) / (TOP*2)); } -int s2cY(real y) { return -((y/gra_global.height) * (TOP*2) - TOP); } - -cs_t c2_to_cs(c2_t p) { - return (cs_t){c2sX(p.x),c2sY(p.y)}; -} -c2_t cs_to_c2(cs_t p) { - return (c2_t){s2cX(p.x),s2cY(p.y)}; -} -/* -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); -} -*/ - -void draw_c2_line(c2_t p1,c2_t p2) { - draw_cs_line(c2_to_cs(p1),c2_to_cs(p2)); -} - -void draw_c3_shape(c3_s_t s) {//outlined. needs to be filled? //draw minimap shit in here too? probably... - int i; - cs_s_t ss; - ss.id=s.id;//it shouldn't disappear and we shouldn't need to make a copy. - ss.len=s.len; - for(i=0;ilen+(s->len==1);i++) { -// total=total+shitdist2(rotate_c3_yr(s->p[i],(c3_t){0,0,0},d2r(camera.yr)),camera.p); - total=total+shitdist2(rotate_c3_yr(s->p[i],camera.p,d2r(camera.r.y)),camera.p); - } - 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=camera.p.x; - real oldz=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,camera.p); - qsort(&zs,i,sizeof(zs[0]),(__compar_fn_t)compar);//sort these zs structs based on d. - 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 - radians tmprad=d2r((degrees){camera.r.y.d+180}); - radians tmprad2=d2r((degrees){camera.r.y.d+180}); - camera.p.z-=(gra_global.split_flip)*((gra_global.split/gra_global.split_screen)*cosl( tmprad.r )); - 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){camera.r.y.d+180}); - radians tmprad2=d2r((degrees){camera.r.y.d+180}); - camera.p.z+=(gra_global.split_flip)*(gra_global.split*cosl( tmprad.r )); - camera.p.x+=(gra_global.split_flip)*(gra_global.split*sinl( tmprad2.r )); - } - -//TODO: figure out what all this shit is and either update or remove. -//DONT USE WIDTH for shit. -/* - x1=nextX(WIDTH/2,HEIGHT/2,d2r(camera.yr),40); - y1=nextY(WIDTH/2,HEIGHT/2,d2r(camera.yr),40); - x2=nextX(WIDTH/2,HEIGHT/2,d2r(camera.yr+180),80); - y2=nextY(WIDTH/2,HEIGHT/2,d2r(camera.yr+180),80); - XDrawLine(global.dpy,w,gc,WIDTH/2,HEIGHT/2,x1,y1); - XDrawLine(global.dpy,w,gc,WIDTH/2,HEIGHT/2,x2,y2); - - XDrawLine(global.dpy,w,gc,0,HEIGHT/2,WIDTH,HEIGHT/2); - - x1=global.mousex; - y1=global.mousey; - real a=points_to_angle((c2_t){0,0},cs_to_c2((cs_t){x1,y1})); - - snprintf(tmp,sizeof(tmp)-1,"%llf",a); - XTextExtents(font,tmp,strlen(tmp),&direction,&ascent,&descent,&overall); - XDrawString(global.dpy,w,gc,global.xoff,(descent+0+ascent)*6,tmp,strlen(tmp)); - snprintf(tmp,sizeof(tmp)-1,"%llf",points_to_angle(cs_to_c2((cs_t){global.mousex,global.mousey}),(c2_t){0,0})+(M_PIl/2)); - XTextExtents(font,tmp,strlen(tmp),&direction,&ascent,&descent,&overall); - XDrawString(global.dpy,w,gc,global.xoff,(descent+0+ascent)*7,tmp,strlen(tmp)); - XDrawLine(global.dpy,w,gc,global.mousex,global.mousey,global.width/2,global.height/2); - - real c=cosl(d2r(camera.yr) - a) * distance((c2_t){x1,y1},(c2_t){WIDTH/2,HEIGHT/2}); - x2=nextX(x1,y1,d2r(camera.yr-90),c); - y2=nextY(x1,y1,d2r(camera.yr-90),c); - XDrawLine(global.dpy,w,gc,x1,y1,x2,y2); -*/ - camera.p.x = oldx; - camera.p.z = oldz; //-= cn*CAMERA_SEPARATION; - flipscreen(); -} diff --git a/src/graphics.h b/src/graphics.h deleted file mode 100644 index eadaa54..0000000 --- a/src/graphics.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef _HACKVR_GRAPHICS_H_ -#define _HACKVR_GRAPHICS_H_ - -//#include //this should be in the x11 one, not this one. - -struct gra_global { - unsigned int width; - unsigned int height; - int mousex; - int mousey; - int greyscale; - int xoff; - int split_screen; - real split; - int split_flip; - char force_redraw; - char red_and_blue; - unsigned int depth;//??? - unsigned int border_width; - int oldtime; - int fps; - int oldfps; - char zsort; - char drawminimap; - char drawsky; - char draw3d; - int mapxoff; - int mapyoff; - int rmousex; - int rmousey; - int buttonpressed; -}; - -typedef struct { - struct c3_shape *s; - real d; -} zsort_t; - -int graphics_init(); -int graphics_event_handler(); -void draw_screen(); -void set_aspect_ratio(); -int selfcommand(char *s); -cs_t c3_to_cs(c3_t p); -radians d2r(degrees d); -real shitdist(struct c3_shape *s,c3_t p); -c3_t rotate_c3_xr(c3_t p1,c3_t p2,radians xr); -c3_t rotate_c3_yr(c3_t p1,c3_t p2,radians yr); -c3_t rotate_c3_zr(c3_t p1,c3_t p2,radians zr); - -int compar(zsort_t *a,zsort_t *b); - -#endif diff --git a/src/graphics_backend.h b/src/graphics_backend.h deleted file mode 100644 index 1f88ec6..0000000 --- a/src/graphics_backend.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef _HACKVR_GRAPHICS_BACKEND_H_ -#define _HACKVR_GRAPHICS_BACKEND_H_ - -//these are just the functions that all backends need to implement to work with hackvr. -//the list of functions are subject to change. - -void draw_cs_line(cs_t p1,cs_t p2); -void draw_cs_text(cs_t p,char *text); -void draw_cs_shape(cs_s_t s); -void draw_cs_filled_shape(cs_s_t s); -void set_aspect_ratio(); -void set_color_based_on_distance(real d); -void flipscreen(); -void set_color(); -void set_color_red(); -void set_color_blue(); -void clear_backbuffer(); -int keypress_handler(int sym); -int graphics_init(); -int graphics_event_handler(); -void set_clipping_rectangle(int x,int y,int width,int height); -void red_and_blue_magic(); - -#endif diff --git a/src/graphics_c2.c b/src/graphics_c2.c new file mode 100644 index 0000000..17f1828 --- /dev/null +++ b/src/graphics_c2.c @@ -0,0 +1,41 @@ +#include "graphics_cs.h" +#include "graphics_c3.h" + +extern struct gra_global gra_global; + +int c2sX(real x) { return (gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen: 1))) * ((x + RIGHT) / (RIGHT *2)) + gra_global.xoff; } +int s2cX(real x) { return (x/(gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue?gra_global.split_screen :1))))*(RIGHT*2)-RIGHT; } + +int c2sY(real y) { return gra_global.height * ((TOP-y) / (TOP*2)); } +int s2cY(real y) { return -((y/gra_global.height) * (TOP*2) - TOP); } + +cs_t c2_to_cs(c2_t p) { + return (cs_t){c2sX(p.x),c2sY(p.y)}; +} +c2_t cs_to_c2(cs_t p) { + return (c2_t){s2cX(p.x),s2cY(p.y)}; +} + +void draw_c2_line(c2_t p1,c2_t p2) { + draw_cs_line(c2_to_cs(p1),c2_to_cs(p2)); +} + +void draw_c2_shape(c2_s_t s) { + int i; + cs_s_t ss; + ss.len=s.len; + ss.id=s.id; + for(i=0;i +#include +#include +#include +#include + +#include "common.h" +#include "graphics_cs.h" +#include "graphics_c2.h" +#include "graphics_c3.h" + +extern struct gra_global gra_global; + +GLenum doubleBuffer; +GLubyte ubImage[65536]; + +#define RIGHT 320.0 +#define LEFT -320.0 +#define TOP 240.0 +#define BOTTOM -240.0 + +int c2sX(real x) { return (gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen: 1))) * ((x + RIGHT) / (RIGHT *2)) + gra_global.xoff; } +int s2cX(real x) { return (x/(gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue?gra_global.split_screen :1))))*(RIGHT*2)-RIGHT; } + +int c2sY(real y) { return gra_global.height * ((TOP-y) / (TOP*2)); } +int s2cY(real y) { return -((y/gra_global.height) * (TOP*2) - TOP); } + +cs_t c2_to_cs(c2_t p) { + return (cs_t){c2sX(p.x),c2sY(p.y)}; +} + +c2_t cs_to_c2(cs_t p) { + return (c2_t){s2cX(p.x),s2cY(p.y)}; +} + +void set_aspect_ratio(int w,int h) { + glViewport(0,0,w,h); +// glMatrixMode(GL_PROJECTION); +// glLoadIdentity(); +// glOrtho(LEFT,RIGHT,BOTTOM,TOP,0,0); +// glTranslatef(0,-h,0); +} + +void set_color_based_on_distance(real d) { + //scale this color based on distance... closer is lighter. +// float g; +// g=d/100.0 + glColor3f(0.0, 1.0, 0.0); +} + +void set_clipping_rectangle(int x,int y,int w,int h) { + +} + +void draw_c2_line(c2_t p1,c2_t p2) { + //glBegin(GL_TRIANGLES); + glBegin(GL_LINES); + glColor3f(0.0, 1.0, 0.0); + glVertex2i(p1.x,p1.y); + glVertex2i(p2.x,p2.y); + //glVertex2i(p2.x,p2.y); + glEnd(); +} + +void draw_c2_text(c2_t p,char *s) { +// printf("@%lF,%lF: %s\n",p.x,p.y,s); +} + +void draw_c2_shape(c2_s_t s) { + int i; +// printf("drawing shaep!\n"); + for(i=0;i +#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? + +//typedef float real; //think this conflicts? + +//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.y=global.zoom * (delta_y * MAGIC(delta_z) - delta_y); + p2.x=global.zoom * (delta_x * MAGIC(delta_z) - delta_x); + 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]); + } + 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); + } + 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 + 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 )); + } +//TODO: figure out what all this shit is and either update or remove. +//DONT USE WIDTH for shit. +/* + x1=nextX(WIDTH/2,HEIGHT/2,d2r(camera.yr),40); + y1=nextY(WIDTH/2,HEIGHT/2,d2r(camera.yr),40); + x2=nextX(WIDTH/2,HEIGHT/2,d2r(camera.yr+180),80); + y2=nextY(WIDTH/2,HEIGHT/2,d2r(camera.yr+180),80); + XDrawLine(global.dpy,w,gc,WIDTH/2,HEIGHT/2,x1,y1); + XDrawLine(global.dpy,w,gc,WIDTH/2,HEIGHT/2,x2,y2); + + XDrawLine(global.dpy,w,gc,0,HEIGHT/2,WIDTH,HEIGHT/2); + + x1=global.mousex; + y1=global.mousey; + real a=points_to_angle((c2_t){0,0},cs_to_c2((cs_t){x1,y1})); + + snprintf(tmp,sizeof(tmp)-1,"%llf",a); + XTextExtents(font,tmp,strlen(tmp),&direction,&ascent,&descent,&overall); + XDrawString(global.dpy,w,gc,global.xoff,(descent+0+ascent)*6,tmp,strlen(tmp)); + snprintf(tmp,sizeof(tmp)-1,"%llf",points_to_angle(cs_to_c2((cs_t){global.mousex,global.mousey}),(c2_t){0,0})+(M_PIl/2)); + XTextExtents(font,tmp,strlen(tmp),&direction,&ascent,&descent,&overall); + XDrawString(global.dpy,w,gc,global.xoff,(descent+0+ascent)*7,tmp,strlen(tmp)); + XDrawLine(global.dpy,w,gc,global.mousex,global.mousey,global.width/2,global.height/2); + + real c=cosl(d2r(camera.yr) - a) * distance((c2_t){x1,y1},(c2_t){WIDTH/2,HEIGHT/2}); + x2=nextX(x1,y1,d2r(camera.yr-90),c); + y2=nextY(x1,y1,d2r(camera.yr-90),c); + XDrawLine(global.dpy,w,gc,x1,y1,x2,y2); +*/ + global.camera.p.x = oldx; + global.camera.p.z = oldz; //-= cn*CAMERA_SEPARATION; + flipscreen(); +} + +int graphics_init() { + + global.zoom=30.0l; + global.camera.r.x.d=270; + global.camera.r.y.d=90; + global.camera.r.z.d=0; + global.mmz=1; + global.camera.p.x=0; + global.camera.p.z=-6; + global.camera.p.y=5; + + 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 +} diff --git a/src/graphics_c3.h b/src/graphics_c3.h new file mode 100644 index 0000000..b6e1a5a --- /dev/null +++ b/src/graphics_c3.h @@ -0,0 +1,55 @@ +#ifndef _HACKVR_GRAPHICS_H_ +#define _HACKVR_GRAPHICS_H_ + +//#include //this should be in the x11 one, not this one. + +struct gra_global { + unsigned int width; + unsigned int height; + int mousex; + int mousey; + int greyscale; + int xoff; + int split_screen; + real split; + int split_flip; + char force_redraw; + char red_and_blue; + unsigned int depth;//??? + unsigned int border_width; + int oldtime; + int fps; + int oldfps; + char zsort; + char drawminimap; + char drawsky; + char draw3d; + int mapxoff; + int mapyoff; + int rmousex; + int rmousey; + int buttonpressed; + int maxshapes; +}; + +typedef struct { + struct c3_shape *s; + real d; +} zsort_t; + +int graphics_init(); +int graphics_sub_init(); +int graphics_event_handler(); +void draw_screen(); +void set_aspect_ratio(); +int selfcommand(char *s); +cs_t c3_to_cs(c3_t p); +radians d2r(degrees d); +real shitdist(struct c3_shape *s,c3_t p); +c3_t rotate_c3_xr(c3_t p1,c3_t p2,radians xr); +c3_t rotate_c3_yr(c3_t p1,c3_t p2,radians yr); +c3_t rotate_c3_zr(c3_t p1,c3_t p2,radians zr); + +int compar(zsort_t *a,zsort_t *b); + +#endif diff --git a/src/graphics_cs.h b/src/graphics_cs.h new file mode 100644 index 0000000..b5e02fb --- /dev/null +++ b/src/graphics_cs.h @@ -0,0 +1,26 @@ +#ifndef _HACKVR_GRAPHICS_CS_H_ +#define _HACKVR_GRAPHICS_CS_H_ + +#include "math.h" + +//these are just the functions that all backends need to implement to work with hackvr. +//the list of functions are subject to change. + +void draw_cs_line(cs_t p1,cs_t p2); +void draw_cs_text(cs_t p,char *text); +void draw_cs_shape(cs_s_t s); +void draw_cs_filled_shape(cs_s_t s); +void set_aspect_ratio(); +void set_color_based_on_distance(real d); +void flipscreen(); +void set_color(); +void set_color_red(); +void set_color_blue(); +void clear_backbuffer(); +void keypress_handler(unsigned char sym,int x,int y); +int graphics_init(); +int graphics_event_handler(); +void set_clipping_rectangle(int x,int y,int width,int height); +void red_and_blue_magic(); + +#endif diff --git a/src/graphics_cs_opengl.c b/src/graphics_cs_opengl.c new file mode 100644 index 0000000..e69de29 diff --git a/src/graphics_cs_x11.c b/src/graphics_cs_x11.c new file mode 100644 index 0000000..ab80368 --- /dev/null +++ b/src/graphics_cs_x11.c @@ -0,0 +1,423 @@ +#define _POSIX_C_SOURCE 200809L //for fileno and strdup +#define _BSD_SOURCE +#include +#include +#include +#include +#include +#include +#include +//#include //code to use select instead of non-blocking is commented out. might decide to use it later. +#include +#include +#include +#include //for size hints +#include +#define __USE_GNU //for longer math constants +#include + +#include "config.h" +#include "common.h" +#include "graphics_c3.h"//not needed? +#include "graphics_x11.h" +#include "graphics_cs.h" + +//typedef float real; //think this conflicts? + +//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) + +//Pixmap skypixmap; +//char sky[SKYH][SKYW]; + +extern struct global global; +extern struct gra_global gra_global; +struct x11_global x11_global; + +#ifdef GRAPHICAL + +/* +struct plane { + c3_t p; + real xr; + real yr; + real zr; +} + +void calculate_shape_color(c3_s_t s,real d) { +//given: at least 3 points on a plane. +//needed: angles of the plane. +// + xa= + ya= + za= + for(i=0;i<;i++) { + s.p.x + } +} +*/ +void set_color_based_on_distance(real d) { + int i=100-((int)((d+100.0) * 16.0) % 100); + XSetForeground(x11_global.dpy,x11_global.backgc,x11_global.colors[i].pixel); +} + +void draw_cs_line(cs_t p1,cs_t p2) { + XDrawLine(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,p1.x,p1.y,p2.x,p2.y); +} + +void draw_cs_text(cs_t p,char *text) { + char t[256]; + int direction,ascent,descent; + XFontStruct *font=XLoadQueryFont(x11_global.dpy,"fixed"); + XCharStruct overall; + snprintf(t,sizeof(t)-1,"%s",text); + XTextExtents(font,text,strlen(text),&direction,&ascent,&descent,&overall); + XDrawString(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,p.x,p.y+(descent+ascent),text,strlen(text)); +} + +void draw_cs_shape(cs_s_t s) {//this is implemented as draw_cs_line... hrm. it could be moved up to graphics.c? probl no. + int h; + int i;//all cs shapes can have 1, 2, or 3+ points. guess I gotta do that logic here too. + switch(s.len) { + case 1: + //cicle + h=max(s.p[0].y,s.p[1].y)-min(s.p[0].y,s.p[1].y); + XDrawArc(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,s.p[0].x-h,s.p[0].y-h,h*2,h*2,0,360*64); + break; + default: + for(i=0;imin_aspect.x=AR_W*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)); + hints->min_aspect.y=AR_H; + hints->max_aspect.x=AR_W*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)); + hints->max_aspect.y=AR_H; + hints->flags=PAspect; + XSetWMNormalHints(x11_global.dpy,x11_global.w,hints); +} + +void x11_keypress_handler(int sym,int x,int y) { + char line[1024]; + radians tmprad; + radians tmprad2; + real tmpx; +// real tmpy; //unused atm + real tmpz; + switch(sym) { + case XK_Return: + snprintf(line,sizeof(line)-1,"%s action %s\n",global.user,global.selected_object); + selfcommand(line); + break; + case XK_Up: + tmprad=d2r((degrees){global.camera.r.y.d+90}); + tmprad2=d2r((degrees){global.camera.r.y.d+90}); + tmpx=WALK_SPEED*sinl(tmprad.r); + tmpz=WALK_SPEED*cosl(tmprad2.r); + snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,-tmpx,tmpz); + selfcommand(line); + break; + case XK_Down: + tmprad=d2r((degrees){global.camera.r.y.d+270}); + tmprad2=d2r((degrees){global.camera.r.y.d+270}); + tmpx=WALK_SPEED*sinl(tmprad.r); + tmpz=WALK_SPEED*cosl(tmprad2.r); + snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,-tmpx,tmpz); + selfcommand(line); + break; + case XK_Left: + tmprad=d2r(global.camera.r.y); + tmprad2=d2r(global.camera.r.y); + tmpx=WALK_SPEED*sinl(tmprad.r); + tmpz=WALK_SPEED*cosl(tmprad2.r); + snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,-tmpx,tmpz); + selfcommand(line); + break; + case XK_Right: + tmprad=d2r((degrees){global.camera.r.y.d+180}); + tmprad2=d2r((degrees){global.camera.r.y.d+180}); + tmpx=WALK_SPEED*sinl(tmprad.r); + tmpz=WALK_SPEED*cosl(tmprad2.r); + snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,-tmpx,tmpz); + selfcommand(line); + break; + case XK_w: + snprintf(line,sizeof(line)-1,"%s move 0 1 0 0 0 0 0 0 0\n",global.user); + selfcommand(line); + break; + case XK_s: + snprintf(line,sizeof(line)-1,"%s move 0 -1 0 0 0 0 0 0 0\n",global.user); + selfcommand(line); + break; + case XK_r: + snprintf(line,sizeof(line)-1,"%s rotate %d 0 0\n",global.user,global.camera.r.x.d+5); + selfcommand(line); + break; + case XK_y: + snprintf(line,sizeof(line)-1,"%s rotate %d 0 0\n",global.user,global.camera.r.x.d-5); + selfcommand(line); + break; + case XK_q: + snprintf(line,sizeof(line)-1,"%s rotate 0 %d 0\n",global.user,global.camera.r.y.d+5); + selfcommand(line); + break; + case XK_e: + snprintf(line,sizeof(line)-1,"%s rotate 0 %d 0\n",global.user,global.camera.r.y.d-5); + selfcommand(line); + break; + case XK_u: + snprintf(line,sizeof(line)-1,"%s rotate 0 0 %d\n",global.user,global.camera.r.z.d+5); + selfcommand(line); + break; + case XK_o: + snprintf(line,sizeof(line)-1,"%s rotate 0 0 %d\n",global.user,global.camera.r.z.d-5); + selfcommand(line); + break; + case XK_p: + gra_global.split+=.1; + break; + case XK_l: + gra_global.split-=.1; + break; + case XK_z: global.zoom+=1; break; + case XK_x: global.zoom-=1; break; + case XK_c: global.mmz*=1.1; break; + case XK_v: global.mmz/=1.1; break; + case XK_h: global.split+=1; break; + case XK_j: global.split-=1; break; + case XK_6: gra_global.maxshapes+=10; break; + case XK_7: gra_global.maxshapes-=10; break; + case XK_d: + global.debug ^= 1; + break; + case XK_f: + global.derp ^= 1; + break; + case XK_m: + gra_global.drawminimap += 1; + gra_global.drawminimap %= 4; + break; + case XK_a: + gra_global.drawsky ^= 1; + break; + case XK_3: + gra_global.draw3d += 1; + gra_global.draw3d %= 4; + break; + case XK_Escape: + exit(0); + default: + break; + } +} +#endif + +int graphics_sub_init() { + int i; + char tmp[64]; + Cursor cursor; + XSetWindowAttributes attributes; +// Window root,child;//why do I have this? +// XColor toss; + printf("# Opening X Display... (%s)\n",getenv("DISPLAY")); + if((x11_global.dpy = XOpenDisplay(0)) == NULL) { + printf("# failure.\n"); + exit(1); + } + else printf("# done.\n"); + x11_global.color_map=DefaultColormap(x11_global.dpy, DefaultScreen(x11_global.dpy)); + printf("# generating grays...\n"); + for(i=0;i<=100;i++) { + snprintf(tmp,sizeof(tmp),"gray%d",i); + XAllocNamedColor(x11_global.dpy,x11_global.color_map,tmp,&x11_global.colors[i],&x11_global.colors[i]); + } + printf("# done.\n"); + assert(x11_global.dpy); + x11_global.root_window=0; + //global.colors[0]=BlackPixel(x11_global.dpy,DefaultScreen(x11_global.dpy)); +// int whiteColor = //WhitePixel(x11_global.dpy, DefaultScreen(x11_global.dpy)); + attributes.background_pixel=x11_global.colors[0].pixel; + if(x11_global.root_window) { + x11_global.w = DefaultRootWindow(x11_global.dpy); //this is still buggy. + } else { + x11_global.w = XCreateWindow(x11_global.dpy,DefaultRootWindow(x11_global.dpy),0,0,WIDTH*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)),HEIGHT,1,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy)),InputOutput,DefaultVisual(x11_global.dpy,DefaultScreen(x11_global.dpy))\ + ,CWBackPixel, &attributes); + set_aspect_ratio(); + XSelectInput(x11_global.dpy, x11_global.w, PointerMotionMask|StructureNotifyMask|ButtonPressMask|ButtonReleaseMask|KeyPressMask|ExposureMask); + } + XMapWindow(x11_global.dpy,x11_global.w); + XStoreName(x11_global.dpy,x11_global.w,"hackvr"); + global.shape[0]=0;//we'll allocate as we need more. + global.camera.id=global.user; + global.group_rot[0]=&global.camera; + global.group_rot[1]=0; + x11_global.gc=XCreateGC(x11_global.dpy,x11_global.w, 0, 0); + x11_global.backbuffer=XCreatePixmap(x11_global.dpy,x11_global.w,gra_global.width,gra_global.height,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); + x11_global.cleanbackbuffer=XCreatePixmap(x11_global.dpy,x11_global.w,gra_global.width,gra_global.height,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); + +//backbuffer is uninitialized + x11_global.backgc=XCreateGC(x11_global.dpy,x11_global.backbuffer,0,0); + + cursor=XCreateFontCursor(x11_global.dpy,XC_crosshair); + XDefineCursor(x11_global.dpy, x11_global.w, cursor); + + XAllocNamedColor(x11_global.dpy, x11_global.color_map, "green", &x11_global.green, &x11_global.green); + XAllocNamedColor(x11_global.dpy, x11_global.color_map, "red", &x11_global.red, &x11_global.red); + XAllocNamedColor(x11_global.dpy, x11_global.color_map, "blue", &x11_global.blue, &x11_global.blue); + XAllocNamedColor(x11_global.dpy, x11_global.color_map, "red", &x11_global.redblue[0], &x11_global.redblue[0]); + XAllocNamedColor(x11_global.dpy, x11_global.color_map, "blue", &x11_global.redblue[1], &x11_global.redblue[1]); + XSetForeground(x11_global.dpy, x11_global.gc, x11_global.green.pixel); + XSetForeground(x11_global.dpy, x11_global.backgc, x11_global.colors[0].pixel);//black. we're about to draw the blank background using this. + XSetBackground(x11_global.dpy, x11_global.gc, x11_global.colors[160].pixel); + XSetBackground(x11_global.dpy, x11_global.backgc, x11_global.colors[140].pixel); + + XFillRectangle(x11_global.dpy, x11_global.cleanbackbuffer,x11_global.backgc,0,0,gra_global.width,gra_global.height); + XSetForeground(x11_global.dpy, x11_global.backgc,x11_global.green.pixel); + +// XSetForeground(x11_global.dpy, gc, whiteColor); +// this was taking a "long" time. +/* + printf("# generating sky... "); + skypixmap=XCreatePixmap(x11_global.dpy,x11_global.w,SKYW,SKYH,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); + for(i=0;i> ((!!sky[i-1][(j+(SKYW)-1)%(SKYW)]<<2) | (!!sky[i-1][j]<<1) | (!!sky[i-1][j+1%(SKYW)])) & 1); + if(sky[i][j]) { + XDrawPoint(x11_global.dpy,skypixmap,x11_global.backgc,j,i); + } + } + } + } + printf("done.\n"); +*/ + + return 0;//we're fine +} + +int graphics_event_handler() { + int redraw=0; + XEvent e; + Window child,root; + //what sets mask? + unsigned int mask; + while(XPending(x11_global.dpy)) {//these are taking too long? + XNextEvent(x11_global.dpy, &e); +// printf("# handling event with type: %d\n",e.type); + switch(e.type) { +// case Expose: +// if(e.xexpose.count == 0) redraw=1; +// break; + case MotionNotify: + if(global.debug >= 2) printf("# MotionNotify\n"); + XQueryPointer(x11_global.dpy,x11_global.w,&root,&child,&gra_global.rmousex,&gra_global.rmousey,&gra_global.mousex,&gra_global.mousey,&mask); + redraw=1; + break; + case ButtonPress: + if(global.debug >= 2) printf("# ButtonPress\n"); + redraw=1; + gra_global.buttonpressed=e.xbutton.button;//what's this for? mouse? + break; + case ButtonRelease: + if(global.debug >= 2) printf("# ButtonRelease\n"); + redraw=1; + gra_global.buttonpressed=0;//what's this for??? + break; + case ConfigureNotify: + if(global.debug >= 2) printf("# ConfigureNotify\n"); + redraw=1; + XGetGeometry(x11_global.dpy,x11_global.w,&root,&global.x,&global.y,&gra_global.width,&gra_global.height,&gra_global.border_width,&gra_global.depth); + if(gra_global.height * AR_W / AR_H != gra_global.width / (gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))) { + // height / AR_H * AR_W = width / (ss / (rab ? ss : 1)) + if(global.debug >= 2) { + printf("# %d != %d for some reason. probably your WM not respecting aspect ratio hints or calculating based on them differently. (would cause an off-by-one or so)\n",gra_global.height * AR_W / AR_H , gra_global.width / (gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))); + } + if(gra_global.width / (gra_global.red_and_blue ? 1 : gra_global.split_screen) * AR_H / AR_W < gra_global.height) { + gra_global.height=gra_global.width / (gra_global.red_and_blue ? 1 : gra_global.split_screen) * AR_H / AR_W; + } else { + gra_global.width=gra_global.height * AR_W / AR_H * (gra_global.red_and_blue ? 1 : gra_global.split_screen); + } + } + gra_global.mapxoff=gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))/2; + gra_global.mapyoff=gra_global.height/2; + break; + case KeyPress: + if(global.debug >= 2) printf("# KeyPress\n"); + redraw=1; + x11_keypress_handler(XLookupKeysym(&e.xkey,0),gra_global.mousex,gra_global.mousey); + break; + default: +// printf("# received unknown event with type: %d\n",e.type); + break; + } + } + return redraw; +} diff --git a/src/graphics_opengl.c b/src/graphics_opengl.c deleted file mode 100644 index 5d2e600..0000000 --- a/src/graphics_opengl.c +++ /dev/null @@ -1,151 +0,0 @@ -#include -#include -#include -#include -#include - -#include "common.h" - -GLenum doubleBuffer; -GLubyte ubImage[65536]; - -void set_aspect_ratio(int w,int h) { - glViewport(0,0,w,h); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0,w,0,h,-1,1); -// glScalef(1,-1,1); -// glTranslatef(0,-h,0); -} - -void set_color_based_on_distance(real d) { - //scale this color based on distance... closer is lighter. -// float g; -// g=d/100.0 - glColor3f(0.0, 1.0, 0.0); -} - -void set_clipping_rectangle(int x,int y,int w,int h) { - -} - -void draw_cs_line(cs_t p1,cs_t p2) { - glBegin(GL_LINES); - glColor3f(0.0, 1.0, 0.0); - glVertex2i(p1.x,p1.y); - glVertex2i(p2.x,p2.y); - glEnd(); -} - -void draw_cs_text() { - -} - -void draw_cs_shape(cs_s_t s) { - int i; - for(i=0;i -#include -#include -#include -#include -#include -#include -//#include //code to use select instead of non-blocking is commented out. might decide to use it later. -#include -#include -#include -#include //for size hints -#include -#define __USE_GNU //for longer math constants -#include - -#include "config.h" -#include "common.h" -#include "graphics.h" -#include "graphics_x11.h" -#include "graphics_backend.h" - -//typedef float real; //think this conflicts? - -//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) - -//Pixmap skypixmap; -//char sky[SKYH][SKYW]; - -extern struct global global; -extern struct gra_global gra_global; -struct x11_global x11_global; - -#ifdef GRAPHICAL - -/* -struct plane { - c3_t p; - real xr; - real yr; - real zr; -} - -void calculate_shape_color(c3_s_t s,real d) { -//given: at least 3 points on a plane. -//needed: angles of the plane. -// - xa= - ya= - za= - for(i=0;i<;i++) { - s.p.x - } -} -*/ -void set_color_based_on_distance(real d) { - int i=100-((int)((d+100.0) * 16.0) % 100); - XSetForeground(x11_global.dpy,x11_global.backgc,x11_global.colors[i].pixel); -} - -void draw_cs_line(cs_t p1,cs_t p2) { - XDrawLine(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,p1.x,p1.y,p2.x,p2.y); -} - -void draw_cs_text(cs_t p,char *text) { - char t[256]; - int direction,ascent,descent; - XFontStruct *font=XLoadQueryFont(x11_global.dpy,"fixed"); - XCharStruct overall; - snprintf(t,sizeof(t)-1,"%s",text); - XTextExtents(font,text,strlen(text),&direction,&ascent,&descent,&overall); - XDrawString(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,p.x,p.y+(descent+ascent),text,strlen(text)); -} - -void draw_cs_shape(cs_s_t s) {//this is implemented as draw_cs_line... hrm. it could be moved up to graphics.c? probl no. - int h; - int i;//all cs shapes can have 1, 2, or 3+ points. guess I gotta do that logic here too. - switch(s.len) { - case 1: - //cicle - h=max(s.p[0].y,s.p[1].y)-min(s.p[0].y,s.p[1].y); - XDrawArc(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,s.p[0].x-h,s.p[0].y-h,h*2,h*2,0,360*64); - break; - default: - for(i=0;imin_aspect.x=AR_W*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)); - hints->min_aspect.y=AR_H; - hints->max_aspect.x=AR_W*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)); - hints->max_aspect.y=AR_H; - hints->flags=PAspect; - XSetWMNormalHints(x11_global.dpy,x11_global.w,hints); -} - -int keypress_handler(int sym) { - char line[1024]; - radians tmprad; - radians tmprad2; - real tmpx; -// real tmpy; //unused atm - real tmpz; - switch(sym) { - case XK_Return: - snprintf(line,sizeof(line)-1,"%s action %s\n",global.user,global.selected_object); - selfcommand(line); - break; - case XK_Up: - tmprad=d2r((degrees){camera.r.y.d+90}); - tmprad2=d2r((degrees){camera.r.y.d+90}); - tmpx=WALK_SPEED*sinl(tmprad.r); - tmpz=WALK_SPEED*cosl(tmprad2.r); - camera.p.x+=tmpx; - camera.p.z+=tmpz; - snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,tmpx,tmpz); - selfcommand(line); - break; - case XK_Down: - tmprad=d2r((degrees){camera.r.y.d+270}); - tmprad2=d2r((degrees){camera.r.y.d+270}); - tmpx=WALK_SPEED*sinl(tmprad.r); - tmpz=WALK_SPEED*cosl(tmprad2.r); - camera.p.x+=tmpx; - camera.p.z+=tmpz; - snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,tmpx,tmpz); - selfcommand(line); - break; - case XK_Left: - tmprad=d2r(camera.r.y); - tmprad2=d2r(camera.r.y); - tmpx=WALK_SPEED*sinl(tmprad.r); - tmpz=WALK_SPEED*cosl(tmprad2.r); - camera.p.x+=tmpx; - camera.p.z+=tmpz; - snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,tmpx,tmpz); - selfcommand(line); - break; - case XK_Right: - tmprad=d2r((degrees){camera.r.y.d+180}); - tmprad2=d2r((degrees){camera.r.y.d+180}); - tmpx=WALK_SPEED*sinl(tmprad.r); - tmpz=WALK_SPEED*cosl(tmprad2.r); - camera.p.x+=tmpx; - camera.p.z+=tmpz; - snprintf(line,sizeof(line)-1,"%s move %Lf 0 %Lf 0 0 0 0 0 0\n",global.user,tmpx,tmpz); - selfcommand(line); - break; - case XK_w: - camera.p.y+=1; - snprintf(line,sizeof(line)-1,"%s move 0 1 0 0 0 0 0 0 0\n",global.user); - selfcommand(line); - break; - case XK_s: - camera.p.y-=1; - snprintf(line,sizeof(line)-1,"%s move 0 -1 0 0 0 0 0 0 0\n",global.user); - selfcommand(line); - break; - case XK_r: - camera.r.x.d+=5; - while(camera.r.x.d > 360) camera.r.x.d-=360; - break; - case XK_y: - camera.r.x.d-=5; - while(camera.r.x.d < 0) camera.r.x.d+=360; - break; - case XK_q: - camera.r.y.d+=5; - while(camera.r.y.d > 360) camera.r.y.d-=360; - break; - case XK_e: - camera.r.y.d-=5; - while(camera.r.y.d < 0) camera.r.y.d+=360; - break; - case XK_u: - camera.r.z.d+=5; - while(camera.r.z.d > 360) camera.r.z.d-=360; - break; - case XK_o: - camera.r.z.d-=5; - while(camera.r.z.d < 0) camera.r.z.d+=360; - break; - case XK_p: - gra_global.split+=.1; - break; - case XK_l: - gra_global.split-=.1; - break; - case XK_z: camera.zoom+=1; break; - case XK_x: camera.zoom-=1; break; - case XK_c: global.mmz*=1.1; break; - case XK_v: global.mmz/=1.1; break; - case XK_h: global.split+=1; break; - case XK_j: global.split-=1; break; - case XK_d: - global.debug ^= 1; - break; - case XK_f: - global.derp ^= 1; - break; - case XK_m: - gra_global.drawminimap += 1; - gra_global.drawminimap %= 4; - break; - case XK_a: - gra_global.drawsky ^= 1; - break; - case XK_3: - gra_global.draw3d += 1; - gra_global.draw3d %= 4; - break; - case XK_Escape: - return -1; - default: - return 0; - break; - } - return 1; -} -#endif - -int graphics_init() { - int i; - char tmp[64]; - Cursor cursor; - XSetWindowAttributes attributes; -// Window root,child;//why do I have this? -// XColor toss; - printf("# Opening X Display... (%s)\n",getenv("DISPLAY")); - if((x11_global.dpy = XOpenDisplay(0)) == NULL) { - printf("# failure.\n"); - exit(1); - } - else printf("# done.\n"); - x11_global.color_map=DefaultColormap(x11_global.dpy, DefaultScreen(x11_global.dpy)); - printf("# generating grays...\n"); - for(i=0;i<=100;i++) { - snprintf(tmp,sizeof(tmp),"gray%d",i); - XAllocNamedColor(x11_global.dpy,x11_global.color_map,tmp,&x11_global.colors[i],&x11_global.colors[i]); - } - printf("# done.\n"); - assert(x11_global.dpy); - gra_global.split_screen=SPLIT_SCREEN; - gra_global.split_flip=-1; - gra_global.split=CAMERA_SEPARATION; - x11_global.root_window=0; - gra_global.red_and_blue=RED_AND_BLUE; - //global.colors[0]=BlackPixel(x11_global.dpy,DefaultScreen(x11_global.dpy)); -// int whiteColor = //WhitePixel(x11_global.dpy, DefaultScreen(x11_global.dpy)); - attributes.background_pixel=x11_global.colors[0].pixel; - if(x11_global.root_window) { - x11_global.w = DefaultRootWindow(x11_global.dpy); //this is still buggy. - } else { - x11_global.w = XCreateWindow(x11_global.dpy,DefaultRootWindow(x11_global.dpy),0,0,WIDTH*(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1)),HEIGHT,1,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy)),InputOutput,DefaultVisual(x11_global.dpy,DefaultScreen(x11_global.dpy))\ - ,CWBackPixel, &attributes); - set_aspect_ratio(); - XSelectInput(x11_global.dpy, x11_global.w, PointerMotionMask|StructureNotifyMask|ButtonPressMask|ButtonReleaseMask|KeyPressMask|ExposureMask); - } - XMapWindow(x11_global.dpy,x11_global.w); - XStoreName(x11_global.dpy,x11_global.w,"hackvr"); - gra_global.greyscale=1; - gra_global.zsort=1; - global.shape[0]=0;//we'll allocate as we need more. - x11_global.gc=XCreateGC(x11_global.dpy,x11_global.w, 0, 0); - - if(gra_global.red_and_blue) { - gra_global.width=WIDTH; - } else { - gra_global.width=WIDTH*gra_global.split_screen; - } - gra_global.height=HEIGHT; - - x11_global.backbuffer=XCreatePixmap(x11_global.dpy,x11_global.w,gra_global.width,gra_global.height,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); - x11_global.cleanbackbuffer=XCreatePixmap(x11_global.dpy,x11_global.w,gra_global.width,gra_global.height,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); - -//backbuffer is uninitialized - x11_global.backgc=XCreateGC(x11_global.dpy,x11_global.backbuffer,0,0); - - cursor=XCreateFontCursor(x11_global.dpy,XC_crosshair); - XDefineCursor(x11_global.dpy, x11_global.w, cursor); - - XAllocNamedColor(x11_global.dpy, x11_global.color_map, "green", &x11_global.green, &x11_global.green); - XAllocNamedColor(x11_global.dpy, x11_global.color_map, "red", &x11_global.red, &x11_global.red); - XAllocNamedColor(x11_global.dpy, x11_global.color_map, "blue", &x11_global.blue, &x11_global.blue); - XAllocNamedColor(x11_global.dpy, x11_global.color_map, "red", &x11_global.redblue[0], &x11_global.redblue[0]); - XAllocNamedColor(x11_global.dpy, x11_global.color_map, "blue", &x11_global.redblue[1], &x11_global.redblue[1]); - XSetForeground(x11_global.dpy, x11_global.gc, x11_global.green.pixel); - XSetForeground(x11_global.dpy, x11_global.backgc, x11_global.colors[0].pixel);//black. we're about to draw the blank background using this. - XSetBackground(x11_global.dpy, x11_global.gc, x11_global.colors[160].pixel); - XSetBackground(x11_global.dpy, x11_global.backgc, x11_global.colors[140].pixel); - - XFillRectangle(x11_global.dpy, x11_global.cleanbackbuffer,x11_global.backgc,0,0,gra_global.width,gra_global.height); - XSetForeground(x11_global.dpy, x11_global.backgc,x11_global.green.pixel); - -// XSetForeground(x11_global.dpy, gc, whiteColor); -// this was taking a "long" time. -/* - printf("# generating sky... "); - skypixmap=XCreatePixmap(x11_global.dpy,x11_global.w,SKYW,SKYH,DefaultDepth(x11_global.dpy,DefaultScreen(x11_global.dpy))); - for(i=0;i> ((!!sky[i-1][(j+(SKYW)-1)%(SKYW)]<<2) | (!!sky[i-1][j]<<1) | (!!sky[i-1][j+1%(SKYW)])) & 1); - if(sky[i][j]) { - XDrawPoint(x11_global.dpy,skypixmap,x11_global.backgc,j,i); - } - } - } - } - printf("done.\n"); -*/ - 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;//use this for checking proper fps I guess. - -//this should be in graphics.c ? - camera.zoom=30.0l; - camera.r.x.d=270; - camera.r.y.d=90; - camera.r.z.d=0; - global.mmz=1; - camera.p.x=0; - camera.p.z=-6; - camera.p.y=5; - return 0; -} - -int graphics_event_handler() { - int redraw=0; - XEvent e; - Window child,root; - //what sets mask? - unsigned int mask; - while(XPending(x11_global.dpy)) {//these are taking too long? - XNextEvent(x11_global.dpy, &e); -// printf("# handling event with type: %d\n",e.type); - switch(e.type) { -// case Expose: -// if(e.xexpose.count == 0) redraw=1; -// break; - case MotionNotify: - if(global.debug >= 2) printf("# MotionNotify\n"); - XQueryPointer(x11_global.dpy,x11_global.w,&root,&child,&gra_global.rmousex,&gra_global.rmousey,&gra_global.mousex,&gra_global.mousey,&mask); - redraw=1; - break; - case ButtonPress: - if(global.debug >= 2) printf("# ButtonPress\n"); - redraw=1; - gra_global.buttonpressed=e.xbutton.button;//what's this for? mouse? - break; - case ButtonRelease: - if(global.debug >= 2) printf("# ButtonRelease\n"); - redraw=1; - gra_global.buttonpressed=0;//what's this for??? - break; - case ConfigureNotify: - if(global.debug >= 2) printf("# ConfigureNotify\n"); - redraw=1; - XGetGeometry(x11_global.dpy,x11_global.w,&root,&global.x,&global.y,&gra_global.width,&gra_global.height,&gra_global.border_width,&gra_global.depth); - if(gra_global.height * AR_W / AR_H != gra_global.width / (gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))) { - // height / AR_H * AR_W = width / (ss / (rab ? ss : 1)) - if(global.debug >= 2) { - printf("# %d != %d for some reason. probably your WM not respecting aspect ratio hints or calculating based on them differently. (would cause an off-by-one or so)\n",gra_global.height * AR_W / AR_H , gra_global.width / (gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))); - } - if(gra_global.width / (gra_global.red_and_blue ? 1 : gra_global.split_screen) * AR_H / AR_W < gra_global.height) { - gra_global.height=gra_global.width / (gra_global.red_and_blue ? 1 : gra_global.split_screen) * AR_H / AR_W; - } else { - gra_global.width=gra_global.height * AR_W / AR_H * (gra_global.red_and_blue ? 1 : gra_global.split_screen); - } - } - gra_global.mapxoff=gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen : 1))/2; - gra_global.mapyoff=gra_global.height/2; - break; - case KeyPress: - if(global.debug >= 2) printf("# KeyPress\n"); - redraw=1; - if(keypress_handler(XLookupKeysym(&e.xkey,0)) == -1) { - printf("# exiting\n"); - return -1; - } - break; - default: -// printf("# received unknown event with type: %d\n",e.type); - break; - } - } - return redraw; -} diff --git a/src/hackvr.c b/src/hackvr.c index 6828f59..c8e3899 100644 --- a/src/hackvr.c +++ b/src/hackvr.c @@ -16,7 +16,7 @@ #include "common.h" #include "math.h" #ifdef GRAPHICAL -#include "graphics.h" +#include "graphics_c3.h" extern struct gra_global gra_global; #endif @@ -210,13 +210,13 @@ int load_stdin() { continue; } if(!strcmp(command,"dump")) { - printf("%s set camera.p.x %Lf\n",global.user,camera.p.x); - printf("%s set camera.p.y %Lf\n",global.user,camera.p.y); - printf("%s set camera.p.z %Lf\n",global.user,camera.p.z); - printf("%s set camera.r.x %d\n",global.user,camera.r.x.d); - printf("%s set camera.r.y %d\n",global.user,camera.r.y.d); - printf("%s set camera.r.z %d\n",global.user,camera.r.z.d); - printf("%s set camera.zoom %Lf\n",global.user,camera.zoom); + printf("%s set global.camera.p.x %Lf\n",global.user,global.camera.p.x); + printf("%s set global.camera.p.y %Lf\n",global.user,global.camera.p.y); + printf("%s set global.camera.p.z %Lf\n",global.user,global.camera.p.z); + printf("%s set global.camera.r.x %d\n",global.user,global.camera.r.x.d); + printf("%s set global.camera.r.y %d\n",global.user,global.camera.r.y.d); + printf("%s set global.camera.r.z %d\n",global.user,global.camera.r.z.d); + printf("%s set global.zoom %Lf\n",global.user,global.zoom); continue; } if(!strcmp(command,"quit")) { @@ -227,13 +227,13 @@ int load_stdin() { if(len == 4) { if(0); #ifdef GRAPHICAL - else if(!strcmp(a[2],"camera.p.x")) camera.p.x=strtold(a[3],0); - else if(!strcmp(a[2],"camera.p.y")) camera.p.y=strtold(a[3],0); - else if(!strcmp(a[2],"camera.p.z")) camera.p.z=strtold(a[3],0); - else if(!strcmp(a[2],"camera.zoom")) camera.zoom=strtold(a[3],0); - else if(!strcmp(a[2],"camera.r.x")) camera.r.x.d=atoi(a[3]); - else if(!strcmp(a[2],"camera.r.y")) camera.r.y.d=atoi(a[3]); - else if(!strcmp(a[2],"camera.r.z")) camera.r.z.d=atoi(a[3]); + else if(!strcmp(a[2],"camera.p.x")) global.camera.p.x=strtold(a[3],0); + else if(!strcmp(a[2],"camera.p.y")) global.camera.p.y=strtold(a[3],0); + else if(!strcmp(a[2],"camera.p.z")) global.camera.p.z=strtold(a[3],0); + else if(!strcmp(a[2],"global.zoom")) global.zoom=strtold(a[3],0); + else if(!strcmp(a[2],"camera.r.x")) global.camera.r.x.d=atoi(a[3]); + else if(!strcmp(a[2],"camera.r.y")) global.camera.r.y.d=atoi(a[3]); + else if(!strcmp(a[2],"camera.r.z")) global.camera.r.z.d=atoi(a[3]); #endif else printf("# unknown variable: %s\n",a[2]); continue; @@ -246,12 +246,13 @@ int load_stdin() { printf("# %s toggled!\n",a[2]); continue; } - if(!strcmp(command,"addshape")) { + if(!strcmp(command,"addshape")) {//need to add a grouprot with this. if(len > 3) { if(len != ((strtold(a[2],0)+(strtold(a[2],0)==1))*3)+3) { printf("# ERROR: wrong amount of parts for addshape. got: %d expected %d\n",len,((int)strtold(a[2],0)+(strtold(a[2],0)==1))*3+3); continue; } + for(i=0;global.shape[i];i++);//just take me to the end. global.shape[i]=malloc(sizeof(struct c3_shape)); global.shape[i]->len=strtold(a[2],0); global.shape[i]->id=strdup(id); @@ -263,6 +264,23 @@ int load_stdin() { i++; global.shapes=i; global.shape[i]=0; + + for(i=0;global.group_rot[i];i++) { + if(!strcmp(global.group_rot[i]->id,id)) { + break; + } + } + if(global.group_rot[i] == 0) {//we have ourselves a new grouprot! + global.group_rot[i]=malloc(sizeof(c3_group_rot_t)); + global.group_rot[i]->id=strdup(id); + global.group_rot[i+1]=0; + global.group_rot[i]->p.x=0; + global.group_rot[i]->p.y=0; + global.group_rot[i]->p.z=0; + global.group_rot[i]->r.x=(degrees){0}; + global.group_rot[i]->r.y=(degrees){0}; + global.group_rot[i]->r.z=(degrees){0}; + } } continue; } @@ -293,37 +311,50 @@ int load_stdin() { } continue; } - if(!strcmp(command,"rotate")) {//this probably won't be needed with the new rotation structs per group. + if(!strcmp(command,"rotate")) { if(len > 4) { - for(i=0;global.shape[i];i++) { - if(!strcmp(global.shape[i]->id,id)) { - for(j=0;j < global.shape[i]->len+(global.shape[i]->len==1);j++) { - radians tmprad=points_to_angle((c2_t){global.shape[i]->p[j].x,global.shape[i]->p[j].z},(c2_t){0,0}); - radians tmprad2=d2r((degrees){atoi(a[2])}); - global.shape[i]->p[j]=rotate_c3_yr(global.shape[i]->p[j],(c3_t){0,0,0},(radians){tmprad.r+tmprad2.r}); - //global.shape[i]->p[j]=rotate_c3_yr(global.shape[i]->p[j],(c3_t){0,0,0},d2r(atoi(a[3]))); - //global.shape[i]->p[j]=rotate_c3_zr(global.shape[i]->p[j],(c3_t){0,0,0},d2r(atoi(a[4]))); - } + for(i=0;global.group_rot[i];i++) { + if(!strcmp(global.group_rot[i]->id,id)) { + break; } } + if(global.group_rot[i] == 0) {//we have ourselves a new grouprot! + global.group_rot[i]=malloc(sizeof(c3_group_rot_t)); + global.group_rot[i]->id=strdup(id); + global.group_rot[i+1]=0; + global.group_rot[i]->p.x=0;//only set these if new. + global.group_rot[i]->p.y=0; + global.group_rot[i]->p.z=0; + } + global.group_rot[i]->r.x=(degrees){atoi(a[2])}; + global.group_rot[i]->r.y=(degrees){atoi(a[3])}; + global.group_rot[i]->r.z=(degrees){atoi(a[4])}; } + continue; } if(!strcmp(command,"move")) { if(len > 4) { - for(i=0;global.shape[i];i++) { - if(!strcmp(global.shape[i]->id,id)) { - for(j=0;j < global.shape[i]->len+(global.shape[i]->len==1);j++) { - global.shape[i]->p[j].x+=strtold(a[2],0); - global.shape[i]->p[j].y+=strtold(a[3],0); - global.shape[i]->p[j].z+=strtold(a[4],0); - } + for(i=0;global.group_rot[i];i++) { + if(!strcmp(global.group_rot[i]->id,id)) { + break; } } + if(global.group_rot[i] == 0) {//we have ourselves a new grouprot! + global.group_rot[i]=malloc(sizeof(c3_group_rot_t)); + global.group_rot[i]->id=strdup(id); + global.group_rot[i+1]=0; + global.group_rot[i]->r.x=(degrees){0};//only set these if new. + global.group_rot[i]->r.y=(degrees){0}; + global.group_rot[i]->r.z=(degrees){0}; + } + global.group_rot[i]->p.x+=strtold(a[2],0); + global.group_rot[i]->p.y+=strtold(a[3],0); + global.group_rot[i]->p.z+=strtold(a[4],0); } else { printf("# ERROR: wrong amount of parts for move. got: %d expected: 11\n",len); } - continue;//??? + continue; } printf("# I don't know what command you're talking about. %s\n",command); free(line); @@ -363,9 +394,10 @@ int main(int argc,char *argv[]) { if((redraw=graphics_event_handler()) == -1) { return 0; } - if((redraw || gra_global.force_redraw) && !global.headless) { - draw_screen(); - } +// printf("redraw=%d gra_global.force_redraw=%d global.headless=%d\n",redraw,gra_global.force_redraw,global.headless); + //if((redraw || gra_global.force_redraw) && !global.headless) { + // draw_screen(); + //} #endif switch(load_stdin()) { case -1: diff --git a/src/math.c b/src/math.c index 0f74f3d..b5c82de 100644 --- a/src/math.c +++ b/src/math.c @@ -1,6 +1,19 @@ +#include +#include "common.h" #include "math.h" -//should these use the radians struct? +extern struct global global; + +//might change this to use hashtables for faster lookups. +c3_group_rot_t *get_group_rotation(char *id) { + int i; + for(i=0;global.group_rot[i];i++) { + if(!strcmp(global.group_rot[i]->id,id)) { + return global.group_rot[i]; + } + } + return 0;//need to be sure to check return value for this function! +} c3_t rotate_c3_xr(c3_t p1,c3_t p2,radians xr) {//rotate y and z around camera based on xr (looking up and down) c2_t tmp; diff --git a/src/math.h b/src/math.h index dac2409..f369082 100644 --- a/src/math.h +++ b/src/math.h @@ -12,6 +12,7 @@ c2_t rotate_c2(c2_t p1,c2_t p2,radians dr); c3_t rotate_c3_xr(c3_t p1,c3_t p2,radians xr); c3_t rotate_c3_yr(c3_t p1,c3_t p2,radians yr); c3_t rotate_c3_zr(c3_t p1,c3_t p2,radians zr); +c3_group_rot_t *get_group_rotation(char *id); radians d2r(degrees d); radians points_to_angle(c2_t p1,c2_t p2); diff --git a/src/testit.sh b/src/testit.sh index 86aca19..ceb7698 100755 --- a/src/testit.sh +++ b/src/testit.sh @@ -3,5 +3,5 @@ cat <(sort -R ../meshes/female_basemesh1_2.hackvr) \ <(echo "woman move 10 0 -50") \ <(sort -R ../meshes/female_basemesh1_2.hackvr | sed 's/woman/woman2/g') \ <(echo "woman2 move -10 0 -200") \ - <(sort -R ../meshes/female_basemesh1_2.hackvr | sed 's/woman/woamn3/g') | ./slowcat 10000 | ./hackvr $USER -#../tools/obj2hackvr.pl woman ../meshes/female_basemesh1_2.obj | ./hackvr epoch + <(sort -R ../meshes/female_basemesh1_2.hackvr | sed 's/woman/woamn3/g') | ./slowcat 10000 | ./hackvr_opengl $USER +#../tools/obj2hackvr.pl woman ../meshes/female_basemesh1_2.obj | ./hackvr_opengl epoch -- cgit v1.2.3